Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2bbfcac381b5662d0cfc35fb6371a150955f6b20 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
 * Copyright (c) 2004 - 2009 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Stefan Winkler - initial API and implementation
 *    Stefan Winkler - bug 249610: [DB] Support external references (Implementation)
 *    Eike Stepper - maintenance
 */
package org.eclipse.emf.cdo.server.internal.db;

import org.eclipse.emf.cdo.common.id.CDOIDExternal;
import org.eclipse.emf.cdo.common.id.CDOIDUtil;
import org.eclipse.emf.cdo.server.db.CDODBUtil;
import org.eclipse.emf.cdo.server.db.IDBStore;
import org.eclipse.emf.cdo.server.db.IDBStoreAccessor;
import org.eclipse.emf.cdo.server.db.IExternalReferenceManager;
import org.eclipse.emf.cdo.server.db.IPreparedStatementCache.ReuseProbability;
import org.eclipse.emf.cdo.server.internal.db.bundle.OM;

import org.eclipse.net4j.db.DBException;
import org.eclipse.net4j.db.DBUtil;
import org.eclipse.net4j.util.ReflectUtil.ExcludeFromDump;
import org.eclipse.net4j.util.lifecycle.Lifecycle;
import org.eclipse.net4j.util.lifecycle.LifecycleUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author Stefan Winkler
 */
public class ExternalReferenceManager extends Lifecycle implements IExternalReferenceManager.Internal
{
  private IDBStore store;

  private AtomicLong lastMappedId = new AtomicLong(0);

  @ExcludeFromDump
  private transient String sqlSelectByLongId;

  @ExcludeFromDump
  private transient String sqlSelectByURI;

  @ExcludeFromDump
  private transient String sqlInsert;

  public ExternalReferenceManager()
  {
  }

  public IDBStore getStore()
  {
    return store;
  }

  public void setStore(IDBStore store)
  {
    this.store = store;
  }

  public long mapExternalReference(IDBStoreAccessor accessor, CDOIDExternal id)
  {
    String uri = id.getURI();
    long result = lookupByID(accessor, uri);
    if (result < 0)
    {
      // mapping found
      return result;
    }

    return insertNew(accessor, uri);
  }

  public CDOIDExternal unmapExternalReference(IDBStoreAccessor accessor, long mappedId)
  {
    PreparedStatement stmt = null;
    ResultSet rs = null;

    try
    {
      stmt = accessor.getStatementCache().getPreparedStatement(sqlSelectByLongId, ReuseProbability.HIGH);
      stmt.setLong(1, mappedId);
      rs = stmt.executeQuery();

      if (!rs.next())
      {
        OM.LOG.error("External ID " + mappedId + " not found. Database inconsistent!");
        throw new IllegalStateException("External ID " + mappedId + " not found. Database inconsistent!");
      }

      String uri = rs.getString(1);
      return CDOIDUtil.createExternal(uri);
    }
    catch (SQLException e)
    {
      throw new DBException(e);
    }
    finally
    {
      DBUtil.close(rs);
      accessor.getStatementCache().releasePreparedStatement(stmt);
    }
  }

  @Override
  protected void doBeforeActivate() throws Exception
  {
    super.doBeforeActivate();
    checkState(store, "Store is not set");
  }

  @Override
  protected void doActivate() throws Exception
  {
    super.doActivate();
    IDBStoreAccessor reader = getStore().getReader(null);
    Connection connection = reader.getConnection();
    Statement statement = null;

    try
    {
      String sql = "SELECT MIN(" + CDODBSchema.EXTERNAL_ID + ") FROM " + CDODBSchema.EXTERNAL_REFS;

      statement = connection.createStatement();
      ResultSet result = statement.executeQuery(sql);

      if (result.next())
      {
        lastMappedId.set(result.getLong(1));
      }

      // else: resultSet is empty => table is empty
      // and lastMappedId stays 0 - as initialized.
    }
    catch (SQLException ex)
    {
      throw new DBException(ex);
    }
    finally
    {
      DBUtil.close(statement);
      LifecycleUtil.deactivate(reader); // Don't let the null-context accessor go to the pool!
    }

    sqlInsert = "INSERT INTO " + CDODBSchema.EXTERNAL_REFS + " VALUES (?,?,?)"; //$NON-NLS-1$ //$NON-NLS-2$
    StringBuilder builder = new StringBuilder();
    builder.append("INSERT INTO ");
    builder.append(CDODBSchema.EXTERNAL_REFS);
    builder.append("(");
    builder.append(CDODBSchema.EXTERNAL_ID);
    builder.append(",");
    builder.append(CDODBSchema.EXTERNAL_URI);
    builder.append(") VALUES (?,?)");

    sqlInsert = builder.toString();

    builder = new StringBuilder();
    builder.append("SELECT "); //$NON-NLS-1$
    builder.append(CDODBSchema.EXTERNAL_ID);
    builder.append(" FROM "); //$NON-NLS-1$
    builder.append(CDODBSchema.EXTERNAL_REFS);
    builder.append(" WHERE "); //$NON-NLS-1$
    builder.append(CDODBSchema.EXTERNAL_URI);
    builder.append(" =? "); //$NON-NLS-1$

    sqlSelectByURI = builder.toString();

    builder = new StringBuilder();
    builder.append("SELECT "); //$NON-NLS-1$
    builder.append(CDODBSchema.EXTERNAL_URI);
    builder.append(" FROM "); //$NON-NLS-1$
    builder.append(CDODBSchema.EXTERNAL_REFS);
    builder.append(" WHERE "); //$NON-NLS-1$
    builder.append(CDODBSchema.EXTERNAL_ID);
    builder.append(" =? "); //$NON-NLS-1$

    sqlSelectByLongId = builder.toString();
  }

  private long insertNew(IDBStoreAccessor accessor, String uri)
  {
    long newMappedId = lastMappedId.decrementAndGet();

    PreparedStatement stmt = null;

    try
    {
      stmt = accessor.getStatementCache().getPreparedStatement(sqlInsert, ReuseProbability.MEDIUM);
      stmt.setLong(1, newMappedId);
      stmt.setString(2, uri);

      CDODBUtil.sqlUpdate(stmt, true);
      return newMappedId;
    }
    catch (SQLException e)
    {
      throw new DBException(e);
    }
    finally
    {
      accessor.getStatementCache().releasePreparedStatement(stmt);
    }
  }

  private long lookupByID(IDBStoreAccessor accessor, String uri)
  {
    PreparedStatement stmt = null;
    ResultSet rs = null;

    try
    {
      stmt = accessor.getStatementCache().getPreparedStatement(sqlSelectByURI, ReuseProbability.HIGH);
      stmt.setString(1, uri);

      rs = stmt.executeQuery();

      if (rs.next())
      {
        return rs.getLong(1);
      }
      else
      {
        // not found ...
        return 0;
      }
    }
    catch (SQLException e)
    {
      throw new DBException(e);
    }
    finally
    {
      DBUtil.close(rs);
      accessor.getStatementCache().releasePreparedStatement(stmt);
    }
  }
}

Back to the top