当前位置: 首页>>代码示例>>Java>>正文


Java LedgerHandle.addEntry方法代码示例

本文整理汇总了Java中org.apache.bookkeeper.client.LedgerHandle.addEntry方法的典型用法代码示例。如果您正苦于以下问题:Java LedgerHandle.addEntry方法的具体用法?Java LedgerHandle.addEntry怎么用?Java LedgerHandle.addEntry使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.bookkeeper.client.LedgerHandle的用法示例。


在下文中一共展示了LedgerHandle.addEntry方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createLedgers

import org.apache.bookkeeper.client.LedgerHandle; //导入方法依赖的package包/类
@Test
public void createLedgers() throws Exception {
    try {
        BookKeeper bookKeeperClient = getKeeper();
        LedgerHandle ledger = bookKeeperClient.createLedger(BookKeeper.DigestType.MAC, "123456".getBytes(Charsets.UTF_8));
        long entryId = ledger.addEntry("Hello World".getBytes());
        System.out.println(entryId);
        Enumeration<LedgerEntry> enumeration = ledger.readEntries(0, 99);
        while (enumeration.hasMoreElements()) {
            LedgerEntry entry = enumeration.nextElement();
            System.out.println(entry.getEntryId());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
开发者ID:aCoder2013,项目名称:fastmq,代码行数:17,代码来源:BookKeeperTest.java

示例2: testBookieRecovery

import org.apache.bookkeeper.client.LedgerHandle; //导入方法依赖的package包/类
@Test
public void testBookieRecovery() throws Exception{
    bkc = new BookKeeper("127.0.0.1");
    
    //Shutdown all but 1 bookie
    bs.get(0).shutdown();
    bs.get(1).shutdown();
    bs.get(2).shutdown();
    
    byte[] passwd = "blah".getBytes();
    LedgerHandle lh = bkc.createLedger(1, 1,digestType, passwd);
    
    int numEntries = 100;
    for (int i=0; i< numEntries; i++){
        byte[] data = (""+i).getBytes();
        lh.addEntry(data);
    }
    
    bs.get(3).shutdown();
    BookieServer server = new BookieServer(initialPort + 3, HOSTPORT, tmpDirs.get(3), new File[] { tmpDirs.get(3)});
    server.start();
    bs.set(3, server);

    assertEquals(numEntries - 1 , lh.getLastAddConfirmed());
    Enumeration<LedgerEntry> entries = lh.readEntries(0, lh.getLastAddConfirmed());
    
    int numScanned = 0;
    while (entries.hasMoreElements()){
        assertEquals((""+numScanned), new String(entries.nextElement().getEntry()));
        numScanned++;
    }
    assertEquals(numEntries, numScanned);
    
    
}
 
开发者ID:gerritjvv,项目名称:bigstreams,代码行数:36,代码来源:BookieFailureTest.java

示例3: testInternal

import org.apache.bookkeeper.client.LedgerHandle; //导入方法依赖的package包/类
private void testInternal(int numEntries) throws Exception {
    /*
     * Create ledger.
     */
    LedgerHandle beforelh = null;
    beforelh = bkc.createLedger(digestType, "".getBytes());

    String tmp = "BookKeeper is cool!";
    for (int i = 0; i < numEntries; i++) {
        beforelh.addEntry(tmp.getBytes());
    }

    long length = (long) (numEntries * tmp.length());
    
    /*
     * Try to open ledger.
     */
    LedgerHandle afterlh = bkc.openLedger(beforelh.getId(), digestType, "".getBytes());

    /*
     * Check if has recovered properly.
     */
    assertTrue("Has not recovered correctly: " + afterlh.getLastAddConfirmed(),
            afterlh.getLastAddConfirmed() == numEntries - 1);       
    assertTrue("Has not set the length correctly: " + afterlh.getLength() + ", " + length, 
            afterlh.getLength() == length);
}
 
开发者ID:gerritjvv,项目名称:bigstreams,代码行数:28,代码来源:LedgerRecoveryTest.java

示例4: testBadVersionOnTwoAllocators

import org.apache.bookkeeper.client.LedgerHandle; //导入方法依赖的package包/类
@Test(timeout = 60000)
public void testBadVersionOnTwoAllocators() throws Exception {
    String allocationPath = "/allocation-bad-version";
    zkc.get().create(allocationPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    Stat stat = new Stat();
    byte[] data = zkc.get().getData(allocationPath, false, stat);
    Versioned<byte[]> allocationData = new Versioned<byte[]>(data, new ZkVersion(stat.getVersion()));

    SimpleLedgerAllocator allocator1 =
            new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
    SimpleLedgerAllocator allocator2 =
            new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
    allocator1.allocate();
    // wait until allocated
    ZKTransaction txn1 = newTxn();
    LedgerHandle lh = FutureUtils.result(allocator1.tryObtain(txn1, NULL_LISTENER));
    allocator2.allocate();
    ZKTransaction txn2 = newTxn();
    try {
        FutureUtils.result(allocator2.tryObtain(txn2, NULL_LISTENER));
        fail("Should fail allocating on second allocator as allocator1 is starting allocating something.");
    } catch (ZKException zke) {
        assertEquals(KeeperException.Code.BADVERSION, zke.getKeeperExceptionCode());
    }
    FutureUtils.result(txn1.execute());
    Utils.close(allocator1);
    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());
    Enumeration<LedgerEntry> entries = readLh.readEntries(eid, eid);
    int i = 0;
    while (entries.hasMoreElements()) {
        LedgerEntry entry = entries.nextElement();
        assertEquals("hello world", new String(entry.getEntry(), UTF_8));
        ++i;
    }
    assertEquals(1, i);
}
 
开发者ID:twitter,项目名称:distributedlog,代码行数:41,代码来源:TestLedgerAllocator.java

示例5: name

import org.apache.bookkeeper.client.LedgerHandle; //导入方法依赖的package包/类
@Test
public void name() throws Exception {
    // Create a client object for the local ensemble. This
    // operation throws multiple exceptions, so make sure to
    // use a try/catch block when instantiating client objects.
    BookKeeper bkc = new BookKeeper("localhost:2181");

    // A password for the new ledger
    byte[] ledgerPassword = "123456".getBytes();

    // Create a new ledger and fetch its identifier
    LedgerHandle lh = bkc.createLedger(BookKeeper.DigestType.MAC, ledgerPassword);
    long ledgerId = lh.getId();
    System.out.println("ledgerId: " + ledgerId);

    // Create a buffer for four-byte entries
    ByteBuffer entry = ByteBuffer.allocate(4);

    int numberOfEntries = 100;

    // Add entries to the ledger, then close it
    for (int i = 0; i < numberOfEntries; i++) {
        entry.putInt(i);
        entry.position(0);
        lh.addEntry(entry.array());
    }
    lh.close();

    // Open the ledger for reading
    lh = bkc.openLedger(ledgerId, BookKeeper.DigestType.MAC, ledgerPassword);

    // Read all available entries
    Enumeration<LedgerEntry> entries = lh.readEntries(0, numberOfEntries-1);

    while (entries.hasMoreElements()) {
        ByteBuffer result = ByteBuffer.wrap(entries.nextElement().getEntry());
        Integer retrEntry = result.getInt();

        // Print the integer stored in each entry
        System.out.println(String.format("Result: %s", retrEntry));
    }

    // Close the ledger and the client
    lh.close();
    bkc.close();
}
 
开发者ID:aCoder2013,项目名称:fastmq,代码行数:47,代码来源:BookKeeperTest.java

示例6: testSuccessAllocatorShouldDeleteUnusedledger

import org.apache.bookkeeper.client.LedgerHandle; //导入方法依赖的package包/类
@Test(timeout = 60000)
public void testSuccessAllocatorShouldDeleteUnusedledger() throws Exception {
    String allocationPath = "/allocation-delete-unused-ledger";
    zkc.get().create(allocationPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    Stat stat = new Stat();
    byte[] data = zkc.get().getData(allocationPath, false, stat);

    Versioned<byte[]> allocationData = new Versioned<byte[]>(data, new ZkVersion(stat.getVersion()));

    SimpleLedgerAllocator allocator1 =
            new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
    allocator1.allocate();
    // wait until allocated
    ZKTransaction txn1 = newTxn();
    LedgerHandle lh1 = FutureUtils.result(allocator1.tryObtain(txn1, NULL_LISTENER));

    // Second allocator kicks in
    stat = new Stat();
    data = zkc.get().getData(allocationPath, false, stat);
    allocationData = new Versioned<byte[]>(data, new ZkVersion(stat.getVersion()));
    SimpleLedgerAllocator allocator2 =
            new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc);
    allocator2.allocate();
    // wait until allocated
    ZKTransaction txn2 = newTxn();
    LedgerHandle lh2 = FutureUtils.result(allocator2.tryObtain(txn2, NULL_LISTENER));

    // should fail to commit txn1 as version is changed by second allocator
    try {
        FutureUtils.result(txn1.execute());
        fail("Should fail commit obtaining ledger handle from first allocator as allocator is modified by second allocator.");
    } catch (ZKException ke) {
        // as expected
    }
    FutureUtils.result(txn2.execute());
    Utils.close(allocator1);
    Utils.close(allocator2);

    // ledger handle should be deleted
    try {
        lh1.close();
        fail("LedgerHandle allocated by allocator1 should be deleted.");
    } catch (BKException bke) {
        // as expected
    }
    try {
        bkc.get().openLedger(lh1.getId(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes());
        fail("LedgerHandle allocated by allocator1 should be deleted.");
    } catch (BKException.BKNoSuchLedgerExistsException nslee) {
        // as expected
    }
    long eid = lh2.addEntry("hello world".getBytes());
    lh2.close();
    LedgerHandle readLh = bkc.get().openLedger(lh2.getId(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes());
    Enumeration<LedgerEntry> entries = readLh.readEntries(eid, eid);
    int i = 0;
    while (entries.hasMoreElements()) {
        LedgerEntry entry = entries.nextElement();
        assertEquals("hello world", new String(entry.getEntry(), UTF_8));
        ++i;
    }
    assertEquals(1, i);
}
 
开发者ID:twitter,项目名称:distributedlog,代码行数:64,代码来源:TestLedgerAllocator.java

示例7: writeEntriestoLedgers

import org.apache.bookkeeper.client.LedgerHandle; //导入方法依赖的package包/类
/**
 * Helper method to write dummy ledger entries to all of the ledgers passed.
 * 
 * @param numEntries
 *            Number of ledger entries to write for each ledger
 * @param startEntryId
 *            The first entry Id we're expecting to write for each ledger
 * @param lhs
 *            List of LedgerHandles for all ledgers to write entries to
 * @throws BKException
 * @throws InterruptedException
 */
private void writeEntriestoLedgers(int numEntries, long startEntryId, List<LedgerHandle> lhs) throws BKException,
        InterruptedException {
    for (LedgerHandle lh : lhs) {
        for (int i = 0; i < numEntries; i++) {
            lh.addEntry(("LedgerId: " + lh.getId() + ", EntryId: " + (startEntryId + i)).getBytes());
        }
    }
}
 
开发者ID:gerritjvv,项目名称:bigstreams,代码行数:21,代码来源:BookieRecoveryTest.java


注:本文中的org.apache.bookkeeper.client.LedgerHandle.addEntry方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。