use of org.h2.command.dml.Delete in project h2database by h2database.
the class Session method unlockReadLocks.
/**
* Unlock all read locks. This is done if the transaction isolation mode is
* READ_COMMITTED.
*/
public void unlockReadLocks() {
if (database.isMultiVersion()) {
// MVCC: keep shared locks (insert / update / delete)
return;
}
// locks is modified in the loop
for (int i = 0; i < locks.size(); i++) {
Table t = locks.get(i);
if (!t.isLockedExclusively()) {
synchronized (database) {
t.unlock(this);
locks.remove(i);
}
i--;
}
}
}
use of org.h2.command.dml.Delete in project h2database by h2database.
the class LobStorageMap method init.
@Override
public void init() {
if (init) {
return;
}
init = true;
Store s = database.getMvStore();
MVStore mvStore;
if (s == null) {
// in-memory database
mvStore = MVStore.open(null);
} else {
mvStore = s.getStore();
}
lobMap = mvStore.openMap("lobMap");
refMap = mvStore.openMap("lobRef");
dataMap = mvStore.openMap("lobData");
streamStore = new StreamStore(dataMap);
// garbage collection of the last blocks
if (database.isReadOnly()) {
return;
}
if (dataMap.isEmpty()) {
return;
}
// search for the last block
// (in theory, only the latest lob can have unreferenced blocks,
// but the latest lob could be a copy of another one, and
// we don't know that, so we iterate over all lobs)
long lastUsedKey = -1;
for (Entry<Long, Object[]> e : lobMap.entrySet()) {
long lobId = e.getKey();
Object[] v = e.getValue();
byte[] id = (byte[]) v[0];
long max = streamStore.getMaxBlockKey(id);
// a lob may not have a referenced blocks if data is kept inline
if (max != -1 && max > lastUsedKey) {
lastUsedKey = max;
if (TRACE) {
trace("lob " + lobId + " lastUsedKey=" + lastUsedKey);
}
}
}
if (TRACE) {
trace("lastUsedKey=" + lastUsedKey);
}
// delete all blocks that are newer
while (true) {
Long last = dataMap.lastKey();
if (last == null || last <= lastUsedKey) {
break;
}
if (TRACE) {
trace("gc " + last);
}
dataMap.remove(last);
}
// don't re-use block ids, except at the very end
Long last = dataMap.lastKey();
if (last != null) {
streamStore.setNextKey(last + 1);
}
}
use of org.h2.command.dml.Delete in project h2database by h2database.
the class PageStore method redoDelete.
/**
* 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);
}
use of org.h2.command.dml.Delete in project h2database by h2database.
the class UpdatableRow method deleteRow.
/**
* Delete the given row in the database.
*
* @param current the row
* @throws SQLException if this row has already been deleted
*/
public void deleteRow(Value[] current) throws SQLException {
StatementBuilder buff = new StatementBuilder("DELETE FROM ");
appendTableName(buff);
appendKeyCondition(buff);
PreparedStatement prep = conn.prepareStatement(buff.toString());
setKey(prep, 1, current);
int count = prep.executeUpdate();
if (count != 1) {
// the row has already been deleted
throw DbException.get(ErrorCode.NO_DATA_AVAILABLE);
}
}
use of org.h2.command.dml.Delete in project h2database by h2database.
the class Test method main.
public static void main(String... args) throws Exception {
H2Database db = H2Utils.openOrCreateDatabase("helloWorld.db", MODE_PRIVATE, null);
log("opened ps=" + db.getPageSize());
try {
// db.execSQL("DROP TABLE IF EXISTS test");
// log("dropped");
db.execSQL("CREATE TABLE if not exists test(ID INTEGER PRIMARY KEY, NAME VARCHAR)");
log("created");
for (int j = 0; j < 10; j++) {
Cursor c = db.rawQuery("select * from test", new String[0]);
int count = c.getCount();
for (int i = 0; i < count; i++) {
c.move(1);
c.getInt(0);
c.getString(1);
}
c.close();
}
// log("select " + count);
db.execSQL("delete from test");
log("delete");
db.beginTransaction();
for (int i = 0; i < 1000; i++) {
db.execSQL("INSERT INTO TEST VALUES(?, 'Hello')", new Object[] { i });
}
db.setTransactionSuccessful();
db.endTransaction();
log("inserted");
for (int i = 0; i < 10; i++) {
Cursor c = db.rawQuery("select * from test where id=?", new String[] { "" + i });
int count = c.getCount();
if (count > 0) {
c.move(1);
c.getInt(0);
c.getString(1);
}
c.close();
}
log("select");
} finally {
db.close();
log("closed");
}
}
Aggregations