@Override public long write(ByteBuffer buffer) { return other.write(buffer) ; }
@Override public Block allocWrite(int maxBytes) { return other.allocWrite(maxBytes) ; }
@Override public void close() { file.close() ; }
@Override protected void _abort(TxnId txnId, TxnObjectFile state) { if ( isWriteTxn() ) { // One will imply the other. objFile.truncate(state.length) ; //objFile.reposition(state.position) ; // sync to make sure the file looses the unwanted part. // Neater, no abandoned sections (outside crashes). objFile.sync() ; } }
public TransObjectFile(ObjectFile objFile, ComponentId cid, BufferChannel bufferChannel) { super(cid) ; stateMgr = new ObjectFileState(bufferChannel, 0L, 0L) ; this.objFile = objFile ; // These may be updated by recovery. Start by setting to the // "clean start" settings. length = new AtomicLong(objFile.length()) ; position = new AtomicLong(objFile.position()) ; }
@Test public void objectfile_02() { Block block = file.allocWrite(10) ; fill(block.getByteBuffer()) ; file.completeWrite(block) ; long x1 = block.getId() ; assertEquals(0, x1) ; ByteBuffer bb = file.read(x1) ; // position assertTrue(sameValue(block.getByteBuffer(), bb)) ; }
@Test public void transObjectFile_4() { String str1 = "Test4" ; String str2 = "TheNext" ; long x1 = writeOne(transactional, transObjectFile, str1) ; ByteBuffer bb = str2bb(str2) ; long x2 = baseObjectFile.write(bb) ; baseObjectFile.sync(); long x3 = Txn.calculateRead(transactional, ()->transObjectFile.length()) ; assertEquals(x2, x3); assertNotEquals(x3, baseObjectFile.length()); // Fake recovery. ByteBuffer bbj = ByteBuffer.allocate(2*Long.BYTES) ; bbj.putLong(x3) ; bbj.putLong(0) ; bbj.rewind() ; journal.write(JournalEntryType.REDO, transObjectFile.getComponentId(), bbj) ; journal.writeJournal(JournalEntry.COMMIT) ; // Recovery. //transObjectFile.recover(bb); TransactionalBase transBase = (TransactionalBase)TransactionalFactory.createTransactional(journal, transObjectFile) ; ByteBuffer bb1 = Txn.calculateRead(transBase, ()->transObjectFile.read(x3)) ; String s1 = Bytes.fromByteBuffer(bb1) ; assertEquals(str2, s1); // Woot! transBase.getTxnMgr().shutdown(); } }
@Test public void objectfile_06() { ByteBuffer bb1 = ByteBuffer.allocate(10) ; fill(bb1) ; ByteBuffer bb2 = ByteBuffer.allocate(20) ; fill(bb2) ; long x1 = file.write(bb1) ; long x2 = file.write(bb2) ; ByteBuffer bb1a = file.read(x1) ; ByteBuffer bb2a = file.read(x2) ; assertNotSame(bb1a, bb2a) ; assertTrue(sameValue(bb1, bb1a)) ; assertTrue(sameValue(bb2, bb2a)) ; }
public long write(String str) { str = compress(str) ; Block block = file.allocWrite(4*str.length()) ; int len = Bytes.toByteBuffer(str, block.getByteBuffer()) ; block.getByteBuffer().flip() ; file.completeWrite(block) ; return block.getId() ; }
@Override public long position() { return other.position() ; }
@Override public ByteBuffer read(long id) { return other.read(id) ; }
@Override public void sync() { file.sync() ; }
@Override public void completeWrite(Block buffer) { other.completeWrite(buffer) ; }
@Override public void truncate(long size) { other.truncate(size) ; }
@Override public long length() { return other.length() ; }
@Override public void abortWrite(Block buffer) { other.abortWrite(buffer) ; }
private void writePrealloc(int sizeOfBuffer, int... sizes) { ObjectFile file = make(sizeOfBuffer) ; int N = sizes.length ; Block blocks[] = new Block[N] ; ByteBuffer read[] = new ByteBuffer[N] ; for ( int i = 0 ; i < N ; i++ ) { blocks[i] = file.allocWrite(sizes[i]) ; fill(blocks[i].getByteBuffer()) ; file.completeWrite(blocks[i]) ; } for ( int i = 0 ; i < N ; i++ ) { read[i] = file.read(blocks[i].getId()) ; assertNotSame(blocks[i].getByteBuffer(), read[i]) ; sameValue(blocks[i].getByteBuffer(), read[i]) ; } }