001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.objectserver.impl;
006:
007: import com.tc.object.ObjectID;
008: import com.tc.objectserver.api.ShutdownError;
009: import com.tc.objectserver.core.api.ManagedObject;
010: import com.tc.objectserver.persistence.api.ManagedObjectPersistor;
011: import com.tc.objectserver.persistence.api.ManagedObjectStore;
012: import com.tc.objectserver.persistence.api.PersistenceTransaction;
013: import com.tc.text.PrettyPrinter;
014: import com.tc.util.Assert;
015: import com.tc.util.ObjectIDSet2;
016: import com.tc.util.SyncObjectIdSet;
017:
018: import java.util.Collection;
019: import java.util.Map;
020: import java.util.Set;
021:
022: public class PersistentManagedObjectStore implements ManagedObjectStore {
023:
024: private final SyncObjectIdSet extantObjectIDs;
025: private final ManagedObjectPersistor objectPersistor;
026: private boolean inShutdown;
027:
028: public PersistentManagedObjectStore(ManagedObjectPersistor persistor) {
029: this .objectPersistor = persistor;
030: this .extantObjectIDs = objectPersistor.getAllObjectIDs();
031: }
032:
033: public long nextObjectIDBatch(int batchSize) {
034: long rv = this .objectPersistor.nextObjectIDBatch(batchSize);
035: return rv;
036: }
037:
038: public void setNextAvailableObjectID(long startID) {
039: this .objectPersistor.setNextAvailableObjectID(startID);
040: }
041:
042: public void addNewRoot(PersistenceTransaction tx, String rootName,
043: ObjectID id) {
044: objectPersistor.addRoot(tx, rootName, id);
045: }
046:
047: public Set getRoots() {
048: return objectPersistor.loadRoots();
049: }
050:
051: public Set getRootNames() {
052: return objectPersistor.loadRootNames();
053: }
054:
055: public ObjectID getRootID(String name) {
056: return objectPersistor.loadRootID(name);
057: }
058:
059: public Map getRootNamesToIDsMap() {
060: return objectPersistor.loadRootNamesToIDs();
061: }
062:
063: public boolean containsObject(ObjectID id) {
064: assertNotInShutdown();
065: return extantObjectIDs.contains(id);
066: }
067:
068: public void addNewObject(ManagedObject managed) {
069: assertNotInShutdown();
070: boolean result = extantObjectIDs.add(managed.getID());
071: Assert.eval(result);
072: }
073:
074: public void commitObject(PersistenceTransaction tx,
075: ManagedObject managed) {
076: assertNotInShutdown();
077: objectPersistor.saveObject(tx, managed);
078: }
079:
080: public void commitAllObjects(PersistenceTransaction tx,
081: Collection managed) {
082: assertNotInShutdown();
083: objectPersistor.saveAllObjects(tx, managed);
084: }
085:
086: public void removeAllObjectsByIDNow(PersistenceTransaction tx,
087: Collection ids) {
088: assertNotInShutdown();
089: this .objectPersistor.deleteAllObjectsByID(tx, ids);
090: basicRemoveAll(ids);
091: }
092:
093: private void basicRemoveAll(Collection ids) {
094: this .extantObjectIDs.removeAll(ids);
095: }
096:
097: public ObjectIDSet2 getAllObjectIDs() {
098: assertNotInShutdown();
099: return this .extantObjectIDs.snapshot();
100: }
101:
102: public ManagedObject getObjectByID(ObjectID id) {
103: assertNotInShutdown();
104:
105: ManagedObject rv = this .objectPersistor.loadObjectByID(id);
106: if (rv == null)
107: return rv;
108: if (rv.isDirty()) {
109: throw new AssertionError(
110: "Object loaded from persistor is dirty. Persistor: "
111: + this .objectPersistor
112: + ", ManagedObject: " + rv);
113: }
114: return rv;
115: }
116:
117: public synchronized void shutdown() {
118: assertNotInShutdown();
119: this .inShutdown = true;
120: }
121:
122: public synchronized boolean inShutdown() {
123: return this .inShutdown;
124: }
125:
126: public synchronized PrettyPrinter prettyPrint(PrettyPrinter out) {
127: PrettyPrinter rv = out;
128: out = out.println(getClass().getName()).duplicateAndIndent();
129: out.indent().print("extantObjectIDs: ").visit(extantObjectIDs)
130: .println();
131: out.indent().print("objectPersistor: ").duplicateAndIndent()
132: .visit(objectPersistor).println();
133: return rv;
134: }
135:
136: private void assertNotInShutdown() {
137: if (this .inShutdown)
138: throw new ShutdownError();
139: }
140: }
|