TableMapStore implements MapStore<String, Table> { @Override public Table load(String key) { logger.info("Load called for: " + key); GetResponse response = elasticsearchConnection.getClient().prepareGet() .setIndex(TABLE_META_INDEX) .setType(TABLE_META_TYPE) .setId(key) .execute() .actionGet(); if (!response.isExists()) { return null; } try { return objectMapper.readValue(response.getSourceAsBytes(), Table.class); } catch (Exception e) { throw new RuntimeException("Error getting data for table: " + key); } } 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 testLoad() throws Exception { Table table = new Table(); table.setName(TEST_TABLE); table.setTtl(30); elasticsearchServer.getClient().prepareIndex() .setIndex(TABLE_META_INDEX) .setType(TABLE_META_TYPE) .setConsistencyLevel(WriteConsistencyLevel.ALL) .setSource(mapper.writeValueAsString(table)) .setId(table.getName()) .setRefresh(true) .execute() .actionGet(); Table responseTable = tableMapStore.load(table.getName()); compareTables(table, responseTable); }
@Test public void testLoadMissingKey() throws Exception { assertNull(tableMapStore.load(UUID.randomUUID().toString())); }
@Test public void testLoadNullKey() throws Exception { assertNull(tableMapStore.load(UUID.randomUUID().toString())); }
@Test(expected = RuntimeException.class) public void testLoadKeyWithWrongJson() throws Exception { elasticsearchServer.getClient().prepareIndex() .setIndex(TABLE_META_INDEX) .setType(TABLE_META_TYPE) .setConsistencyLevel(WriteConsistencyLevel.ALL) .setSource("{ \"test\" : \"test\"}") .setId(TEST_TABLE) .setRefresh(true) .execute() .actionGet(); tableMapStore.load(TEST_TABLE); }