123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596 |
- /*
- Copyright (c) 2007 Health Market Science, Inc.
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
-
- package com.healthmarketscience.jackcess;
-
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.NoSuchElementException;
- import java.util.TreeSet;
- import java.util.stream.Collectors;
-
- import static com.healthmarketscience.jackcess.Database.*;
- import com.healthmarketscience.jackcess.impl.ColumnImpl;
- import com.healthmarketscience.jackcess.impl.JetFormatTest;
- import static com.healthmarketscience.jackcess.impl.JetFormatTest.*;
- import com.healthmarketscience.jackcess.impl.RowIdImpl;
- import com.healthmarketscience.jackcess.impl.TableImpl;
- import com.healthmarketscience.jackcess.util.CaseInsensitiveColumnMatcher;
- import com.healthmarketscience.jackcess.util.ColumnMatcher;
- import com.healthmarketscience.jackcess.util.RowFilterTest;
- import com.healthmarketscience.jackcess.util.SimpleColumnMatcher;
- import static com.healthmarketscience.jackcess.TestUtil.*;
- import static com.healthmarketscience.jackcess.DatabaseBuilder.*;
- import org.junit.jupiter.api.AfterEach;
- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.Test;
-
- /**
- * @author James Ahlborn
- */
- public class CursorTest
- {
-
- static final List<TestDB> INDEX_CURSOR_DBS =
- TestDB.getSupportedForBasename(Basename.INDEX_CURSOR);
-
-
- @BeforeEach
- protected void setUp() {
- TestUtil.setTestAutoSync(false);
- }
-
- @AfterEach
- protected void tearDown() {
- TestUtil.clearTestAutoSync();
- }
-
- private static List<Map<String,Object>> createTestTableData()
- throws Exception
- {
- List<Map<String,Object>> expectedRows =
- new ArrayList<Map<String,Object>>();
- for(int i = 0; i < 10; ++i) {
- expectedRows.add(createExpectedRow("id", i, "value", "data" + i));
- }
- return expectedRows;
- }
-
- private static List<Map<String,Object>> createTestTableData(
- int startIdx,
- int endIdx)
- throws Exception
- {
- List<Map<String,Object>> expectedRows = createTestTableData();
- expectedRows.subList(endIdx, expectedRows.size()).clear();
- expectedRows.subList(0, startIdx).clear();
- return expectedRows;
- }
-
- private static Database createTestTable(final FileFormat fileFormat)
- throws Exception
- {
- Database db = createMem(fileFormat);
-
- Table table = newTable("test")
- .addColumn(newColumn("id", DataType.LONG))
- .addColumn(newColumn("value", DataType.TEXT))
- .toTable(db);
-
- for(Map<String,Object> row : createTestTableData()) {
- table.addRow(row.get("id"), row.get("value"));
- }
-
- return db;
- }
-
- private static List<Map<String,Object>> createUnorderedTestTableData()
- throws Exception
- {
- List<Map<String,Object>> expectedRows =
- new ArrayList<Map<String,Object>>();
- int[] ids = new int[]{3, 7, 6, 1, 2, 9, 0, 5, 4, 8};
- for(int i : ids) {
- expectedRows.add(createExpectedRow("id", i, "value", "data" + i));
- }
- return expectedRows;
- }
-
- static Database createTestIndexTable(final TestDB indexCursorDB)
- throws Exception
- {
- Database db = openMem(indexCursorDB);
-
- Table table = db.getTable("test");
-
- for(Map<String,Object> row : createUnorderedTestTableData()) {
- table.addRow(row.get("id"), row.get("value"));
- }
-
- return db;
- }
-
- private static List<Map<String,Object>> createDupeTestTableData()
- throws Exception
- {
- List<Map<String,Object>> expectedRows =
- new ArrayList<Map<String,Object>>();
- int[] ids = new int[]{3, 7, 6, 1, 2, 9, 0, 5, 4, 8};
- for(int i : ids) {
- expectedRows.add(createExpectedRow("id", i, "value", "data" + (i % 3)));
- }
- for(int i : ids) {
- expectedRows.add(createExpectedRow("id", i, "value", "data" + (i % 5)));
- }
- return expectedRows;
- }
-
- private static Database createDupeTestTable(final FileFormat fileFormat)
- throws Exception
- {
- Database db = createMem(fileFormat);
-
- Table table = newTable("test")
- .addColumn(newColumn("id", DataType.LONG))
- .addColumn(newColumn("value", DataType.TEXT))
- .toTable(db);
-
- for(Map<String,Object> row : createDupeTestTableData()) {
- table.addRow(row.get("id"), row.get("value"));
- }
-
- return db;
- }
-
- static Database createDupeTestTable(final TestDB indexCursorDB)
- throws Exception
- {
- Database db = openMem(indexCursorDB);
-
- Table table = db.getTable("test");
-
- for(Map<String,Object> row : createDupeTestTableData()) {
- table.addRow(row.get("id"), row.get("value"));
- }
-
- return db;
- }
-
- private static Cursor createIndexSubRangeCursor(Table table,
- Index idx,
- int type)
- throws Exception
- {
- return table.newCursor()
- .setIndex(idx)
- .setStartEntry(3 - type)
- .setStartRowInclusive(type == 0)
- .setEndEntry(8 + type)
- .setEndRowInclusive(type == 0)
- .toCursor();
- }
-
- @Test
- public void testRowId() throws Exception {
- // test special cases
- RowIdImpl rowId1 = new RowIdImpl(1, 2);
- RowIdImpl rowId2 = new RowIdImpl(1, 3);
- RowIdImpl rowId3 = new RowIdImpl(2, 1);
-
- List<RowIdImpl> sortedRowIds =
- new ArrayList<RowIdImpl>(new TreeSet<RowIdImpl>(
- Arrays.asList(rowId1, rowId2, rowId3, RowIdImpl.FIRST_ROW_ID,
- RowIdImpl.LAST_ROW_ID)));
-
- assertEquals(Arrays.asList(RowIdImpl.FIRST_ROW_ID, rowId1, rowId2, rowId3,
- RowIdImpl.LAST_ROW_ID),
- sortedRowIds);
- }
-
- @Test
- public void testSimple() throws Exception {
- for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
- Database db = createTestTable(fileFormat);
-
- Table table = db.getTable("test");
- Cursor cursor = CursorBuilder.createCursor(table);
- doTestSimple(cursor, null);
- db.close();
- }
- }
-
- private static void doTestSimple(Cursor cursor,
- List<Map<String, Object>> expectedRows)
- throws Exception
- {
- if(expectedRows == null) {
- expectedRows = createTestTableData();
- }
-
- List<Map<String, Object>> foundRows =
- new ArrayList<Map<String, Object>>();
- for(Map<String, Object> row : cursor) {
- foundRows.add(row);
- }
- assertEquals(expectedRows, foundRows);
- }
-
- @Test
- public void testMove() throws Exception {
- for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
- Database db = createTestTable(fileFormat);
-
- Table table = db.getTable("test");
- Cursor cursor = CursorBuilder.createCursor(table);
- doTestMove(cursor, null);
-
- db.close();
- }
- }
-
- private static void doTestMove(Cursor cursor,
- List<Map<String, Object>> expectedRows)
- throws Exception
- {
- if(expectedRows == null) {
- expectedRows = createTestTableData();
- }
- expectedRows.subList(1, 4).clear();
-
- List<Map<String, Object>> foundRows =
- new ArrayList<Map<String, Object>>();
- assertTrue(cursor.isBeforeFirst());
- assertFalse(cursor.isAfterLast());
- foundRows.add(cursor.getNextRow());
- assertEquals(3, cursor.moveNextRows(3));
- assertFalse(cursor.isBeforeFirst());
- assertFalse(cursor.isAfterLast());
-
- Map<String,Object> expectedRow = cursor.getCurrentRow();
- Cursor.Savepoint savepoint = cursor.getSavepoint();
- assertEquals(2, cursor.movePreviousRows(2));
- assertEquals(2, cursor.moveNextRows(2));
- assertTrue(cursor.moveToNextRow());
- assertTrue(cursor.moveToPreviousRow());
- assertEquals(expectedRow, cursor.getCurrentRow());
-
- while(cursor.moveToNextRow()) {
- foundRows.add(cursor.getCurrentRow());
- }
- assertEquals(expectedRows, foundRows);
- assertFalse(cursor.isBeforeFirst());
- assertTrue(cursor.isAfterLast());
-
- assertEquals(0, cursor.moveNextRows(3));
-
- cursor.beforeFirst();
- assertTrue(cursor.isBeforeFirst());
- assertFalse(cursor.isAfterLast());
-
- cursor.afterLast();
- assertFalse(cursor.isBeforeFirst());
- assertTrue(cursor.isAfterLast());
-
- cursor.restoreSavepoint(savepoint);
- assertEquals(expectedRow, cursor.getCurrentRow());
- }
-
- @Test
- public void testMoveNoReset() throws Exception {
- for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
- Database db = createTestTable(fileFormat);
-
- Table table = db.getTable("test");
- Cursor cursor = CursorBuilder.createCursor(table);
- doTestMoveNoReset(cursor);
-
- db.close();
- }
- }
-
- private static void doTestMoveNoReset(Cursor cursor)
- 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();
- assertEquals(expectedRows.get(5), row);
- iter.next();
-
- iter = cursor.newIterable().reset(false).iterator();
- for(int i = 6; i < 10; ++i) {
- foundRows.add(iter.next());
- }
-
- assertEquals(expectedRows, foundRows);
- }
-
- @Test
- public void testSearch() throws Exception {
- for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
- Database db = createTestTable(fileFormat);
-
- Table table = db.getTable("test");
- Cursor cursor = CursorBuilder.createCursor(table);
- doTestSearch(table, cursor, null, 42, -13);
-
- db.close();
- }
- }
-
- private static void doTestSearch(Table table, Cursor cursor, Index index,
- Integer... outOfRangeValues)
- throws Exception
- {
- assertTrue(cursor.findFirstRow(table.getColumn("id"), 3));
- assertEquals(createExpectedRow("id", 3,
- "value", "data" + 3),
- cursor.getCurrentRow());
-
- assertTrue(cursor.findFirstRow(createExpectedRow(
- "id", 6,
- "value", "data" + 6)));
- assertEquals(createExpectedRow("id", 6,
- "value", "data" + 6),
- cursor.getCurrentRow());
-
- assertFalse(cursor.findFirstRow(createExpectedRow(
- "id", 8,
- "value", "data" + 13)));
- assertFalse(cursor.findFirstRow(table.getColumn("id"), 13));
- assertEquals(createExpectedRow("id", 6,
- "value", "data" + 6),
- cursor.getCurrentRow());
-
- assertTrue(cursor.findFirstRow(createExpectedRow(
- "value", "data" + 7)));
- assertEquals(createExpectedRow("id", 7,
- "value", "data" + 7),
- cursor.getCurrentRow());
-
- assertTrue(cursor.findFirstRow(table.getColumn("value"), "data" + 4));
- assertEquals(createExpectedRow("id", 4,
- "value", "data" + 4),
- cursor.getCurrentRow());
-
- for(Integer outOfRangeValue : outOfRangeValues) {
- assertFalse(cursor.findFirstRow(table.getColumn("id"),
- outOfRangeValue));
- assertFalse(cursor.findFirstRow(table.getColumn("value"),
- "data" + outOfRangeValue));
- assertFalse(cursor.findFirstRow(createExpectedRow(
- "id", outOfRangeValue,
- "value", "data" + outOfRangeValue)));
- }
-
- assertEquals("data" + 5,
- CursorBuilder.findValue(table,
- table.getColumn("value"),
- table.getColumn("id"), 5));
- assertEquals(createExpectedRow("id", 5,
- "value", "data" + 5),
- CursorBuilder.findRow(table,
- createExpectedRow("id", 5)));
- if(index != null) {
- assertEquals("data" + 5,
- CursorBuilder.findValue(index,
- table.getColumn("value"),
- table.getColumn("id"), 5));
- assertEquals(createExpectedRow("id", 5,
- "value", "data" + 5),
- CursorBuilder.findRow(index,
- createExpectedRow("id", 5)));
-
- assertNull(CursorBuilder.findValue(index,
- table.getColumn("value"),
- table.getColumn("id"),
- -17));
- assertNull(CursorBuilder.findRow(index,
- createExpectedRow("id", 13)));
- }
- }
-
- @Test
- public void testReverse() throws Exception {
- for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
- Database db = createTestTable(fileFormat);
-
- Table table = db.getTable("test");
- Cursor cursor = CursorBuilder.createCursor(table);
- doTestReverse(cursor, null);
-
- db.close();
- }
- }
-
- private static void doTestReverse(Cursor cursor,
- List<Map<String, Object>> expectedRows)
- throws Exception
- {
- if(expectedRows == null) {
- expectedRows = createTestTableData();
- }
- Collections.reverse(expectedRows);
-
- List<Map<String, Object>> foundRows =
- new ArrayList<Map<String, Object>>();
- for(Map<String, Object> row : cursor.newIterable().reverse()) {
- foundRows.add(row);
- }
- assertEquals(expectedRows, foundRows);
- }
-
- @Test
- public void testLiveAddition() throws Exception {
- for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
- Database db = createTestTable(fileFormat);
-
- Table table = db.getTable("test");
-
- Cursor cursor1 = CursorBuilder.createCursor(table);
- Cursor cursor2 = CursorBuilder.createCursor(table);
- doTestLiveAddition(table, cursor1, cursor2, 11);
-
- db.close();
- }
- }
-
- private static void doTestLiveAddition(Table table,
- Cursor cursor1,
- Cursor cursor2,
- Integer newRowNum) throws Exception
- {
- cursor1.moveNextRows(11);
- cursor2.moveNextRows(11);
-
- assertTrue(cursor1.isAfterLast());
- assertTrue(cursor2.isAfterLast());
-
- table.addRow(newRowNum, "data" + newRowNum);
- Map<String,Object> expectedRow =
- createExpectedRow("id", newRowNum, "value", "data" + newRowNum);
-
- assertFalse(cursor1.isAfterLast());
- assertFalse(cursor2.isAfterLast());
-
- assertEquals(expectedRow, cursor1.getCurrentRow());
- assertEquals(expectedRow, cursor2.getCurrentRow());
- assertFalse(cursor1.moveToNextRow());
- assertFalse(cursor2.moveToNextRow());
- assertTrue(cursor1.isAfterLast());
- assertTrue(cursor2.isAfterLast());
- }
-
-
- @Test
- public void testLiveDeletion() throws Exception {
- for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
- Database db = createTestTable(fileFormat);
-
- Table table = db.getTable("test");
-
- Cursor cursor1 = CursorBuilder.createCursor(table);
- Cursor cursor2 = CursorBuilder.createCursor(table);
- Cursor cursor3 = CursorBuilder.createCursor(table);
- Cursor cursor4 = CursorBuilder.createCursor(table);
- doTestLiveDeletion(cursor1, cursor2, cursor3, cursor4, 1);
-
- db.close();
- }
- }
-
- private static void doTestLiveDeletion(
- Cursor cursor1,
- Cursor cursor2,
- Cursor cursor3,
- Cursor cursor4,
- int firstValue) throws Exception
- {
- assertEquals(2, cursor1.moveNextRows(2));
- assertEquals(3, cursor2.moveNextRows(3));
- assertEquals(3, cursor3.moveNextRows(3));
- assertEquals(4, cursor4.moveNextRows(4));
-
- Map<String,Object> expectedPrevRow =
- createExpectedRow("id", firstValue, "value", "data" + firstValue);
- ++firstValue;
- Map<String,Object> expectedDeletedRow =
- createExpectedRow("id", firstValue, "value", "data" + firstValue);
- ++firstValue;
- Map<String,Object> expectedNextRow =
- createExpectedRow("id", firstValue, "value", "data" + firstValue);
-
- assertEquals(expectedDeletedRow, cursor2.getCurrentRow());
- assertEquals(expectedDeletedRow, cursor3.getCurrentRow());
-
- assertFalse(cursor2.isCurrentRowDeleted());
- assertFalse(cursor3.isCurrentRowDeleted());
-
- cursor2.deleteCurrentRow();
-
- assertTrue(cursor2.isCurrentRowDeleted());
- assertTrue(cursor3.isCurrentRowDeleted());
-
- assertEquals(expectedNextRow, cursor1.getNextRow());
- assertEquals(expectedNextRow, cursor2.getNextRow());
- assertEquals(expectedNextRow, cursor3.getNextRow());
-
- assertEquals(expectedPrevRow, cursor3.getPreviousRow());
-
- assertTrue(cursor3.moveToNextRow());
- cursor3.deleteCurrentRow();
- assertTrue(cursor3.isCurrentRowDeleted());
-
- firstValue += 2;
- expectedNextRow =
- createExpectedRow("id", firstValue, "value", "data" + firstValue);
- assertTrue(cursor3.moveToNextRow());
- assertEquals(expectedNextRow, cursor3.getNextRow());
-
- cursor1.beforeFirst();
- assertTrue(cursor1.moveToNextRow());
- cursor1.deleteCurrentRow();
- assertFalse(cursor1.isBeforeFirst());
- assertFalse(cursor1.isAfterLast());
- assertFalse(cursor1.moveToPreviousRow());
- assertTrue(cursor1.isBeforeFirst());
- assertFalse(cursor1.isAfterLast());
-
- cursor1.afterLast();
- assertTrue(cursor1.moveToPreviousRow());
- cursor1.deleteCurrentRow();
- assertFalse(cursor1.isBeforeFirst());
- assertFalse(cursor1.isAfterLast());
- assertFalse(cursor1.moveToNextRow());
- assertFalse(cursor1.isBeforeFirst());
- assertTrue(cursor1.isAfterLast());
-
- cursor1.beforeFirst();
- while(cursor1.moveToNextRow()) {
- cursor1.deleteCurrentRow();
- }
-
- assertTrue(cursor1.isAfterLast());
- assertTrue(cursor2.isCurrentRowDeleted());
- assertTrue(cursor3.isCurrentRowDeleted());
- assertTrue(cursor4.isCurrentRowDeleted());
- }
-
- @Test
- public void testSimpleIndex() throws Exception {
- for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) {
- Database db = createTestIndexTable(indexCursorDB);
-
- Table table = db.getTable("test");
- Index idx = table.getIndexes().get(0);
-
- assertTable(createUnorderedTestTableData(), table);
-
- Cursor cursor = CursorBuilder.createCursor(idx);
- doTestSimple(cursor, null);
-
- db.close();
- }
- }
-
- @Test
- public void testMoveIndex() throws Exception {
- for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) {
- Database db = createTestIndexTable(indexCursorDB);
-
- Table table = db.getTable("test");
- Index idx = table.getIndexes().get(0);
- Cursor cursor = CursorBuilder.createCursor(idx);
- doTestMove(cursor, null);
-
- db.close();
- }
- }
-
- @Test
- public void testReverseIndex() throws Exception {
- for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) {
- Database db = createTestIndexTable(indexCursorDB);
-
- Table table = db.getTable("test");
- Index idx = table.getIndexes().get(0);
- Cursor cursor = CursorBuilder.createCursor(idx);
- doTestReverse(cursor, null);
-
- db.close();
- }
- }
-
- @Test
- public void testSearchIndex() throws Exception {
- for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) {
- Database db = createTestIndexTable(indexCursorDB);
-
- Table table = db.getTable("test");
- Index idx = table.getIndexes().get(0);
- Cursor cursor = CursorBuilder.createCursor(idx);
- doTestSearch(table, cursor, idx, 42, -13);
-
- db.close();
- }
- }
-
- @Test
- public void testLiveAdditionIndex() throws Exception {
- for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) {
- Database db = createTestIndexTable(indexCursorDB);
-
- Table table = db.getTable("test");
- Index idx = table.getIndexes().get(0);
-
- Cursor cursor1 = CursorBuilder.createCursor(idx);
- Cursor cursor2 = CursorBuilder.createCursor(idx);
- doTestLiveAddition(table, cursor1, cursor2, 11);
-
- db.close();
- }
- }
-
- @Test
- public void testLiveDeletionIndex() throws Exception {
- for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) {
- Database db = createTestIndexTable(indexCursorDB);
-
- Table table = db.getTable("test");
- Index idx = table.getIndexes().get(0);
-
- Cursor cursor1 = CursorBuilder.createCursor(idx);
- Cursor cursor2 = CursorBuilder.createCursor(idx);
- Cursor cursor3 = CursorBuilder.createCursor(idx);
- Cursor cursor4 = CursorBuilder.createCursor(idx);
- doTestLiveDeletion(cursor1, cursor2, cursor3, cursor4, 1);
-
- db.close();
- }
- }
-
- @Test
- public void testSimpleIndexSubRange() throws Exception {
- for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) {
- for(int i = 0; i < 2; ++i) {
- Database db = createTestIndexTable(indexCursorDB);
-
- Table table = db.getTable("test");
- Index idx = table.getIndexes().get(0);
-
- Cursor cursor = createIndexSubRangeCursor(table, idx, i);
-
- List<Map<String,Object>> expectedRows =
- createTestTableData(3, 9);
-
- doTestSimple(cursor, expectedRows);
-
- db.close();
- }
- }
- }
-
- @Test
- public void testMoveIndexSubRange() throws Exception {
- for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) {
- for(int i = 0; i < 2; ++i) {
- Database db = createTestIndexTable(indexCursorDB);
-
- Table table = db.getTable("test");
- Index idx = table.getIndexes().get(0);
-
- Cursor cursor = createIndexSubRangeCursor(table, idx, i);
-
- List<Map<String,Object>> expectedRows =
- createTestTableData(3, 9);
-
- doTestMove(cursor, expectedRows);
-
- db.close();
- }
- }
- }
-
- @Test
- public void testSearchIndexSubRange() throws Exception {
- for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) {
- for(int i = 0; i < 2; ++i) {
- Database db = createTestIndexTable(indexCursorDB);
-
- Table table = db.getTable("test");
- Index idx = table.getIndexes().get(0);
-
- Cursor cursor = createIndexSubRangeCursor(table, idx, i);
-
- doTestSearch(table, cursor, idx, 2, 9);
-
- db.close();
- }
- }
- }
-
- @Test
- public void testReverseIndexSubRange() throws Exception {
- for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) {
- for(int i = 0; i < 2; ++i) {
- Database db = createTestIndexTable(indexCursorDB);
-
- Table table = db.getTable("test");
- Index idx = table.getIndexes().get(0);
-
- Cursor cursor = createIndexSubRangeCursor(table, idx, i);
-
- List<Map<String,Object>> expectedRows =
- createTestTableData(3, 9);
-
- doTestReverse(cursor, expectedRows);
-
- db.close();
- }
- }
- }
-
- @Test
- public void testLiveAdditionIndexSubRange() throws Exception {
- for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) {
- for(int i = 0; i < 2; ++i) {
- Database db = createTestIndexTable(indexCursorDB);
-
- Table table = db.getTable("test");
- Index idx = table.getIndexes().get(0);
-
- Cursor cursor1 = createIndexSubRangeCursor(table, idx, i);
- Cursor cursor2 = createIndexSubRangeCursor(table, idx, i);
-
- doTestLiveAddition(table, cursor1, cursor2, 8);
-
- db.close();
- }
- }
- }
-
- @Test
- public void testLiveDeletionIndexSubRange() throws Exception {
- for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) {
- for(int i = 0; i < 2; ++i) {
- Database db = createTestIndexTable(indexCursorDB);
-
- Table table = db.getTable("test");
- Index idx = table.getIndexes().get(0);
-
- Cursor cursor1 = createIndexSubRangeCursor(table, idx, i);
- Cursor cursor2 = createIndexSubRangeCursor(table, idx, i);
- Cursor cursor3 = createIndexSubRangeCursor(table, idx, i);
- Cursor cursor4 = createIndexSubRangeCursor(table, idx, i);
-
- doTestLiveDeletion(cursor1, cursor2, cursor3, cursor4, 4);
-
- db.close();
- }
- }
- }
-
- @Test
- public void testFindAllIndex() throws Exception {
- for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
- Database db = createDupeTestTable(fileFormat);
-
- Table table = db.getTable("test");
- Cursor cursor = CursorBuilder.createCursor(table);
-
- doTestFindAll(table, cursor, null);
-
- db.close();
- }
- }
-
- @Test
- public void testFindAll() throws Exception {
- for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) {
- Database db = createDupeTestTable(indexCursorDB);
-
- Table table = db.getTable("test");
- Index idx = table.getIndexes().get(0);
- Cursor cursor = CursorBuilder.createCursor(idx);
-
- doTestFindAll(table, cursor, idx);
-
- db.close();
- }
- }
-
- private static void doTestFindAll(Table table, Cursor cursor, Index index)
- throws Exception
- {
- List<? extends Map<String,Object>> rows = RowFilterTest.toList(
- cursor.newIterable().setMatchPattern("value", "data2"));
-
- List<? extends Map<String, Object>> expectedRows = null;
-
- if(index == null) {
- expectedRows =
- createExpectedTable(
- createExpectedRow(
- "id", 2, "value", "data2"),
- createExpectedRow(
- "id", 5, "value", "data2"),
- createExpectedRow(
- "id", 8, "value", "data2"),
- createExpectedRow(
- "id", 7, "value", "data2"),
- createExpectedRow(
- "id", 2, "value", "data2"));
- } else {
- expectedRows =
- createExpectedTable(
- createExpectedRow(
- "id", 2, "value", "data2"),
- createExpectedRow(
- "id", 2, "value", "data2"),
- createExpectedRow(
- "id", 5, "value", "data2"),
- createExpectedRow(
- "id", 7, "value", "data2"),
- createExpectedRow(
- "id", 8, "value", "data2"));
- }
- assertEquals(expectedRows, rows);
-
- Column valCol = table.getColumn("value");
- rows = RowFilterTest.toList(
- cursor.newIterable().setMatchPattern(valCol, "data4"));
-
- if(index == null) {
- expectedRows =
- createExpectedTable(
- createExpectedRow(
- "id", 9, "value", "data4"),
- createExpectedRow(
- "id", 4, "value", "data4"));
- } else {
- expectedRows =
- createExpectedTable(
- createExpectedRow(
- "id", 4, "value", "data4"),
- createExpectedRow(
- "id", 9, "value", "data4"));
- }
- assertEquals(expectedRows, rows);
-
- rows = RowFilterTest.toList(
- cursor.newIterable().setMatchPattern(valCol, "data9"));
-
- assertTrue(rows.isEmpty());
-
- rows = RowFilterTest.toList(
- cursor.newIterable().setMatchPattern(
- Collections.singletonMap("id", 8)));
-
- expectedRows =
- createExpectedTable(
- createExpectedRow(
- "id", 8, "value", "data2"),
- createExpectedRow(
- "id", 8, "value", "data3"));
- 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)) {
- tmpRows.add(tmpRow);
- }
- }
- expectedRows = tmpRows;
- assertFalse(expectedRows.isEmpty());
-
- rows = RowFilterTest.toList(cursor.newIterable().setMatchPattern(row));
-
- assertEquals(expectedRows, rows);
- }
-
- rows = RowFilterTest.toList(
- cursor.newIterable().addMatchPattern("id", 8)
- .addMatchPattern("value", "data13"));
- assertTrue(rows.isEmpty());
- }
-
- @Test
- public void testId() throws Exception
- {
- for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) {
- Database db = createTestIndexTable(indexCursorDB);
-
- Table table = db.getTable("test");
- Index idx = table.getIndexes().get(0);
-
- Cursor tCursor = CursorBuilder.createCursor(table);
- Cursor iCursor = CursorBuilder.createCursor(idx);
-
- Cursor.Savepoint tSave = tCursor.getSavepoint();
- Cursor.Savepoint iSave = iCursor.getSavepoint();
-
- tCursor.restoreSavepoint(tSave);
- iCursor.restoreSavepoint(iSave);
-
- assertThrows(IllegalArgumentException.class, () -> tCursor.restoreSavepoint(iSave));
-
- assertThrows(IllegalArgumentException.class, () -> iCursor.restoreSavepoint(tSave));
-
- Cursor tCursor2 = CursorBuilder.createCursor(table);
- Cursor iCursor2 = CursorBuilder.createCursor(idx);
-
- tCursor2.restoreSavepoint(tSave);
- iCursor2.restoreSavepoint(iSave);
-
- db.close();
- }
- }
-
- @Test
- public void testColumnMatcher() throws Exception {
-
-
- for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
- Database db = createTestTable(fileFormat);
-
- Table table = db.getTable("test");
-
- doTestMatchers(table, SimpleColumnMatcher.INSTANCE, false);
- doTestMatchers(table, CaseInsensitiveColumnMatcher.INSTANCE, true);
-
- Cursor cursor = CursorBuilder.createCursor(table);
- doTestMatcher(table, cursor, SimpleColumnMatcher.INSTANCE, false);
- doTestMatcher(table, cursor, CaseInsensitiveColumnMatcher.INSTANCE,
- true);
- db.close();
- }
- }
-
- private static void doTestMatchers(Table table, ColumnMatcher columnMatcher,
- boolean caseInsensitive)
- throws Exception
- {
- assertTrue(columnMatcher.matches(table, "value", null, null));
- assertFalse(columnMatcher.matches(table, "value", "foo", null));
- assertFalse(columnMatcher.matches(table, "value", null, "foo"));
- assertTrue(columnMatcher.matches(table, "value", "foo", "foo"));
- assertTrue(columnMatcher.matches(table, "value", "foo", "Foo")
- == caseInsensitive);
-
- assertFalse(columnMatcher.matches(table, "value", 13, null));
- assertFalse(columnMatcher.matches(table, "value", null, 13));
- assertTrue(columnMatcher.matches(table, "value", 13, 13));
- }
-
- private static void doTestMatcher(Table table, Cursor cursor,
- ColumnMatcher columnMatcher,
- boolean caseInsensitive)
- throws Exception
- {
- cursor.setColumnMatcher(columnMatcher);
-
- assertTrue(cursor.findFirstRow(table.getColumn("id"), 3));
- assertEquals(createExpectedRow("id", 3,
- "value", "data" + 3),
- cursor.getCurrentRow());
-
- assertTrue(cursor.findFirstRow(createExpectedRow(
- "id", 6,
- "value", "data" + 6)));
- assertEquals(createExpectedRow("id", 6,
- "value", "data" + 6),
- cursor.getCurrentRow());
-
- assertTrue(cursor.findFirstRow(createExpectedRow(
- "id", 6,
- "value", "Data" + 6)) == caseInsensitive);
- if(caseInsensitive) {
- assertEquals(createExpectedRow("id", 6,
- "value", "data" + 6),
- cursor.getCurrentRow());
- }
-
- assertFalse(cursor.findFirstRow(createExpectedRow(
- "id", 8,
- "value", "data" + 13)));
- assertFalse(cursor.findFirstRow(table.getColumn("id"), 13));
- assertEquals(createExpectedRow("id", 6,
- "value", "data" + 6),
- cursor.getCurrentRow());
-
- assertTrue(cursor.findFirstRow(createExpectedRow(
- "value", "data" + 7)));
- assertEquals(createExpectedRow("id", 7,
- "value", "data" + 7),
- cursor.getCurrentRow());
-
- assertTrue(cursor.findFirstRow(createExpectedRow(
- "value", "Data" + 7)) == caseInsensitive);
- if(caseInsensitive) {
- assertEquals(createExpectedRow("id", 7,
- "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)
- == caseInsensitive);
- if(caseInsensitive) {
- assertEquals(createExpectedRow("id", 4,
- "value", "data" + 4),
- cursor.getCurrentRow());
- }
-
- assertEquals(Arrays.asList(createExpectedRow("id", 4,
- "value", "data" + 4)),
- RowFilterTest.toList(
- cursor.newIterable()
- .setMatchPattern("value", "data4")
- .setColumnMatcher(SimpleColumnMatcher.INSTANCE)));
-
- assertEquals(Arrays.asList(createExpectedRow("id", 3,
- "value", "data" + 3)),
- RowFilterTest.toList(
- cursor.newIterable()
- .setMatchPattern("value", "DaTa3")
- .setColumnMatcher(CaseInsensitiveColumnMatcher.INSTANCE)));
-
- assertEquals(Arrays.asList(createExpectedRow("id", 2,
- "value", "data" + 2)),
- RowFilterTest.toList(
- cursor.newIterable()
- .addMatchPattern("value", "DaTa2")
- .addMatchPattern("id", 2)
- .setColumnMatcher(CaseInsensitiveColumnMatcher.INSTANCE)));
- }
-
- @Test
- public void testIndexCursor() throws Exception
- {
- for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX, true)) {
-
- Database db = openMem(testDB);
- Table t1 = db.getTable("Table1");
- Index idx = t1.getIndex(IndexBuilder.PRIMARY_KEY_NAME);
- IndexCursor cursor = CursorBuilder.createCursor(idx);
-
- assertFalse(cursor.findFirstRowByEntry(-1));
- cursor.findClosestRowByEntry(-1);
- assertEquals(0, cursor.getCurrentRow().get("id"));
-
- assertTrue(cursor.findFirstRowByEntry(1));
- assertEquals(1, cursor.getCurrentRow().get("id"));
-
- cursor.findClosestRowByEntry(2);
- assertEquals(2, cursor.getCurrentRow().get("id"));
-
- assertFalse(cursor.findFirstRowByEntry(4));
- cursor.findClosestRowByEntry(4);
- assertTrue(cursor.isAfterLast());
-
- db.close();
- }
- }
-
- @Test
- public void testIndexCursorDelete() throws Exception
- {
- for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) {
-
- Database db = openMem(testDB);
- Table t1 = db.getTable("Table1");
- Index idx = t1.getIndex("Table2Table1");
- IndexCursor cursor = CursorBuilder.createCursor(idx);
-
- List<String> expectedData = cursor.newEntryIterable(1)
- .addColumnNames("data")
- .stream().map(r -> r.getString("data"))
- .collect(Collectors.toList());
-
- assertEquals(Arrays.asList("baz11", "baz11-2"), expectedData);
-
- expectedData = new ArrayList<String>();
- for(Iterator<? extends Row> iter =
- cursor.newEntryIterable(1).iterator();
- iter.hasNext(); ) {
- expectedData.add(iter.next().getString("data"));
- iter.remove();
- assertThrows(IllegalStateException.class, () -> iter.remove());
-
- if (!iter.hasNext()) {
- assertThrows(NoSuchElementException.class, () -> iter.next());
- }
- }
-
- assertEquals(Arrays.asList("baz11", "baz11-2"), expectedData);
-
- expectedData = new ArrayList<String>();
- for(Row row : cursor.newEntryIterable(1)
- .addColumnNames("data")) {
- expectedData.add(row.getString("data"));
- }
-
- assertTrue(expectedData.isEmpty());
-
- db.close();
- }
- }
-
- @Test
- public void testCursorDelete() throws Exception
- {
- for (final TestDB testDB : TestDB.getSupportedForBasename(Basename.INDEX)) {
-
- Database db = openMem(testDB);
- Table t1 = db.getTable("Table1");
- Cursor cursor = CursorBuilder.createCursor(t1);
-
- List<String> expectedData = cursor.newIterable().setColumnNames(
- Arrays.asList("otherfk1", "data")).stream()
- .filter(r -> r.get("otherfk1").equals(1))
- .map(r -> r.getString("data"))
- .collect(Collectors.toList());
-
- assertEquals(Arrays.asList("baz11", "baz11-2"), expectedData);
-
- expectedData = new ArrayList<String>();
- for(Iterator<? extends Row> iter = cursor.iterator();
- iter.hasNext(); ) {
- Row row = iter.next();
- if(row.get("otherfk1").equals(1)) {
- expectedData.add(row.getString("data"));
- iter.remove();
- assertThrows(IllegalStateException.class, () -> iter.remove());
- }
-
- if (!iter.hasNext()) {
- assertThrows(NoSuchElementException.class, () -> iter.next());
- }
- }
-
- assertEquals(Arrays.asList("baz11", "baz11-2"), expectedData);
-
- expectedData = new ArrayList<String>();
- for(Row row : cursor.newIterable().setColumnNames(
- Arrays.asList("otherfk1", "data"))) {
- if(row.get("otherfk1").equals(1)) {
- expectedData.add(row.getString("data"));
- }
- }
-
- assertTrue(expectedData.isEmpty());
-
- db.close();
- }
- }
-
- @Test
- public void testFindByRowId() throws Exception {
- for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
- Database db = createTestTable(fileFormat);
-
- Table table = db.getTable("test");
- Cursor cursor = CursorBuilder.createCursor(table);
- doTestFindByRowId(cursor);
- db.close();
- }
- }
-
- @Test
- public void testFindByRowIdIndex() throws Exception {
- for (final TestDB indexCursorDB : INDEX_CURSOR_DBS) {
- Database db = createTestIndexTable(indexCursorDB);
-
- Table table = db.getTable("test");
- Index idx = table.getIndexes().get(0);
-
- assertTable(createUnorderedTestTableData(), table);
-
- Cursor cursor = CursorBuilder.createCursor(idx);
- doTestFindByRowId(cursor);
-
- db.close();
- }
- }
-
- 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
- {
- cursor.reset();
- assertTrue(cursor.findRow(row.getId()));
- Row rFound = cursor.getCurrentRow();
- assertEquals(id, rFound.get("id"));
- assertEquals(row, rFound);
- Cursor.Savepoint save = cursor.getSavepoint();
-
- assertTrue(cursor.moveToNextRow());
- assertEquals(id + 1, cursor.getCurrentRow().get("id"));
-
- cursor.restoreSavepoint(save);
-
- assertTrue(cursor.moveToPreviousRow());
- assertEquals(id - 1, cursor.getCurrentRow().get("id"));
-
- assertFalse(cursor.findRow(RowIdImpl.FIRST_ROW_ID));
-
- assertEquals(id - 1, cursor.getCurrentRow().get("id"));
- }
-
- @Test
- public void testIterationEarlyExit() throws Exception {
- for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
-
- Database db = createMem(fileFormat);
-
- Table table = newTable("test")
- .addColumn(newColumn("id", DataType.LONG))
- .addColumn(newColumn("value", DataType.TEXT))
- .addColumn(newColumn("memo", DataType.MEMO))
- .addIndex(newIndex("value_idx")
- .addColumns("value"))
- .toTable(db);
-
- for(int i = 0; i < 20; ++i) {
- Object memo = "memo-" + i;
- table.addRow(i, "val-" + (i/2), memo);
- }
-
- // generate an "invalid" memo
- byte[] b = new byte[12];
- b[3] = (byte)0xC0;
- table.addRow(20, "val-9", ColumnImpl.rawDataWrapper(b));
-
- IndexCursor cursor = CursorBuilder.createCursor(
- table.getIndex("value_idx"));
-
- assertThrows(RuntimeIOException.class, () -> cursor.newIterable()
- .addMatchPattern("value", "val-9")
- .addMatchPattern("memo", "anything")
- .iterator().hasNext());
-
- List<Row> rows = new ArrayList<Row>();
- for (Row row : cursor.newIterable()
- .addMatchPattern("value", "val-5")
- .addMatchPattern("memo", "memo-11")) {
- rows.add(row);
- }
-
- assertEquals(rows, createExpectedTable(
- createExpectedRow("id", 11,
- "value", "val-5",
- "memo", "memo-11")));
-
- assertFalse(cursor.newIterable()
- .addMatchPattern("value", "val-31")
- .addMatchPattern("memo", "anything")
- .iterator().hasNext());
-
- db.close();
- }
- }
-
- @Test
- public void testPartialIndexFind() throws Exception
- {
- for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
-
- Database db = createMem(fileFormat);
-
- TableImpl t = (TableImpl)newTable("Test")
- .addColumn(newColumn("id", DataType.LONG))
- .addColumn(newColumn("data1", DataType.TEXT))
- .addColumn(newColumn("num2", DataType.LONG))
- .addColumn(newColumn("key3", DataType.TEXT))
- .addColumn(newColumn("value", DataType.TEXT))
- .addIndex(newIndex("idx3").addColumns("data1", "num2", "key3"))
- .toTable(db);
-
- Index idx = t.findIndexForColumns(Arrays.asList("data1"),
- TableImpl.IndexFeature.ANY_MATCH);
- assertEquals("idx3", idx.getName());
-
- idx = t.findIndexForColumns(Arrays.asList("data1", "num2"),
- TableImpl.IndexFeature.ANY_MATCH);
- assertEquals("idx3", idx.getName());
-
- idx = t.findIndexForColumns(Arrays.asList("data1", "num2", "key3"),
- TableImpl.IndexFeature.ANY_MATCH);
- assertEquals("idx3", idx.getName());
-
- assertNull(t.findIndexForColumns(Arrays.asList("num2"),
- TableImpl.IndexFeature.ANY_MATCH));
- assertNull(t.findIndexForColumns(Arrays.asList("data1", "key3"),
- TableImpl.IndexFeature.ANY_MATCH));
- assertNull(t.findIndexForColumns(Arrays.asList("data1"),
- TableImpl.IndexFeature.EXACT_MATCH));
-
-
- newIndex("idx2")
- .addColumns("data1", "num2")
- .addToTable(t);
-
- idx = t.findIndexForColumns(Arrays.asList("data1"),
- TableImpl.IndexFeature.ANY_MATCH);
- assertEquals("idx2", idx.getName());
-
- idx = t.findIndexForColumns(Arrays.asList("data1", "num2"),
- TableImpl.IndexFeature.ANY_MATCH);
- assertEquals("idx2", idx.getName());
-
- idx = t.findIndexForColumns(Arrays.asList("data1", "num2", "key3"),
- TableImpl.IndexFeature.ANY_MATCH);
- assertEquals("idx3", idx.getName());
-
- assertNull(t.findIndexForColumns(Arrays.asList("num2"),
- TableImpl.IndexFeature.ANY_MATCH));
- assertNull(t.findIndexForColumns(Arrays.asList("data1", "key3"),
- TableImpl.IndexFeature.ANY_MATCH));
- assertNull(t.findIndexForColumns(Arrays.asList("data1"),
- TableImpl.IndexFeature.EXACT_MATCH));
-
-
- newIndex("idx1")
- .addColumns("data1")
- .addToTable(t);
-
- idx = t.findIndexForColumns(Arrays.asList("data1"),
- TableImpl.IndexFeature.ANY_MATCH);
- assertEquals("idx1", idx.getName());
-
- idx = t.findIndexForColumns(Arrays.asList("data1", "num2"),
- TableImpl.IndexFeature.ANY_MATCH);
- assertEquals("idx2", idx.getName());
-
- idx = t.findIndexForColumns(Arrays.asList("data1", "num2", "key3"),
- TableImpl.IndexFeature.ANY_MATCH);
- assertEquals("idx3", idx.getName());
-
- assertNull(t.findIndexForColumns(Arrays.asList("num2"),
- TableImpl.IndexFeature.ANY_MATCH));
- assertNull(t.findIndexForColumns(Arrays.asList("data1", "key3"),
- TableImpl.IndexFeature.ANY_MATCH));
-
- db.close();
- }
- }
-
- @Test
- public void testPartialIndexLookup() throws Exception
- {
- for (final FileFormat fileFormat : JetFormatTest.SUPPORTED_FILEFORMATS) {
-
- Database db = createMem(fileFormat);
-
- TableImpl t = (TableImpl)newTable("Test")
- .addColumn(newColumn("id", DataType.LONG))
- .addColumn(newColumn("data1", DataType.TEXT))
- .addColumn(newColumn("num2", DataType.LONG))
- .addColumn(newColumn("key3", DataType.TEXT))
- .addColumn(newColumn("value", DataType.TEXT))
- .addIndex(newIndex("idx3")
- .addColumns(true, "data1")
- .addColumns(false, "num2")
- .addColumns(true, "key3")
- )
- .toTable(db);
-
- int id = 1;
- for(String str : Arrays.asList("A", "B", "C", "D")) {
- for(int i = 4; i >= 0; --i) {
- // for(int i = 0; i < 5; ++i) {
- for(int j = 1; j < 3; ++j) {
- t.addRow(id, str, i, "K" + j, "value" + id);
- ++id;
- }
- }
- }
-
- Index idx = t.getIndex("idx3");
- doPartialIndexLookup(idx);
-
- idx = newIndex("idx2")
- .addColumns(true, "data1")
- .addColumns(false, "num2")
- .addToTable(t);
- doPartialIndexLookup(idx);
-
- idx = newIndex("idx1")
- .addColumns(true, "data1")
- .addToTable(t);
- doPartialIndexLookup(idx);
-
- db.close();
- }
- }
-
- private static void doPartialIndexLookup(Index idx) throws Exception
- {
- int colCount = idx.getColumnCount();
- IndexCursor c = idx.newCursor().toIndexCursor();
-
- doFindFirstByEntry(c, 21, "C");
- doFindFirstByEntry(c, null, "Z");
-
- if(colCount > 1) {
- doFindFirstByEntry(c, 23, "C", 3);
- doFindFirstByEntry(c, null, "C", 20);
- }
-
- if(colCount > 2) {
- doFindFirstByEntry(c, 27, "C", 1, "K1");
- doFindFirstByEntry(c, null, "C", 4, "K3");
- }
-
- assertThrows(IllegalArgumentException.class, () ->
- {
- if (colCount > 2) {
- c.findFirstRowByEntry("C", 4, "K1", 14);
- } else if(colCount > 1) {
- c.findFirstRowByEntry("C", 4, "K1");
- } else {
- c.findFirstRowByEntry("C", 4);
- }
- });
-
- doFindByEntryRange(c, 11, 20, "B");
- doFindByEntry(c, new int[]{}, "Z");
-
- if(colCount > 1) {
- doFindByEntryRange(c, 13, 14, "B", 3);
- doFindByEntry(c, new int[]{}, "B", 20);
- }
-
- if(colCount > 2) {
- doFindByEntryRange(c, 14, 14, "B", 3, "K2");
- doFindByEntry(c, new int[]{}, "B", 3, "K3");
- }
-
- doFindByRow(idx, 13,
- "data1", "B", "value", "value13");
- doFindByRow(idx, 13,
- "data1", "B", "key3", "K1", "value", "value13");
- doFindByRow(idx, 13,
- "data1", "B", "num2", 3, "key3", "K1", "value", "value13");
- doFindByRow(idx, 13,
- "num2", 3, "value", "value13");
- doFindByRow(idx, 13,
- "value", "value13");
- doFindByRow(idx, null,
- "data1", "B", "num2", 5, "key3", "K1", "value", "value13");
- doFindByRow(idx, null,
- "data1", "B", "value", "value4");
-
- Column col = idx.getTable().getColumn("data1");
- doFindValue(idx, 21, col, "C");
- doFindValue(idx, null, col, "Z");
- col = idx.getTable().getColumn("value");
- doFindValue(idx, 21, col, "value21");
- doFindValue(idx, null, col, "valueZ");
- }
-
- private static void doFindFirstByEntry(IndexCursor c, Integer expectedId,
- Object... entry)
- throws Exception
- {
- if(expectedId != null) {
- assertTrue(c.findFirstRowByEntry(entry));
- assertEquals(expectedId, c.getCurrentRow().get("id"));
- } else {
- assertFalse(c.findFirstRowByEntry(entry));
- }
- }
-
- private static void doFindByEntryRange(IndexCursor c, int start, int end,
- Object... entry)
- {
- List<Integer> expectedIds = new ArrayList<Integer>();
- for(int i = start; i <= end; ++i) {
- expectedIds.add(i);
- }
- doFindByEntry(c, expectedIds, entry);
- }
-
- private static void doFindByEntry(IndexCursor c, int[] ids,
- Object... entry)
- {
- List<Integer> expectedIds = new ArrayList<Integer>();
- for(int id : ids) {
- expectedIds.add(id);
- }
- doFindByEntry(c, expectedIds, entry);
- }
-
- private static void doFindByEntry(IndexCursor c, List<Integer> expectedIds,
- Object... entry)
- {
- List<Integer> foundIds = new ArrayList<Integer>();
- for(Row row : c.newEntryIterable(entry)) {
- foundIds.add((Integer)row.get("id"));
- }
- assertEquals(expectedIds, foundIds);
- }
-
- private static void doFindByRow(Index idx, Integer id, Object... rowPairs)
- throws Exception
- {
- Map<String,Object> map = createExpectedRow(
- rowPairs);
- Row r = CursorBuilder.findRow(idx, map);
- if(id != null) {
- assertEquals(id, r.get("id"));
- } else {
- assertNull(r);
- }
- }
-
- private static void doFindValue(Index idx, Integer id,
- Column columnPattern, Object valuePattern)
- throws Exception
- {
- Object value = CursorBuilder.findValue(
- idx, idx.getTable().getColumn("id"), columnPattern, valuePattern);
- if(id != null) {
- assertEquals(id, value);
- } else {
- assertNull(value);
- }
- }
- }
|