@Test public void testData() throws Exception { final String INDEX_NAME = "dataIndex"; 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); String[] values = new String[] {"foo", "bar"}; for (String value : values) { IndexEntry entry = new IndexEntry(); entry.addField("field1", value); entry.addData(Bytes.toBytes("originalValue"), Bytes.toBytes(value)); index.addEntry(entry, Bytes.toBytes(value)); } Query query = new Query(); query.setRangeCondition("field1", Query.MIN_VALUE, Query.MAX_VALUE); QueryResult result = index.performQuery(query); assertNotNull(result.next()); assertEquals("bar", result.getDataAsString("originalValue")); assertNotNull(result.next()); assertEquals("foo", result.getDataAsString("originalValue")); }
@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 testSingleLongFieldIndex() throws Exception { final String INDEX_NAME = "singleLongField"; IndexManager indexManager = new IndexManager(TEST_UTIL.getConfiguration()); IndexDefinition indexDef = new IndexDefinition(INDEX_NAME, INDEX_NAME); indexDef.addLongField("field1"); indexManager.createIndex(indexDef); Index index = indexManager.getIndex(INDEX_NAME, INDEX_NAME); long values[] = {Long.MIN_VALUE, -1, 0, 1, Long.MAX_VALUE}; for (long value : values) { IndexEntry entry = new IndexEntry(); entry.addField("field1", value); index.addEntry(entry, Bytes.toBytes("key" + value)); } Query query = new Query(); query.setRangeCondition("field1", Long.MIN_VALUE, Long.MAX_VALUE); QueryResult result = index.performQuery(query); for (long value : values) { assertEquals("key" + value, Bytes.toString(result.next())); } assertNull(result.next()); }
private void assertResultIds(QueryResult result, String... identifiers) throws IOException { int i = 0; byte[] identifier; while ((identifier = result.next()) != null) { if (i >= identifiers.length) { fail("Too many query results."); } assertEquals(identifiers[i], Bytes.toString(identifier)); i++; } assertNull(result.next()); }
private void assertResultSize(int expectedCount, QueryResult result) throws IOException { int matchCount = 0; while (result.next() != null) { matchCount++; } assertEquals(expectedCount, matchCount); }
@Test public void testSingleIntFieldIndex() throws Exception { final String INDEX_NAME = "singleIntField"; IndexManager indexManager = new IndexManager(TEST_UTIL.getConfiguration()); IndexDefinition indexDef = new IndexDefinition(INDEX_NAME, INDEX_NAME); indexDef.addIntegerField("field1"); indexManager.createIndex(indexDef); Index index = indexManager.getIndex(INDEX_NAME, INDEX_NAME); final int COUNT = 1000; final int MAXVALUE = Integer.MAX_VALUE; int[] values = new int[COUNT]; for (int i = 0; i < COUNT; i++) { values[i] = (int) (Math.random() * MAXVALUE); } for (int value : values) { IndexEntry entry = new IndexEntry(); entry.addField("field1", value); index.addEntry(entry, Bytes.toBytes("key" + value)); } Query query = new Query(); query.setRangeCondition("field1", new Integer(0), new Integer(MAXVALUE)); QueryResult result = index.performQuery(query); Arrays.sort(values); for (int value : values) { assertEquals("key" + value, Bytes.toString(result.next())); } assertNull(result.next()); }