*/
public class CursorTest extends TestCase {
- static final List<TestDB> INDEX_CURSOR_DBS =
+ static final List<TestDB> INDEX_CURSOR_DBS =
TestDB.getSupportedForBasename(Basename.INDEX_CURSOR);
expectedRows.subList(0, startIdx).clear();
return expectedRows;
}
-
- private static Database createTestTable(final FileFormat fileFormat)
- throws Exception
+
+ private static Database createTestTable(final FileFormat fileFormat)
+ throws Exception
{
Database db = createMem(fileFormat);
expectedRows.add(createExpectedRow("id", i, "value", "data" + i));
}
return expectedRows;
- }
+ }
- static Database createTestIndexTable(final TestDB indexCursorDB)
- throws Exception
+ static Database createTestIndexTable(final TestDB indexCursorDB)
+ throws Exception
{
Database db = openMem(indexCursorDB);
return expectedRows;
}
- private static Database createDupeTestTable(final FileFormat fileFormat)
- throws Exception
+ private static Database createDupeTestTable(final FileFormat fileFormat)
+ throws Exception
{
Database db = createMem(fileFormat);
return db;
}
- static Database createDupeTestTable(final TestDB indexCursorDB)
- throws Exception
+ static Database createDupeTestTable(final TestDB indexCursorDB)
+ throws Exception
{
Database db = openMem(indexCursorDB);
.setEndRowInclusive(type == 0)
.toCursor();
}
-
+
public void testRowId() throws Exception {
// test special cases
RowIdImpl rowId1 = new RowIdImpl(1, 2);
RowIdImpl.LAST_ROW_ID),
sortedRowIds);
}
-
+
public void testSimple() throws Exception {
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
Database db = createTestTable(fileFormat);
assertTrue(cursor.moveToNextRow());
assertTrue(cursor.moveToPreviousRow());
assertEquals(expectedRow, cursor.getCurrentRow());
-
+
while(cursor.moveToNextRow()) {
foundRows.add(cursor.getCurrentRow());
}
assertTrue(cursor.isAfterLast());
cursor.restoreSavepoint(savepoint);
- assertEquals(expectedRow, cursor.getCurrentRow());
+ assertEquals(expectedRow, cursor.getCurrentRow());
}
public void testMoveNoReset() throws Exception {
{
List<Map<String, Object>> expectedRows = createTestTableData();
List<Map<String, Object>> foundRows = new ArrayList<Map<String, Object>>();
-
+
Iterator<Row> iter = cursor.newIterable().iterator();
for(int i = 0; i < 6; ++i) {
foundRows.add(iter.next());
- }
+ }
iter = cursor.newIterable().reset(false).reverse().iterator();
iter.next();
Map<String, Object> row = iter.next();
assertEquals(expectedRows.get(4), row);
-
+
iter = cursor.newIterable().reset(false).iterator();
iter.next();
row = iter.next();
iter = cursor.newIterable().reset(false).iterator();
for(int i = 6; i < 10; ++i) {
foundRows.add(iter.next());
- }
+ }
assertEquals(expectedRows, foundRows);
}
-
+
public void testSearch() throws Exception {
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
Database db = createTestTable(fileFormat);
assertEquals(createExpectedRow("id", 7,
"value", "data" + 7),
cursor.getCurrentRow());
-
+
assertTrue(cursor.findFirstRow(table.getColumn("value"), "data" + 4));
assertEquals(createExpectedRow("id", 4,
"value", "data" + 4),
"id", outOfRangeValue,
"value", "data" + outOfRangeValue)));
}
-
+
assertEquals("data" + 5,
CursorBuilder.findValue(table,
table.getColumn("value"),
for(Map<String, Object> row : cursor.newIterable().reverse()) {
foundRows.add(row);
}
- assertEquals(expectedRows, foundRows);
+ assertEquals(expectedRows, foundRows);
}
-
+
public void testLiveAddition() throws Exception {
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
Database db = createTestTable(fileFormat);
assertTrue(cursor2.isAfterLast());
table.addRow(newRowNum, "data" + newRowNum);
- Map<String,Object> expectedRow =
+ Map<String,Object> expectedRow =
createExpectedRow("id", newRowNum, "value", "data" + newRowNum);
assertFalse(cursor1.isAfterLast());
assertTrue(cursor2.isAfterLast());
}
-
+
public void testLiveDeletion() throws Exception {
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
Database db = createTestTable(fileFormat);
assertEquals(expectedDeletedRow, cursor2.getCurrentRow());
assertEquals(expectedDeletedRow, cursor3.getCurrentRow());
-
+
assertFalse(cursor2.isCurrentRowDeleted());
assertFalse(cursor3.isCurrentRowDeleted());
assertEquals(expectedNextRow, cursor1.getNextRow());
assertEquals(expectedNextRow, cursor2.getNextRow());
assertEquals(expectedNextRow, cursor3.getNextRow());
-
+
assertEquals(expectedPrevRow, cursor3.getPreviousRow());
assertTrue(cursor3.moveToNextRow());
db.close();
}
}
-
+
public void testReverseIndex() throws Exception {
for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) {
Database db = createTestIndexTable(indexCursorDB);
}
}
}
-
+
public void testMoveIndexSubRange() throws Exception {
for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) {
for(int i = 0; i < 2; ++i) {
}
}
}
-
+
public void testSearchIndexSubRange() throws Exception {
for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) {
for(int i = 0; i < 2; ++i) {
}
}
}
-
+
public void testLiveDeletionIndexSubRange() throws Exception {
for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) {
for(int i = 0; i < 2; ++i) {
rows = RowFilterTest.toList(
cursor.newIterable().setMatchPattern(
Collections.singletonMap("id", 8)));
-
+
expectedRows =
createExpectedTable(
createExpectedRow(
assertEquals(expectedRows, rows);
for(Map<String,Object> row : table) {
-
+
List<Map<String,Object>> tmpRows = new ArrayList<Map<String,Object>>();
for(Map<String,Object> tmpRow : cursor) {
if(row.equals(tmpRow)) {
}
expectedRows = tmpRows;
assertFalse(expectedRows.isEmpty());
-
+
rows = RowFilterTest.toList(cursor.newIterable().setMatchPattern(row));
assertEquals(expectedRows, rows);
db.close();
}
}
-
+
public void testColumnMatcher() throws Exception {
-
+
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
Database db = createTestTable(fileFormat);
Cursor cursor = CursorBuilder.createCursor(table);
doTestMatcher(table, cursor, SimpleColumnMatcher.INSTANCE, false);
- doTestMatcher(table, cursor, CaseInsensitiveColumnMatcher.INSTANCE,
+ doTestMatcher(table, cursor, CaseInsensitiveColumnMatcher.INSTANCE,
true);
db.close();
}
assertFalse(columnMatcher.matches(table, "value", null, 13));
assertTrue(columnMatcher.matches(table, "value", 13, 13));
}
-
- private static void doTestMatcher(Table table, Cursor cursor,
+
+ private static void doTestMatcher(Table table, Cursor cursor,
ColumnMatcher columnMatcher,
boolean caseInsensitive)
throws Exception
assertEquals(createExpectedRow("id", 7,
"value", "data" + 7),
cursor.getCurrentRow());
-
+
assertTrue(cursor.findFirstRow(createExpectedRow(
"value", "Data" + 7)) == caseInsensitive);
if(caseInsensitive) {
"value", "data" + 7),
cursor.getCurrentRow());
}
-
+
assertTrue(cursor.findFirstRow(table.getColumn("value"), "data" + 4));
assertEquals(createExpectedRow("id", 4,
"value", "data" + 4),
cursor.getCurrentRow());
- assertTrue(cursor.findFirstRow(table.getColumn("value"), "Data" + 4)
+ assertTrue(cursor.findFirstRow(table.getColumn("value"), "Data" + 4)
== caseInsensitive);
if(caseInsensitive) {
assertEquals(createExpectedRow("id", 4,
}
public void testIndexCursor() throws Exception
- {
- for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) {
+ {
+ for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) {
Database db = openMem(testDB);
Table t1 = db.getTable("Table1");
assertTrue(cursor.findFirstRowByEntry(1));
assertEquals(1, cursor.getCurrentRow().get("id"));
-
+
cursor.findClosestRowByEntry(2);
assertEquals(2, cursor.getCurrentRow().get("id"));
assertTrue(cursor.isAfterLast());
db.close();
- }
+ }
}
-
+
public void testIndexCursorDelete() throws Exception
- {
+ {
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) {
Database db = openMem(testDB);
assertEquals(Arrays.asList("baz11", "baz11-2"), expectedData);
expectedData = new ArrayList<String>();
- for(Iterator<? extends Row> iter =
+ for(Iterator<? extends Row> iter =
cursor.newEntryIterable(1).iterator();
iter.hasNext(); ) {
expectedData.add(iter.next().getString("data"));
}
assertEquals(Arrays.asList("baz11", "baz11-2"), expectedData);
-
+
expectedData = new ArrayList<String>();
for(Row row : cursor.newEntryIterable(1)
.addColumnNames("data")) {
}
assertTrue(expectedData.isEmpty());
-
+
db.close();
- }
+ }
}
-
+
public void testCursorDelete() throws Exception
- {
+ {
for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) {
Database db = openMem(testDB);
}
assertEquals(Arrays.asList("baz11", "baz11-2"), expectedData);
-
+
expectedData = new ArrayList<String>();
for(Row row : cursor.newIterable().setColumnNames(
Arrays.asList("otherfk1", "data"))) {
}
assertTrue(expectedData.isEmpty());
-
+
db.close();
- }
+ }
}
-
+
public void testFindByRowId() throws Exception {
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
Database db = createTestTable(fileFormat);
}
}
- private static void doTestFindByRowId(Cursor cursor)
+ private static void doTestFindByRowId(Cursor cursor)
throws Exception
{
for(int i = 0; i < 3; ++i) {
cursor.moveToNextRow();
- }
+ }
Row r1 = cursor.getCurrentRow();
for(int i = 0; i < 3; ++i) {
cursor.moveToNextRow();
- }
+ }
Row r2 = cursor.getCurrentRow();
doTestFindByRowId(cursor, r1, 2);
doTestFindByRowId(cursor, r2, 5);
- }
+ }
private static void doTestFindByRowId(Cursor cursor, Row row, int id)
throws Exception
assertFalse(cursor.findRow(RowIdImpl.FIRST_ROW_ID));
- assertEquals(id - 1, cursor.getCurrentRow().get("id"));
+ assertEquals(id - 1, cursor.getCurrentRow().get("id"));
}
public void testIterationEarlyExit() throws Exception {
IndexCursor cursor = CursorBuilder.createCursor(
table.getIndex("value_idx"));
-
+
try {
cursor.newIterable()
.addMatchPattern("value", "val-9")
public void testPartialIndexFind() throws Exception
{
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
-
+
Database db = createMem(fileFormat);
TableImpl t = (TableImpl)new TableBuilder("Test")
.addIndex(new IndexBuilder("idx3").addColumns("data1", "num2", "key3"))
.toTable(db);
- Index idx = t.findIndexForColumns(Arrays.asList("data1"),
+ Index idx = t.findIndexForColumns(Arrays.asList("data1"),
TableImpl.IndexFeature.ANY_MATCH);
assertEquals("idx3", idx.getName());
- idx = t.findIndexForColumns(Arrays.asList("data1", "num2"),
+ idx = t.findIndexForColumns(Arrays.asList("data1", "num2"),
TableImpl.IndexFeature.ANY_MATCH);
assertEquals("idx3", idx.getName());
- idx = t.findIndexForColumns(Arrays.asList("data1", "num2", "key3"),
+ idx = t.findIndexForColumns(Arrays.asList("data1", "num2", "key3"),
TableImpl.IndexFeature.ANY_MATCH);
assertEquals("idx3", idx.getName());
- assertNull(t.findIndexForColumns(Arrays.asList("num2"),
+ assertNull(t.findIndexForColumns(Arrays.asList("num2"),
TableImpl.IndexFeature.ANY_MATCH));
- assertNull(t.findIndexForColumns(Arrays.asList("data1", "key3"),
+ assertNull(t.findIndexForColumns(Arrays.asList("data1", "key3"),
TableImpl.IndexFeature.ANY_MATCH));
- assertNull(t.findIndexForColumns(Arrays.asList("data1"),
+ assertNull(t.findIndexForColumns(Arrays.asList("data1"),
TableImpl.IndexFeature.EXACT_MATCH));
.addColumns("data1", "num2")
.addToTable(t);
- idx = t.findIndexForColumns(Arrays.asList("data1"),
+ idx = t.findIndexForColumns(Arrays.asList("data1"),
TableImpl.IndexFeature.ANY_MATCH);
assertEquals("idx2", idx.getName());
- idx = t.findIndexForColumns(Arrays.asList("data1", "num2"),
+ idx = t.findIndexForColumns(Arrays.asList("data1", "num2"),
TableImpl.IndexFeature.ANY_MATCH);
assertEquals("idx2", idx.getName());
- idx = t.findIndexForColumns(Arrays.asList("data1", "num2", "key3"),
+ idx = t.findIndexForColumns(Arrays.asList("data1", "num2", "key3"),
TableImpl.IndexFeature.ANY_MATCH);
assertEquals("idx3", idx.getName());
- assertNull(t.findIndexForColumns(Arrays.asList("num2"),
+ assertNull(t.findIndexForColumns(Arrays.asList("num2"),
TableImpl.IndexFeature.ANY_MATCH));
- assertNull(t.findIndexForColumns(Arrays.asList("data1", "key3"),
+ assertNull(t.findIndexForColumns(Arrays.asList("data1", "key3"),
TableImpl.IndexFeature.ANY_MATCH));
- assertNull(t.findIndexForColumns(Arrays.asList("data1"),
+ assertNull(t.findIndexForColumns(Arrays.asList("data1"),
TableImpl.IndexFeature.EXACT_MATCH));
.addColumns("data1")
.addToTable(t);
- idx = t.findIndexForColumns(Arrays.asList("data1"),
+ idx = t.findIndexForColumns(Arrays.asList("data1"),
TableImpl.IndexFeature.ANY_MATCH);
assertEquals("idx1", idx.getName());
- idx = t.findIndexForColumns(Arrays.asList("data1", "num2"),
+ idx = t.findIndexForColumns(Arrays.asList("data1", "num2"),
TableImpl.IndexFeature.ANY_MATCH);
assertEquals("idx2", idx.getName());
- idx = t.findIndexForColumns(Arrays.asList("data1", "num2", "key3"),
+ idx = t.findIndexForColumns(Arrays.asList("data1", "num2", "key3"),
TableImpl.IndexFeature.ANY_MATCH);
assertEquals("idx3", idx.getName());
- assertNull(t.findIndexForColumns(Arrays.asList("num2"),
+ assertNull(t.findIndexForColumns(Arrays.asList("num2"),
TableImpl.IndexFeature.ANY_MATCH));
- assertNull(t.findIndexForColumns(Arrays.asList("data1", "key3"),
+ assertNull(t.findIndexForColumns(Arrays.asList("data1", "key3"),
TableImpl.IndexFeature.ANY_MATCH));
db.close();
public void testPartialIndexLookup() throws Exception
{
for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
-
+
Database db = createMem(fileFormat);
TableImpl t = (TableImpl)new TableBuilder("Test")
doFindFirstByEntry(c, 27, "C", 1, "K1");
doFindFirstByEntry(c, null, "C", 4, "K3");
}
-
+
try {
if(colCount > 2) {
c.findFirstRowByEntry("C", 4, "K1", 14);
doFindByEntry(c, new int[]{}, "B", 3, "K3");
}
- doFindByRow(idx, 13,
+ doFindByRow(idx, 13,
"data1", "B", "value", "value13");
- doFindByRow(idx, 13,
+ doFindByRow(idx, 13,
"data1", "B", "key3", "K1", "value", "value13");
- doFindByRow(idx, 13,
+ doFindByRow(idx, 13,
"data1", "B", "num2", 3, "key3", "K1", "value", "value13");
- doFindByRow(idx, 13,
+ doFindByRow(idx, 13,
"num2", 3, "value", "value13");
- doFindByRow(idx, 13,
+ doFindByRow(idx, 13,
"value", "value13");
- doFindByRow(idx, null,
+ doFindByRow(idx, null,
"data1", "B", "num2", 5, "key3", "K1", "value", "value13");
- doFindByRow(idx, null,
+ doFindByRow(idx, null,
"data1", "B", "value", "value4");
Column col = idx.getTable().getColumn("data1");
doFindValue(idx, 21, col, "C");
- doFindValue(idx, null, col, "Z");
+ doFindValue(idx, null, col, "Z");
col = idx.getTable().getColumn("value");
doFindValue(idx, 21, col, "value21");
- doFindValue(idx, null, col, "valueZ");
+ doFindValue(idx, null, col, "valueZ");
}
private static void doFindFirstByEntry(IndexCursor c, Integer expectedId,
assertFalse(c.findFirstRowByEntry(entry));
}
}
-
+
private static void doFindByEntryRange(IndexCursor c, int start, int end,
Object... entry)
{
}
doFindByEntry(c, expectedIds, entry);
}
-
+
private static void doFindByEntry(IndexCursor c, int[] ids,
Object... entry)
{
}
doFindByEntry(c, expectedIds, entry);
}
-
+
private static void doFindByEntry(IndexCursor c, List<Integer> expectedIds,
Object... entry)
{
} else {
assertNull(r);
}
- }
+ }
- private static void doFindValue(Index idx, Integer id,
+ private static void doFindValue(Index idx, Integer id,
Column columnPattern, Object valuePattern)
throws Exception
{
} else {
assertNull(value);
}
- }
+ }
}
-