ArrayList<ByteArray> belongToServer0ButOutsideBothKeys = new ArrayList<ByteArray>(belongToServer0ButOutsideBoth.keySet());
ArrayList<ByteArray> notBelongToServer0AndOutsideBothKeys = new ArrayList<ByteArray>(notBelongToServer0AndOutsideBoth.keySet());
List<ByteArray> queryKeys;
Iterator<QueryKeyResult> results;
QueryKeyResult entry;
// test one key on store 0
queryKeys = new ArrayList<ByteArray>();
queryKeys.add(belongToAndInsideServer0Keys.get(0));
results = getAdminClient().streamingOps.queryKeys(0, testStoreName, queryKeys.iterator());
assertTrue("Results should not be empty", results.hasNext());
entry = results.next();
assertEquals(queryKeys.get(0), entry.getKey());
assertNull("There should not be exception in response", entry.getException());
assertEquals("There should be only 1 value in versioned list", 1, entry.getValues().size());
assertEquals("Two byte[] should be equal",
0,
ByteUtils.compare(belongToAndInsideServer0.get(queryKeys.get(0)),
entry.getValues().get(0).getValue()));
assertFalse("There should be only one result", results.hasNext());
// test one key belongs to but not exists in server 0
queryKeys = new ArrayList<ByteArray>();
queryKeys.add(belongToServer0ButOutsideBothKeys.get(0));
results = getAdminClient().streamingOps.queryKeys(0, testStoreName, queryKeys.iterator());
assertTrue("Results should not be empty", results.hasNext());
entry = results.next();
assertFalse("There should not be more results", results.hasNext());
assertEquals("Not the right key", queryKeys.get(0), entry.getKey());
assertFalse("There should not be exception", entry.hasException());
assertTrue("There should be values", entry.hasValues());
assertNotNull("Response should be non-null", entry.getValues());
assertEquals("Value should be empty list", 0, entry.getValues().size());
assertNull("There should not be exception", entry.getException());
// test one key not exist and does not belong to server 0
queryKeys = new ArrayList<ByteArray>();
queryKeys.add(notBelongToServer0AndOutsideBothKeys.get(0));
results = getAdminClient().streamingOps.queryKeys(0, testStoreName, queryKeys.iterator());
assertTrue("Results should not be empty", results.hasNext());
entry = results.next();
assertFalse("There should not be more results", results.hasNext());
assertEquals("Not the right key", queryKeys.get(0), entry.getKey());
assertTrue("There should be exception", entry.hasException());
assertFalse("There should not be values", entry.hasValues());
assertNull("Value should be null", entry.getValues());
assertTrue("There should be InvalidMetadataException exception",
entry.getException() instanceof InvalidMetadataException);
// test one key that exists on server 0 but does not belong to server 0
queryKeys = new ArrayList<ByteArray>();
queryKeys.add(notBelongServer0ButInsideServer0Keys.get(0));
results = getAdminClient().streamingOps.queryKeys(0, testStoreName, queryKeys.iterator());
assertTrue("Results should not be empty", results.hasNext());
entry = results.next();
assertFalse("There should not be more results", results.hasNext());
assertEquals("Not the right key", queryKeys.get(0), entry.getKey());
assertTrue("There should be exception", entry.hasException());
assertFalse("There should not be values", entry.hasValues());
assertNull("Value should be null", entry.getValues());
assertTrue("There should be InvalidMetadataException exception",
entry.getException() instanceof InvalidMetadataException);
// test one key deleted
store0.delete(belongToAndInsideServer0Keys.get(4), null);
queryKeys = new ArrayList<ByteArray>();
queryKeys.add(belongToAndInsideServer0Keys.get(4));
results = getAdminClient().streamingOps.queryKeys(0, testStoreName, queryKeys.iterator());
assertTrue("Results should not be empty", results.hasNext());
entry = results.next();
assertFalse("There should not be more results", results.hasNext());
assertFalse("There should not be exception", entry.hasException());
assertTrue("There should be values", entry.hasValues());
assertEquals("Not the right key", queryKeys.get(0), entry.getKey());
assertEquals("Value should be empty list", 0, entry.getValues().size());
// test empty request
queryKeys = new ArrayList<ByteArray>();
results = getAdminClient().streamingOps.queryKeys(0, testStoreName, queryKeys.iterator());
assertFalse("Results should be empty", results.hasNext());
// test null key
queryKeys = new ArrayList<ByteArray>();
queryKeys.add(null);
assertEquals(1, queryKeys.size());
results = getAdminClient().streamingOps.queryKeys(0, testStoreName, queryKeys.iterator());
assertTrue("Results should not be empty", results.hasNext());
entry = results.next();
assertFalse("There should not be more results", results.hasNext());
assertTrue("There should be exception", entry.hasException());
assertFalse("There should not be values", entry.hasValues());
assertNull("Value should be null", entry.getValues());
assertTrue("There should be IllegalArgumentException exception",
entry.getException() instanceof IllegalArgumentException);
// test multiple keys (3) on store 1
queryKeys = new ArrayList<ByteArray>();
queryKeys.add(belongToAndInsideServer1Keys.get(0));
queryKeys.add(belongToAndInsideServer1Keys.get(1));
queryKeys.add(belongToAndInsideServer1Keys.get(2));
results = getAdminClient().streamingOps.queryKeys(1, testStoreName, queryKeys.iterator());
assertTrue("Results should not be empty", results.hasNext());
Map<ByteArray, List<Versioned<byte[]>>> entries = new HashMap<ByteArray, List<Versioned<byte[]>>>();
int resultCount = 0;
while(results.hasNext()) {
resultCount++;
entry = results.next();
assertNull("There should not be exception in response", entry.getException());
assertNotNull("Value should not be null for Key: ", entry.getValues());
entries.put(entry.getKey(), entry.getValues());
}
assertEquals("There should 3 and only 3 results", 3, resultCount);
for(ByteArray key: queryKeys) {
// this loop and the count ensure one-to-one mapping
assertNotNull("This key should exist in the results: " + key, entries.get(key));
assertEquals("Two byte[] should be equal for key: " + key,
0,
ByteUtils.compare(belongToAndInsideServer1.get(key),
entries.get(key).get(0).getValue()));
}
// test multiple keys, mixed situation
// key 0: Exists and belongs to
// key 1: Exists but does not belong to
// key 2: Does not exist but belongs to
// key 3: Does not belong and not exist
// key 4: Same situation with key0
// key 5: Deleted
// key 6: Same situation with key2
store0.delete(belongToAndInsideServer0Keys.get(5), null);
queryKeys = new ArrayList<ByteArray>();
queryKeys.add(belongToAndInsideServer0Keys.get(2));
queryKeys.add(notBelongServer0ButInsideServer0Keys.get(1));
queryKeys.add(belongToServer0ButOutsideBothKeys.get(1));
queryKeys.add(notBelongToServer0AndOutsideBothKeys.get(1));
queryKeys.add(belongToAndInsideServer0Keys.get(3));
queryKeys.add(belongToAndInsideServer0Keys.get(5));
queryKeys.add(notBelongServer0ButInsideServer0Keys.get(2));
results = getAdminClient().streamingOps.queryKeys(0, testStoreName, queryKeys.iterator());
// key 0
entry = results.next();
assertEquals(0, ByteUtils.compare(queryKeys.get(0).get(), entry.getKey().get()));
assertEquals(0, ByteUtils.compare(belongToAndInsideServer0.get(queryKeys.get(0)),
entry.getValues().get(0).getValue()));
assertNull(entry.getException());
// key 1
entry = results.next();
assertEquals(0, ByteUtils.compare(queryKeys.get(1).get(), entry.getKey().get()));
assertTrue("There should be InvalidMetadataException exception",
entry.getException() instanceof InvalidMetadataException);
// key 2
entry = results.next();
assertEquals(0, ByteUtils.compare(queryKeys.get(2).get(), entry.getKey().get()));
assertEquals(0, entry.getValues().size());
assertNull(entry.getException());
// key 3
entry = results.next();
assertEquals(0, ByteUtils.compare(queryKeys.get(3).get(), entry.getKey().get()));
assertTrue("There should be InvalidMetadataException exception",
entry.getException() instanceof InvalidMetadataException);
// key 4
entry = results.next();
assertEquals(0, ByteUtils.compare(queryKeys.get(4).get(), entry.getKey().get()));
assertEquals(0, ByteUtils.compare(belongToAndInsideServer0.get(queryKeys.get(4)),
entry.getValues().get(0).getValue()));
assertNull(entry.getException());
// key 5
entry = results.next();
assertEquals(0, ByteUtils.compare(queryKeys.get(5).get(), entry.getKey().get()));
assertEquals(0, entry.getValues().size());
assertNull(entry.getException());
// key 6
entry = results.next();
assertEquals(0, ByteUtils.compare(queryKeys.get(6).get(), entry.getKey().get()));
assertTrue("There should be InvalidMetadataException exception",
entry.getException() instanceof InvalidMetadataException);
// no more keys
assertFalse(results.hasNext());
}