Пример #1
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;
    }
  }