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.persistence.impl;
006:
007: import com.tc.exception.TCRuntimeException;
008: import com.tc.memorydatastore.client.MemoryDataStoreClient;
009: import com.tc.memorydatastore.message.TCByteArrayKeyValuePair;
010: import com.tc.object.ObjectID;
011: import com.tc.util.Conversion;
012:
013: import java.io.IOException;
014: import java.util.Collection;
015: import java.util.HashMap;
016: import java.util.HashSet;
017: import java.util.Iterator;
018: import java.util.Map;
019: import java.util.Set;
020:
021: public class MemoryStorePersistableMap implements Map {
022:
023: private MemoryDataStoreClient db;
024: private MemoryStoreCollectionsPersistor persistor;
025: private int mapSize;
026: private final long id;
027:
028: public MemoryStorePersistableMap(ObjectID id,
029: MemoryStoreCollectionsPersistor persistor,
030: MemoryDataStoreClient db) {
031: this .id = id.toLong();
032: this .persistor = persistor;
033: this .db = db;
034: }
035:
036: public int size() {
037: return mapSize;
038: }
039:
040: public boolean isEmpty() {
041: return mapSize == 0;
042: }
043:
044: public boolean containsKey(Object key) {
045: return keySet().contains(key);
046: }
047:
048: public boolean containsValue(Object value) {
049: return values().contains(value);
050: }
051:
052: public Object get(Object key) {
053: try {
054: byte[] kb = persistor.serialize(id, key);
055: Object value = persistor.deserialize(db.get(kb));
056: return value;
057: } catch (Exception e) {
058: throw new TCRuntimeException(e);
059: }
060: }
061:
062: public Object put(Object key, Object value) {
063: try {
064: byte[] kb = persistor.serialize(id, key);
065: byte[] vb = persistor.serialize(value);
066: Object returnVal = db.get(kb);
067: db.put(kb, vb);
068: if (returnVal != null)
069: ++mapSize;
070: return (returnVal);
071: } catch (IOException e) {
072: throw new TCRuntimeException(e);
073: }
074: }
075:
076: public Object remove(Object key) {
077: try {
078: byte[] kb = persistor.serialize(id, key);
079: Object returnVal = db.get(kb);
080: if (returnVal != null) {
081: db.remove(kb);
082: --mapSize;
083: }
084: return returnVal;
085: } catch (IOException e) {
086: throw new TCRuntimeException(e);
087: }
088: }
089:
090: public void putAll(Map m) {
091: for (Iterator i = m.entrySet().iterator(); i.hasNext();) {
092: Map.Entry entry = (Map.Entry) i.next();
093: put(entry.getKey(), entry.getValue());
094: }
095: }
096:
097: public void clear() {
098: try {
099: db.removeAll(persistor.serialize(id));
100: mapSize = 0;
101: } catch (IOException e) {
102: throw new TCRuntimeException(e);
103: }
104: }
105:
106: public Set keySet() {
107: Set keys = new HashSet();
108: try {
109: Collection all = db.getAll(persistor.serialize(id));
110: for (Iterator i = all.iterator(); i.hasNext();) {
111: TCByteArrayKeyValuePair pair = (TCByteArrayKeyValuePair) i
112: .next();
113: keys.add(persistor.deserialize(Conversion
114: .long2Bytes(id).length, pair.getKey()));
115: }
116: } catch (Exception e) {
117: throw new TCRuntimeException(e);
118: }
119: return keys;
120: }
121:
122: public Collection values() {
123: Collection values = new HashSet();
124: try {
125: Collection all = db.getAll(persistor.serialize(id));
126: for (Iterator i = all.iterator(); i.hasNext();) {
127: TCByteArrayKeyValuePair pair = (TCByteArrayKeyValuePair) i
128: .next();
129: values.add(persistor.deserialize(pair.getValue()));
130: }
131: } catch (Exception e) {
132: throw new TCRuntimeException(e);
133: }
134:
135: return values;
136: }
137:
138: public Set entrySet() {
139: Map maps = new HashMap();
140: try {
141: Collection all = db.getAll(persistor.serialize(id));
142: for (Iterator i = all.iterator(); i.hasNext();) {
143: TCByteArrayKeyValuePair pair = (TCByteArrayKeyValuePair) i
144: .next();
145: maps.put(persistor.deserialize(Conversion
146: .long2Bytes(id).length, pair.getKey()),
147: persistor.deserialize(pair.getValue()));
148: }
149: } catch (Exception e) {
150: throw new TCRuntimeException(e);
151: }
152:
153: return maps.entrySet();
154: }
155:
156: public void commit(MemoryStoreCollectionsPersistor persistor,
157: MemoryDataStoreClient db) throws IOException {
158: }
159:
160: public boolean equals(Object other) {
161: if (!(other instanceof Map)) {
162: return false;
163: }
164: Map that = (Map) other;
165: if (that.size() != this .size()) {
166: return false;
167: }
168: return entrySet().containsAll(that.entrySet());
169: }
170:
171: public int hashCode() {
172: int h = 0;
173: for (Iterator i = entrySet().iterator(); i.hasNext();) {
174: h += i.next().hashCode();
175: }
176: return h;
177: }
178:
179: public String toString() {
180: return "MemoryStorePersistableMap(" + id + ")={ size() = "
181: + mapSize + " }";
182: }
183:
184: public void load() {
185: try {
186: Collection all = db.getAll(persistor.serialize(id));
187: mapSize = all.size();
188: } catch (Exception e) {
189: throw new TCRuntimeException(e);
190: }
191: }
192:
193: }
|