/* ### * IP: GHIDRA * * 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 db; import static org.junit.Assert.*; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.junit.Before; import org.junit.Test; import generic.test.AbstractGenericTest; import ghidra.util.task.TaskMonitor; public class TableTest extends AbstractGenericTest { private static final int RECORD_KEY_SPACING = 10; private static final int KEY_RANGE_SIZE_PER_BUFFER = 100; private static final int BUFFER_SIZE = 256; private static final int CACHE_SIZE = 4 * 1024 * 1024; private static final Field[] FIXED_SIZE_SCHEMA_FIELDS = new Field[] { LongField.INSTANCE, IntField.INSTANCE, ShortField.INSTANCE, FixedField10.INSTANCE }; private static final Field[] VARIABLE_SIZE_SCHEMA_FIELDS = new Field[] { StringField.INSTANCE, }; private static final String[] FIXED_SIZE_SCHEMA_COLUMN_NAMES = { "Long1", "Int2", "Short3", "Fixed4" }; private static final String[] VARIABLE_SIZE_SCHEMA_COLUMN_NAMES = { "String" }; private static final Schema FIXED_SIZE_SCHEMA = new Schema(0, "LongKey", FIXED_SIZE_SCHEMA_FIELDS, FIXED_SIZE_SCHEMA_COLUMN_NAMES); private static final Schema VARIABLE_SIZE_SCHEMA = new Schema(0, "LongKey", VARIABLE_SIZE_SCHEMA_FIELDS, VARIABLE_SIZE_SCHEMA_COLUMN_NAMES); private static final int BUFFER_COUNT = 5; private static final int FIRST_KEY = 0; private static final int END_KEY = BUFFER_COUNT * 100 - 10; private DBHandle dbh; private long txId; private Table table; private List startKeys = new ArrayList(); private List endKeys = new ArrayList(); public TableTest() { super(); } @Before public void setUp() throws Exception { startKeys.add(getFirstRecordKeyInBuffer(0)); startKeys.add(getInvalidKeyAfterBufferBeginning(0)); startKeys.add(getInvalidRecordKeyBeforeBuffer(1)); startKeys.add(getFirstRecordKeyInBuffer(1)); startKeys.add(getInvalidKeyAfterBufferBeginning(1)); startKeys.add(getMiddleRecordKeyInBuffer(1)); startKeys.add(getInvalidKeyBeforeBufferEnding(1)); startKeys.add(getLastRecordKeyInBuffer(1)); endKeys.add(getInvalidRecordKeyBeforeBuffer(1)); endKeys.add(getFirstRecordKeyInBuffer(1)); endKeys.add(getInvalidKeyAfterBufferBeginning(1)); endKeys.add(getMiddleRecordKeyInBuffer(1)); endKeys.add(getInvalidKeyBeforeBufferEnding(1)); endKeys.add(getLastRecordKeyInBuffer(1)); endKeys.add(getInvalidKeyAfterBufferEnding(1)); endKeys.add(getInvalidRecordKeyBeforeBuffer(2)); endKeys.add(getFirstRecordKeyInBuffer(2)); endKeys.add(getInvalidKeyAfterBufferBeginning(2)); endKeys.add(getMiddleRecordKeyInBuffer(2)); endKeys.add(getInvalidKeyBeforeBufferEnding(2)); endKeys.add(getLastRecordKeyInBuffer(2)); endKeys.add(getInvalidKeyAfterBufferEnding(2)); endKeys.add(getFirstRecordKeyInBuffer(0)); endKeys.add(getInvalidKeyAfterBufferBeginning(0)); endKeys.add(getMiddleRecordKeyInBuffer(0)); endKeys.add(getLastRecordKeyInBuffer(0)); endKeys.add(getInvalidKeyAfterBufferEnding(0)); endKeys.add(getInvalidRecordKeyBeforeBuffer(3)); endKeys.add(getFirstRecordKeyInBuffer(3)); endKeys.add(getInvalidKeyAfterBufferBeginning(3)); endKeys.add(getMiddleRecordKeyInBuffer(3)); endKeys.add(getInvalidKeyBeforeBufferEnding(3)); endKeys.add(getLastRecordKeyInBuffer(3)); endKeys.add(getInvalidKeyAfterBufferEnding(3)); endKeys.add(getInvalidKeyBeforeBufferEnding(BUFFER_COUNT - 1)); endKeys.add(getLastRecordKeyInBuffer(BUFFER_COUNT - 1)); endKeys.add(getInvalidKeyAfterBufferEnding(BUFFER_COUNT - 1)); } @Test public void testFixedSizeDeleteRecords() throws Exception { for (int startKey : startKeys) { for (int endKey : endKeys) { if (startKey <= endKey) { initializeDatabase(BUFFER_COUNT, true); deleteRangeAndVerify(startKey, endKey); closeDatabase(); } } } } @Test public void testVariableSizeDeleteRecords() throws Exception { for (int startKey : startKeys) { for (int endKey : endKeys) { if (startKey <= endKey) { initializeDatabase(BUFFER_COUNT, false); deleteRangeAndVerify(startKey, endKey); closeDatabase(); } } } } // public void testAllCombinationsWithInteriorNode() throws Exception { // int bufferCount = 22; // int maxKey = bufferCount*10; // for(int i=0;i keyList = new ArrayList(); // for(int i=0;i<10000;i++) { // Record record = generateRandomStringRecord(schema, keyList); // table.putRecord(record); // } // for(int i=0;i<1000;i++) { // long startKey = (long)(Math.random() * 1000000000F); // int numRecordsToDelete = (int)(Math.random() * 30); // RecordIterator iterator = table.iterator(startKey); // for(int j =0;j FIRST_KEY) { assertNotNull(table.getRecord(validKeyBeforeKey)); } if (endKey < END_KEY) { assertNotNull("startKey = " + startKey + ", endkey = " + endKey, table.getRecord(validKeyAfterKey)); } } private int getKeyAfterKey(int fromKey) { return ((fromKey / RECORD_KEY_SPACING) + 1) * RECORD_KEY_SPACING; } private int getKeyBeforeKey(int fromKey) { return ((fromKey - 1) / RECORD_KEY_SPACING) * RECORD_KEY_SPACING; } }