001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.objectserver.persistence.sleepycat;
006:
007: import com.sleepycat.je.Database;
008: import com.sleepycat.je.DatabaseEntry;
009: import com.sleepycat.je.LockMode;
010: import com.sleepycat.je.OperationStatus;
011: import com.tc.logging.TCLogger;
012: import com.tc.objectserver.persistence.api.PersistenceTransaction;
013: import com.tc.objectserver.persistence.api.PersistenceTransactionProvider;
014: import com.tc.objectserver.persistence.api.PersistentMapStore;
015: import com.tc.objectserver.persistence.sleepycat.SleepycatPersistor.SleepycatPersistorBase;
016: import com.tc.util.Conversion;
017:
018: public class SleepycatMapStore extends SleepycatPersistorBase implements
019: PersistentMapStore {
020:
021: private final PersistenceTransactionProvider persistenceTransactionProvider;
022: private final TCLogger logger;
023: private final Database database;
024:
025: public SleepycatMapStore(
026: PersistenceTransactionProvider persistenceTransactionProvider,
027: TCLogger logger, Database clusterStateStoreDatabase) {
028: this .persistenceTransactionProvider = persistenceTransactionProvider;
029: this .logger = logger;
030: this .database = clusterStateStoreDatabase;
031: }
032:
033: public String get(String key) {
034: if (key == null) {
035: throw new NullPointerException();
036: }
037:
038: PersistenceTransaction tx = persistenceTransactionProvider
039: .newTransaction();
040: try {
041: DatabaseEntry dkey = new DatabaseEntry();
042: dkey.setData(Conversion.string2Bytes(key));
043: DatabaseEntry dvalue = new DatabaseEntry();
044: OperationStatus status = this .database.get(pt2nt(tx), dkey,
045: dvalue, LockMode.DEFAULT);
046: tx.commit();
047:
048: if (OperationStatus.SUCCESS.equals(status)) {
049: return Conversion.bytes2String(dvalue.getData());
050: } else if (OperationStatus.NOTFOUND.equals(status)) {
051: return null;
052: } else {
053: throw new DBException(
054: "Unable to retrieve value for key " + key
055: + " in SleepycatMapStore : " + status);
056: }
057: } catch (Exception t) {
058: abortOnError(tx);
059: logger.error("Exception on get ", t);
060: throw (t instanceof DBException ? (DBException) t
061: : new DBException(t));
062: }
063: }
064:
065: public void put(String key, String value) {
066: if (key == null || value == null) {
067: throw new NullPointerException();
068: }
069:
070: PersistenceTransaction tx = persistenceTransactionProvider
071: .newTransaction();
072: try {
073: DatabaseEntry dkey = new DatabaseEntry();
074: dkey.setData(Conversion.string2Bytes(key));
075: DatabaseEntry dvalue = new DatabaseEntry();
076: dvalue.setData(Conversion.string2Bytes(value));
077: OperationStatus status = this .database.put(pt2nt(tx), dkey,
078: dvalue);
079:
080: if (!OperationStatus.SUCCESS.equals(status)) {
081: throw new DBException("Unable to store value: " + value
082: + " for key: " + key + "): " + status);
083: }
084: tx.commit();
085: } catch (Exception t) {
086: abortOnError(tx);
087: logger.error("Exception on put ", t);
088: throw (t instanceof DBException ? (DBException) t
089: : new DBException(t));
090: }
091: }
092:
093: public boolean remove(String key) {
094: if (key == null) {
095: throw new NullPointerException();
096: }
097:
098: PersistenceTransaction tx = persistenceTransactionProvider
099: .newTransaction();
100: try {
101: DatabaseEntry dkey = new DatabaseEntry();
102: dkey.setData(Conversion.string2Bytes(key));
103: OperationStatus status = this .database.delete(pt2nt(tx),
104: dkey);
105: tx.commit();
106:
107: if (OperationStatus.NOTFOUND.equals(status)) {
108: return false;
109: } else if (!OperationStatus.SUCCESS.equals(status)) {
110: throw new DBException("Unable to remove value for key "
111: + key + " in SleepycatMapStore : " + status);
112: }
113: return true;
114: } catch (Exception t) {
115: abortOnError(tx);
116: logger.error("Exception on remove ", t);
117: throw (t instanceof DBException ? (DBException) t
118: : new DBException(t));
119: }
120: }
121:
122: }
|