@Override public void delete(String storeName, String tableName) throws IOException { propStore.remove(formPath(storeName, tableName), 0); }
@Override public void delete(String storeName) throws IOException { propStore.remove(formPath(storeName), 0); }
@Override public boolean exists(String storeName, String tableName) throws IOException { String path = formPath(storeName, tableName); return propStore.exists(path, 0); }
@Override public List<String> getTableNames(String storeName, Predicate<String> predicate) throws IOException { List<String> names = Lists.newArrayList(); String path = formPath(storeName); List<String> children = propStore.getChildNames(path, 0); if (children != null) { for (String c : children) { if (predicate.apply(c)) { names.add(c); } } } return names; }
@Override public T get(String storeName, String tableName, String stateId) throws IOException { String path = formPath(storeName, tableName); byte[] data = propStore.get(path, null, 0); List<T> states = Lists.newArrayList(); deserialize(data, states, stateId); if (states.isEmpty()) { return null; } else { return states.get(0); } }
@Override public boolean create(String storeName, String tableName) throws IOException { String path = formPath(storeName, tableName); if (propStore.exists(path, 0)) { throw new IOException(String.format("State already exists for storeName %s tableName %s", storeName, tableName)); } return propStore.create(path, ArrayUtils.EMPTY_BYTE_ARRAY, AccessOption.PERSISTENT); }
/** * Get store names in the state store * * @param predicate only returns names matching predicate * @return (possibly empty) list of store names from the given store * @throws IOException */ public List<String> getStoreNames(Predicate<String> predicate) throws IOException { List<String> names = Lists.newArrayList(); String path = formPath(""); List<String> children = propStore.getChildNames(path, 0); if (children != null) { for (String c : children) { if (predicate.apply(c)) { names.add(c); } } } return names; }
@Override public List<T> getAll(String storeName, String tableName) throws IOException { List<T> states = Lists.newArrayList(); String path = formPath(storeName, tableName); byte[] data = propStore.get(path, null, 0); deserialize(data, states); return states; }
@Override public void createAlias(String storeName, String original, String alias) throws IOException { String pathOriginal = formPath(storeName, original); byte[] data; if (!propStore.exists(pathOriginal, 0)) { throw new IOException(String.format("State does not exist for table %s", original)); } data = propStore.get(pathOriginal, null, 0); putData(storeName, alias, data); }
@Override public boolean create(String storeName) throws IOException { String path = formPath(storeName); return propStore.exists(path, 0) || propStore.create(path, ArrayUtils.EMPTY_BYTE_ARRAY, AccessOption.PERSISTENT); }
/** * Retrieve states from the state store based on the store name and a filtering predicate * @param storeName The store name enclosing the state files * @param predicate The predicate for state file filtering * @return list of states matching matching the predicate * @throws IOException */ protected List<T> getAll(String storeName, Predicate<String> predicate) throws IOException { List<T> states = Lists.newArrayList(); String path = formPath(storeName); byte[] data; List<String> children = propStore.getChildNames(path, 0); if (children == null) { return Collections.emptyList(); } for (String c : children) { if (predicate.apply(c)) { data = propStore.get(path + "/" + c, null, 0); deserialize(data, states); } } return states; }
/** * Create a new znode with data if it does not exist otherwise update with data * @param storeName storeName portion of znode path * @param tableName tableName portion of znode path * @param data znode data * @throws IOException */ private void putData(String storeName, String tableName, byte[] data) throws IOException { String path = formPath(storeName, tableName); if (!propStore.exists(path, 0)) { // create with data if (!propStore.create(path, data, AccessOption.PERSISTENT)) { throw new IOException("Failed to create a state file for table " + tableName); } } else { // Update propStore.set(path, data, AccessOption.PERSISTENT); } }