Esempio n. 1
0
  public void selectRecord(String tableName, ArrayList<Condition> conditions)
      throws TableNotFoundException {
    Table table = cm.getTable(tableName);
    if (table == null) throw new TableNotFoundException(tableName);
    Data selectResult = new Data();

    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;

      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)) {
          byte[] bytes = bn.getBytes(pos + 1, table.totalLength);
          selectResult.add(new Row(bytesToString(table, bytes)));
        }
        recordIndex++;
        accessedRecordNum++;
      }
    }

    displaySelectResult(table, selectResult);
  }
Esempio n. 2
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;
  }