}
if(isLeaf) {
- entries.add(new Entry(curEntryBuffer, curEntryLen, _columns));
+ entries.add(new Entry(curEntryBuffer, curEntryLen));
} else {
- nodeEntries.add(new NodeEntry(
- curEntryBuffer, curEntryLen, _columns));
+ nodeEntries.add(new NodeEntry(curEntryBuffer, curEntryLen));
}
// read any shared "compressed" bytes
public void addRow(Object[] row, RowId rowId)
throws IOException
{
- if(shouldIgnoreNulls() && isNullEntry(row)) {
+ int nullCount = countNullValues(row);
+ boolean isNullEntry = (nullCount == _columns.size());
+ if(shouldIgnoreNulls() && isNullEntry) {
// nothing to do
return;
}
+ if(isPrimaryKey() && (nullCount > 0)) {
+ throw new IOException("Null value found in row " + Arrays.asList(row)
+ + " for primary key index " + this);
+ }
// make sure we've parsed the entries
initialize();
- Entry newEntry = new Entry(row, rowId, this);
- if(addEntry(newEntry)) {
+ Entry newEntry = new Entry(createEntryBytes(row), rowId);
+ if(addEntry(newEntry, isNullEntry, row)) {
++_rowCount;
++_modCount;
} else {
public void deleteRow(Object[] row, RowId rowId)
throws IOException
{
- if(shouldIgnoreNulls() && isNullEntry(row)) {
+ int nullCount = countNullValues(row);
+ if(shouldIgnoreNulls() && (nullCount == _columns.size())) {
// nothing to do
return;
}
// make sure we've parsed the entries
initialize();
- Entry oldEntry = new Entry(row, rowId, this);
+ Entry oldEntry = new Entry(createEntryBytes(row), rowId);
if(removeEntry(oldEntry)) {
--_rowCount;
++_modCount;
initialize();
Position startPos = FIRST_POSITION;
if(startRow != null) {
- Entry startEntry = new Entry(startRow,
+ Entry startEntry = new Entry(createEntryBytes(startRow),
(startInclusive ?
- RowId.FIRST_ROW_ID : RowId.LAST_ROW_ID),
- this);
+ RowId.FIRST_ROW_ID : RowId.LAST_ROW_ID));
startPos = new Position(FIRST_ENTRY_IDX, startEntry);
}
Position endPos = LAST_POSITION;
if(endRow != null) {
- Entry endEntry = new Entry(endRow,
+ Entry endEntry = new Entry(createEntryBytes(endRow),
(endInclusive ?
- RowId.LAST_ROW_ID : RowId.FIRST_ROW_ID),
- this);
+ RowId.LAST_ROW_ID : RowId.FIRST_ROW_ID));
endPos = new Position(LAST_ENTRY_IDX, endEntry);
}
return new EntryCursor(startPos, endPos);
/**
* Adds an entry to the _entries list, maintaining the order.
*/
- private boolean addEntry(Entry newEntry)
+ private boolean addEntry(Entry newEntry, boolean isNullEntry, Object[] row)
throws IOException
{
int idx = findEntry(newEntry);
idx = missingIndexToInsertionPoint(idx);
// determine if the addition of this entry would break the uniqueness
- // constraint
- if(isUnique() &&
+ // constraint (note, access does not seem to consider multiple null
+ // entries invalid for a "unique" index)
+ if(isUnique() && !isNullEntry &&
(((idx > 0) &&
newEntry.equalsEntryBytes(_entries.get(idx - 1))) ||
((idx < _entries.size()) &&
newEntry.equalsEntryBytes(_entries.get(idx)))))
{
throw new IOException(
- "New entry " + newEntry +
- " violates uniqueness constrain for index " + this);
+ "New row " + Arrays.asList(row) +
+ " violates uniqueness constraint for index " + this);
}
_entries.add(idx, newEntry);
@Override
public String toString() {
StringBuilder rtn = new StringBuilder();
- rtn.append("\tName: " + _name);
+ rtn.append("\tName: (" + _table.getName() + ") " + _name);
rtn.append("\n\tNumber: " + _indexNumber);
rtn.append("\n\tPage number: " + _pageNumber);
rtn.append("\n\tIs Primary Key: " + isPrimaryKey());
}
/**
- * Determines if all values for this index from the given row are
- * {@code null}.
+ * Determines the number of {@code null} values for this index from the
+ * given row.
*/
- private boolean isNullEntry(Object[] values)
+ private int countNullValues(Object[] values)
{
if(values == null) {
- return true;
+ return _columns.size();
}
// annoyingly, the values array could come from different sources, one
// of which will make it a different size than the other. we need to
// handle both situations.
boolean useColNumber = (values.length >= _table.getMaxColumnCount());
+ int nullCount = 0;
for(ColumnDescriptor col : _columns) {
Object value = values[
useColNumber ? col.getColumnNumber() : col.getColumnIndex()];
- if(!col.isNullValue(value)) {
- return false;
+ if(col.isNullValue(value)) {
+ ++nullCount;
}
}
- return true;
+
+ return nullCount;
}
/**
*/
private static Entry createSpecialEntry(RowId rowId) {
try {
- return new Entry(null, rowId, null);
+ return new Entry((byte[])null, rowId);
} catch(IOException e) {
// should never happen
throw new IllegalStateException(e);
/**
* Create a new entry
- * @param values Indexed row values
+ * @param entryBytes encoded bytes for this index entry
* @param rowId rowId in which the row is stored
- * @param columns map of columns for this index
*/
- private Entry(Object[] values, RowId rowId, Index parent)
+ private Entry(byte[] entryBytes, RowId rowId)
throws IOException
{
_rowId = rowId;
- if(values != null) {
- _entryBytes = parent.createEntryBytes(values);
+ _entryBytes = entryBytes;
+ if(_entryBytes != null) {
_type = ((_rowId.getType() == RowId.Type.NORMAL) ?
EntryType.NORMAL :
((_rowId.getType() == RowId.Type.ALWAYS_FIRST) ?
EntryType.FIRST_VALID : EntryType.LAST_VALID));
+ } else if(!_rowId.isValid()) {
+ // this is a "special" entry (first/last)
+ _type = ((_rowId.getType() == RowId.Type.ALWAYS_FIRST) ?
+ EntryType.ALWAYS_FIRST : EntryType.ALWAYS_LAST);
} else {
- if(!_rowId.isValid()) {
- // this is a "special" entry (first/last)
- _entryBytes = null;
- _type = ((_rowId.getType() == RowId.Type.ALWAYS_FIRST) ?
- EntryType.ALWAYS_FIRST : EntryType.ALWAYS_LAST);
- } else {
- throw new IllegalArgumentException("Values was null");
- }
+ throw new IllegalArgumentException("Values was null for valid entry");
}
}
/**
* Read an existing entry in from a buffer
*/
- private Entry(ByteBuffer buffer, int entryLen,
- List<ColumnDescriptor> columns)
+ private Entry(ByteBuffer buffer, int entryLen)
throws IOException
{
- this(buffer, entryLen, columns, 0);
+ this(buffer, entryLen, 0);
}
/**
* Read an existing entry in from a buffer
*/
- private Entry(ByteBuffer buffer, int entryLen,
- List<ColumnDescriptor> columns, int extraTrailingLen)
+ private Entry(ByteBuffer buffer, int entryLen, int extraTrailingLen)
throws IOException
{
// we need 4 trailing bytes for the rowId, plus whatever the caller
public boolean isValid() {
return(_entryBytes != null);
}
-
+
protected final byte[] getEntryBytes() {
return _entryBytes;
}
/**
* Read an existing node entry in from a buffer
*/
- private NodeEntry(ByteBuffer buffer, int entryLen,
- List<ColumnDescriptor> columns)
+ private NodeEntry(ByteBuffer buffer, int entryLen)
throws IOException
{
// we need 4 trailing bytes for the sub-page number
- super(buffer, entryLen, columns, 4);
+ super(buffer, entryLen, 4);
_subPageNumber = ByteUtil.getInt(buffer, ByteOrder.BIG_ENDIAN);
}
public void beforeEntry(Object[] row)
throws IOException
{
- restorePosition(new Entry(row, RowId.FIRST_ROW_ID, Index.this));
+ restorePosition(
+ new Entry(Index.this.createEntryBytes(row), RowId.FIRST_ROW_ID));
}
/**
public void afterEntry(Object[] row)
throws IOException
{
- restorePosition(new Entry(row, RowId.LAST_ROW_ID, Index.this));
+ restorePosition(
+ new Entry(Index.this.createEntryBytes(row), RowId.LAST_ROW_ID));
}
/**
package com.healthmarketscience.jackcess;
import java.io.File;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
db.close();
// copy to temp file and attemp to edit
- File testFile = File.createTempFile("databaseTest", ".mdb");
- testFile.deleteOnExit();
-
- copyFile(origFile, testFile);
-
- db = Database.open(testFile);
+ db = openCopy(origFile);
t = db.getTable("Table1");
try {
}
public void testEntryDeletion() throws Exception {
- File srcFile = new File("test/data/test.mdb");
- File dbFile = File.createTempFile("databaseTest", ".mdb");
- dbFile.deleteOnExit();
- copyFile(srcFile, dbFile);
-
- Table table = Database.open(dbFile).getTable("Table1");
+ Table table = openCopy(new File("test/data/test.mdb")).getTable("Table1");
for(int i = 0; i < 10; ++i) {
table.addRow("foo" + i, "bar" + i, (byte)42 + i, (short)53 + i, 13 * i,
}
}
+ public void testIgnoreNulls() throws Exception
+ {
+ Database db = openCopy(new File("test/data/testIndexProperties.mdb"));
+
+ doTestIgnoreNulls(db, "TableIgnoreNulls1");
+ doTestIgnoreNulls(db, "TableIgnoreNulls2");
+
+ db.close();
+ }
+
+ private void doTestIgnoreNulls(Database db, String tableName)
+ throws Exception
+ {
+ Table orig = db.getTable(tableName);
+ Index origI = orig.getIndex("DataIndex");
+ Table temp = db.getTable(tableName + "_temp");
+ Index tempI = temp.getIndex("DataIndex");
+
+ // copy from orig table to temp table
+ for(Map<String,Object> row : orig) {
+ temp.addRow(orig.asRow(row));
+ }
+
+ assertEquals(origI.getEntryCount(), tempI.getEntryCount());
+
+ Cursor origC = Cursor.createIndexCursor(orig, origI);
+ Cursor tempC = Cursor.createIndexCursor(temp, tempI);
+
+ while(true) {
+ boolean origHasNext = origC.moveToNextRow();
+ boolean tempHasNext = tempC.moveToNextRow();
+ assertTrue(origHasNext == tempHasNext);
+ if(!origHasNext) {
+ break;
+ }
+
+ Map<String,Object> origRow = origC.getCurrentRow();
+ Cursor.Position origCurPos = origC.getSavepoint().getCurrentPosition();
+ Map<String,Object> tempRow = tempC.getCurrentRow();
+ Cursor.Position tempCurPos = tempC.getSavepoint().getCurrentPosition();
+
+ assertEquals(origRow, tempRow);
+ assertEquals(IndexCodesTest.entryToString(origCurPos),
+ IndexCodesTest.entryToString(tempCurPos));
+ }
+ }
+
+ public void testUnique() throws Exception
+ {
+ Database db = openCopy(new File("test/data/testIndexProperties.mdb"));
+
+ Table t = db.getTable("TableUnique1_temp");
+ Index index = t.getIndex("DataIndex");
+
+ doTestUnique(t, index, 1,
+ null, true,
+ "unique data", true,
+ null, true,
+ "more", false,
+ "stuff", false,
+ "unique data", false);
+
+ t = db.getTable("TableUnique2_temp");
+ index = t.getIndex("DataIndex");
+
+ doTestUnique(t, index, 2,
+ null, null, true,
+ "unique data", 42, true,
+ "unique data", null, true,
+ null, null, true,
+ "some", 42, true,
+ "more unique data", 13, true,
+ null, -4242, true,
+ "another row", -3462, false,
+ null, 49, false,
+ "more", null, false,
+ "unique data", 42, false,
+ "unique data", null, false,
+ null, -4242, false);
+
+ db.close();
+ }
+
+ private void doTestUnique(Table t, Index index, int numValues,
+ Object... testData)
+ throws Exception
+ {
+ for(int i = 0; i < testData.length; i += (numValues + 1)) {
+ Object[] row = new Object[numValues + 1];
+ row[0] = "testRow" + i;
+ for(int j = 1; j < (numValues + 1); ++j) {
+ row[j] = testData[i + j - 1];
+ }
+ boolean expectedSuccess = (Boolean)testData[i + numValues];
+
+ IOException failure = null;
+ try {
+ index.addRow(row, new RowId(400 + i, 0));
+ } catch(IOException e) {
+ failure = e;
+ }
+ if(expectedSuccess) {
+ assertNull(failure);
+ } else {
+ assertTrue(failure != null);
+ assertTrue(failure.getMessage().contains("uniqueness"));
+ }
+ }
+ }
+
private void checkIndexColumns(Table table, String... idxInfo)
throws Exception
{