Exemplo n.º 1
0
  @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());
  }
Exemplo n.º 2
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);
  }
Exemplo n.º 3
0
  @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");
    }
  }
Exemplo n.º 4
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");
  }
Exemplo n.º 5
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"));
  }
Exemplo n.º 6
0
  @Test
  public void testSingleByteFieldIndex() throws Exception {
    final String INDEX_NAME = "singleByteField";
    IndexManager indexManager = new IndexManager(TEST_UTIL.getConfiguration());

    IndexDefinition indexDef = new IndexDefinition(INDEX_NAME, INDEX_NAME);
    ByteIndexFieldDefinition fieldDef = indexDef.addByteField("field1");
    fieldDef.setLength(3);
    indexManager.createIndex(indexDef);
    Index index = indexManager.getIndex(INDEX_NAME, INDEX_NAME);

    // Create a few index entries, inserting them in non-sorted order
    byte[][] values = {Bytes.toBytes("aaa"), Bytes.toBytes("aab")};

    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", Bytes.toBytes("aaa"), Bytes.toBytes("aab"));
    QueryResult result = index.performQuery(query);
    assertResultIds(result, "key0", "key1");
  }
Exemplo n.º 7
0
  @Test
  public void testDescendingIntAscendingKeyIndex() throws Exception {
    final String INDEX_NAME = "descendingIntAscendingKey";
    IndexManager indexManager = new IndexManager(TEST_UTIL.getConfiguration());

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

    Integer[] values = {1, 1, 2, 2};
    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)));
    }

    // The index on the value is descending, the identifiers themselves are ascending!

    Query query = new Query();
    query.setRangeCondition("field1", 2, 1);
    QueryResult result = index.performQuery(query);
    assertResultIds(result, "key3", "key4", "key1", "key2");
  }
Exemplo n.º 8
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"));
  }
Exemplo n.º 9
0
  @Test
  public void testSingleDateTimeFieldIndex() throws Exception {
    final String INDEX_NAME = "singleDateTimeField";
    IndexManager indexManager = new IndexManager(TEST_UTIL.getConfiguration());

    IndexDefinition indexDef = new IndexDefinition(INDEX_NAME, INDEX_NAME);
    DateTimeIndexFieldDefinition fieldDef = indexDef.addDateTimeField("field1");
    fieldDef.setPrecision(DateTimeIndexFieldDefinition.Precision.DATETIME_NOMILLIS);
    indexManager.createIndex(indexDef);

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

    Date[] values = {
      new GregorianCalendar(2010, 1, 15, 14, 5, 0).getTime(),
      new GregorianCalendar(2010, 1, 15, 14, 5, 1).getTime(),
      new GregorianCalendar(2010, 1, 16, 10, 0, 0).getTime(),
      new GregorianCalendar(2010, 1, 17, 10, 0, 0).getTime()
    };

    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",
        new GregorianCalendar(2010, 1, 15, 14, 5, 0).getTime(),
        new GregorianCalendar(2010, 1, 15, 14, 5, 1).getTime());
    QueryResult result = index.performQuery(query);
    assertResultIds(result, "key0", "key1");
  }
Exemplo n.º 10
0
  @Test
  public void testNotExistingIndex() throws Exception {
    final String INDEX_NAME = "notExisting";
    IndexManager indexManager = new IndexManager(TEST_UTIL.getConfiguration());

    try {
      indexManager.getIndex(INDEX_NAME, INDEX_NAME);
      fail("Expected an IndexNotFoundException.");
    } catch (IndexNotFoundException e) {
      // ok
    }
  }
Exemplo n.º 11
0
  public void insertRecord(String tableName, List<String> row)
      throws TableNotFoundException, UniqueKeyException, Exception {
    Table table = cm.getTable(tableName);
    if (table == null) throw new TableNotFoundException(tableName);

    byte[] bytesToInsert = getInsertBytes(table, row);

    // check uniqueness
    ArrayList<Index> allTableIndices = cm.getAllIndicesOfTable(tableName);
    for (Index idx : allTableIndices) {
      byte[] key = Arrays.copyOfRange(bytesToInsert, idx.pos, idx.pos + idx.columnLength);
      if (im.searchEqual(idx, key) != null) throw new UniqueKeyException(idx.indexName);
    }

    // Use free list for insertion and deletion
    int recordSize = table.totalLength + POINTER_SIZE;

    while (true) {
      // BufferNode bn = bm.getIfIsInBuffer(table.name + ".table", table.nextInsertBlock);
      BufferNode bn = bm.getBufferNode(table.name + ".table", table.nextInsertBlock);
      byte[] block = bn.data;
      int insertIndex = getInsertIndex(block);
      int pos = getPositionFromIndex(table, insertIndex);

      // No free space, get a new block
      if (pos + recordSize > BufferManager.BLOCK_SIZE) {
        table.nextInsertBlock++;
        if (table.nextInsertBlock >= table.blockNum) bm.addBlockInFile(table);
        continue;
      }

      // Write to buffer
      block[pos] = NOT_EMPTY;
      System.arraycopy(bytesToInsert, 0, block, pos + 1, table.totalLength);

      // Modify available insert index value and increase record number
      int nextIndex = getNextInsertIndex(block, table, insertIndex);
      setInsertIndex(block, nextIndex);
      incRecordNum(block);

      // Update index
      for (Attribute attr : table.attributes) {
        if (!attr.index.equals("")) { // has index
          Index idx = cm.getIndex(attr.index);
          byte[] key = Arrays.copyOfRange(bytesToInsert, idx.pos, idx.pos + idx.columnLength);
          im.insertKey(idx, key, table.nextInsertBlock, pos);
        }
      }

      bn.isWritten = true;
      return;
    }
  }
Exemplo n.º 12
0
  @Test
  public void testEmptyDefinition() throws Exception {
    final String INDEX_NAME = "emptyDef";
    IndexManager indexManager = new IndexManager(TEST_UTIL.getConfiguration());

    IndexDefinition indexDef = new IndexDefinition(INDEX_NAME, INDEX_NAME);
    try {
      indexManager.createIndex(indexDef);
      fail("Exception expected.");
    } catch (IllegalArgumentException e) {
    }
  }
Exemplo n.º 13
0
  /// extract from index values
  private ArrayList<Long> ExtractFromIndex(
      NodeReference node,
      ArrayList<Long> previousSetToScan,
      FilterExpression expression,
      ArrayList<FilterCondition> conditions,
      String indexName) {
    ArrayList<Long> setToScan = new ArrayList<Long>();

    // prepare range
    ArrayList<FilterCondition> range = PrepareIndexRangeByConditions(conditions);
    if (range == null) return setToScan;

    FilterCondition left = range.get(0);
    FilterCondition right = range.get(1);

    String leftsubscript = "";
    if (left != null) {
      leftsubscript = left.FilterValue.toString();
    }

    String rightsubscript = "";
    if (right != null) {
      rightsubscript = right.FilterValue.toString();
    }

    rightsubscript = IndexManager.ConvertToIndex(rightsubscript);
    leftsubscript = IndexManager.ConvertToIndex(leftsubscript);

    String key = "";
    while (true) {
      key = node.nextSubscript(indexName, leftsubscript, key);
      if (key.equals("")) {
        leftsubscript = node.nextSubscript(indexName, leftsubscript);
        key = node.nextSubscript(indexName, leftsubscript, key);
      }

      if (key.equals("")) {
        break;
      }

      if (leftsubscript.equals(rightsubscript)) {
        break;
      }

      Long parsedKey = Long.parseLong(key);
      if (previousSetToScan == null || (!previousSetToScan.contains(parsedKey))) continue;

      setToScan.add(parsedKey);
    }

    return setToScan;
  }
Exemplo n.º 14
0
  public void testAddIndexForTableWhenStringAndValLengthIsZero() throws Exception {
    IndexManager im = IndexManager.getInstance();
    assertNotNull("Index Manager should not be null.", im);

    List<IndexSpecification> indexList = new ArrayList<IndexSpecification>(1);
    IndexSpecification iSpec = new IndexSpecification("index_name");

    iSpec.addIndexColumn(new HColumnDescriptor("cf"), "cq", null, 0);
    indexList.add(iSpec);
    im.addIndexForTable("index_name", indexList);
    indexList = im.getIndicesForTable("index_name");
    assertEquals("the total value length should be 2", 2, indexList.get(0).getTotalValueLength());
  }
Exemplo n.º 15
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());
    }
  }
Exemplo n.º 16
0
  public void testRemoveIndicesForTable() throws Exception {

    IndexManager im = IndexManager.getInstance();
    assertNotNull("Index Manager should not be null.", im);

    List<IndexSpecification> indexList = new ArrayList<IndexSpecification>(1);
    IndexSpecification iSpec = new IndexSpecification("index_name");

    iSpec.addIndexColumn(new HColumnDescriptor("cf"), "cq", null, 10);
    indexList.add(iSpec);
    im.removeIndices("index_name");
    indexList = im.getIndicesForTable("index_name");
    assertNull("Index specification List should be null.", indexList);
  }
Exemplo n.º 17
0
  @BeforeClass
  public static void setUpBeforeClass() throws Exception {
    TestHelper.setupLogging();
    TEST_UTIL.startMiniCluster(1);

    IndexManager.createIndexMetaTable(TEST_UTIL.getConfiguration());
  }
Exemplo n.º 18
0
 @Override
 public ResultPage<T> search(ResultPage<T> resultPage, Mapper<T> mapper) {
   ElasticSearchCriteria criteria = resultPage.getCriteria();
   if (criteria == null) return resultPage;
   SearchRequestBuilder srb = criteria2builder(criteria);
   if (resultPage.isPaginating()) {
     srb.setFrom(resultPage.getStart());
     srb.setSize(resultPage.getPageSize());
   } else {
     srb.setFrom(0);
     srb.setSize(ResultPage.DEFAULT_MAX_PAGESIZE);
   }
   try {
     SearchResponse response = srb.execute().get();
     SearchHits shs = response.getHits();
     if (shs != null) {
       resultPage.setTookInMillis(response.getTookInMillis());
       resultPage.setTotalResults(shs.getTotalHits());
       List list = new ArrayList(shs.getHits().length);
       resultPage.setResult(list);
       for (SearchHit sh : shs.getHits()) {
         T data = (T) indexManager.searchHitToEntity(sh);
         data = mapper == null ? data : mapper.map(data);
         if (data != null) list.add(data);
       }
     }
   } catch (Exception e) {
     logger.error(e.getMessage(), e);
   }
   return resultPage;
 }
Exemplo n.º 19
0
  private String createIndexTableStatementForIndex(String indexName, List<String> columns) {
    String tableName = IndexManager.tableNameForIndex(indexName);
    Joiner joiner = Joiner.on(" NONE,").skipNulls();
    String cols = joiner.join(columns);

    return String.format("CREATE TABLE %s ( %s NONE )", tableName, cols);
  }
Exemplo n.º 20
0
 private SearchRequestBuilder criteria2builder(ElasticSearchCriteria criteria) {
   String[] indices = criteria.getIndices();
   if (indices == null || indices.length == 0)
     indices = new String[] {indexManager.getIndexName()};
   SearchRequestBuilder srb = client.prepareSearch(indices);
   srb.setTimeout(new TimeValue(10, TimeUnit.SECONDS));
   String[] types = criteria.getTypes();
   if (types != null && types.length > 0) srb.setTypes(types);
   QueryBuilder qb = criteria.getQueryBuilder();
   String query = criteria.getQuery();
   if (qb == null && StringUtils.isBlank(query))
     throw new NullPointerException("queryBuilder is null and queryString is blank");
   if (qb == null && StringUtils.isNotBlank(query)) {
     if (wildcardQueryPattern.matcher(query).matches()) {
       String[] arr = query.split(":", 2);
       qb = QueryBuilders.wildcardQuery(arr[0], arr[1]);
     } else {
       QueryStringQueryBuilder qsqb = new QueryStringQueryBuilder(query);
       qsqb.defaultOperator(Operator.AND);
       qb = qsqb;
     }
   }
   srb.setQuery(qb);
   Map<String, Boolean> sorts = criteria.getSorts();
   for (Map.Entry<String, Boolean> entry : sorts.entrySet())
     srb.addSort(entry.getKey(), entry.getValue() ? SortOrder.DESC : SortOrder.ASC);
   return srb;
 }
  /**
   * This helper method handles the case when a tuple is being removed from the table, before the
   * row has actually been removed from the table. All indexes on the table are updated to remove
   * the row.
   *
   * @param tblFileInfo details of the table being updated
   * @param ptup the tuple about to be removed from the table
   */
  private void removeRowFromIndexes(TableInfo tblFileInfo, PageTuple ptup) {

    logger.debug(
        "Removing tuple " + ptup + " from indexes for table " + tblFileInfo.getTableName());

    // Iterate over the indexes in the table.
    TableSchema schema = tblFileInfo.getSchema();
    for (ColumnRefs indexDef : schema.getIndexes().values()) {
      TupleLiteral idxTup =
          IndexUtils.makeSearchKeyValue(indexDef, ptup, /* findExactTuple */ true);

      logger.debug("Removing tuple " + idxTup + " from index " + indexDef.getIndexName());

      try {
        IndexInfo indexInfo = indexManager.openIndex(tblFileInfo, indexDef.getIndexName());

        TupleFile tupleFile = indexInfo.getTupleFile();

        PageTuple idxPageTup = IndexUtils.findTupleInIndex(idxTup, tupleFile);
        if (idxPageTup == null) {
          throw new IllegalStateException(
              "Can't find tuple in " + "index corresponding to table's tuple.");
        }

        tupleFile.deleteTuple(idxPageTup);
      } catch (IOException e) {
        throw new EventDispatchException(
            "Couldn't update index "
                + indexDef.getIndexName()
                + " for table "
                + tblFileInfo.getTableName());
      }
    }
  }
Exemplo n.º 22
0
  public int init() {
    try {
      manager = new IndexManager();
      monitor = new Monitor(manager.getMetaManager().getLayoutManager());
      String ip =
          PublicParam.CONFIG.getStringValue(Configure.CONFIG_MANAGER, Configure.CONF_IP_ADDR, null);
      int port = PublicParam.CONFIG.getIntValue(Configure.CONFIG_MANAGER, Configure.CONF_PORT, 0);

      managerAddr = new Address(ip, port);
      String url = "rmi:/" + managerAddr.getAddr().toString() + "/manager";
      // echo
      System.out.println(url);
      LocateRegistry.createRegistry(port);
      Naming.rebind(url, manager);
      System.out.println("rmi:manager is ready!");
    } catch (RemoteException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (MalformedURLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    return GlobalMessage.ISS_SUCCESS;
  }
Exemplo n.º 23
0
  public int deleteRecord(String tableName, List<Condition> conditions)
      throws TableNotFoundException, Exception {
    Table table = cm.getTable(tableName);
    if (table == null) throw new TableNotFoundException(tableName);

    int count = 0;

    for (int blockOffset = 0; blockOffset < table.blockNum; blockOffset++) {
      BufferNode bn = bm.getBufferNode(table.name + ".table", blockOffset);
      byte[] block = bn.data;
      int recordNum = getRecordNum(block);
      int recordIndex = 0;
      int accessedRecordNum = 0;
      int nextDeleted = getInsertIndex(block);
      int prevDeleted = -1;

      ArrayList<Index> allTableIndices = cm.getAllIndicesOfTable(tableName);

      while (accessedRecordNum < recordNum) {
        int pos = getPositionFromIndex(table, recordIndex);
        if (block[pos] == EMPTY) { // record is empty, skip
          recordIndex++;
          continue;
        }

        byte[] recordBytes = bn.getBytes(pos + 1, table.totalLength);
        if (matchAllCond(table, recordBytes, conditions)) {
          block[pos] = EMPTY;
          if (recordIndex < nextDeleted) {
            setNextInsertIndex(block, table, prevDeleted, recordIndex);
            setNextInsertIndex(block, table, recordIndex, nextDeleted);
            prevDeleted = recordIndex;
          } else {
            int nextOfNext = getNextInsertIndex(block, table, nextDeleted);
            setNextInsertIndex(block, table, nextDeleted, recordIndex);
            setNextInsertIndex(block, table, recordIndex, nextOfNext);
            nextDeleted = nextOfNext;
            prevDeleted = recordIndex;
          }

          decRecordNum(block);
          // there remains some space for insertion
          if (table.nextInsertBlock > blockOffset) table.nextInsertBlock = blockOffset;

          // Delete in index
          for (Index idx : allTableIndices) {
            byte[] key = Arrays.copyOfRange(recordBytes, idx.pos, idx.pos + idx.columnLength);
            im.deleteKey(idx, key);
          }

          bn.isWritten = true;
          count++;
        }
        recordIndex++;
        accessedRecordNum++;
      }
    }
    return count;
  }
Exemplo n.º 24
0
  private String createIndexIndexStatementForIndex(String indexName, List<String> columns) {
    String tableName = IndexManager.tableNameForIndex(indexName);
    String sqlIndexName = tableName.concat("_index");
    Joiner joiner = Joiner.on(",").skipNulls();
    String cols = joiner.join(columns);

    return String.format("CREATE INDEX %s ON %s ( %s )", sqlIndexName, tableName, cols);
  }
Exemplo n.º 25
0
  public void testShouldNotThrowNPEIfValueTypeIsNull() throws Exception {
    IndexManager im = IndexManager.getInstance();
    assertNotNull("Index Manager should not be null.", im);

    List<IndexSpecification> indexList = new ArrayList<IndexSpecification>(1);
    IndexSpecification iSpec = new IndexSpecification("index_name");

    iSpec.addIndexColumn(new HColumnDescriptor("cf"), "cq", null, 5);
    indexList.add(iSpec);
    im.addIndexForTable("index_name", indexList);
    indexList = im.getIndicesForTable("index_name");

    Set<ColumnQualifier> indexColumns = indexList.get(0).getIndexColumns();
    for (ColumnQualifier columnQualifier : indexColumns) {
      assertNotNull(columnQualifier.getType());
    }
  }
 @Override
 public void close() throws IndexException {
   try {
     index.releaseIndexSearcher(searcher);
   } catch (IOException io) {
     throw new IndexException(io);
   }
 }
Exemplo n.º 27
0
  @Test
  public void testExclusiveRanges() throws Exception {
    final String INDEX_NAME = "exclusiveRanges";
    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);

    int[] values = {1, 2, 3, 4};
    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", 1, 4);
      QueryResult result = index.performQuery(query);
      assertResultIds(result, "key1", "key2", "key3", "key4");
    }

    {
      Query query = new Query();
      query.setRangeCondition("field1", 1, 4, false, false);
      QueryResult result = index.performQuery(query);
      assertResultIds(result, "key2", "key3");
    }

    {
      Query query = new Query();
      query.setRangeCondition("field1", 1, 4, false, true);
      QueryResult result = index.performQuery(query);
      assertResultIds(result, "key2", "key3", "key4");
    }

    {
      Query query = new Query();
      query.setRangeCondition("field1", 1, 4, true, false);
      QueryResult result = index.performQuery(query);
      assertResultIds(result, "key1", "key2", "key3");
    }
  }
Exemplo n.º 28
0
  public void testAddIndexForTable() throws Exception {

    IndexManager im = IndexManager.getInstance();
    assertNotNull("Index Manager should not be null.", im);

    List<IndexSpecification> indexList = new ArrayList<IndexSpecification>(1);
    IndexSpecification iSpec = new IndexSpecification("index_name");

    iSpec.addIndexColumn(new HColumnDescriptor("cf"), "cq", null, 10);
    indexList.add(iSpec);
    im.addIndexForTable("index_name", indexList);
    indexList = im.getIndicesForTable("index_name");
    assertEquals(
        "Index name should be equal with actual value.", "index_name", indexList.get(0).getName());
    assertTrue(
        "Column qualifier state mismatch.",
        indexList.get(0).getIndexColumns().contains(new ColumnQualifier("cf", "cq", null, 10)));
  }
Exemplo n.º 29
0
  /**
   * This method generates the virtual table create SQL for the specified index. Note: Any column
   * that contains an '=' will cause the statement to fail because it triggers SQLite to expect that
   * a parameter/value is being passed in.
   *
   * @param indexName the index name to be used when creating the SQLite virtual table
   * @param columns the columns in the table
   * @param indexSettings the special settings to apply to the virtual table - (only 'tokenize' is
   *     current supported)
   * @return the SQL to create the SQLite virtual table
   */
  private String createVirtualTableStatementForIndex(
      String indexName, List<String> columns, List<String> indexSettings) {
    String tableName = IndexManager.tableNameForIndex(indexName);
    Joiner joiner = Joiner.on(",").skipNulls();
    String cols = joiner.join(columns);
    String settings = joiner.join(indexSettings);

    return String.format(
        "CREATE VIRTUAL TABLE %s USING FTS4 ( %s, %s )", tableName, cols, settings);
  }
Exemplo n.º 30
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
    }
  }