@Override public void put(K key, V value) { if (value != null) { instrument(metrics.numPuts, metrics.putNs, () -> kvStore.put(key, value)); } else { delete(key); } }
@Override public CompletableFuture<Void> deleteAllAsync(List<K> keys) { CompletableFuture<Void> future = new CompletableFuture(); try { deleteAll(keys); future.complete(null); } catch (Exception e) { future.completeExceptionally(e); } return future; }
@Override public CompletableFuture<V> getAsync(K key) { CompletableFuture<V> future = new CompletableFuture(); try { future.complete(get(key)); } catch (Exception e) { future.completeExceptionally(e); } return future; }
private LocalTable createTable(boolean isTimerDisabled) { Map<String, String> config = new HashMap<>(); if (isTimerDisabled) { config.put(MetricsConfig.METRICS_TIMER_ENABLED(), "false"); } Context context = mock(Context.class); JobContext jobContext = mock(JobContext.class); when(context.getJobContext()).thenReturn(jobContext); when(jobContext.getConfig()).thenReturn(new MapConfig(config)); ContainerContext containerContext = mock(ContainerContext.class); when(context.getContainerContext()).thenReturn(containerContext); when(containerContext.getContainerMetricsRegistry()).thenReturn(metricsRegistry); LocalTable table = new LocalTable("t1", kvStore); table.init(context); return table; } }
@Override public ReadWriteTable getTable() { Preconditions.checkNotNull(context, String.format("Table %s not initialized", tableId)); Preconditions.checkNotNull(kvStore, "Store not initialized for table " + tableId); ReadWriteTable table = new LocalTable(tableId, kvStore); table.init(this.context); return table; } }
@Override public void putAll(List<Entry<K, V>> entries) { List<Entry<K, V>> toPut = new LinkedList<>(); List<K> toDelete = new LinkedList<>(); entries.forEach(e -> { if (e.getValue() != null) { toPut.add(e); } else { toDelete.add(e.getKey()); } }); if (!toPut.isEmpty()) { instrument(metrics.numPutAlls, metrics.putAllNs, () -> kvStore.putAll(toPut)); } if (!toDelete.isEmpty()) { deleteAll(toDelete); } }
@Override public void flush() { // Since the KV store will be flushed by task storage manager, only update metrics here instrument(metrics.numFlushes, metrics.flushNs, () -> { }); }
@Override public CompletableFuture<Void> deleteAsync(K key) { CompletableFuture<Void> future = new CompletableFuture(); try { delete(key); future.complete(null); } catch (Exception e) { future.completeExceptionally(e); } return future; }
@Override public CompletableFuture<Map<K, V>> getAllAsync(List<K> keys) { CompletableFuture<Map<K, V>> future = new CompletableFuture(); try { future.complete(getAll(keys)); } catch (Exception e) { future.completeExceptionally(e); } return future; }
@Override public CompletableFuture<Void> putAsync(K key, V value) { CompletableFuture<Void> future = new CompletableFuture(); try { put(key, value); future.complete(null); } catch (Exception e) { future.completeExceptionally(e); } return future; }
@Override public CompletableFuture<Void> putAllAsync(List<Entry<K, V>> entries) { CompletableFuture<Void> future = new CompletableFuture(); try { putAll(entries); future.complete(null); } catch (Exception e) { future.completeExceptionally(e); } return future; }
private LocalTable createTable(boolean isTimerDisabled) { Map<String, String> config = new HashMap<>(); if (isTimerDisabled) { config.put(MetricsConfig.METRICS_TIMER_ENABLED(), "false"); } Context context = mock(Context.class); JobContext jobContext = mock(JobContext.class); when(context.getJobContext()).thenReturn(jobContext); when(jobContext.getConfig()).thenReturn(new MapConfig(config)); ContainerContext containerContext = mock(ContainerContext.class); when(context.getContainerContext()).thenReturn(containerContext); when(containerContext.getContainerMetricsRegistry()).thenReturn(metricsRegistry); LocalTable table = new LocalTable("t1", kvStore); table.init(context); return table; } }
@Override public void delete(K key) { instrument(metrics.numDeletes, metrics.deleteNs, () -> kvStore.delete(key)); }
@Override public void deleteAll(List<K> keys) { instrument(metrics.numDeleteAlls, metrics.deleteAllNs, () -> kvStore.deleteAll(keys)); }
@Override public Map<K, V> getAll(List<K> keys) { Map<K, V> result = instrument(metrics.numGetAlls, metrics.getAllNs, () -> kvStore.getAll(keys)); result.values().stream().filter(Objects::isNull).forEach(v -> incCounter(metrics.numMissedLookups)); return result; }
@Override public V get(K key) { V result = instrument(metrics.numGets, metrics.getNs, () -> kvStore.get(key)); if (result == null) { incCounter(metrics.numMissedLookups); } return result; }