|
32 | 32 | import java.util.HashMap;
|
33 | 33 | import java.util.List;
|
34 | 34 | import java.util.Map;
|
| 35 | +import java.util.Optional; |
35 | 36 | import java.util.concurrent.ConcurrentHashMap;
|
36 | 37 | import java.util.concurrent.ExecutionException;
|
37 | 38 | import java.util.concurrent.TimeUnit;
|
@@ -158,7 +159,24 @@ public void subscribe(String path, SubscribeListener listener) {
|
158 | 159 | watcherMap.computeIfAbsent(path,
|
159 | 160 | $ -> client.getWatchClient().watch(watchKey, watchOption, watchResponse -> {
|
160 | 161 | for (WatchEvent event : watchResponse.getEvents()) {
|
161 |
| - listener.notify(new EventAdaptor(event, path)); |
| 162 | + final String eventPath = event.getKeyValue().getKey().toString(StandardCharsets.UTF_8); |
| 163 | + switch (listener.getSubscribeScope()) { |
| 164 | + case PATH_ONLY: |
| 165 | + if (eventPath.equals(path)) { |
| 166 | + listener.notify(toEvent(event, path)); |
| 167 | + } |
| 168 | + break; |
| 169 | + case CHILDREN_ONLY: |
| 170 | + if (!eventPath.equals(path)) { |
| 171 | + listener.notify(toEvent(event, path)); |
| 172 | + } |
| 173 | + break; |
| 174 | + case ALL: |
| 175 | + listener.notify(toEvent(event, path)); |
| 176 | + break; |
| 177 | + default: |
| 178 | + throw new RegistryException("Unknown event scope: " + listener.getSubscribeScope()); |
| 179 | + } |
162 | 180 | }
|
163 | 181 | }));
|
164 | 182 | } catch (Exception e) {
|
@@ -373,30 +391,31 @@ private static ByteSequence byteSequence(String val) {
|
373 | 391 | return ByteSequence.from(val, StandardCharsets.UTF_8);
|
374 | 392 | }
|
375 | 393 |
|
376 |
| - static final class EventAdaptor extends Event { |
377 |
| - |
378 |
| - public EventAdaptor(WatchEvent event, String key) { |
379 |
| - key(key); |
380 |
| - |
381 |
| - switch (event.getEventType()) { |
382 |
| - case PUT: |
383 |
| - if (event.getPrevKV().getKey().isEmpty()) { |
384 |
| - type(Type.ADD); |
385 |
| - } else { |
386 |
| - type(Type.UPDATE); |
387 |
| - } |
388 |
| - break; |
389 |
| - case DELETE: |
390 |
| - type(Type.REMOVE); |
391 |
| - break; |
392 |
| - default: |
393 |
| - break; |
394 |
| - } |
395 |
| - KeyValue keyValue = event.getKeyValue(); |
396 |
| - if (keyValue != null) { |
397 |
| - path(keyValue.getKey().toString(StandardCharsets.UTF_8)); |
398 |
| - data(keyValue.getValue().toString(StandardCharsets.UTF_8)); |
399 |
| - } |
| 394 | + private Event toEvent(final WatchEvent watchEvent, final String watchedPath) { |
| 395 | + Event.Type eventType = null; |
| 396 | + switch (watchEvent.getEventType()) { |
| 397 | + case PUT: |
| 398 | + if (watchEvent.getPrevKV().getKey().isEmpty()) { |
| 399 | + eventType = Event.Type.ADD; |
| 400 | + } else { |
| 401 | + eventType = Event.Type.UPDATE; |
| 402 | + } |
| 403 | + break; |
| 404 | + case DELETE: |
| 405 | + eventType = Event.Type.REMOVE; |
| 406 | + break; |
| 407 | + default: |
| 408 | + break; |
400 | 409 | }
|
| 410 | + final KeyValue keyValue = watchEvent.getKeyValue(); |
| 411 | + return Event.builder() |
| 412 | + .type(eventType) |
| 413 | + .watchedPath(watchedPath) |
| 414 | + .eventPath(Optional.ofNullable(keyValue).map(kv -> kv.getKey().toString(StandardCharsets.UTF_8)) |
| 415 | + .orElse(null)) |
| 416 | + .eventData(Optional.ofNullable(keyValue).map(kv -> kv.getValue().toString(StandardCharsets.UTF_8)) |
| 417 | + .orElse(null)) |
| 418 | + .build(); |
401 | 419 | }
|
| 420 | + |
402 | 421 | }
|
0 commit comments