KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > persistence > sleepycat > SleepycatPersistor


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.objectserver.persistence.sleepycat;
5
6 import com.sleepycat.bind.serial.SerialBinding;
7 import com.sleepycat.je.Cursor;
8 import com.sleepycat.je.CursorConfig;
9 import com.sleepycat.je.DatabaseException;
10 import com.sleepycat.je.Transaction;
11 import com.tc.io.serializer.api.StringIndex;
12 import com.tc.logging.TCLogger;
13 import com.tc.logging.TCLogging;
14 import com.tc.object.tx.ServerTransactionID;
15 import com.tc.objectserver.gtx.GlobalTransactionDescriptor;
16 import com.tc.objectserver.persistence.api.ClassPersistor;
17 import com.tc.objectserver.persistence.api.ClientStatePersistor;
18 import com.tc.objectserver.persistence.api.ManagedObjectPersistor;
19 import com.tc.objectserver.persistence.api.PersistenceTransaction;
20 import com.tc.objectserver.persistence.api.PersistenceTransactionProvider;
21 import com.tc.objectserver.persistence.api.PersistentCollectionFactory;
22 import com.tc.objectserver.persistence.api.PersistentMapStore;
23 import com.tc.objectserver.persistence.api.PersistentSequence;
24 import com.tc.objectserver.persistence.api.Persistor;
25 import com.tc.objectserver.persistence.api.StringIndexPersistor;
26 import com.tc.objectserver.persistence.api.TransactionPersistor;
27 import com.tc.objectserver.persistence.impl.StringIndexImpl;
28
29 import java.io.IOException JavaDoc;
30
31 public class SleepycatPersistor implements Persistor {
32   private static final int DEFAULT_CAPACITY = 50000;
33
34   private final StringIndexPersistor stringIndexPersistor;
35   private final StringIndex stringIndex;
36   private final ManagedObjectPersistorImpl managedObjectPersistor;
37   private final ClientStatePersistor clientStatePersistor;
38   private final TransactionPersistor transactionPerisistor;
39   private final PersistentSequence globalTransactionIDSequence;
40   private final ClassPersistor classPersistor;
41   private final PersistenceTransactionProvider persistenceTransactionProvider;
42   private final DBEnvironment env;
43   private final SleepycatCollectionFactory sleepycatCollectionFactory;
44   private final PersistentMapStore clusterStateStore;
45
46   private SleepycatCollectionsPersistor sleepycatCollectionsPersistor;
47
48   public SleepycatPersistor(TCLogger logger, DBEnvironment env, SerializationAdapterFactory serializationAdapterFactory)
49       throws DatabaseException {
50     DatabaseOpenResult result = env.open();
51     if (!result.isClean()) {
52       //
53
throw new DatabaseDirtyException("Attempt to open a dirty database. "
54                                        + "This may be because a previous instance of the server didn't exit cleanly."
55                                        + " Since the integrity of the data cannot be assured, "
56                                        + "the server is refusing to start."
57                                        + " Please remove the database files in the following directory and restart "
58                                        + "the server: " + env.getEnvironmentHome());
59     }
60
61     CursorConfig dbCursorConfig = new CursorConfig();
62     dbCursorConfig.setReadCommitted(true);
63     CursorConfig rootDBCursorConfig = new CursorConfig();
64     rootDBCursorConfig.setReadCommitted(true);
65     CursorConfig stringIndexCursorConfig = new CursorConfig();
66     stringIndexCursorConfig.setReadCommitted(true);
67     this.env = env;
68     this.persistenceTransactionProvider = new SleepycatPersistenceTransactionProvider(env.getEnvironment());
69     this.stringIndexPersistor = new SleepycatStringIndexPersistor(persistenceTransactionProvider, env
70         .getStringIndexDatabase(), stringIndexCursorConfig, env.getClassCatalogWrapper().getClassCatalog());
71     this.stringIndex = new StringIndexImpl(this.stringIndexPersistor, DEFAULT_CAPACITY);
72     this.sleepycatCollectionFactory = new SleepycatCollectionFactory();
73     this.sleepycatCollectionsPersistor = new SleepycatCollectionsPersistor(logger, env.getMapsDatabase(),
74                                                                            sleepycatCollectionFactory);
75     this.managedObjectPersistor = new ManagedObjectPersistorImpl(
76                                                                  logger,
77                                                                  env.getClassCatalogWrapper().getClassCatalog(),
78                                                                  serializationAdapterFactory,
79                                                                  env.getObjectDatabase(),
80                                                                  dbCursorConfig,
81                                                                  new SleepycatSequence(
82                                                                                        this.persistenceTransactionProvider,
83                                                                                        logger, 1, 1000, env
84                                                                                            .getObjectIDDB()), env
85                                                                      .getRootDatabase(), rootDBCursorConfig,
86                                                                  this.persistenceTransactionProvider,
87                                                                  this.sleepycatCollectionsPersistor);
88     this.clientStatePersistor = new ClientStatePersistorImpl(logger, this.persistenceTransactionProvider,
89                                                              new SleepycatSequence(this.persistenceTransactionProvider,
90                                                                                    logger, 1, 0, env
91                                                                                        .getClientIDDatabase()), env
92                                                                  .getClientStateDatabase());
93     this.transactionPerisistor = new TransactionPersistorImpl(env.getTransactionDatabase(), new SerialBinding(env
94         .getClassCatalogWrapper().getClassCatalog(), ServerTransactionID.class), new SerialBinding(env
95         .getClassCatalogWrapper().getClassCatalog(), GlobalTransactionDescriptor.class),
96                                                               this.persistenceTransactionProvider);
97     this.globalTransactionIDSequence = new SleepycatSequence(this.persistenceTransactionProvider, logger, 1, 1, env
98         .getTransactionSequenceDatabase());
99     this.classPersistor = new ClassPersistorImpl(this.persistenceTransactionProvider, logger, env.getClassDatabase());
100     this.clusterStateStore = new SleepycatMapStore(this.persistenceTransactionProvider, logger, env.getClusterStateStoreDatabase());
101   }
102
103   public StringIndex getStringIndex() {
104     return this.stringIndex;
105   }
106
107   public PersistenceTransactionProvider getPersistenceTransactionProvider() {
108     return this.persistenceTransactionProvider;
109   }
110
111   public PersistentSequence getGlobalTransactionIDSequence() {
112     return this.globalTransactionIDSequence;
113   }
114
115   public TransactionPersistor getTransactionPersistor() {
116     return this.transactionPerisistor;
117   }
118
119   public ManagedObjectPersistor getManagedObjectPersistor() {
120     return this.managedObjectPersistor;
121   }
122
123   public ClientStatePersistor getClientStatePersistor() {
124     return this.clientStatePersistor;
125   }
126
127   public ClassPersistor getClassPersistor() {
128     return this.classPersistor;
129   }
130
131   public PersistentCollectionFactory getPersistentCollectionFactory() {
132     return this.sleepycatCollectionFactory;
133   }
134
135   public PersistentMapStore getClusterStateStore() {
136     return clusterStateStore;
137   }
138
139   public void close() {
140     try {
141       env.close();
142     } catch (DatabaseException e) {
143       throw new DBException(e);
144     }
145   }
146
147   public boolean isOpen() {
148     return env.isOpen();
149   }
150
151   static class SleepycatPersistorBase {
152
153     private static final TCLogger logger = TCLogging.getLogger(SleepycatPersistorBase.class);
154
155     protected Transaction pt2nt(PersistenceTransaction tx) {
156       // XXX: Yuck.
157
return (tx instanceof TransactionWrapper) ? ((TransactionWrapper) tx).getTransaction() : null;
158     }
159
160     protected void abortOnError(PersistenceTransaction ptx) {
161       abortOnError(pt2nt(ptx));
162     }
163
164     protected void abortOnError(Transaction tx) {
165       try {
166         if(tx != null) tx.abort();
167       } catch (DatabaseException e) {
168         // This doesnt throw an exception as we dont want to create a Red herring.
169
logger.error("Error on abortOnError", e);
170       }
171       
172     }
173
174     protected void abortOnError(Cursor cursor, PersistenceTransaction ptx) {
175      abortOnError(cursor, pt2nt(ptx));
176     }
177     
178     protected void abortOnError(Cursor cursor, Transaction tx) {
179       if(cursor != null) {
180         try {
181           cursor.close();
182         } catch (DatabaseException e) {
183         // This doesnt throw an exception as we dont want to create a Red herring.
184
logger.error("Error on abortOnError", e);
185         }
186       }
187       abortOnError(tx);
188     }
189
190   }
191
192   /**
193    * This is only exposed for tests.
194    */

195   public SerializationAdapter getSerializationAdapter() throws IOException JavaDoc {
196     return this.managedObjectPersistor.getSerializationAdapter();
197   }
198
199   /**
200    * This is only exposed for tests.
201    */

202   public SleepycatCollectionsPersistor getCollectionsPersistor() {
203     return sleepycatCollectionsPersistor;
204   }
205
206 }
207
Popular Tags