@Test public void simpleTest() throws Exception { LedgerHandle ledger = bkc.createLedger(DigestType.MAC, SECRET.getBytes()); long ledgerId = ledger.getId(); log.info("Writing to ledger: {}", ledgerId); for (int i = 0; i < 10; i++) { String content = "entry-" + i; ledger.addEntry(content.getBytes(Encoding)); } ledger.close(); ledger = bkc.openLedger(ledgerId, DigestType.MAC, SECRET.getBytes()); Enumeration<LedgerEntry> entries = ledger.readEntries(0, 9); while (entries.hasMoreElements()) { LedgerEntry entry = entries.nextElement(); String content = new String(entry.getEntry(), Encoding); log.info("Entry {} lenght={} content='{}'", entry.getEntryId(), entry.getLength(), content); } ledger.close(); }
Utils.close(allocator2); long eid = lh.addEntry("hello world".getBytes()); lh.close(); LedgerHandle readLh = bkc.get().openLedger(lh.getId(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes());
long eid = lh2.addEntry("hello world".getBytes()); lh2.close(); LedgerHandle readLh = bkc.get().openLedger(lh2.getId(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes());
/** * Add entry synchronously to an open ledger. * * @param data * array of bytes to be written to the ledger * @return the entryId of the new inserted entry */ public long addEntry(byte[] data) throws InterruptedException, BKException { return addEntry(data, 0, data.length); }
@Override public synchronized void write(byte[] b) { if (makeSpace(b.length)) { bytebuff.put(b); } else { try { lh.addEntry(b); } catch (InterruptedException ie) { LOG.warn("Interrupted while writing", ie); Thread.currentThread().interrupt(); } catch (BKException bke) { LOG.warn("BookKeeper exception", bke); } } }
@Override public synchronized void flush() { // lets flush all the data // into the ledger entry if (bytebuff.position() > 0) { // copy the bytes into // a new byte buffer and send it out byte[] b = new byte[bytebuff.position()]; LOG.info("Comment: flushing with params " + " " + bytebuff.position()); System.arraycopy(bbytes, 0, b, 0, bytebuff.position()); try { lh.addEntry(b); } catch (InterruptedException ie) { LOG.warn("Interrupted while flusing " + ie); Thread.currentThread().interrupt(); } catch (BKException bke) { LOG.warn("BookKeeper exception ", bke); } } }
lh.addEntry(content.getBytes(UTF_8));