Java Code Examples for io.etcd.jetcd.ByteSequence

The following examples show how to use io.etcd.jetcd.ByteSequence. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: jetcd   Source File: Main.java    License: Apache License 2.0 7 votes vote down vote up
public static void main(String[] args) {
    Args cmd = new Args();

    JCommander.newBuilder().addObject(cmd).build().parse(args);

    CountDownLatch latch = new CountDownLatch(cmd.maxEvents);
    ByteSequence key = ByteSequence.from(cmd.key, StandardCharsets.UTF_8);
    Collection<URI> endpoints = Util.toURIs(cmd.endpoints);

    Watch.Listener listener = Watch.listener(response -> {
        LOGGER.info("Watching for key={}", cmd.key);

        for (WatchEvent event : response.getEvents()) {
            LOGGER.info("type={}, key={}, value={}", event.getEventType(),
                Optional.ofNullable(event.getKeyValue().getKey()).map(bs -> bs.toString(StandardCharsets.UTF_8)).orElse(""),
                Optional.ofNullable(event.getKeyValue().getValue()).map(bs -> bs.toString(StandardCharsets.UTF_8))
                    .orElse(""));
        }

        latch.countDown();
    });

    try (Client client = Client.builder().endpoints(endpoints).build();
        Watch watch = client.getWatchClient();
        Watch.Watcher watcher = watch.watch(key, listener)) {

        latch.await();
    } catch (Exception e) {
        LOGGER.error("Watching Error {}", e);
        System.exit(1);
    }
}
 
Example 2
Source Project: Sentinel   Source File: EtcdDataSource.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Create an etcd data-source. The connection configuration will be retrieved from {@link EtcdConfig}.
 *
 * @param key    config key
 * @param parser data parser
 */
public EtcdDataSource(String key, Converter<String, T> parser) {
    super(parser);
    if (!EtcdConfig.isAuthEnable()) {
        this.client = Client.builder()
            .endpoints(EtcdConfig.getEndPoints().split(",")).build();
    } else {
        this.client = Client.builder()
            .endpoints(EtcdConfig.getEndPoints().split(","))
            .user(ByteSequence.from(EtcdConfig.getUser(), charset))
            .password(ByteSequence.from(EtcdConfig.getPassword(), charset))
            .authority(EtcdConfig.getAuthority())
            .build();
    }
    this.key = key;
    loadInitialConfig();
    initWatcher();
}
 
Example 3
Source Project: Sentinel   Source File: EtcdDataSource.java    License: Apache License 2.0 6 votes vote down vote up
private void initWatcher() {
    watcher = client.getWatchClient().watch(ByteSequence.from(key, charset), (watchResponse) -> {
        for (WatchEvent watchEvent : watchResponse.getEvents()) {
            WatchEvent.EventType eventType = watchEvent.getEventType();
            if (eventType == WatchEvent.EventType.PUT) {
                try {
                    T newValue = loadConfig();
                    getProperty().updateValue(newValue);
                } catch (Exception e) {
                    RecordLog.warn("[EtcdDataSource] Failed to update config", e);
                }
            } else if (eventType == WatchEvent.EventType.DELETE) {
                RecordLog.info("[EtcdDataSource] Cleaning config for key <{}>", key);
                getProperty().updateValue(null);
            }
        }
    });
}
 
Example 4
Source Project: shardingsphere   Source File: EtcdCenterRepositoryTest.java    License: Apache License 2.0 6 votes vote down vote up
@SneakyThrows({InterruptedException.class, ExecutionException.class})
@SuppressWarnings("unchecked")
private Client mockClient() {
    when(client.getKVClient()).thenReturn(kv);
    when(kv.get(any(ByteSequence.class))).thenReturn(getFuture);
    when(kv.get(any(ByteSequence.class), any(GetOption.class))).thenReturn(getFuture);
    when(kv.put(any(ByteSequence.class), any(ByteSequence.class))).thenReturn(putFuture);
    when(kv.put(any(ByteSequence.class), any(ByteSequence.class), any(PutOption.class))).thenReturn(putFuture);
    when(getFuture.get()).thenReturn(getResponse);
    when(client.getLeaseClient()).thenReturn(lease);
    when(lease.grant(anyLong())).thenReturn(leaseFuture);
    when(leaseFuture.get()).thenReturn(leaseGrantResponse);
    when(leaseGrantResponse.getID()).thenReturn(123L);
    when(client.getWatchClient()).thenReturn(watch);
    return client;
}
 
Example 5
Source Project: shardingsphere   Source File: EtcdCenterRepositoryTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void assertGetChildrenKeys() {
    io.etcd.jetcd.api.KeyValue keyValue1 = io.etcd.jetcd.api.KeyValue.newBuilder()
        .setKey(ByteString.copyFromUtf8("/key/key1/key1-1"))
        .setValue(ByteString.copyFromUtf8("value1")).build();
    io.etcd.jetcd.api.KeyValue keyValue2 = io.etcd.jetcd.api.KeyValue.newBuilder()
        .setKey(ByteString.copyFromUtf8("/key/key2"))
        .setValue(ByteString.copyFromUtf8("value3")).build();
    List<KeyValue> keyValues = Arrays.asList(new KeyValue(keyValue1, ByteSequence.EMPTY), new KeyValue(keyValue2, ByteSequence.EMPTY), 
            new KeyValue(keyValue1, ByteSequence.EMPTY));
    when(getResponse.getKvs()).thenReturn(keyValues);
    List<String> actual = centerRepository.getChildrenKeys("/key");
    assertThat(actual.size(), is(2));
    Iterator<String> iterator = actual.iterator();
    assertThat(iterator.next(), is("key1"));
    assertThat(iterator.next(), is("key2"));
}
 
Example 6
Source Project: jetcd   Source File: GetOption.java    License: Apache License 2.0 6 votes vote down vote up
private GetOption(Optional<ByteSequence> endKey, long limit, long revision, SortOrder sortOrder, SortTarget sortTarget,
    boolean serializable, boolean keysOnly, boolean countOnly, long minCreateRevision, long maxCreateRevision,
    long minModRevision, long maxModRevision) {
    this.endKey = endKey;
    this.limit = limit;
    this.revision = revision;
    this.sortOrder = sortOrder;
    this.sortTarget = sortTarget;
    this.serializable = serializable;
    this.keysOnly = keysOnly;
    this.countOnly = countOnly;
    this.minCreateRevision = minCreateRevision;
    this.maxCreateRevision = maxCreateRevision;
    this.minModRevision = minModRevision;
    this.maxModRevision = maxModRevision;
}
 
Example 7
Source Project: jetcd   Source File: Op.java    License: Apache License 2.0 6 votes vote down vote up
RequestOp toRequestOp(ByteSequence namespace) {
    TxnRequest.Builder txn = TxnRequest.newBuilder();

    if (cmps != null) {
        for (Cmp cmp : cmps) {
            txn.addCompare(cmp.toCompare(namespace));
        }
    }

    if (thenOps != null) {
        for (Op thenOp : thenOps) {
            txn.addSuccess(thenOp.toRequestOp(namespace));
        }
    }

    if (elseOps != null) {
        for (Op elseOp : elseOps) {
            txn.addFailure(elseOp.toRequestOp(namespace));
        }
    }

    return RequestOp.newBuilder().setRequestTxn(txn).build();
}
 
Example 8
Source Project: jetcd   Source File: WatchResponse.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * convert API watch event to client event.
 */
private static WatchEvent toEvent(Event event, ByteSequence namespace) {
    WatchEvent.EventType eventType;
    switch (event.getType()) {
        case DELETE:
            eventType = WatchEvent.EventType.DELETE;
            break;
        case PUT:
            eventType = WatchEvent.EventType.PUT;
            break;
        default:
            eventType = WatchEvent.EventType.UNRECOGNIZED;
    }

    return new WatchEvent(new KeyValue(event.getKv(), namespace), new KeyValue(event.getPrevKv(), namespace), eventType);
}
 
Example 9
Source Project: jetcd   Source File: AuthRoleGetResponse.java    License: Apache License 2.0 6 votes vote down vote up
private static Permission toPermission(io.etcd.jetcd.api.Permission perm) {
    ByteSequence key = ByteSequence.from(perm.getKey());
    ByteSequence rangeEnd = ByteSequence.from(perm.getRangeEnd());

    Permission.Type type;
    switch (perm.getPermType()) {
        case READ:
            type = Type.READ;
            break;
        case WRITE:
            type = Type.WRITE;
            break;
        case READWRITE:
            type = Type.READWRITE;
            break;
        default:
            type = Type.UNRECOGNIZED;
    }

    return new Permission(type, key, rangeEnd);
}
 
Example 10
Source Project: jetcd   Source File: EtcdClusterUsingTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testUseEtcd() throws Exception {
    try (EtcdCluster etcd = EtcdClusterFactory.buildCluster(getClass().getSimpleName(), 3, false)) {
        etcd.start();
        try (Client client = Client.builder().endpoints(etcd.getClientEndpoints()).build()) {
            try (KV kvClient = client.getKVClient()) {
                ByteSequence key = bytesOf("test_key");
                ByteSequence value = bytesOf("test_value");
                kvClient.put(key, value).get();

                CompletableFuture<GetResponse> getFuture = kvClient.get(key);
                GetResponse response = getFuture.get();
                List<KeyValue> values = response.getKvs();
                assertThat(values.size()).isEqualTo(1);
                KeyValue value1 = values.get(0);
                assertThat(value1.getValue()).isEqualTo(value);
                assertThat(value1.getKey()).isEqualTo(key);
            }
        }
    }
}
 
Example 11
Source Project: joyrpc   Source File: EtcdRegistry.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<Void> doRegister(final Registion registion) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    if (leaseId <= 0) {
        //没有租约
        future.completeExceptionally(new IllegalStateException(
                String.format("Error occurs while register provider of %s, caused by no leaseId. retry....", registion.getService())));
    } else {
        //有租约
        PutOption putOption = PutOption.newBuilder().withLeaseId(leaseId).build();
        ByteSequence key = ByteSequence.from(registion.getPath(), UTF_8);
        ByteSequence value = ByteSequence.from(registion.getUrl().toString(), UTF_8);
        client.getKVClient().put(key, value, putOption).whenComplete((r, t) -> {
            if (!isOpen()) {
                //已经关闭,或者创建了新的客户端
                future.completeExceptionally(new IllegalStateException("controller is closed."));
            } else if (t != null) {
                //TODO 判断租期失效异常,触发重连逻辑
                logger.error(String.format("Error occurs while register provider of %s, caused by %s. retry....",
                        registion.getPath(), t.getMessage()), t);
                future.completeExceptionally(t);
            } else {
                future.complete(null);
            }
        });
    }
    return future;
}
 
Example 12
Source Project: joyrpc   Source File: EtcdRegistry.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<Void> doDeregister(final Registion registion) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    ByteSequence key = ByteSequence.from(registion.getPath(), UTF_8);
    client.getKVClient().delete(key).whenComplete((r, t) -> {
        if (t != null) {
            future.completeExceptionally(t);
        } else {
            future.complete(null);
        }
    });
    return future;
}
 
Example 13
Source Project: joyrpc   Source File: EtcdRegistry.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<Void> doSubscribe(final ClusterBooking booking) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    EtcdClusterBooking etcdBooking = (EtcdClusterBooking) booking;
    //先查询
    ByteSequence key = ByteSequence.from(etcdBooking.getPath(), UTF_8);
    //先查询,无异常后添加watcher,若结果不为空,通知FULL事件
    GetOption getOption = GetOption.newBuilder().withPrefix(key).build();
    client.getKVClient().get(key, getOption).whenComplete((res, err) -> {
        if (!isOpen()) {
            future.completeExceptionally(new IllegalStateException("controller is closed."));
        } else if (err != null) {
            logger.error(String.format("Error occurs while subscribe of %s, caused by %s. retry....", etcdBooking.getService(), err.getMessage()), err);
            future.completeExceptionally(err);
        } else {
            List<WatchEvent> events = new ArrayList<>();
            res.getKvs().forEach(kv -> events.add(new WatchEvent(kv, null, PUT)));
            etcdBooking.onUpdate(events, res.getHeader().getRevision(), FULL);
            //添加watch
            try {
                WatchOption watchOption = WatchOption.newBuilder().withPrefix(key).build();
                Watch.Watcher watcher = client.getWatchClient().watch(key, watchOption, etcdBooking);
                etcdBooking.setWatcher(watcher);
                future.complete(null);
            } catch (Exception e) {
                logger.error(String.format("Error occurs while subscribe of %s, caused by %s. retry....", etcdBooking.getService(), e.getMessage()), e);
                future.completeExceptionally(e);
            }
        }
    });
    return future;
}
 
Example 14
Source Project: joyrpc   Source File: EtcdRegistry.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<Void> doSubscribe(final ConfigBooking booking) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    EtcdConfigBooking etcdBooking = (EtcdConfigBooking) booking;
    //先查询
    ByteSequence key = ByteSequence.from(etcdBooking.getPath(), UTF_8);
    //先查询,无异常后添加watcher,若结果不为空,通知FULL事件
    client.getKVClient().get(key).whenComplete((res, err) -> {
        if (!isOpen()) {
            future.completeExceptionally(new IllegalStateException("controller is closed."));
        } else if (err != null) {
            logger.error(String.format("Error occurs while subscribe of %s, caused by %s. retry....", etcdBooking.getInterface(), err.getMessage()), err);
            future.completeExceptionally(err);
        } else {
            List<WatchEvent> events = new ArrayList<>();
            res.getKvs().forEach(kv -> events.add(new WatchEvent(kv, null, PUT)));
            etcdBooking.onUpdate(events, res.getHeader().getRevision());
            //添加watch
            try {
                WatchOption watchOption = WatchOption.newBuilder().withPrefix(key).build();
                Watch.Watcher watcher = client.getWatchClient().watch(key, watchOption, etcdBooking);
                etcdBooking.setWatcher(watcher);
                future.complete(null);
            } catch (Exception e) {
                logger.error(String.format("Error occurs while subscribe of %s, caused by %s. retry....", etcdBooking.getInterface(), e.getMessage()), e);
                future.completeExceptionally(e);
            }
        }
    });
    return future;
}
 
Example 15
Source Project: Sentinel   Source File: EtcdConfigSender.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException {


        String rule_key = "sentinel_demo_rule_key";

        Client client = Client.builder()
                .endpoints("http://127.0.0.1:2379")
                .user(ByteSequence.from("root".getBytes()))
                .password(ByteSequence.from("12345".getBytes()))
                .build();
        final String rule = "[\n"
                + "  {\n"
                + "    \"resource\": \"TestResource\",\n"
                + "    \"controlBehavior\": 0,\n"
                + "    \"count\": 5.0,\n"
                + "    \"grade\": 1,\n"
                + "    \"limitApp\": \"default\",\n"
                + "    \"strategy\": 0\n"
                + "  }\n"
                + "]";
        client.getKVClient()
                .put(ByteSequence.from(rule_key.getBytes()), ByteSequence.from(rule.getBytes()));

        System.out.println("setting rule success");
        Thread.sleep(10000);

    }
 
Example 16
Source Project: Sentinel   Source File: EtcdDataSourceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testReadSource() throws Exception {
    EtcdDataSource dataSource = new EtcdDataSource("foo", value -> value);
    KV kvClient = Client.builder()
            .endpoints(endPoints)
            .build().getKVClient();

    kvClient.put(ByteSequence.from("foo".getBytes()), ByteSequence.from("test".getBytes()));
    Assert.assertNotNull(dataSource.readSource().equals("test"));

    kvClient.put(ByteSequence.from("foo".getBytes()), ByteSequence.from("test2".getBytes()));
    Assert.assertNotNull(dataSource.getProperty().equals("test2"));
}
 
Example 17
Source Project: shardingsphere   Source File: EtcdCenterRepository.java    License: Apache License 2.0 5 votes vote down vote up
@SneakyThrows({InterruptedException.class, ExecutionException.class})
@Override
public List<String> getChildrenKeys(final String key) {
    String prefix = key + "/";
    ByteSequence prefixByteSequence = ByteSequence.from(prefix, Charsets.UTF_8);
    GetOption getOption = GetOption.newBuilder().withPrefix(prefixByteSequence).withSortField(GetOption.SortTarget.KEY).withSortOrder(GetOption.SortOrder.ASCEND).build();
    List<KeyValue> keyValues = client.getKVClient().get(prefixByteSequence, getOption).get().getKvs();
    return keyValues.stream().map(e -> getSubNodeKeyName(prefix, e.getKey().toString(Charsets.UTF_8))).distinct().collect(Collectors.toList());
}
 
Example 18
Source Project: shardingsphere   Source File: EtcdCenterRepository.java    License: Apache License 2.0 5 votes vote down vote up
@SneakyThrows({InterruptedException.class, ExecutionException.class})
@Override
public void persistEphemeral(final String key, final String value) {
    long leaseId = client.getLeaseClient().grant(this.etcdProperties.getValue(EtcdPropertyKey.TIME_TO_LIVE_SECONDS)).get().getID();
    client.getLeaseClient().keepAlive(leaseId, Observers.observer(response -> { }));
    client.getKVClient().put(ByteSequence.from(key, Charsets.UTF_8), ByteSequence.from(value, Charsets.UTF_8), PutOption.newBuilder().withLeaseId(leaseId).build()).get();
}
 
Example 19
Source Project: shardingsphere   Source File: EtcdCenterRepository.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void watch(final String key, final DataChangedEventListener dataChangedEventListener) {
    Watch.Listener listener = Watch.listener(response -> {
        for (WatchEvent each : response.getEvents()) {
            DataChangedEvent.ChangedType changedType = getEventChangedType(each);
            if (DataChangedEvent.ChangedType.IGNORED != changedType) {
                dataChangedEventListener.onChange(new DataChangedEvent(each.getKeyValue().getKey().toString(Charsets.UTF_8), each.getKeyValue().getValue().toString(Charsets.UTF_8), changedType));
            }
        }
    });
    client.getWatchClient().watch(ByteSequence.from(key, Charsets.UTF_8), listener);
}
 
Example 20
Source Project: shardingsphere   Source File: EtcdCenterRepositoryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void assertPersistEphemeral() {
    centerRepository.persistEphemeral("key1", "value1");
    verify(lease).grant(anyLong());
    verify(lease).keepAlive(anyLong(), any(StreamObserver.class));
    verify(kv).put(any(ByteSequence.class), any(ByteSequence.class), any(PutOption.class));
}
 
Example 21
Source Project: shardingsphere   Source File: EtcdCenterRepositoryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void assertWatchUpdate() {
    doAnswer(invocationOnMock -> {
        Watch.Listener listener = (Watch.Listener) invocationOnMock.getArguments()[1];
        listener.onNext(buildWatchResponse(WatchEvent.EventType.PUT));
        return mock(Watch.Watcher.class);
    }).when(watch).watch(any(ByteSequence.class), any(Watch.Listener.class));
    centerRepository.watch("key1", dataChangedEvent -> {
    });
    verify(watch).watch(any(ByteSequence.class), any(Watch.Listener.class));
}
 
Example 22
Source Project: shardingsphere   Source File: EtcdCenterRepositoryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void assertWatchDelete() {
    doAnswer(invocationOnMock -> {
        Watch.Listener listener = (Watch.Listener) invocationOnMock.getArguments()[1];
        listener.onNext(buildWatchResponse(WatchEvent.EventType.DELETE));
        return mock(Watch.Watcher.class);
    }).when(watch).watch(any(ByteSequence.class), any(Watch.Listener.class));
    centerRepository.watch("key1", dataChangedEvent -> {
    });
    verify(watch).watch(any(ByteSequence.class), any(Watch.Listener.class));
}
 
Example 23
Source Project: shardingsphere   Source File: EtcdCenterRepositoryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void assertWatchIgnored() {
    doAnswer(invocationOnMock -> {
        Watch.Listener listener = (Watch.Listener) invocationOnMock.getArguments()[1];
        listener.onNext(buildWatchResponse(WatchEvent.EventType.UNRECOGNIZED));
        return mock(Watch.Watcher.class);
    }).when(watch).watch(any(ByteSequence.class), any(Watch.Listener.class));
    centerRepository.watch("key1", dataChangedEvent -> {
    });
    verify(watch).watch(any(ByteSequence.class), any(Watch.Listener.class));
}
 
Example 24
Source Project: shardingsphere   Source File: EtcdCenterRepositoryTest.java    License: Apache License 2.0 5 votes vote down vote up
@SneakyThrows({NoSuchFieldException.class, SecurityException.class})
private WatchResponse buildWatchResponse(final WatchEvent.EventType eventType) {
    WatchResponse watchResponse = new WatchResponse(mock(io.etcd.jetcd.api.WatchResponse.class), ByteSequence.EMPTY);
    List<WatchEvent> events = new ArrayList<>();
    io.etcd.jetcd.api.KeyValue keyValue1 = io.etcd.jetcd.api.KeyValue.newBuilder()
            .setKey(ByteString.copyFromUtf8("key1"))
            .setValue(ByteString.copyFromUtf8("value1")).build();
    KeyValue keyValue = new KeyValue(keyValue1, ByteSequence.EMPTY);
    events.add(new WatchEvent(keyValue, mock(KeyValue.class), eventType));
    FieldSetter.setField(watchResponse, watchResponse.getClass().getDeclaredField("events"), events);
    return watchResponse;
}
 
Example 25
Source Project: jetcd   Source File: OptionsUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Gets the range end of the given prefix.
 *
 * <p>
 * The range end is the key plus one (e.g., "aa"+1 == "ab", "a\xff"+1 == "b").
 *
 * @param  prefix the given prefix
 * @return        the range end of the given prefix
 */
static ByteSequence prefixEndOf(ByteSequence prefix) {
    byte[] endKey = prefix.getBytes().clone();
    for (int i = endKey.length - 1; i >= 0; i--) {
        if (endKey[i] != (byte) 0xff) {
            endKey[i] = (byte) (endKey[i] + 1);
            return ByteSequence.from(Arrays.copyOf(endKey, i + 1));
        }
    }

    return ByteSequence.from(NO_PREFIX_END);
}
 
Example 26
Source Project: jetcd   Source File: WatchOption.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Enables watch all the keys with matching prefix.
 *
 * @param  prefix the common prefix of all the keys that you want to watch
 * @return        builder
 */
public Builder withPrefix(ByteSequence prefix) {
    checkNotNull(prefix, "prefix should not be null");
    ByteSequence prefixEnd = OptionsUtil.prefixEndOf(prefix);
    this.withRange(prefixEnd);
    return this;
}
 
Example 27
Source Project: jetcd   Source File: WatchOption.java    License: Apache License 2.0 5 votes vote down vote up
private WatchOption(Optional<ByteSequence> endKey, long revision, boolean prevKV, boolean progressNotify, boolean noPut,
    boolean noDelete) {
    this.endKey = endKey;
    this.revision = revision;
    this.prevKV = prevKV;
    this.progressNotify = progressNotify;
    this.noPut = noPut;
    this.noDelete = noDelete;
}
 
Example 28
Source Project: jetcd   Source File: Op.java    License: Apache License 2.0 5 votes vote down vote up
RequestOp toRequestOp(ByteSequence namespace) {
    RangeRequest.Builder range = RangeRequest.newBuilder().setKey(Util.prefixNamespace(this.key, namespace))
        .setCountOnly(this.option.isCountOnly()).setLimit(this.option.getLimit())
        .setRevision(this.option.getRevision()).setKeysOnly(this.option.isKeysOnly())
        .setSerializable(this.option.isSerializable())
        .setSortOrder(toRangeRequestSortOrder(this.option.getSortOrder()))
        .setSortTarget(toRangeRequestSortTarget(this.option.getSortField()));

    this.option.getEndKey()
        .map(endKey -> Util.prefixNamespaceToRangeEnd(ByteString.copyFrom(endKey.getBytes()), namespace))
        .ifPresent(range::setRangeEnd);

    return RequestOp.newBuilder().setRequestRange(range).build();
}
 
Example 29
Source Project: jetcd   Source File: Op.java    License: Apache License 2.0 5 votes vote down vote up
RequestOp toRequestOp(ByteSequence namespace) {
    DeleteRangeRequest.Builder delete = DeleteRangeRequest.newBuilder()
        .setKey(Util.prefixNamespace(this.key, namespace)).setPrevKv(this.option.isPrevKV());

    this.option.getEndKey()
        .map(endKey -> Util.prefixNamespaceToRangeEnd(ByteString.copyFrom(endKey.getBytes()), namespace))
        .ifPresent(delete::setRangeEnd);

    return RequestOp.newBuilder().setRequestDeleteRange(delete).build();
}
 
Example 30
Source Project: jetcd   Source File: LeaseTimeToLiveResponse.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @return the list of keys attached to this lease.
 */
public synchronized List<ByteSequence> getKeys() {
    if (keys == null) {
        keys = getResponse().getKeysList().stream().map(ByteSequence::from).collect(Collectors.toList());
    }

    return keys;
}