Пример #1
0
  @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"));
  }
Пример #2
0
  @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"));
  }
Пример #3
0
  @Test
  public void testStringPrefixQuery() throws Exception {
    final String INDEX_NAME = "stringPrefixQuery";
    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 = {"baard", "boer", "beek", "kanaal", "paard"};

    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.setRangeCondition("field1", "b", "b");
    QueryResult result = index.performQuery(query);
    assertResultIds(result, "key0", "key2", "key1");
  }
Пример #4
0
  @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);
  }
Пример #5
0
  @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());
    }
  }
Пример #6
0
  @Test
  public void testIndexEntryVerificationIndex() throws Exception {
    final String INDEX_NAME = "indexEntryVerification";
    IndexManager indexManager = new IndexManager(TEST_UTIL.getConfiguration());

    IndexDefinition indexDef = new IndexDefinition(INDEX_NAME, INDEX_NAME);
    indexDef.addStringField("stringfield");
    indexDef.addFloatField("floatfield");

    indexManager.createIndex(indexDef);

    Index index = indexManager.getIndex(INDEX_NAME, INDEX_NAME);

    IndexEntry entry = new IndexEntry();
    entry.addField("nonexistingfield", "foobar");

    try {
      index.addEntry(entry, Bytes.toBytes("key"));
      fail("Expected a MalformedIndexEntryException.");
    } catch (MalformedIndexEntryException e) {
      // ok
    }

    entry = new IndexEntry();
    entry.addField("stringfield", new Integer(55));

    try {
      index.addEntry(entry, Bytes.toBytes("key"));
      fail("Expected a MalformedIndexEntryException.");
    } catch (MalformedIndexEntryException e) {
      // ok
    }

    entry = new IndexEntry();
    entry.addField("floatfield", "hello world");

    try {
      index.addEntry(entry, Bytes.toBytes("key"));
      fail("Expected a MalformedIndexEntryException.");
    } catch (MalformedIndexEntryException e) {
      // ok
    }
  }
Пример #7
0
  @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");
  }
Пример #8
0
  @Test
  public void testDeleteIndex() throws Exception {
    final String INDEX_NAME = "deleteIndex";
    IndexManager indexManager = new IndexManager(TEST_UTIL.getConfiguration());

    IndexDefinition indexDef = new IndexDefinition(INDEX_NAME, INDEX_NAME);
    indexDef.addStringField("foo");
    indexManager.createIndex(indexDef);

    indexManager.getIndex(INDEX_NAME, INDEX_NAME);

    indexManager.deleteIndex(INDEX_NAME, INDEX_NAME);

    try {
      indexManager.getIndex(INDEX_NAME, INDEX_NAME);
      fail("Expected an IndexNotFoundException.");
    } catch (IndexNotFoundException e) {
      // ok
    }
  }
Пример #9
0
  @Test
  public void testDescendingStringIndex() throws Exception {
    final String INDEX_NAME = "descendingStringIndex";
    IndexManager indexManager = new IndexManager(TEST_UTIL.getConfiguration());

    IndexDefinition indexDef = new IndexDefinition(INDEX_NAME, INDEX_NAME);
    indexDef.setIdentifierOrder(Order.DESCENDING);
    StringIndexFieldDefinition fieldDef = indexDef.addStringField("field1");
    fieldDef.setOrder(Order.DESCENDING);
    indexManager.createIndex(indexDef);
    Index index = indexManager.getIndex(INDEX_NAME, INDEX_NAME);

    String[] values = {"a", "ab", "abc", "b"};
    for (int i = 0; i < values.length; i++) {
      IndexEntry entry = new IndexEntry();
      entry.addField("field1", values[i]);
      index.addEntry(entry, Bytes.toBytes("key" + (i + 1)));
    }

    {
      Query query = new Query();
      query.setRangeCondition("field1", Query.MIN_VALUE, Query.MAX_VALUE);
      QueryResult result = index.performQuery(query);
      assertResultIds(result, "key4", "key3", "key2", "key1");
    }

    {
      Query query = new Query();
      query.setRangeCondition("field1", "b", "a");
      QueryResult result = index.performQuery(query);
      assertResultIds(result, "key4", "key3", "key2", "key1");
    }

    {
      Query query = new Query();
      query.setRangeCondition("field1", "a", "a");
      QueryResult result = index.performQuery(query);
      assertResultIds(result, "key3", "key2", "key1");
    }
  }
Пример #10
0
  @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);
  }
Пример #11
0
  /** 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());
      }
    }
  }