TableMapStore implements MapStore<String, Table> { @Override public void storeAll(Map<String, Table> map) { if (map == null) { throw new RuntimeException("Illegal Store Request - Null Map"); } if (map.containsKey(null)) { throw new RuntimeException("Illegal Store Request - Null Key is Present"); } logger.info("Store all called for multiple values"); BulkRequestBuilder bulkRequestBuilder = elasticsearchConnection.getClient().prepareBulk().setConsistencyLevel(WriteConsistencyLevel.ALL).setRefresh(true); for (Map.Entry<String, Table> mapEntry : map.entrySet()) { try { if (mapEntry.getValue() == null) { throw new RuntimeException(String.format("Illegal Store Request - Object is Null for Table - %s", mapEntry.getKey())); } bulkRequestBuilder.add(elasticsearchConnection.getClient() .prepareIndex(TABLE_META_INDEX, TABLE_META_TYPE, mapEntry.getKey()) .setSource(objectMapper.writeValueAsString(mapEntry.getValue()))); } catch (JsonProcessingException e) { throw new RuntimeException("Error bulk saving meta: ", e); } } bulkRequestBuilder.execute().actionGet(); } 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 testStoreAll() throws Exception { Map<String, Table> tables = new HashMap<String, Table>(); for (int i = 0; i < 10; i++) { Table table = new Table(); table.setName(UUID.randomUUID().toString()); table.setTtl(20); tables.put(table.getName(), table); } tableMapStore.storeAll(tables); MultiGetResponse response = elasticsearchConnection.getClient() .prepareMultiGet() .add(TABLE_META_INDEX, TABLE_META_TYPE, tables.keySet()) .execute() .actionGet(); Map<String, Table> responseTables = Maps.newHashMap(); for (MultiGetItemResponse multiGetItemResponse : response) { Table table = mapper.readValue(multiGetItemResponse.getResponse().getSourceAsString(), Table.class); responseTables.put(table.getName(), table); } for (String name : tables.keySet()) { compareTables(tables.get(name), responseTables.get(name)); } }
@Test(expected = RuntimeException.class) public void testStoreAllNull() throws Exception { tableMapStore.storeAll(null); }
@Test(expected = RuntimeException.class) public void testStoreAllNullTableKey() throws Exception { Map<String, Table> tables = new HashMap<String, Table>(); for (int i = 0; i < 10; i++) { Table table = new Table(); table.setName(UUID.randomUUID().toString()); table.setTtl(20); tables.put(null, table); } tableMapStore.storeAll(tables); }
@Test(expected = RuntimeException.class) public void testStoreAllNullTableValue() throws Exception { Map<String, Table> tables = new HashMap<String, Table>(); for (int i = 0; i < 10; i++) { tables.put(UUID.randomUUID().toString(), null); } tableMapStore.storeAll(tables); }
@Test(expected = RuntimeException.class) public void testStoreAllNullTableKeyValue() throws Exception { Map<String, Table> tables = new HashMap<String, Table>(); for (int i = 0; i < 10; i++) { tables.put(null, null); } tableMapStore.storeAll(tables); }
@Test(expected = RuntimeException.class) public void testStoreAllSomeNullKeys() throws Exception { Map<String, Table> tables = new HashMap<String, Table>(); for (int i = 0; i < 10; i++) { Table table = new Table(); table.setName(UUID.randomUUID().toString()); table.setTtl(20); tables.put(table.getName(), table); } Table table = new Table(); table.setName(UUID.randomUUID().toString()); table.setTtl(20); tables.put(null, table); tableMapStore.storeAll(tables); }
@Test(expected = RuntimeException.class) public void testStoreAllSomeNullValues() throws Exception { Map<String, Table> tables = new HashMap<String, Table>(); for (int i = 0; i < 10; i++) { Table table = new Table(); table.setName(UUID.randomUUID().toString()); table.setTtl(20); tables.put(table.getName(), table); } Table table = new Table(); table.setName(UUID.randomUUID().toString()); table.setTtl(20); tables.put(table.getName(), null); tableMapStore.storeAll(tables); }
@Test(expected = RuntimeException.class) public void testStoreAllSomeNullKeyValues() throws Exception { Map<String, Table> tables = new HashMap<String, Table>(); for (int i = 0; i < 10; i++) { Table table = new Table(); table.setName(UUID.randomUUID().toString()); table.setTtl(20); tables.put(table.getName(), table); } tables.put(null, null); tableMapStore.storeAll(tables); }