@Test public void testDataTypeChecks() throws Exception { final String INDEX_NAME = "dataTypeChecks"; IndexManager indexManager = new IndexManager(TEST_UTIL.getConfiguration()); IndexDefinition indexDef = new IndexDefinition(INDEX_NAME, INDEX_NAME); indexDef.addStringField("field1"); indexDef.addIntegerField("field2"); indexManager.createIndex(indexDef); Index index = indexManager.getIndex(INDEX_NAME, INDEX_NAME); // // Index entry checks // // First test correct situation IndexEntry entry = new IndexEntry(); entry.addField("field1", "a"); index.addEntry(entry, Bytes.toBytes("1")); // Now test incorrect situation entry = new IndexEntry(); entry.addField("field1", 55); try { index.addEntry(entry, Bytes.toBytes("1")); fail("Expected exception."); } catch (MalformedIndexEntryException e) { // System.out.println(e.getMessage()); } // // Query checks // // First test correct situation Query query = new Query(); query.addEqualsCondition("field1", "a"); index.performQuery(query); // Now test incorrect situation query = new Query(); query.addEqualsCondition("field1", 55); try { index.performQuery(query); fail("Expected exception."); } catch (MalformedQueryException e) { // System.out.println(e.getMessage()); } }
@Test public void testDeleteFromIndex() throws Exception { final String INDEX_NAME = "deleteFromIndex"; IndexManager indexManager = new IndexManager(TEST_UTIL.getConfiguration()); IndexDefinition indexDef = new IndexDefinition(INDEX_NAME, INDEX_NAME); indexDef.addStringField("field1"); indexManager.createIndex(indexDef); Index index = indexManager.getIndex(INDEX_NAME, INDEX_NAME); // Add the entry IndexEntry entry = new IndexEntry(); entry.addField("field1", "foobar"); index.addEntry(entry, Bytes.toBytes("key1")); // Test it is there Query query = new Query(); query.addEqualsCondition("field1", "foobar"); QueryResult result = index.performQuery(query); assertEquals("key1", Bytes.toString(result.next())); assertNull(result.next()); // Delete the entry index.removeEntry(entry, Bytes.toBytes("key1")); // Test it is gone result = index.performQuery(query); assertNull(result.next()); // Delete the entry again, this should not give an error index.removeEntry(entry, Bytes.toBytes("key1")); }
@Test public void testDuplicateValuesIndex() throws Exception { final String INDEX_NAME = "duplicateValues"; IndexManager indexManager = new IndexManager(TEST_UTIL.getConfiguration()); IndexDefinition indexDef = new IndexDefinition(INDEX_NAME, INDEX_NAME); indexDef.addStringField("field1"); indexManager.createIndex(indexDef); Index index = indexManager.getIndex(INDEX_NAME, INDEX_NAME); // Create a few index entries, inserting them in non-sorted order String[] values = {"a", "a", "a", "a", "b", "c", "d"}; for (int i = 0; i < values.length; i++) { IndexEntry entry = new IndexEntry(); entry.addField("field1", values[i]); index.addEntry(entry, Bytes.toBytes("key" + i)); } Query query = new Query(); query.addEqualsCondition("field1", "a"); QueryResult result = index.performQuery(query); assertResultSize(4, result); }
@Test public void testSingleDecimalFieldIndex() throws Exception { final String INDEX_NAME = "singleDecimalField"; IndexManager indexManager = new IndexManager(TEST_UTIL.getConfiguration()); IndexDefinition indexDef = new IndexDefinition(INDEX_NAME, INDEX_NAME); indexDef.addDecimalField("field1"); indexManager.createIndex(indexDef); Index index = indexManager.getIndex(INDEX_NAME, INDEX_NAME); String[] values = {"33.66", "-1", "-3.00007E77"}; for (int i = 0; i < values.length; i++) { IndexEntry entry = new IndexEntry(); entry.addField("field1", new BigDecimal(values[i])); index.addEntry(entry, Bytes.toBytes("key" + i)); } { Query query = new Query(); query.setRangeCondition("field1", new BigDecimal(values[2]), new BigDecimal(values[0])); QueryResult result = index.performQuery(query); assertResultIds(result, "key2", "key1", "key0"); } { Query query = new Query(); query.addEqualsCondition("field1", new BigDecimal(values[2])); QueryResult result = index.performQuery(query); assertResultIds(result, "key2"); } }
@Test public void testNullIndex() throws Exception { final String INDEX_NAME = "nullIndex"; IndexManager indexManager = new IndexManager(TEST_UTIL.getConfiguration()); IndexDefinition indexDef = new IndexDefinition(INDEX_NAME, INDEX_NAME); indexDef.addStringField("field1"); indexDef.addStringField("field2"); indexManager.createIndex(indexDef); Index index = indexManager.getIndex(INDEX_NAME, INDEX_NAME); IndexEntry entry = new IndexEntry(); entry.addField("field1", "foobar"); index.addEntry(entry, Bytes.toBytes("key1")); entry = new IndexEntry(); index.addEntry(entry, Bytes.toBytes("key2")); entry = new IndexEntry(); entry.addField("field2", "foobar"); index.addEntry(entry, Bytes.toBytes("key3")); Query query = new Query(); query.addEqualsCondition("field1", "foobar"); query.addEqualsCondition("field2", null); QueryResult result = index.performQuery(query); assertResultIds(result, "key1"); query = new Query(); query.addEqualsCondition("field1", null); query.addEqualsCondition("field2", null); result = index.performQuery(query); assertResultIds(result, "key2"); query = new Query(); query.addEqualsCondition("field1", null); query.addEqualsCondition("field2", "foobar"); result = index.performQuery(query); assertResultIds(result, "key3"); }
@Test public void testMultiFieldIndex() throws Exception { final String INDEX_NAME = "multiField"; IndexManager indexManager = new IndexManager(TEST_UTIL.getConfiguration()); IndexDefinition indexDef = new IndexDefinition(INDEX_NAME, INDEX_NAME); indexDef.addIntegerField("field1"); indexDef.addStringField("field2"); indexManager.createIndex(indexDef); Index index = indexManager.getIndex(INDEX_NAME, INDEX_NAME); IndexEntry entry = new IndexEntry(); entry.addField("field1", 10); entry.addField("field2", "a"); index.addEntry(entry, Bytes.toBytes("key1")); index.addEntry(entry, Bytes.toBytes("key2")); index.addEntry(entry, Bytes.toBytes("key3")); entry = new IndexEntry(); entry.addField("field1", 11); entry.addField("field2", "a"); index.addEntry(entry, Bytes.toBytes("key4")); entry = new IndexEntry(); entry.addField("field1", 10); entry.addField("field2", "b"); index.addEntry(entry, Bytes.toBytes("key5")); Query query = new Query(); query.addEqualsCondition("field1", 10); query.addEqualsCondition("field2", "a"); QueryResult result = index.performQuery(query); assertResultSize(3, result); }
/** Test searching on a subset of the fields. */ @Test public void testPartialQuery() throws Exception { final String INDEX_NAME = "partialQuery"; IndexManager indexManager = new IndexManager(TEST_UTIL.getConfiguration()); IndexDefinition indexDef = new IndexDefinition(INDEX_NAME, INDEX_NAME); indexDef.addStringField("field1"); indexDef.addIntegerField("field2"); indexDef.addStringField("field3"); indexManager.createIndex(indexDef); Index index = indexManager.getIndex(INDEX_NAME, INDEX_NAME); for (int i = 0; i < 3; i++) { IndexEntry entry = new IndexEntry(); entry.addField("field1", "value A " + i); entry.addField("field2", 10 + i); entry.addField("field3", "value B " + i); index.addEntry(entry, Bytes.toBytes("key" + i)); } // Search only on the leftmost field { Query query = new Query(); query.addEqualsCondition("field1", "value A 0"); QueryResult result = index.performQuery(query); assertResultIds(result, "key0"); } // Search only on the two leftmost fields { Query query = new Query(); query.addEqualsCondition("field1", "value A 0"); query.addEqualsCondition("field2", 10); QueryResult result = index.performQuery(query); assertResultIds(result, "key0"); } // Search only on the two leftmost fields, with range query on the second { Query query = new Query(); query.addEqualsCondition("field1", "value A 0"); query.setRangeCondition("field2", 9, 11); QueryResult result = index.performQuery(query); assertResultIds(result, "key0"); } // Try searching on just the second field, should give error { Query query = new Query(); query.addEqualsCondition("field2", 10); try { index.performQuery(query); fail("Exception expected"); } catch (MalformedQueryException e) { // System.out.println(e.getMessage()); } } // Try searching on just the second field, should give error { Query query = new Query(); query.setRangeCondition("field2", 9, 11); try { index.performQuery(query); fail("Exception expected"); } catch (MalformedQueryException e) { // System.out.println(e.getMessage()); } } // Try not using all fields from left to right, should give error { Query query = new Query(); query.addEqualsCondition("field1", "value A 0"); // skip field 2 query.addEqualsCondition("field3", "value B 0"); try { index.performQuery(query); fail("Exception expected"); } catch (MalformedQueryException e) { // System.out.println(e.getMessage()); } } // Try not using all fields from left to right, should give error { Query query = new Query(); query.addEqualsCondition("field1", "value A 0"); // skip field 2 query.setRangeCondition("field3", "a", "b"); try { index.performQuery(query); fail("Exception expected"); } catch (MalformedQueryException e) { // System.out.println(e.getMessage()); } } }