use of herddb.model.commands.DeleteStatement in project herddb by diennea.
the class ScanTest method test.
@Test
public void test() throws Exception {
for (int i = 0; i < 100; i++) {
Map<String, Object> data = new HashMap<>();
data.put("id", "key_" + i);
data.put("number", i);
Record record = RecordSerializer.toRecord(data, table);
InsertStatement st = new InsertStatement(tableSpace, tableName, record);
assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
}
{
ScanStatement scan = new ScanStatement(tableSpace, table, new Predicate() {
@Override
public boolean evaluate(Record record, StatementEvaluationContext context) throws StatementExecutionException {
int value = (Integer) record.toBean(table).get("number");
return value >= 50;
}
});
DataScanner scanner = manager.scan(scan, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
List<?> result = scanner.consumeAndClose();
assertEquals(50, result.size());
}
for (int i = 0; i < 20; i++) {
DeleteStatement st = new DeleteStatement(tableSpace, tableName, Bytes.from_string("key_" + i), null);
assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
}
{
ScanStatement scan = new ScanStatement(tableSpace, table, new Predicate() {
@Override
public boolean evaluate(Record record, StatementEvaluationContext context) throws StatementExecutionException {
int value = (Integer) record.toBean(table).get("number");
return value < 50;
}
});
DataScanner scanner = manager.scan(scan, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
List<?> result = scanner.consumeAndClose();
assertEquals(30, result.size());
}
{
ScanStatement scan = new ScanStatement(tableSpace, table, null);
DataScanner scanner = manager.scan(scan, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
List<?> result = scanner.consumeAndClose();
assertEquals(80, result.size());
}
}
use of herddb.model.commands.DeleteStatement in project herddb by diennea.
the class NewPageTest method checkpointEmptyNewPage.
/**
* Attempt to executre a checkpoint with empty new pages
*/
@Test
public void checkpointEmptyNewPage() throws Exception {
String nodeId = "localhost";
ServerConfiguration config1 = newServerConfigurationWithAutoPort();
/* Smaller pages to avoid too many record creations */
config1.set(ServerConfiguration.PROPERTY_MAX_LOGICAL_PAGE_SIZE, 10 * 1024);
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null, config1, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (K1 string, S1 string, primary key(k1))", Collections.emptyList());
manager.checkpoint();
final TableManager tableManager = (TableManager) manager.getTableSpaceManager("tblspace1").getTableManager("tsql");
final Table table = tableManager.getTable();
int records = 0;
/*
* Fill table until there are some loaded page (not only kept in memory and unknown to page
* replacement policy).
*/
while (tableManager.getLoadedPages().size() < 2) {
Bytes key = Bytes.from_string("ins_key_" + records++);
assertEquals(1, manager.executeUpdate(new InsertStatement(table.tablespace, table.name, new Record(key, key)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
}
/* Checks that every page is still writable and filled */
for (DataPage page : tableManager.getLoadedPages()) {
assertTrue(page.writable);
assertFalse(page.immutable);
assertFalse(page.isEmpty());
assertNotEquals(0, page.getUsedMemory());
}
/* Fully remove all the data, existing (still mutable) pages should be empty now */
for (int i = 0; i < records; ++i) {
Bytes key = Bytes.from_string("ins_key_" + i);
assertEquals(1, manager.executeUpdate(new DeleteStatement(table.tablespace, table.name, key, null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
}
/* Checks that every page is still writable and empty */
for (DataPage page : tableManager.getLoadedPages()) {
assertTrue(page.writable);
assertFalse(page.immutable);
assertTrue(page.isEmpty());
assertEquals(0, page.getUsedMemory());
}
manager.checkpoint();
/* Expect just one (empty) new page remaining */
assertEquals(1, tableManager.getLoadedPages().size());
}
}
use of herddb.model.commands.DeleteStatement in project herddb by diennea.
the class NewPageTest method unloadEmptyNewPage.
/**
* Attempt to unload an empty new page from memory
*/
@Test
public void unloadEmptyNewPage() throws Exception {
String nodeId = "localhost";
ServerConfiguration config1 = newServerConfigurationWithAutoPort();
/* Smaller pages to avoid too many record creations */
config1.set(ServerConfiguration.PROPERTY_MAX_LOGICAL_PAGE_SIZE, 10 * 1024);
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null, config1, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (K1 string, S1 string, primary key(k1))", Collections.emptyList());
manager.checkpoint();
final TableManager tableManager = (TableManager) manager.getTableSpaceManager("tblspace1").getTableManager("tsql");
final Table table = tableManager.getTable();
int records = 0;
/*
* Fill table until there are some loaded page (not only kept in memory and unknown to page
* replacement policy).
*/
while (tableManager.getLoadedPages().size() < 2) {
Bytes key = Bytes.from_string("ins_key_" + records++);
assertEquals(1, manager.executeUpdate(new InsertStatement(table.tablespace, table.name, new Record(key, key)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
}
/* Checks that every page is still writable and filled */
for (DataPage page : tableManager.getLoadedPages()) {
assertTrue(page.writable);
assertFalse(page.immutable);
assertFalse(page.isEmpty());
assertNotEquals(0, page.getUsedMemory());
}
/* Fully remove all the data, existing (still mutable) pages should be empty now */
for (int i = 0; i < records; ++i) {
Bytes key = Bytes.from_string("ins_key_" + i);
assertEquals(1, manager.executeUpdate(new DeleteStatement(table.tablespace, table.name, key, null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
}
/* Checks that every page is still writable and empty */
for (DataPage page : tableManager.getLoadedPages()) {
assertTrue(page.writable);
assertFalse(page.immutable);
assertTrue(page.isEmpty());
assertEquals(0, page.getUsedMemory());
}
/* Expect that every empty new page remains */
assertEquals(2, tableManager.getLoadedPages().size());
/* Attempt to unload new pages known to page replacement policy */
boolean unloadedAny = false;
for (DataPage page : tableManager.getLoadedPages()) {
if (manager.getMemoryManager().getDataPageReplacementPolicy().remove(page)) {
unloadedAny = true;
tableManager.unload(page.pageId);
}
}
/* Ensure that some page was unloaded */
assertTrue(unloadedAny);
}
}
Aggregations