private List<KnapsackState> get(String name) throws IOException { ImmutableList.Builder<KnapsackState> builder = ImmutableList.builder(); try { logger.debug("get knapsack states: {}", name); final Client client = injector.getInstance(Client.class); createIndexIfNotExist(client); GetResponse getResponse = client.prepareGet(INDEX_NAME, MAPPING_NAME, name).execute().actionGet(); if (!getResponse.isExists()) { return builder.build(); } XContentParser parser = xContent(JSON).createParser(getResponse.getSourceAsBytes()); while (parser.nextToken() != START_ARRAY) { // forward } while (parser.nextToken() != END_ARRAY) { KnapsackState state = new KnapsackState(); builder.add(state.fromXContent(parser)); } return builder.build(); } catch (Throwable t) { logger.error("get settings failed", t); return null; } }
private void remove(final String name) { try { logger.debug("remove: {}", name); final Client client = injector.getInstance(Client.class); createIndexIfNotExist(client); client.prepareDelete(INDEX_NAME, MAPPING_NAME, name).setRefresh(true).execute().actionGet(); } catch (Throwable t) { logger.error("remove failed", t); } }
private void put(final String name, final XContentBuilder builder) { try { logger.debug("put knapsack state: {} -> {}", name, builder.string()); final Client client = injector.getInstance(Client.class); createIndexIfNotExist(client); client .prepareIndex(INDEX_NAME, MAPPING_NAME, name) .setSource(builder) .setRefresh(true) .execute() .actionGet(); } catch (Throwable t) { logger.error("update settings failed", t); } }