KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package com.tc.objectserver.persistence.sleepycat;
5
6 import com.sleepycat.je.Cursor;
7 import com.sleepycat.je.CursorConfig;
8 import com.sleepycat.je.Database;
9 import com.sleepycat.je.DatabaseEntry;
10 import com.sleepycat.je.DatabaseException;
11 import com.sleepycat.je.LockMode;
12 import com.sleepycat.je.OperationStatus;
13 import com.sleepycat.je.Transaction;
14 import com.tc.logging.TCLogger;
15 import com.tc.objectserver.persistence.api.ClassPersistor;
16 import com.tc.objectserver.persistence.api.PersistenceTransactionProvider;
17 import com.tc.objectserver.persistence.sleepycat.SleepycatPersistor.SleepycatPersistorBase;
18 import com.tc.util.Conversion;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 class ClassPersistorImpl extends SleepycatPersistorBase implements ClassPersistor {
24   private final Database classDB;
25   private final PersistenceTransactionProvider ptxp;
26   private final CursorConfig cursorConfig;
27
28   ClassPersistorImpl(PersistenceTransactionProvider ptxp, TCLogger logger, Database classDB) {
29     this.ptxp = ptxp;
30     this.classDB = classDB;
31     this.cursorConfig = new CursorConfig();
32     this.cursorConfig.setReadCommitted(true);
33   }
34
35   public void storeClass(int clazzId, byte[] clazzBytes) {
36     Transaction tx = null;
37     try {
38       tx = pt2nt(ptxp.newTransaction());
39       DatabaseEntry key = new DatabaseEntry();
40       key.setData(Conversion.int2Bytes(clazzId));
41       DatabaseEntry value = new DatabaseEntry();
42       value.setData(clazzBytes);
43       OperationStatus status = this.classDB.put(tx, key, value);
44       tx.commit();
45
46       if (!OperationStatus.SUCCESS.equals(status)) {
47         // Formatting
48
throw new DBException("Unable to store class Bytes: " + clazzId);
49       }
50     } catch (DatabaseException t) {
51       abortOnError(tx);
52       t.printStackTrace();
53       throw new DBException(t);
54     }
55   }
56
57   public byte[] retrieveClass(int clazzId) {
58     Transaction tx = null;
59     try {
60       tx = pt2nt(ptxp.newTransaction());
61       DatabaseEntry key = new DatabaseEntry();
62       key.setData(Conversion.int2Bytes(clazzId));
63       DatabaseEntry value = new DatabaseEntry();
64       OperationStatus status = this.classDB.get(tx, key, value, LockMode.DEFAULT);
65       tx.commit();
66       if (!OperationStatus.SUCCESS.equals(status)) {
67         // Formatting
68
throw new DBException("Unable to retrieve class Bytes: " + clazzId);
69       }
70       return value.getData();
71     } catch (DatabaseException t) {
72       abortOnError(tx);
73       t.printStackTrace();
74       throw new DBException(t);
75     }
76   }
77
78   public Map JavaDoc retrieveAllClasses() {
79     Map JavaDoc allClazzBytes = new HashMap JavaDoc();
80     Cursor cursor = null;
81     Transaction tx = null;
82     try {
83       tx = pt2nt(ptxp.newTransaction());
84       cursor = classDB.openCursor(tx, cursorConfig);
85       DatabaseEntry key = new DatabaseEntry();
86       DatabaseEntry value = new DatabaseEntry();
87       while (OperationStatus.SUCCESS.equals(cursor.getNext(key, value, LockMode.DEFAULT))) {
88         allClazzBytes.put(new Integer JavaDoc(Conversion.bytes2Int(key.getData())), value.getData());
89       }
90       cursor.close();
91       tx.commit();
92     } catch (DatabaseException e) {
93       abortOnError(cursor, tx);
94       e.printStackTrace();
95       throw new DBException(e);
96     }
97     return allClazzBytes;
98   }
99 }
Popular Tags