1 4 package com.tc.objectserver.persistence.sleepycat; 5 6 import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean; 7 8 import com.sleepycat.bind.serial.ClassCatalog; 9 import com.sleepycat.bind.serial.SerialBinding; 10 import com.sleepycat.je.Cursor; 11 import com.sleepycat.je.CursorConfig; 12 import com.sleepycat.je.Database; 13 import com.sleepycat.je.DatabaseEntry; 14 import com.sleepycat.je.LockMode; 15 import com.sleepycat.je.OperationStatus; 16 import com.tc.objectserver.persistence.api.PersistenceTransaction; 17 import com.tc.objectserver.persistence.api.PersistenceTransactionProvider; 18 import com.tc.objectserver.persistence.api.StringIndexPersistor; 19 import com.tc.objectserver.persistence.sleepycat.SleepycatPersistor.SleepycatPersistorBase; 20 import com.tc.util.Conversion; 21 22 import gnu.trove.TLongObjectHashMap; 23 24 public final class SleepycatStringIndexPersistor extends SleepycatPersistorBase implements StringIndexPersistor { 25 26 private final PersistenceTransactionProvider ptp; 27 private final Database stringIndexDatabase; 28 private final CursorConfig stringIndexCursorConfig; 29 private final SerialBinding serialBinding; 30 private final SynchronizedBoolean initialized = new SynchronizedBoolean(false); 31 32 public SleepycatStringIndexPersistor(PersistenceTransactionProvider ptp, Database stringIndexDatabase, 33 CursorConfig stringIndexCursorConfig, ClassCatalog classCatalog) { 34 this.ptp = ptp; 35 this.stringIndexDatabase = stringIndexDatabase; 36 this.stringIndexCursorConfig = stringIndexCursorConfig; 37 this.serialBinding = new SerialBinding(classCatalog, String .class); 38 } 39 40 public TLongObjectHashMap loadMappingsInto(TLongObjectHashMap target) { 41 if (initialized.set(true)) throw new AssertionError ("Attempt to use more than once."); 42 Cursor cursor = null; 43 PersistenceTransaction tx = null; 44 try { 45 tx = ptp.newTransaction(); 46 DatabaseEntry key = new DatabaseEntry(), value = new DatabaseEntry(); 47 cursor = stringIndexDatabase.openCursor(pt2nt(tx), stringIndexCursorConfig); 48 while (OperationStatus.SUCCESS.equals(cursor.getNext(key, value, LockMode.DEFAULT))) { 49 target.put(Conversion.bytes2Long(key.getData()), bytes2String(value)); 50 } 51 cursor.close(); 52 tx.commit(); 53 } catch (Throwable t) { 54 abortOnError(cursor, tx); 55 throw new DBException(t); 56 } 57 return target; 58 } 59 60 public void saveMapping(long index, String string) { 61 PersistenceTransaction tx = null; 62 try { 63 tx = ptp.newTransaction(); 64 DatabaseEntry key = new DatabaseEntry(), value = new DatabaseEntry(); 65 key.setData(Conversion.long2Bytes(index)); 66 string2Bytes(string, value); 67 stringIndexDatabase.put(pt2nt(tx), key, value); 68 tx.commit(); 69 } catch (Throwable t) { 70 abortOnError(tx); 71 throw new DBException(t); 72 } 73 } 74 75 private String bytes2String(DatabaseEntry entry) { 76 return (String ) serialBinding.entryToObject(entry); 77 } 78 79 private void string2Bytes(String string, DatabaseEntry entry) { 80 serialBinding.objectToEntry(string, entry); 81 } 82 83 } | Popular Tags |