001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.objectserver.persistence.sleepycat;
005:
006: import com.sleepycat.je.Cursor;
007: import com.sleepycat.je.CursorConfig;
008: import com.sleepycat.je.Database;
009: import com.sleepycat.je.DatabaseEntry;
010: import com.sleepycat.je.DatabaseException;
011: import com.sleepycat.je.LockMode;
012: import com.sleepycat.je.OperationStatus;
013: import com.sleepycat.je.Transaction;
014: import com.tc.logging.TCLogger;
015: import com.tc.objectserver.persistence.api.ClassPersistor;
016: import com.tc.objectserver.persistence.api.PersistenceTransactionProvider;
017: import com.tc.objectserver.persistence.sleepycat.SleepycatPersistor.SleepycatPersistorBase;
018: import com.tc.util.Conversion;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: class ClassPersistorImpl extends SleepycatPersistorBase implements
024: ClassPersistor {
025: private final Database classDB;
026: private final PersistenceTransactionProvider ptxp;
027: private final CursorConfig cursorConfig;
028:
029: ClassPersistorImpl(PersistenceTransactionProvider ptxp,
030: TCLogger logger, Database classDB) {
031: this .ptxp = ptxp;
032: this .classDB = classDB;
033: this .cursorConfig = new CursorConfig();
034: this .cursorConfig.setReadCommitted(true);
035: }
036:
037: public void storeClass(int clazzId, byte[] clazzBytes) {
038: Transaction tx = null;
039: try {
040: tx = pt2nt(ptxp.newTransaction());
041: DatabaseEntry key = new DatabaseEntry();
042: key.setData(Conversion.int2Bytes(clazzId));
043: DatabaseEntry value = new DatabaseEntry();
044: value.setData(clazzBytes);
045: OperationStatus status = this .classDB.put(tx, key, value);
046: tx.commit();
047:
048: if (!OperationStatus.SUCCESS.equals(status)) {
049: // Formatting
050: throw new DBException("Unable to store class Bytes: "
051: + clazzId);
052: }
053: } catch (DatabaseException t) {
054: abortOnError(tx);
055: t.printStackTrace();
056: throw new DBException(t);
057: }
058: }
059:
060: public byte[] retrieveClass(int clazzId) {
061: Transaction tx = null;
062: try {
063: tx = pt2nt(ptxp.newTransaction());
064: DatabaseEntry key = new DatabaseEntry();
065: key.setData(Conversion.int2Bytes(clazzId));
066: DatabaseEntry value = new DatabaseEntry();
067: OperationStatus status = this .classDB.get(tx, key, value,
068: LockMode.DEFAULT);
069: tx.commit();
070: if (!OperationStatus.SUCCESS.equals(status)) {
071: // Formatting
072: throw new DBException(
073: "Unable to retrieve class Bytes: " + clazzId);
074: }
075: return value.getData();
076: } catch (DatabaseException t) {
077: abortOnError(tx);
078: t.printStackTrace();
079: throw new DBException(t);
080: }
081: }
082:
083: public Map retrieveAllClasses() {
084: Map allClazzBytes = new HashMap();
085: Cursor cursor = null;
086: Transaction tx = null;
087: try {
088: tx = pt2nt(ptxp.newTransaction());
089: cursor = classDB.openCursor(tx, cursorConfig);
090: DatabaseEntry key = new DatabaseEntry();
091: DatabaseEntry value = new DatabaseEntry();
092: while (OperationStatus.SUCCESS.equals(cursor.getNext(key,
093: value, LockMode.DEFAULT))) {
094: allClazzBytes.put(new Integer(Conversion.bytes2Int(key
095: .getData())), value.getData());
096: }
097: cursor.close();
098: tx.commit();
099: } catch (DatabaseException e) {
100: abortOnError(cursor, tx);
101: e.printStackTrace();
102: throw new DBException(e);
103: }
104: return allClazzBytes;
105: }
106: }
|