/** Get data as string - convenience operation */ public String getString() { ByteBuffer bb = getBlob() ; if (bb == null ) return null ; int x = bb.position() ; String s = Bytes.fromByteBuffer(bb) ; bb.position(x) ; return s ; }
/** Set data from string - convenience operation */ public void setString(String dataStr) { checkWriteTxn(); if ( dataStr == null ) { setBlob(null); return ; } // Attempt to reuse the write-transaction byte buffer // We can't reuse if it's the blobRef (shared by other transactions) // but if it's a new to this write transaction buffer we can reuse. int maxNeeded = dataStr.length()*4 ; ByteBuffer bb = getDataState().getByteBuffer() ; if ( bb == blobRef.get() ) bb = ByteBuffer.allocate(maxNeeded) ; else if ( bb.capacity() >= maxNeeded ) bb.clear() ; else bb = ByteBuffer.allocate(maxNeeded) ; Bytes.toByteBuffer(dataStr, bb) ; bb.flip() ; setBlob(bb); }
@Override protected BlobState _promote(TxnId txnId, BlobState state) { // Our write state is the read state. return createState(); }
/** Set the byte buffer. * The byte buffer should not be accessed except by {@link #getBlob}. * We avoid a copy in and copy out - we trust the caller. * The byte buffer should be configured for read if used with {@link #getString}. */ public void setBlob(ByteBuffer bb) { checkWriteTxn(); getDataState().setByteBuffer(bb); }
public ByteBuffer getBlob() { if ( isActiveTxn() ) return getDataState().getByteBuffer() ; return blobRef.get() ; }
TransBlob tBlob1 = new TransBlob(cid1, chan) ; TransBlob tBlob2 = new TransBlob(cid2, chan) ; ByteBuffer blob1 = tBlob1.getBlob() ; assertNotNull(blob1); String s1 = L.byteBufferToString(blob1) ; assertEquals(str1,s1) ; ByteBuffer blob2 = tBlob2.getBlob() ; assertNotNull(blob2); String s2 = L.byteBufferToString(blob2) ;
@Test public void transBlobFile_2() throws Exception { Journal journal = Journal.create(Location.create(DIR)) ; BufferChannel chan = BufferChannelFile.create(DATA) ; ComponentId cid = ComponentId.allocLocal() ; TransBlob transBlob = new TransBlob(cid, chan) ; Transactional transactional = TransactionalFactory.createTransactional(journal, transBlob) ; String str = "Hello1" ; Txn.executeWrite(transactional, ()->{ transBlob.setString("one"); }) ; Txn.executeWrite(transactional, ()->{ transBlob.setString("two"); }) ; chan.close() ; journal.close() ; String s = FileUtils.readWholeFileAsUTF8(DATA) ; assertEquals("two", s); }
@Test public void transBlob_3() { String str1 = "one" ; String str2 = "two" ; write(transactional, transBlob, str1) ; String s1 = transBlob.getString() ; assertEquals(str1, s1) ; String s2 = read(transactional, transBlob) ; assertEquals(str1, s2) ; // Start now. ThreadAction tt = ThreadTxn.threadTxnRead(transactional, ()-> { String sr = transBlob.getString() ; Assert.assertEquals(str1, sr) ; }) ; write(transactional, transBlob, str2) ; Txn.executeWrite(transactional, ()->{ transBlob.setString(str2) ; String s = transBlob.getString() ; assertEquals(str2, s) ; }) ; // Run later, after W transaction. tt.run(); } }
public static String read(Transactional transactional, TransBlob transBlob) { return Txn.calculateRead(transactional, ()->{ return transBlob.getString() ; }) ; }
@Before public void before() { journal = Journal.create(Location.mem()) ; BufferChannel chan = BufferChannelMem.create("TestTransBlob") ; ComponentId cid = ComponentId.allocLocal() ; transBlob = new TransBlob(cid, chan) ; transactional = TransactionalFactory.createTransactional(journal, transBlob) ; }
public TransBlob(ComponentId cid, BufferChannel file) { super(cid) ; this.file = file ; read() ; }
@Override public String toString() { return getComponentId().label() ; }
TransBlob tBlob1 = new TransBlob(cid1, chan) ; TransBlob tBlob2 = new TransBlob(cid2, chan) ; ByteBuffer blob1 = tBlob1.getBlob() ; assertNotNull(blob1); String s1 = L.byteBufferToString(blob1) ; assertEquals(str1,s1) ; ByteBuffer blob2 = tBlob2.getBlob() ; assertNotNull(blob2); String s2 = L.byteBufferToString(blob2) ;
/** Set the byte buffer. * The byte buffer should not be accessed except by {@link #getBlob}. * We avoid a copy in and copy out - we trust the caller. * The byte buffer should be configured for read if used with {@link #getString}. */ public void setBlob(ByteBuffer bb) { checkWriteTxn(); getDataState().setByteBuffer(bb); }
public ByteBuffer getBlob() { if ( isActiveTxn() ) return getDataState().getByteBuffer() ; return blobRef.get() ; }
@Test public void transBlobFile_2() throws Exception { Journal journal = Journal.create(Location.create(DIR)) ; BufferChannel chan = BufferChannelFile.create(DATA) ; ComponentId cid = ComponentId.allocLocal() ; TransBlob transBlob = new TransBlob(cid, chan) ; Transactional transactional = TransactionalFactory.createTransactional(journal, transBlob) ; String str = "Hello1" ; Txn.executeWrite(transactional, ()->{ transBlob.setString("one"); }) ; Txn.executeWrite(transactional, ()->{ transBlob.setString("two"); }) ; chan.close() ; journal.close() ; String s = FileUtils.readWholeFileAsUTF8(DATA) ; assertEquals("two", s); }
@Test public void transBlob_3() { String str1 = "one" ; String str2 = "two" ; write(transactional, transBlob, str1) ; String s1 = transBlob.getString() ; assertEquals(str1, s1) ; String s2 = read(transactional, transBlob) ; assertEquals(str1, s2) ; // Start now. ThreadAction tt = ThreadTxn.threadTxnRead(transactional, ()-> { String sr = transBlob.getString() ; Assert.assertEquals(str1, sr) ; }) ; write(transactional, transBlob, str2) ; Txn.executeWrite(transactional, ()->{ transBlob.setString(str2) ; String s = transBlob.getString() ; assertEquals(str2, s) ; }) ; // Run later, after W transaction. tt.run(); } }
public static String read(Transactional transactional, TransBlob transBlob) { return Txn.calculateRead(transactional, ()->{ return transBlob.getString() ; }) ; }
@Before public void before() { journal = Journal.create(Location.mem()) ; BufferChannel chan = BufferChannelMem.create("TestTransBlob") ; ComponentId cid = ComponentId.allocLocal() ; transBlob = new TransBlob(cid, chan) ; transactional = TransactionalFactory.createTransactional(journal, transBlob) ; }
public TransBlob(ComponentId cid, BufferChannel file) { super(cid) ; this.file = file ; read() ; }