Search in sources :

Example 26 with KVEntry

use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.

the class RawKVGetBenchmark method put.

public void put() {
    final List<KVEntry> batch = Lists.newArrayListWithCapacity(100);
    for (int i = 0; i < KEY_COUNT; i++) {
        byte[] key = BytesUtil.writeUtf8("benchmark_" + i);
        batch.add(new KVEntry(key, VALUE_BYTES));
        if (batch.size() >= 100) {
            this.kvStore.put(batch, null);
            batch.clear();
        }
    }
}
Also used : KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry)

Example 27 with KVEntry

use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.

the class RheaKVGetBenchmark method put.

public void put() {
    final List<KVEntry> batch = Lists.newArrayListWithCapacity(100);
    for (int i = 0; i < KEY_COUNT; i++) {
        byte[] key = BytesUtil.writeUtf8("benchmark_" + i);
        batch.add(new KVEntry(key, VALUE_BYTES));
        if (batch.size() >= 10) {
            this.kvStore.bPut(batch);
            batch.clear();
        }
    }
}
Also used : KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry)

Example 28 with KVEntry

use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.

the class ReverseScanExample method scan.

@SuppressWarnings("unchecked")
public static void scan(final RheaKVStore rheaKVStore) {
    final List<byte[]> keys = Lists.newArrayList();
    for (int i = 0; i < 10; i++) {
        final byte[] bytes = writeUtf8("scan_demo_" + i);
        keys.add(bytes);
        rheaKVStore.bPut(bytes, bytes);
    }
    final byte[] firstKey = keys.get(keys.size() - 1);
    final byte[] lastKey = keys.get(0);
    final String firstKeyString = readUtf8(firstKey);
    final String lastKeyString = readUtf8(lastKey);
    // async scan
    final CompletableFuture<List<KVEntry>> f1 = rheaKVStore.reverseScan(firstKey, lastKey);
    final CompletableFuture<List<KVEntry>> f2 = rheaKVStore.reverseScan(firstKey, lastKey, false);
    final CompletableFuture<List<KVEntry>> f3 = rheaKVStore.reverseScan(firstKeyString, lastKeyString);
    final CompletableFuture<List<KVEntry>> f4 = rheaKVStore.reverseScan(firstKeyString, lastKeyString, false);
    CompletableFuture.allOf(f1, f2, f3, f4).join();
    for (final CompletableFuture<List<KVEntry>> f : new CompletableFuture[] { f1, f2, f3, f4 }) {
        for (final KVEntry kv : f.join()) {
            LOG.info("Async reverseScan: key={}, value={}", readUtf8(kv.getKey()), readUtf8(kv.getValue()));
        }
    }
    // sync scan
    final List<KVEntry> l1 = rheaKVStore.bReverseScan(firstKey, lastKey);
    final List<KVEntry> l2 = rheaKVStore.bReverseScan(firstKey, lastKey, false);
    final List<KVEntry> l3 = rheaKVStore.bReverseScan(firstKeyString, lastKeyString);
    final List<KVEntry> l4 = rheaKVStore.bReverseScan(firstKeyString, lastKeyString, false);
    for (final List<KVEntry> l : new List[] { l1, l2, l3, l4 }) {
        for (final KVEntry kv : l) {
            LOG.info("sync reverseScan: key={}, value={}", readUtf8(kv.getKey()), readUtf8(kv.getValue()));
        }
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry) List(java.util.List)

Example 29 with KVEntry

use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.

the class IteratorExample method iterator.

@SuppressWarnings("unchecked")
public static void iterator(final RheaKVStore rheaKVStore) {
    final List<byte[]> keys = Lists.newArrayList();
    for (int i = 0; i < 10; i++) {
        final byte[] bytes = writeUtf8("iterator_demo_" + i);
        keys.add(bytes);
        rheaKVStore.bPut(bytes, bytes);
    }
    final byte[] firstKey = keys.get(0);
    final byte[] lastKey = keys.get(keys.size() - 1);
    final String firstKeyString = readUtf8(firstKey);
    final String lastKeyString = readUtf8(lastKey);
    final RheaIterator<KVEntry> it1 = rheaKVStore.iterator(firstKey, lastKey, 5);
    final RheaIterator<KVEntry> it2 = rheaKVStore.iterator(firstKey, lastKey, 6, false);
    final RheaIterator<KVEntry> it3 = rheaKVStore.iterator(firstKeyString, lastKeyString, 5);
    final RheaIterator<KVEntry> it4 = rheaKVStore.iterator(firstKeyString, lastKeyString, 6, false);
    for (final RheaIterator<KVEntry> it : new RheaIterator[] { it1, it2, it3, it4 }) {
        while (it.hasNext()) {
            final KVEntry kv = it.next();
            LOG.info("Sync iterator: key={}, value={}", readUtf8(kv.getKey()), readUtf8(kv.getValue()));
        }
    }
}
Also used : KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry) RheaIterator(com.alipay.sofa.jraft.rhea.client.RheaIterator)

Example 30 with KVEntry

use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.

the class RegionRouteTable method findRegionsByKvEntries.

/**
 * Returns the list of regions to which the keys belongs.
 */
public Map<Region, List<KVEntry>> findRegionsByKvEntries(final List<KVEntry> kvEntries) {
    Requires.requireNonNull(kvEntries, "kvEntries");
    final Map<Region, List<KVEntry>> regionMap = Maps.newHashMap();
    final StampedLock stampedLock = this.stampedLock;
    final long stamp = stampedLock.readLock();
    try {
        for (final KVEntry kvEntry : kvEntries) {
            final Region region = findRegionByKeyWithoutLock(kvEntry.getKey());
            regionMap.computeIfAbsent(region, k -> Lists.newArrayList()).add(kvEntry);
        }
        return regionMap;
    } finally {
        stampedLock.unlockRead(stamp);
    }
}
Also used : Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) RouteTableException(com.alipay.sofa.jraft.rhea.errors.RouteTableException) NavigableMap(java.util.NavigableMap) Region(com.alipay.sofa.jraft.rhea.metadata.Region) Lists(com.alipay.sofa.jraft.rhea.util.Lists) List(java.util.List) RegionEpoch(com.alipay.sofa.jraft.rhea.metadata.RegionEpoch) TreeMap(java.util.TreeMap) Maps(com.alipay.sofa.jraft.rhea.util.Maps) Map(java.util.Map) KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry) Comparator(java.util.Comparator) StampedLock(java.util.concurrent.locks.StampedLock) Requires(com.alipay.sofa.jraft.util.Requires) CASEntry(com.alipay.sofa.jraft.rhea.storage.CASEntry) BytesUtil(com.alipay.sofa.jraft.util.BytesUtil) KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry) Region(com.alipay.sofa.jraft.rhea.metadata.Region) StampedLock(java.util.concurrent.locks.StampedLock) List(java.util.List)

Aggregations

KVEntry (com.alipay.sofa.jraft.rhea.storage.KVEntry)36 List (java.util.List)17 Test (org.junit.Test)14 Status (com.alipay.sofa.jraft.Status)8 Region (com.alipay.sofa.jraft.rhea.metadata.Region)8 KVStoreClosure (com.alipay.sofa.jraft.rhea.storage.KVStoreClosure)8 RawKVStore (com.alipay.sofa.jraft.rhea.storage.RawKVStore)8 CompletableFuture (java.util.concurrent.CompletableFuture)8 RouteTable (com.alipay.sofa.jraft.RouteTable)6 PeerId (com.alipay.sofa.jraft.entity.PeerId)6 DescriberManager (com.alipay.sofa.jraft.rhea.DescriberManager)6 FollowerStateListener (com.alipay.sofa.jraft.rhea.FollowerStateListener)6 JRaftHelper (com.alipay.sofa.jraft.rhea.JRaftHelper)6 LeaderStateListener (com.alipay.sofa.jraft.rhea.LeaderStateListener)6 RegionEngine (com.alipay.sofa.jraft.rhea.RegionEngine)6 StateListener (com.alipay.sofa.jraft.rhea.StateListener)6 StateListenerContainer (com.alipay.sofa.jraft.rhea.StateListenerContainer)6 StoreEngine (com.alipay.sofa.jraft.rhea.StoreEngine)6 FailoverClosure (com.alipay.sofa.jraft.rhea.client.failover.FailoverClosure)6 ListRetryCallable (com.alipay.sofa.jraft.rhea.client.failover.ListRetryCallable)6