Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 139e98705cf2fcc2d5cbf6b11bdb34a2345821a9 (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
/***************************************************************************
 * Copyright (c) 2004 - 2008 Eike Stepper, Germany.
 * 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:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/
package org.eclipse.emf.cdo.server.internal.db;

import org.eclipse.emf.cdo.protocol.CDOID;
import org.eclipse.emf.cdo.protocol.model.CDOClass;
import org.eclipse.emf.cdo.protocol.model.CDOClassRef;
import org.eclipse.emf.cdo.server.db.IDBStoreReader;
import org.eclipse.emf.cdo.server.db.IDBStoreWriter;
import org.eclipse.emf.cdo.server.db.IObjectTypeCache;

import org.eclipse.net4j.db.DBException;
import org.eclipse.net4j.db.DBType;
import org.eclipse.net4j.db.DBUtil;
import org.eclipse.net4j.db.IDBAdapter;
import org.eclipse.net4j.db.IDBField;
import org.eclipse.net4j.db.IDBIndex;
import org.eclipse.net4j.db.IDBSchema;
import org.eclipse.net4j.db.IDBTable;
import org.eclipse.net4j.internal.util.lifecycle.Lifecycle;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author Eike Stepper
 */
public class ObjectTypeCache extends Lifecycle implements IObjectTypeCache
{
  private MappingStrategy mappingStrategy;

  private IDBTable table;

  private IDBField idField;

  private IDBField typeField;

  private transient Object initializeLock = new Object();

  public ObjectTypeCache()
  {
  }

  public MappingStrategy getMappingStrategy()
  {
    return mappingStrategy;
  }

  public void setMappingStrategy(MappingStrategy mappingStrategy)
  {
    this.mappingStrategy = mappingStrategy;
  }

  public CDOClassRef getObjectType(IDBStoreReader storeReader, CDOID id)
  {
    Statement statement = storeReader.getStatement();
    initialize(statement);

    StringBuilder builder = new StringBuilder();
    builder.append("SELECT ");
    builder.append(typeField);
    builder.append(" FROM ");
    builder.append(table);
    builder.append(" WHERE ");
    builder.append(idField);
    builder.append("=");
    builder.append(id.getValue());
    String sql = builder.toString();
    DBUtil.trace(sql);

    ResultSet resultSet = null;

    try
    {
      resultSet = statement.executeQuery(sql);
      if (!resultSet.next())
      {
        return null;
      }

      int classID = resultSet.getInt(1);
      return mappingStrategy.getClassRef(storeReader, classID);
    }
    catch (SQLException ex)
    {
      throw new DBException(ex);
    }
    finally
    {
      DBUtil.close(resultSet);
    }
  }

  public void putObjectType(IDBStoreWriter storeWriter, CDOID id, CDOClass type)
  {
    Statement statement = storeWriter.getStatement();
    initialize(statement);

    StringBuilder builder = new StringBuilder();
    builder.append("INSERT INTO ");
    builder.append(table);
    builder.append(" VALUES (");
    builder.append(id.getValue());
    builder.append(", ");
    builder.append(ClassServerInfo.getDBID(type));
    builder.append(")");
    String sql = builder.toString();
    DBUtil.trace(sql);

    try
    {
      statement.execute(sql);
      if (statement.getUpdateCount() != 1)
      {
        throw new DBException("Object type not inserted: " + id + " -> " + type);
      }
    }
    catch (SQLException ex)
    {
      throw new DBException(ex);
    }
  }

  private void initialize(Statement statement)
  {
    synchronized (initializeLock)
    {
      if (table == null)
      {
        IDBSchema schema = mappingStrategy.getStore().getSchema();
        table = schema.addTable(CDODBSchema.CDO_OBJECTS);
        idField = table.addField(CDODBSchema.ATTRIBUTES_ID, DBType.BIGINT);
        typeField = table.addField(CDODBSchema.ATTRIBUTES_CLASS, DBType.INTEGER);
        table.addIndex(IDBIndex.Type.PRIMARY_KEY, idField);

        IDBAdapter dbAdapter = mappingStrategy.getStore().getDBAdapter();
        dbAdapter.createTable(table, statement);
      }
    }
  }

  @Override
  protected void doBeforeActivate() throws Exception
  {
    super.doBeforeActivate();
    checkState(mappingStrategy, "mappingStrategy");
  }

  @Override
  protected void doDeactivate() throws Exception
  {
    table = null;
    idField = null;
    typeField = null;
    super.doDeactivate();
  }
}

Back to the top