@Override long getLastKey() { // TODO re-use keys, but remove this mechanism if (entryCount == 0) { return 0; } return getRowAt(entryCount - 1).getKey(); }
/** * Get the next row that is stored after this row. * * @param row the current row or null to start the scan * @return the next row or null if there are no more rows */ Row getNextRow(Row row) { long key; if (row == null) { key = -1; } else { key = row.getKey(); } while (true) { key++; if (key >= rows.size()) { return null; } row = rows.get((int) key); if (!row.isEmpty()) { return row; } } }
@Override public void add(Session session, Row row) { if (closed) { throw DbException.throwInternalError(); } treeMap.add(getKey(row), row.getKey()); }
@Override public void add(Session session, Row row) { Value key = row.getValue(indexColumn); ArrayList<Long> positions = rows.get(key); if (positions == null) { positions = New.arrayList(); rows.put(key, positions); } positions.add(row.getKey()); rowCount++; }
@Override public void remove(Session session, Row row) { if (rowCount == 1) { // last row in table reset(); } else { Value key = row.getValue(indexColumn); ArrayList<Long> positions = rows.get(key); if (positions.size() == 1) { // last row with such key rows.remove(key); } else { positions.remove(row.getKey()); } rowCount--; } }
/** * Redo a delete in a table. * * @param tableId the object id of the table * @param key the key of the row to delete */ void redoDelete(int tableId, long key) { Index index = metaObjects.get(tableId); PageDataIndex scan = (PageDataIndex) index; Row row = scan.getRowWithKey(key); if (row == null || row.getKey() != key) { trace.error(null, "Entry not found: " + key + " found instead: " + row + " - ignoring"); return; } redo(tableId, row, false); }
@Override public void add(Session session, Row row) { // in-memory if (firstFree == -1) { int key = rows.size(); row.setKey(key); rows.add(row); } else { long key = firstFree; Row free = rows.get((int) key); firstFree = free.getKey(); row.setKey(key); rows.set((int) key, row); } row.setDeleted(false); if (database.isMultiVersion()) { if (delta == null) { delta = new HashSet<>(); } boolean wasDeleted = delta.remove(row); if (!wasDeleted) { delta.add(row); } incrementRowCount(session.getId(), 1); } rowCount++; }
@Override public void remove(Session session, Row row) { if (closed) { throw DbException.throwInternalError(); } if (!treeMap.remove(getKey(row), row.getKey())) { throw DbException.throwInternalError("row not found"); } }
Row free = session.createRow(null, 1); free.setKey(firstFree); long key = row.getKey(); if (rows.size() <= key) { throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1,
private void removeMetaIndex(Index index, Session session) { int key = index.getId() + 1; Row row = metaIndex.getRow(session, key); if (row.getKey() != key) { throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "key: " + key + " index: " + index + " table: " + index.getTable() + " row: " + row); } metaIndex.remove(session, row); }
@Override public void add(Session session, Row row) { Value key = row.getValue(indexColumn); Object old = rows.get(key); if (old != null) { // TODO index duplicate key for hash indexes: is this allowed? throw getDuplicateKeyException(key.toString()); } rows.put(key, row.getKey()); }
private boolean removeIfExists(Session session, Row row) { // maybe it was inserted by the same session just before Cursor c = delta.find(session, row, row); while (c.next()) { Row r = c.get(); if (r.getKey() == row.getKey() && r.getVersion() == row.getVersion()) { if (r != row && table.getScanIndex(session).compareRows(r, row) != 0) { row.setVersion(r.getVersion() + 1); } else { delta.remove(session, r); return true; } } } return false; }
@Override public void remove(Session session, Row row) { ValueArray array = convertToKey(row); TransactionMap<Value, Value> map = getMap(session); try { Value old = map.remove(array); if (old == null) { throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1, getSQL() + ": " + row.getKey()); } } catch (IllegalStateException e) { throw mvTable.convertException(e); } }
@Override public void remove(Session session, Row row) { if (mvTable.getContainsLargeObject()) { for (int i = 0, len = row.getColumnCount(); i < len; i++) { Value v = row.getValue(i); if (v.isLinkedToTable()) { session.removeAtCommit(v); } } } TransactionMap<Value, Value> map = getMap(session); try { Value old = map.remove(ValueLong.get(row.getKey())); if (old == null) { throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1, getSQL() + ": " + row.getKey()); } } catch (IllegalStateException e) { throw mvTable.convertException(e); } }
@Override int addRowTry(Row row) { index.getPageStore().logUndo(this, data); int keyOffsetPairLen = 4 + Data.getVarLongLen(row.getKey()); while (true) { int x = find(row.getKey()); PageData page = index.getPage(childPageIds[x], getPos()); int splitPoint = page.addRowTry(row); if (splitPoint == -1) { break; } if (length + keyOffsetPairLen > index.getPageStore().getPageSize()) { return entryCount / 2; } long pivot = splitPoint == 0 ? row.getKey() : page.getKey(splitPoint - 1); PageData page2 = page.split(splitPoint); index.getPageStore().update(page); index.getPageStore().update(page2); addChild(x, page2.getPos(), pivot); index.getPageStore().update(this); } updateRowCount(1); return -1; }
@Override public void remove(Session session, Row row) { SpatialKey key = getKey(row); if (key.isNull()) { return; } TransactionMap<SpatialKey, Value> map = getMap(session); try { Value old = map.remove(key); if (old == null) { old = map.remove(key); throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1, getSQL() + ": " + row.getKey()); } } catch (IllegalStateException e) { throw mvTable.convertException(e); } }
/** * Get the value for the given column. * * @param filterId table filter id * @param column the column * @return column value for current row */ public Value getValue(int filterId, Column column) { if (current == null) { return null; } Object x = current.row(filterId); assert x != null; Row row = current.isRow(filterId) ? (Row) x : ((Cursor) x).get(); int columnId = column.getColumnId(); if (columnId == -1) { return ValueLong.get(row.getKey()); } Value value = row.getValue(column.getColumnId()); if (value == null) { throw DbException.throwInternalError("value is null: " + column + " " + row); } return value; }
private boolean existsRow(Session session, Index searchIndex, SearchRow check, Row excluding) { Table searchTable = searchIndex.getTable(); searchTable.lock(session, false, false); Cursor cursor = searchIndex.find(session, check, check); while (cursor.next()) { SearchRow found; found = cursor.getSearchRow(); if (excluding != null && found.getKey() == excluding.getKey()) { continue; } Column[] cols = searchIndex.getColumns(); boolean allEqual = true; int len = Math.min(columns.length, cols.length); for (int i = 0; i < len; i++) { int idx = cols[i].getColumnId(); Value c = check.getValue(idx); Value f = found.getValue(idx); if (searchTable.compareTypeSafe(c, f) != 0) { allEqual = false; break; } } if (allEqual) { return true; } } return false; }
/** * Append the row to the buffer. * * @param buff the buffer * @param log the undo log */ void append(Data buff, UndoLog log) { int p = buff.length(); buff.writeInt(0); buff.writeInt(operation); buff.writeByte(row.isDeleted() ? (byte) 1 : (byte) 0); buff.writeInt(log.getTableId(table)); buff.writeLong(row.getKey()); buff.writeInt(row.getSessionId()); int count = row.getColumnCount(); buff.writeInt(count); for (int i = 0; i < count; i++) { Value v = row.getValue(i); buff.checkCapacity(buff.getValueLen(v)); buff.writeValue(v); } buff.fillAligned(); buff.setInt(p, (buff.length() - p) / Constants.FILE_BLOCK_SIZE); }
int columnCount = r.getColumnCount(); buff.writeInt(columnCount); buff.writeLong(r.getKey()); buff.writeInt(r.getVersion()); buff.writeInt(r.isDeleted() ? 1 : 0);