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.managedobject.ManagedObjectStateFactory;
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:
017: import java.util.Collection;
018: import java.util.HashMap;
019: import java.util.HashSet;
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.Set;
023:
024: public class InMemoryManagedObjectStore implements ManagedObjectStore {
025:
026: private long objectIDSequence = 1000;
027: private final Map roots = new HashMap();
028: private final Map managed;
029: private boolean inShutdown = false;
030:
031: public InMemoryManagedObjectStore(Map managed) {
032: this .managed = managed;
033: }
034:
035: public synchronized boolean containsObject(ObjectID id) {
036: assertNotInShutdown();
037: return this .managed.containsKey(id);
038: }
039:
040: public synchronized void addNewObject(ManagedObject managedObject) {
041: assertNotInShutdown();
042: localPut(managedObject);
043: }
044:
045: private void localPut(ManagedObject managedObject) {
046: this .managed.put(managedObject.getID(), managedObject);
047: }
048:
049: public synchronized void commitObject(PersistenceTransaction tx,
050: ManagedObject managedObject) {
051: assertNotInShutdown();
052: assertContains(managedObject);
053: }
054:
055: public synchronized void commitAllObjects(
056: PersistenceTransaction tx, Collection managedObjects) {
057: assertNotInShutdown();
058: for (Iterator i = managedObjects.iterator(); i.hasNext();) {
059: assertContains((ManagedObject) i.next());
060: }
061: }
062:
063: private void removeObjectByID(PersistenceTransaction tx, ObjectID id) {
064: this .managed.remove(id);
065: }
066:
067: public synchronized void removeAllObjectsByID(
068: PersistenceTransaction tx, Collection ids) {
069: assertNotInShutdown();
070: for (Iterator i = ids.iterator(); i.hasNext();) {
071: removeObjectByID(tx, (ObjectID) i.next());
072: }
073: }
074:
075: public void removeAllObjectsByIDNow(PersistenceTransaction tx,
076: Collection objectIds) {
077: removeAllObjectsByID(tx, objectIds);
078: }
079:
080: public synchronized ObjectIDSet2 getAllObjectIDs() {
081: assertNotInShutdown();
082: return new ObjectIDSet2(managed.keySet());
083: }
084:
085: public synchronized int getObjectCount() {
086: return managed.size();
087: }
088:
089: public synchronized ManagedObject getObjectByID(ObjectID id) {
090: assertNotInShutdown();
091: return (ManagedObject) this .managed.get(id);
092: }
093:
094: public synchronized PrettyPrinter prettyPrint(PrettyPrinter out) {
095: out.println(getClass().getName()).duplicateAndIndent().print(
096: "managed: ").visit(managed).println();
097: return out;
098: }
099:
100: public synchronized void shutdown() {
101: assertNotInShutdown();
102: this .inShutdown = true;
103: }
104:
105: public synchronized boolean inShutdown() {
106: return this .inShutdown;
107: }
108:
109: private synchronized void assertNotInShutdown() {
110: if (inShutdown)
111: throw new ShutdownError();
112: }
113:
114: private synchronized void assertContains(ManagedObject managedObject) {
115: if (!containsObject(managedObject.getID()))
116: throw new AssertionError("Object store does not contain "
117: + managedObject);
118: }
119:
120: public ObjectID getRootID(String name) {
121: return (ObjectID) (roots.containsKey(name) ? roots.get(name)
122: : ObjectID.NULL_ID);
123: }
124:
125: public Set getRoots() {
126: return new HashSet(roots.values());
127: }
128:
129: public Set getRootNames() {
130: return roots.keySet();
131: }
132:
133: public void addNewRoot(PersistenceTransaction tx, String rootName,
134: ObjectID id) {
135: roots.put(rootName, id);
136: }
137:
138: public synchronized long nextObjectIDBatch(int batchSize) {
139: long rv = objectIDSequence;
140: objectIDSequence += batchSize;
141: return rv;
142: }
143:
144: public void setNextAvailableObjectID(long startID) {
145: Assert.assertTrue(startID >= objectIDSequence);
146: objectIDSequence = startID;
147: }
148:
149: public void setTransientData(ManagedObjectStateFactory stateFactory) {
150: assertNotInShutdown();
151: }
152:
153: public Map getRootNamesToIDsMap() {
154: return roots;
155: }
156:
157: }
|