TableMapStore implements MapStore<String, Table> { @Override public void store(String key, Table value) { if (key == null || value == null || value.getName() == null) { throw new RuntimeException(String.format("Illegal Store Request - %s - %s", key, value)); } logger.info("Storing key: " + key); try { elasticsearchConnection.getClient().prepareIndex() .setIndex(TABLE_META_INDEX) .setType(TABLE_META_TYPE) .setConsistencyLevel(WriteConsistencyLevel.ALL) .setSource(objectMapper.writeValueAsString(value)) .setId(key) .setRefresh(true) .execute() .actionGet(); } catch (JsonProcessingException e) { throw new RuntimeException("Error saving meta: ", e); } } TableMapStore(ElasticsearchConnection elasticsearchConnection); static Factory factory(ElasticsearchConnection elasticsearchConnection); @Override void store(String key, Table value); @Override void storeAll(Map<String, Table> map); @Override void delete(String key); @Override void deleteAll(Collection<String> keys); @Override Table load(String key); @Override Map<String, Table> loadAll(Collection<String> keys); @Override Set<String> loadAllKeys(); static final String TABLE_META_INDEX; static final String TABLE_META_TYPE; }
@Test public void testStore() throws Exception { Table table = new Table(); table.setName(TEST_TABLE); table.setTtl(30); tableMapStore.store(table.getName(), table); GetResponse response = elasticsearchConnection.getClient().prepareGet() .setIndex(TABLE_META_INDEX) .setType(TABLE_META_TYPE) .setId(table.getName()) .execute() .actionGet(); compareTables(table, mapper.readValue(response.getSourceAsBytes(), Table.class)); }
@Test(expected = RuntimeException.class) public void testStoreNullKey() throws Exception { Table table = new Table(); table.setName(TEST_TABLE); table.setTtl(30); tableMapStore.store(null, table); }
@Test(expected = RuntimeException.class) public void testStoreNullTable() throws Exception { Table table = new Table(); table.setName(TEST_TABLE); table.setTtl(30); tableMapStore.store(table.getName(), null); }
@Test(expected = RuntimeException.class) public void testStoreNullTableName() throws Exception { Table table = new Table(); table.setName(null); table.setTtl(30); tableMapStore.store(TEST_TABLE, table); }