private <T> Map<MDSKey, T> listKV(Scan runScan, Type typeOfT, int limit, @Nullable Predicate<MDSKey> keyFilter, @Nullable Predicate<T> valueFilter) { try { Map<MDSKey, T> map = Maps.newLinkedHashMap(); try (Scanner scan = table.scan(runScan)) { Row next; while ((limit > 0) && (next = scan.next()) != null) { MDSKey key = new MDSKey(next.getRow()); byte[] columnValue = next.get(COLUMN); if (columnValue == null) { continue; } T value = deserialize(key, columnValue, typeOfT); // Key Filter doesn't pass if (keyFilter != null && !keyFilter.test(key)) { continue; } // If Value Filter doesn't pass if (valueFilter != null && !valueFilter.test(value)) { continue; } map.put(key, value); limit--; } return map; } } catch (Exception e) { throw Throwables.propagate(e); } }
private <T> Map<MDSKey, T> listKV(Scan runScan, Type typeOfT, int limit, @Nullable Predicate<MDSKey> keyFilter, @Nullable Predicate<T> valueFilter) { try { Map<MDSKey, T> map = Maps.newLinkedHashMap(); try (Scanner scan = table.scan(runScan)) { Row next; while ((limit > 0) && (next = scan.next()) != null) { MDSKey key = new MDSKey(next.getRow()); byte[] columnValue = next.get(COLUMN); if (columnValue == null) { continue; } T value = deserialize(key, columnValue, typeOfT); // Key Filter doesn't pass if (keyFilter != null && !keyFilter.test(key)) { continue; } // If Value Filter doesn't pass if (valueFilter != null && !valueFilter.test(value)) { continue; } map.put(key, value); limit--; } return map; } } catch (Exception e) { throw Throwables.propagate(e); } }
/** * Get the value at the row and default COLUMN, of type T * * @param id the mds key for the row * @param typeOfT the type of the result * @return the deserialized value of the result, null if not exist */ @Nullable public <T> T get(MDSKey id, Type typeOfT) { Row row = table.get(id.getKey()); if (row.isEmpty()) { return null; } byte[] value = row.get(COLUMN); if (value == null) { return null; } return deserialize(id, value, typeOfT); }
/** * Get the value at the row and default COLUMN, of type T * * @param id the mds key for the row * @param typeOfT the type of the result * @return the deserialized value of the result, null if not exist */ @Nullable public <T> T get(MDSKey id, Type typeOfT) { Row row = table.get(id.getKey()); if (row.isEmpty()) { return null; } byte[] value = row.get(COLUMN); if (value == null) { return null; } return deserialize(id, value, typeOfT); }
/** * Get all non-null values of type T with the given ids for default COLUMN in a map * * @param ids set of the mds keys * @param typeOfT the type of the result * @return a map of the deserialized value of the result */ protected <T> Map<MDSKey, T> getKV(Set<MDSKey> ids, Type typeOfT) { Map<MDSKey, T> resultMap = new HashMap<>(); List<Get> getList = new ArrayList<>(); for (MDSKey id : ids) { getList.add(new Get(id.getKey())); } List<Row> rowList = table.get(getList); for (Row row : rowList) { if (row.isEmpty()) { continue; } byte[] value = row.get(COLUMN); if (value == null) { continue; } MDSKey key = new MDSKey(row.getRow()); T result = deserialize(key, value, typeOfT); resultMap.put(key, result); } return resultMap; }
private <T> Map<MDSKey, T> listCombinedFilterKV(Scan runScan, Type typeOfT, int limit, @Nullable Predicate<KeyValue<T>> combinedFilter) { try { Map<MDSKey, T> map = Maps.newLinkedHashMap(); try (Scanner scan = table.scan(runScan)) { Row next; while ((limit > 0) && (next = scan.next()) != null) { MDSKey key = new MDSKey(next.getRow()); byte[] columnValue = next.get(COLUMN); if (columnValue == null) { continue; } T value = deserialize(key, columnValue, typeOfT); KeyValue<T> kv = new KeyValue<>(key, value); // Combined Filter doesn't pass if (combinedFilter != null && !combinedFilter.test(kv)) { continue; } map.put(kv.getKey(), kv.getValue()); limit--; } return map; } } catch (Exception e) { throw Throwables.propagate(e); } }
/** * Get all non-null values of type T with the given ids for default COLUMN in a map * * @param ids set of the mds keys * @param typeOfT the type of the result * @return a map of the deserialized value of the result */ protected <T> Map<MDSKey, T> getKV(Set<MDSKey> ids, Type typeOfT) { Map<MDSKey, T> resultMap = new HashMap<>(); List<Get> getList = new ArrayList<>(); for (MDSKey id : ids) { getList.add(new Get(id.getKey())); } List<Row> rowList = table.get(getList); for (Row row : rowList) { if (row.isEmpty()) { continue; } byte[] value = row.get(COLUMN); if (value == null) { continue; } MDSKey key = new MDSKey(row.getRow()); T result = deserialize(key, value, typeOfT); resultMap.put(key, result); } return resultMap; }
private <T> Map<MDSKey, T> listCombinedFilterKV(Scan runScan, Type typeOfT, int limit, @Nullable Predicate<KeyValue<T>> combinedFilter) { try { Map<MDSKey, T> map = Maps.newLinkedHashMap(); try (Scanner scan = table.scan(runScan)) { Row next; while ((limit > 0) && (next = scan.next()) != null) { MDSKey key = new MDSKey(next.getRow()); byte[] columnValue = next.get(COLUMN); if (columnValue == null) { continue; } T value = deserialize(key, columnValue, typeOfT); KeyValue<T> kv = new KeyValue<>(key, value); // Combined Filter doesn't pass if (combinedFilter != null && !combinedFilter.test(kv)) { continue; } map.put(kv.getKey(), kv.getValue()); limit--; } return map; } } catch (Exception e) { throw Throwables.propagate(e); } }
/** * Get the first element in the row and default COLUMN, of type T * * @param id the mds key for the row * @param typeOfT the type of the result * @return the deserialized value of the result, null if not exist */ @Nullable public <T> T getFirst(MDSKey id, Type typeOfT) { try { try (Scanner scan = table.scan(id.getKey(), Bytes.stopKeyForPrefix(id.getKey()))) { Row row = scan.next(); if (row == null || row.isEmpty()) { return null; } byte[] value = row.get(COLUMN); if (value == null) { return null; } return deserialize(id, value, typeOfT); } } catch (Exception e) { throw Throwables.propagate(e); } }
/** * Get the first element in the row and default COLUMN, of type T * * @param id the mds key for the row * @param typeOfT the type of the result * @return the deserialized value of the result, null if not exist */ @Nullable public <T> T getFirst(MDSKey id, Type typeOfT) { try { try (Scanner scan = table.scan(id.getKey(), Bytes.stopKeyForPrefix(id.getKey()))) { Row row = scan.next(); if (row == null || row.isEmpty()) { return null; } byte[] value = row.get(COLUMN); if (value == null) { return null; } return deserialize(id, value, typeOfT); } } catch (Exception e) { throw Throwables.propagate(e); } }
/** * Run a scan on MDS for default COLUMN * * @param startId scan start key * @param stopId scan stop key * @param typeOfT type of value * @param function function to process each element returned from scan. * If function.apply returns false then the scan is stopped. * Also, function.apply should not return null. * @param <T> type of value */ public <T> void scan(MDSKey startId, @Nullable MDSKey stopId, Type typeOfT, Function<KeyValue<T>, Boolean> function) { byte[] startKey = startId.getKey(); byte[] stopKey = stopId == null ? Bytes.stopKeyForPrefix(startKey) : stopId.getKey(); try (Scanner scan = table.scan(startKey, stopKey)) { Row next; while ((next = scan.next()) != null) { byte[] columnValue = next.get(COLUMN); if (columnValue == null) { continue; } MDSKey key = new MDSKey(next.getRow()); T value = deserialize(key, columnValue, typeOfT); //noinspection ConstantConditions if (!function.apply(new KeyValue<>(key, value))) { break; } } } }
/** * Run a scan on MDS for default COLUMN * * @param startId scan start key * @param stopId scan stop key * @param typeOfT type of value * @param function function to process each element returned from scan. * If function.apply returns false then the scan is stopped. * Also, function.apply should not return null. * @param <T> type of value */ public <T> void scan(MDSKey startId, @Nullable MDSKey stopId, Type typeOfT, Function<KeyValue<T>, Boolean> function) { byte[] startKey = startId.getKey(); byte[] stopKey = stopId == null ? Bytes.stopKeyForPrefix(startKey) : stopId.getKey(); try (Scanner scan = table.scan(startKey, stopKey)) { Row next; while ((next = scan.next()) != null) { byte[] columnValue = next.get(COLUMN); if (columnValue == null) { continue; } MDSKey key = new MDSKey(next.getRow()); T value = deserialize(key, columnValue, typeOfT); //noinspection ConstantConditions if (!function.apply(new KeyValue<>(key, value))) { break; } } } }