1 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 ; 21 import java.util.Map ; 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 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 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 retrieveAllClasses() { 79 Map allClazzBytes = new HashMap (); 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 (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 |