KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > persistence > sleepycat > SleepycatMapStore


1 /*
2  * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.objectserver.persistence.sleepycat;
6
7 import com.sleepycat.je.Database;
8 import com.sleepycat.je.DatabaseEntry;
9 import com.sleepycat.je.LockMode;
10 import com.sleepycat.je.OperationStatus;
11 import com.tc.logging.TCLogger;
12 import com.tc.objectserver.persistence.api.PersistenceTransaction;
13 import com.tc.objectserver.persistence.api.PersistenceTransactionProvider;
14 import com.tc.objectserver.persistence.api.PersistentMapStore;
15 import com.tc.objectserver.persistence.sleepycat.SleepycatPersistor.SleepycatPersistorBase;
16 import com.tc.util.Conversion;
17
18 public class SleepycatMapStore extends SleepycatPersistorBase implements PersistentMapStore {
19
20   private final PersistenceTransactionProvider persistenceTransactionProvider;
21   private final TCLogger logger;
22   private final Database database;
23
24   public SleepycatMapStore(PersistenceTransactionProvider persistenceTransactionProvider, TCLogger logger,
25                            Database clusterStateStoreDatabase) {
26     this.persistenceTransactionProvider = persistenceTransactionProvider;
27     this.logger = logger;
28     this.database = clusterStateStoreDatabase;
29   }
30
31   public String JavaDoc get(String JavaDoc key) {
32     if(key == null) { throw new NullPointerException JavaDoc(); }
33     
34     PersistenceTransaction tx = persistenceTransactionProvider.newTransaction();
35     try {
36       DatabaseEntry dkey = new DatabaseEntry();
37       dkey.setData(Conversion.string2Bytes(key));
38       DatabaseEntry dvalue = new DatabaseEntry();
39       OperationStatus status = this.database.get(pt2nt(tx), dkey, dvalue, LockMode.DEFAULT);
40       tx.commit();
41
42       if (OperationStatus.SUCCESS.equals(status)) {
43         return Conversion.bytes2String(dvalue.getData());
44       } else if (OperationStatus.NOTFOUND.equals(status)) {
45         return null;
46       } else {
47         throw new DBException("Unable to retrieve value for key " + key + " in SleepycatMapStore : " + status);
48       }
49     } catch (Exception JavaDoc t) {
50       abortOnError(tx);
51       logger.error("Exception on get ", t);
52       throw (t instanceof DBException ? (DBException) t : new DBException(t));
53     }
54   }
55
56   public void put(String JavaDoc key, String JavaDoc value) {
57     if(key == null || value == null) { throw new NullPointerException JavaDoc(); }
58     
59     PersistenceTransaction tx = persistenceTransactionProvider.newTransaction();
60     try {
61       DatabaseEntry dkey = new DatabaseEntry();
62       dkey.setData(Conversion.string2Bytes(key));
63       DatabaseEntry dvalue = new DatabaseEntry();
64       dvalue.setData(Conversion.string2Bytes(value));
65       OperationStatus status = this.database.put(pt2nt(tx), dkey, dvalue);
66
67       if (!OperationStatus.SUCCESS.equals(status)) { throw new DBException("Unable to store value: " + value
68                                                                            + " for key: " + key + "): " + status); }
69       tx.commit();
70     } catch (Exception JavaDoc t) {
71       abortOnError(tx);
72       logger.error("Exception on put ", t);
73       throw (t instanceof DBException ? (DBException) t : new DBException(t));
74     }
75   }
76
77   public boolean remove(String JavaDoc key) {
78     if(key == null) { throw new NullPointerException JavaDoc(); }
79     
80     PersistenceTransaction tx = persistenceTransactionProvider.newTransaction();
81     try {
82       DatabaseEntry dkey = new DatabaseEntry();
83       dkey.setData(Conversion.string2Bytes(key));
84       OperationStatus status = this.database.delete(pt2nt(tx), dkey);
85       tx.commit();
86
87       if (OperationStatus.NOTFOUND.equals(status)) {
88         return false;
89       } else if (!OperationStatus.SUCCESS.equals(status)) { throw new DBException("Unable to remove value for key "
90                                                                                   + key + " in SleepycatMapStore : "
91                                                                                   + status); }
92       return true;
93     } catch (Exception JavaDoc t) {
94       abortOnError(tx);
95       logger.error("Exception on remove ", t);
96       throw (t instanceof DBException ? (DBException) t : new DBException(t));
97     }
98   }
99
100 }
101
Popular Tags