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.io.serializer.DSOSerializerPolicy;
008: import com.tc.io.serializer.TCObjectInputStream;
009: import com.tc.io.serializer.TCObjectOutputStream;
010: import com.tc.io.serializer.api.BasicSerializer;
011: import com.tc.logging.TCLogger;
012: import com.tc.memorydatastore.client.MemoryDataStoreClient;
013: import com.tc.object.ObjectID;
014: import com.tc.util.Conversion;
015:
016: import java.io.ByteArrayInputStream;
017: import java.io.ByteArrayOutputStream;
018: import java.io.IOException;
019: import java.io.ObjectInput;
020:
021: public class MemoryStoreCollectionsPersistor {
022:
023: private final MemoryDataStoreClient database;
024: private final BasicSerializer serializer;
025: private final ByteArrayOutputStream bao;
026: private final MemoryStoreCollectionFactory collectionFactory;
027: private final TCObjectOutputStream oo;
028:
029: public MemoryStoreCollectionsPersistor(TCLogger logger,
030: MemoryDataStoreClient mapsDatabase,
031: MemoryStoreCollectionFactory memoryStoreCollectionFactory) {
032: this .database = mapsDatabase;
033: this .collectionFactory = memoryStoreCollectionFactory;
034: DSOSerializerPolicy policy = new DSOSerializerPolicy();
035: this .serializer = new BasicSerializer(policy);
036: this .bao = new ByteArrayOutputStream(1024);
037: this .oo = new TCObjectOutputStream(bao);
038: }
039:
040: public void saveMap(MemoryStorePersistableMap map)
041: throws IOException {
042: map.commit(this , database);
043: }
044:
045: public synchronized byte[] serialize(long id, Object o)
046: throws IOException {
047: oo.writeLong(id);
048: serializer.serializeTo(o, oo);
049: oo.flush();
050: byte b[] = bao.toByteArray();
051: bao.reset();
052: return b;
053: }
054:
055: public synchronized byte[] serialize(long id) throws IOException {
056: oo.writeLong(id);
057: oo.flush();
058: byte b[] = bao.toByteArray();
059: bao.reset();
060: return b;
061: }
062:
063: public synchronized byte[] serialize(Object o) throws IOException {
064: serializer.serializeTo(o, oo);
065: oo.flush();
066: byte b[] = bao.toByteArray();
067: bao.reset();
068: return b;
069: }
070:
071: public MemoryStorePersistableMap loadMap(ObjectID id)
072: throws IOException, ClassNotFoundException {
073: MemoryStorePersistableMap map = (MemoryStorePersistableMap) collectionFactory
074: .createPersistentMap(id);
075: map.load();
076: return map;
077: }
078:
079: public Object deserialize(int start, byte[] data)
080: throws IOException, ClassNotFoundException {
081: if (start >= data.length)
082: return null;
083: ByteArrayInputStream bai = new ByteArrayInputStream(data,
084: start, data.length - start);
085: ObjectInput ois = new TCObjectInputStream(bai);
086: return serializer.deserializeFrom(ois);
087: }
088:
089: public Object deserialize(byte[] data) throws IOException,
090: ClassNotFoundException {
091: return deserialize(0, data);
092: }
093:
094: public boolean deleteCollection(ObjectID id) {
095: byte idb[] = Conversion.long2Bytes(id.toLong());
096: database.removeAll(idb);
097: return true;
098: }
099:
100: }
|