KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > impl > PersistentManagedObjectStore


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

5 package com.tc.objectserver.impl;
6
7 import com.tc.object.ObjectID;
8 import com.tc.objectserver.api.ShutdownError;
9 import com.tc.objectserver.core.api.ManagedObject;
10 import com.tc.objectserver.persistence.api.ManagedObjectPersistor;
11 import com.tc.objectserver.persistence.api.ManagedObjectStore;
12 import com.tc.objectserver.persistence.api.PersistenceTransaction;
13 import com.tc.text.PrettyPrinter;
14 import com.tc.util.Assert;
15 import com.tc.util.SyncObjectIdSet;
16
17 import java.util.Collection JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
20
21 public class PersistentManagedObjectStore implements ManagedObjectStore {
22
23   private final SyncObjectIdSet extantObjectIDs;
24   private final ManagedObjectPersistor objectPersistor;
25   private boolean inShutdown;
26
27   public PersistentManagedObjectStore(ManagedObjectPersistor persistor) {
28     this.objectPersistor = persistor;
29     this.extantObjectIDs = objectPersistor.getAllObjectIDs();
30   }
31
32   public long nextObjectIDBatch(int batchSize) {
33     long rv = this.objectPersistor.nextObjectIDBatch(batchSize);
34     return rv;
35   }
36
37   public void setNextAvailableObjectID(long startID) {
38     this.objectPersistor.setNextAvailableObjectID(startID);
39   }
40
41   public void addNewRoot(PersistenceTransaction tx, String JavaDoc rootName, ObjectID id) {
42     objectPersistor.addRoot(tx, rootName, id);
43   }
44
45   public Set getRoots() {
46     return objectPersistor.loadRoots();
47   }
48
49   public Set getRootNames() {
50     return objectPersistor.loadRootNames();
51   }
52
53   public ObjectID getRootID(String JavaDoc name) {
54     return objectPersistor.loadRootID(name);
55   }
56   
57
58   public Map JavaDoc getRootNamesToIDsMap() {
59     return objectPersistor.loadRootNamesToIDs();
60   }
61
62   public boolean containsObject(ObjectID id) {
63     assertNotInShutdown();
64     return extantObjectIDs.contains(id);
65   }
66
67   public void addNewObject(ManagedObject managed) {
68     assertNotInShutdown();
69     boolean result = extantObjectIDs.add(managed.getID());
70     Assert.eval(result);
71   }
72
73   public void commitObject(PersistenceTransaction tx, ManagedObject managed) {
74     assertNotInShutdown();
75     objectPersistor.saveObject(tx, managed);
76   }
77
78   public void commitAllObjects(PersistenceTransaction tx, Collection JavaDoc managed) {
79     assertNotInShutdown();
80     objectPersistor.saveAllObjects(tx, managed);
81   }
82
83   public void removeAllObjectsByIDNow(PersistenceTransaction tx, Collection JavaDoc ids) {
84     assertNotInShutdown();
85     this.objectPersistor.deleteAllObjectsByID(tx, ids);
86     basicRemoveAll(ids);
87   }
88
89   private void basicRemoveAll(Collection JavaDoc ids) {
90     this.extantObjectIDs.removeAll(ids);
91   }
92
93   public SyncObjectIdSet getAllObjectIDs() {
94     assertNotInShutdown();
95     return this.extantObjectIDs.snapshot();
96   }
97
98   public ManagedObject getObjectByID(ObjectID id) {
99     assertNotInShutdown();
100
101     ManagedObject rv = this.objectPersistor.loadObjectByID(id);
102     if (rv == null) return rv;
103     if (rv.isDirty()) { throw new AssertionError JavaDoc("Object loaded from persistor is dirty. Persistor: "
104                                                  + this.objectPersistor + ", ManagedObject: " + rv); }
105     return rv;
106   }
107
108   public synchronized void shutdown() {
109     assertNotInShutdown();
110     this.inShutdown = true;
111   }
112
113   public synchronized boolean inShutdown() {
114     return this.inShutdown;
115   }
116
117   public synchronized PrettyPrinter prettyPrint(PrettyPrinter out) {
118     PrettyPrinter rv = out;
119     out = out.println(getClass().getName()).duplicateAndIndent();
120     out.indent().print("extantObjectIDs: ").visit(extantObjectIDs).println();
121     out.indent().print("objectPersistor: ").duplicateAndIndent().visit(objectPersistor).println();
122     return rv;
123   }
124
125   private void assertNotInShutdown() {
126     if (this.inShutdown) throw new ShutdownError();
127   }
128 }
129
Popular Tags