Search in sources :

Example 1 with IncrementOption

use of org.apache.bookkeeper.api.kv.options.IncrementOption in project bookkeeper by apache.

the class TestPByteBufTableImpl method testBasicOperations.

@SuppressWarnings("unchecked")
@Test
public void testBasicOperations() throws Exception {
    when(mockMetaRangeClient.getActiveDataRanges()).thenReturn(FutureUtils.value(streamRanges1));
    ConcurrentMap<Long, PTable<ByteBuf, ByteBuf>> tableRanges = Maps.newConcurrentMap();
    for (RangeProperties rangeProps : streamRanges1.getRanges().values()) {
        tableRanges.put(rangeProps.getRangeId(), mock(PTable.class));
    }
    RangeRouter<ByteBuf> mockRouter = mock(RangeRouter.class);
    when(mockRouter.getRange(any(ByteBuf.class))).thenAnswer(invocationOnMock -> {
        ByteBuf key = invocationOnMock.getArgument(0);
        byte[] keyData = ByteBufUtil.getBytes(key);
        return Bytes.toLong(keyData, 0);
    });
    TableRangeFactory<ByteBuf, ByteBuf> trFactory = (streamProps1, rangeProps, executor, opFactory, resultFactory, kvFactory) -> tableRanges.get(rangeProps.getRangeId());
    PByteBufTableImpl table = new PByteBufTableImpl(runtime.getMethodName(), streamProps, mockClientManager, scheduler.chooseThread(), trFactory, Optional.of(mockRouter));
    assertEquals(0, table.getTableRanges().size());
    verify(mockRouter, times(0)).setRanges(any(HashStreamRanges.class));
    // initialize the table
    assertTrue(table == FutureUtils.result(table.initialize()));
    verify(mockRouter, times(1)).setRanges(eq(streamRanges1));
    assertEquals(4, table.getTableRanges().size());
    // test get
    for (RangeProperties rangeProps : streamRanges1.getRanges().values()) {
        ByteBuf pkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId()));
        ByteBuf lkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId()));
        try (RangeOption<ByteBuf> option = optionFactory.newRangeOption().build()) {
            table.get(pkey, lkey, option);
            verify(tableRanges.get(rangeProps.getRangeId()), times(1)).get(eq(pkey), eq(lkey), eq(option));
        }
    }
    // test put
    for (RangeProperties rangeProps : streamRanges1.getRanges().values()) {
        ByteBuf pkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId()));
        ByteBuf lkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId()));
        ByteBuf value = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId()));
        try (PutOption<ByteBuf> option = optionFactory.newPutOption().build()) {
            table.put(pkey, lkey, value, option);
            verify(tableRanges.get(rangeProps.getRangeId()), times(1)).put(eq(pkey), eq(lkey), eq(value), eq(option));
        }
    }
    // test increment
    for (RangeProperties rangeProps : streamRanges1.getRanges().values()) {
        ByteBuf pkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId()));
        ByteBuf lkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId()));
        long amount = 100L;
        try (IncrementOption<ByteBuf> option = optionFactory.newIncrementOption().build()) {
            table.increment(pkey, lkey, amount, option);
            verify(tableRanges.get(rangeProps.getRangeId()), times(1)).increment(eq(pkey), eq(lkey), eq(amount), same(option));
        }
    }
    // test delete
    for (RangeProperties rangeProps : streamRanges1.getRanges().values()) {
        ByteBuf pkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId()));
        ByteBuf lkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId()));
        try (DeleteOption<ByteBuf> option = optionFactory.newDeleteOption().build()) {
            table.delete(pkey, lkey, option);
            verify(tableRanges.get(rangeProps.getRangeId()), times(1)).delete(eq(pkey), eq(lkey), eq(option));
        }
    }
    // test txn
    for (RangeProperties rangeProps : streamRanges1.getRanges().values()) {
        ByteBuf pkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId()));
        Txn<ByteBuf, ByteBuf> txn = table.txn(pkey);
        verify(tableRanges.get(rangeProps.getRangeId()), times(1)).txn(eq(pkey));
    }
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HashRouter(org.apache.bookkeeper.common.router.HashRouter) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) MetaRangeClient(org.apache.bookkeeper.clients.impl.internal.api.MetaRangeClient) RangeKeyType(org.apache.bookkeeper.stream.proto.RangeKeyType) ClientException(org.apache.bookkeeper.clients.exceptions.ClientException) OptionFactoryImpl(org.apache.bookkeeper.api.kv.impl.options.OptionFactoryImpl) Unpooled(io.netty.buffer.Unpooled) ConcurrentMap(java.util.concurrent.ConcurrentMap) RangeRouter(org.apache.bookkeeper.clients.impl.routing.RangeRouter) Lists(com.google.common.collect.Lists) ByteBuf(io.netty.buffer.ByteBuf) RangeProperties(org.apache.bookkeeper.stream.proto.RangeProperties) TestName(org.junit.rules.TestName) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Assert.fail(org.junit.Assert.fail) HashStreamRanges(org.apache.bookkeeper.clients.impl.internal.api.HashStreamRanges) DEFAULT_STREAM_CONF(org.apache.bookkeeper.stream.protocol.ProtocolConstants.DEFAULT_STREAM_CONF) Before(org.junit.Before) OrderedScheduler(org.apache.bookkeeper.common.util.OrderedScheduler) Txn(org.apache.bookkeeper.api.kv.Txn) StorageServerClientManager(org.apache.bookkeeper.clients.impl.internal.api.StorageServerClientManager) Assert.assertTrue(org.junit.Assert.assertTrue) DeleteOption(org.apache.bookkeeper.api.kv.options.DeleteOption) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Bytes(org.apache.bookkeeper.common.util.Bytes) NavigableMap(java.util.NavigableMap) PTable(org.apache.bookkeeper.api.kv.PTable) FutureUtils(org.apache.bookkeeper.common.concurrent.FutureUtils) Maps(com.google.common.collect.Maps) OptionFactory(org.apache.bookkeeper.api.kv.options.OptionFactory) Mockito.verify(org.mockito.Mockito.verify) RangeOption(org.apache.bookkeeper.api.kv.options.RangeOption) ByteBufUtil(io.netty.buffer.ByteBufUtil) List(java.util.List) Rule(org.junit.Rule) ProtoUtils(org.apache.bookkeeper.stream.protocol.util.ProtoUtils) StreamProperties(org.apache.bookkeeper.stream.proto.StreamProperties) Optional(java.util.Optional) PutOption(org.apache.bookkeeper.api.kv.options.PutOption) IncrementOption(org.apache.bookkeeper.api.kv.options.IncrementOption) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.same(org.mockito.ArgumentMatchers.same) Mockito.mock(org.mockito.Mockito.mock) HashStreamRanges(org.apache.bookkeeper.clients.impl.internal.api.HashStreamRanges) RangeProperties(org.apache.bookkeeper.stream.proto.RangeProperties) ByteBuf(io.netty.buffer.ByteBuf) PTable(org.apache.bookkeeper.api.kv.PTable) Test(org.junit.Test)

Aggregations

Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufUtil (io.netty.buffer.ByteBufUtil)1 Unpooled (io.netty.buffer.Unpooled)1 List (java.util.List)1 NavigableMap (java.util.NavigableMap)1 Optional (java.util.Optional)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 PTable (org.apache.bookkeeper.api.kv.PTable)1 Txn (org.apache.bookkeeper.api.kv.Txn)1 OptionFactoryImpl (org.apache.bookkeeper.api.kv.impl.options.OptionFactoryImpl)1 DeleteOption (org.apache.bookkeeper.api.kv.options.DeleteOption)1 IncrementOption (org.apache.bookkeeper.api.kv.options.IncrementOption)1 OptionFactory (org.apache.bookkeeper.api.kv.options.OptionFactory)1 PutOption (org.apache.bookkeeper.api.kv.options.PutOption)1 RangeOption (org.apache.bookkeeper.api.kv.options.RangeOption)1 ClientException (org.apache.bookkeeper.clients.exceptions.ClientException)1 HashStreamRanges (org.apache.bookkeeper.clients.impl.internal.api.HashStreamRanges)1