//Rebuild the keyspace and get the object mapper
cm.buildKeyspace(keyspaceDefinition, true);
logger.debug("Built keyspace: {}", keyspaceDefinition.getName());
cm.setDefaultKeyspace(keyspaceDefinition);
ObjectMapper om = cm.getObjectMapper();
om.setLogCql(true);
om.truncateTables();
// This is the only static table definition this test keyspace has
List<String> staticTableNames = Arrays.asList(testUniqueTableName);
//Insert our test data into the SSTable
// For this test, all this data goes into the one table we have defined
List<Map<String, Object>> values = JsonUtil.rhombusMapFromResource(this.getClass().getClassLoader(), "SSTableWriterSimpleTestData.js");
// Tack on time based UUIDs because we don't really care what the UUID values are
for (Map<String, Object> map : values) {
map.put("id", UUIDs.startOf(Long.parseLong(map.get("created_at").toString(), 10)));
}
// Build the map to insert that we'll actually pass in
Map<String, List<Map<String, Object>>> insert = new HashMap<String, List<Map<String, Object>>>();
for (String staticTableName : staticTableNames) {
insert.put(staticTableName, values);
}
// Actually insert the data into the SSTableWriters
om.initializeSSTableWriters(false);
om.insertIntoSSTable(insert);
om.completeSSTableWrites();
// Figure out all the table names (including index tables) so we can load them into Cassandra
File[] tableDirs = keyspaceDir.listFiles();
assertNotNull(tableDirs);
List<String> allTableNames = Lists.newArrayList();
for (File file : tableDirs) {
if (file != null) {
allTableNames.add(file.getName());
}
}
for (String tableName : allTableNames) {
String SSTablePath = keyspaceName + "/" + tableName;
// Load the SSTables into Cassandra
ProcessBuilder builder = new ProcessBuilder("sstableloader", "-d", "localhost", SSTablePath);
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
long startTime = System.currentTimeMillis();
// TODO: sleep is the devil
while (!r.readLine().contains("100%") && ((System.currentTimeMillis() - startTime) < 10000)) {
Thread.sleep(100);
}
}
String staticTableName = staticTableNames.get(0);
for (Map<String, Object> expected : values) {
Map<String, Object> actual = om.getByKey(staticTableName, expected.get("id"));
assertEquals(expected, actual);
}
Map<String, Map<String, Object>> indexedExpected = Maps.uniqueIndex(values, new Function<Map<String, Object>,String>() {
public String apply(Map<String, Object> input) {
return input.get("id").toString();
}});
// Confirm get by index_1 query
SortedMap<String, Object> indexValues = Maps.newTreeMap();
indexValues.put("index_1", "index1");
Criteria criteria = new Criteria();
criteria.setIndexKeys(indexValues);
criteria.setLimit(50L);
List<Map<String, Object>> results = om.list(testUniqueTableName, criteria);
Map<String, Map<String, Object>> indexedResults;
indexedResults = Maps.uniqueIndex(results, new Function<Map<String, Object>, String>() {
public String apply(Map<String, Object> input) {
return input.get("id").toString();
}
});
Map<String, Map<String, Object>> indexedExpected1 = Maps.newHashMap(indexedExpected);
// Index_1 test data doesn't include value 4
indexedExpected1.remove("864f1400-2a7e-11b2-8080-808080808080");
assertEquals(indexedExpected1, indexedResults);
// Confirm get by index_2 query
indexValues = Maps.newTreeMap();
indexValues.put("index_2", "index2");
criteria = new Criteria();
criteria.setIndexKeys(indexValues);
criteria.setLimit(50L);
results = om.list(testUniqueTableName, criteria);
indexedResults = Maps.uniqueIndex(results, new Function<Map<String, Object>,String>() {
public String apply(Map<String, Object> input) {
return input.get("id").toString();
}});