io.etcd.jetcd.KeyValue Java Examples

The following examples show how to use io.etcd.jetcd.KeyValue. 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 File: EtcdClusterUsingTest.java    From jetcd with 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 #2
Source File: WatchResponse.java    From jetcd with 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 #3
Source File: EtcdCenterRepositoryTest.java    From shardingsphere with 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 #4
Source File: OpLogClientFactory.java    From kkbinlog with Apache License 2.0 5 votes vote down vote up
private Set<ClientInfo> getClientInfos(KeyValue keyValue) {
    Set<ClientInfo> clientInfos;
    if (keyValue == null || keyValue.getValue() == null) {
        clientInfos = new HashSet<>();
    } else {
        clientInfos = new HashSet<>(JSON.parseArray(keyValue.getValue().toString(StandardCharsets.UTF_8), ClientInfo.class));
    }
    return clientInfos;
}
 
Example #5
Source File: DeleteResponse.java    From jetcd with Apache License 2.0 5 votes vote down vote up
/**
 * @return previous key-value pairs.
 */
public synchronized List<KeyValue> getPrevKvs() {
    if (prevKvs == null) {
        prevKvs = getResponse().getPrevKvsList().stream().map(kv -> new KeyValue(kv, namespace))
            .collect(Collectors.toList());
    }

    return prevKvs;
}
 
Example #6
Source File: GetResponse.java    From jetcd with Apache License 2.0 5 votes vote down vote up
/**
 * @return a list of key-value pairs matched by the range request.
 */
public synchronized List<KeyValue> getKvs() {
    if (kvs == null) {
        kvs = getResponse().getKvsList().stream().map(kv -> new KeyValue(kv, namespace)).collect(Collectors.toList());
    }

    return kvs;
}
 
Example #7
Source File: EtcdCenterRepositoryTest.java    From shardingsphere with 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 #8
Source File: EtcdCenterRepository.java    From shardingsphere with 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 #9
Source File: WatchEvent.java    From jetcd with Apache License 2.0 4 votes vote down vote up
public WatchEvent(KeyValue keyValue, KeyValue prevKV, EventType eventType) {
    this.keyValue = keyValue;
    this.prevKV = prevKV;
    this.eventType = eventType;
}
 
Example #10
Source File: WatchEvent.java    From jetcd with Apache License 2.0 4 votes vote down vote up
public KeyValue getKeyValue() {
    return keyValue;
}
 
Example #11
Source File: WatchEvent.java    From jetcd with Apache License 2.0 4 votes vote down vote up
public KeyValue getPrevKV() {
    return prevKV;
}
 
Example #12
Source File: LeaderResponse.java    From jetcd with Apache License 2.0 4 votes vote down vote up
public LeaderResponse(io.etcd.jetcd.api.LeaderResponse response, ByteSequence namespace) {
    super(response, response.getHeader());
    this.kv = new KeyValue(getResponse().getKv(), namespace);
}
 
Example #13
Source File: LeaderResponse.java    From jetcd with Apache License 2.0 4 votes vote down vote up
/**
 * @return the key-value pair representing the latest leader update.
 */
public KeyValue getKv() {
    return kv;
}
 
Example #14
Source File: EtcdCenterRepository.java    From shardingsphere with Apache License 2.0 4 votes vote down vote up
@SneakyThrows({InterruptedException.class, ExecutionException.class})
@Override
public String get(final String key) {
    List<KeyValue> keyValues = client.getKVClient().get(ByteSequence.from(key, Charsets.UTF_8)).get().getKvs();
    return keyValues.isEmpty() ? null : keyValues.iterator().next().getValue().toString(Charsets.UTF_8);
}
 
Example #15
Source File: PutResponse.java    From jetcd with Apache License 2.0 4 votes vote down vote up
/**
 * @return previous key-value pair.
 */
public KeyValue getPrevKv() {
    return new KeyValue(getResponse().getPrevKv(), namespace);
}
 
Example #16
Source File: EtcdDataSource.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Override
public String readSource() throws Exception {
    CompletableFuture<GetResponse> responseFuture = client.getKVClient().get(ByteSequence.from(key, charset));
    List<KeyValue> kvs = responseFuture.get().getKvs();
    return kvs.size() == 0 ? null : kvs.get(0).getValue().toString(charset);
}
 
Example #17
Source File: OpLogClientFactory.java    From kkbinlog with Apache License 2.0 4 votes vote down vote up
/**
 * 注册Client列表更新监听
 *
 * @param binaryLogConfig
 * @param opLogEventHandlerFactory
 */
private void registerMetaDataWatcher(BinaryLogConfig binaryLogConfig, OpLogEventHandlerFactory opLogEventHandlerFactory) {

    String namespace = binaryLogConfig.getNamespace();
    String binLogClientSet = binaryLogConfig.getBinLogClientSet();

    Watch watchClient = etcdClient.getWatchClient();
    watchClient.watch(
            ByteSequence.from(etcdKeyPrefixUtil.withPrefix(namespace).concat(Constants.PATH_SEPARATOR).concat(binLogClientSet), StandardCharsets.UTF_8),
            WatchOption.newBuilder().withNoDelete(true).build(),
            new Watch.Listener() {
                @Override
                public void onNext(WatchResponse response) {
                    List<WatchEvent> eventList = response.getEvents();
                    for (WatchEvent event : eventList) {
                        if (WatchEvent.EventType.PUT.equals(event.getEventType())) {

                            KeyValue currentKV = event.getKeyValue();
                            Set<ClientInfo> currentClientInfoSet = getClientInfos(currentKV);

                            currentClientInfoSet
                                    .stream()
                                    .collect(Collectors.groupingBy(ClientInfo::getDatabaseEvent))
                                    .forEach(opLogEventHandlerFactory::updateClientBatch);
                        }
                    }
                }

                @Override
                public void onError(Throwable throwable) {
                    log.error("Watch clientInfo list change error.", throwable);
                }

                @Override
                public void onCompleted() {
                    log.info("Watch clientInfo list change completed.");
                }
            }
    );

}