From: James Ahlborn Date: Tue, 18 Mar 2008 20:11:48 +0000 (+0000) Subject: clean up updating of the index uniqueEntryCount X-Git-Tag: rel_1_1_13~9 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=404056c33b047e8ec75b0f6def90b4a27bf8079b;p=jackcess.git clean up updating of the index uniqueEntryCount git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@283 f203690c-595d-4dc9-a70b-905162fa7fd2 --- diff --git a/src/java/com/healthmarketscience/jackcess/Index.java b/src/java/com/healthmarketscience/jackcess/Index.java index df2445f..944866d 100644 --- a/src/java/com/healthmarketscience/jackcess/Index.java +++ b/src/java/com/healthmarketscience/jackcess/Index.java @@ -151,10 +151,13 @@ public class Index implements Comparable { private final Table _table; /** Page number of the index data */ private int _pageNumber; - /** Number of rows in the index - NOTE: this does not actually seem to be the row count, unclear what the - value means*/ - private int _rowCount; + /** offset within the tableDefinition buffer of the uniqueEntryCount for + this index */ + private final int _uniqueEntryCountOffset; + /** The number of unique entries which have been added to this index. note, + however, that it is never decremented, only incremented (as observed in + Access). */ + private int _uniqueEntryCount; /** sorted collection of index entries. this is kept in a list instead of a SortedSet because the SortedSet has lame traversal utilities */ private final List _entries = new ArrayList(); @@ -180,8 +183,10 @@ public class Index implements Comparable { /** FIXME, for now, we can't write multi-page indexes or indexes using the funky primary key compression scheme */ boolean _readOnly; - public Index(Table table) { + public Index(Table table, int uniqueEntryCount, int uniqueEntryCountOffset) { _table = table; + _uniqueEntryCount = uniqueEntryCount; + _uniqueEntryCountOffset = uniqueEntryCountOffset; } public Table getTable() { @@ -212,12 +217,12 @@ public class Index implements Comparable { return _indexFlags; } - public void setRowCount(int rowCount) { - _rowCount = rowCount; + public int getUniqueEntryCount() { + return _uniqueEntryCount; } - public int getRowCount() { - return _rowCount; + public int getUniqueEntryCountOffset() { + return _uniqueEntryCountOffset; } public String getName() { @@ -587,7 +592,6 @@ public class Index implements Comparable { Entry newEntry = new Entry(createEntryBytes(row), rowId); if(addEntry(newEntry, isNullEntry, row)) { - ++_rowCount; ++_modCount; } else { LOG.warn("Added duplicate index entry " + newEntry + " for row: " + @@ -617,7 +621,6 @@ public class Index implements Comparable { Entry oldEntry = new Entry(createEntryBytes(row), rowId); if(removeEntry(oldEntry)) { - --_rowCount; ++_modCount; } else { LOG.warn("Failed removing index entry " + oldEntry + " for row: " + @@ -710,16 +713,21 @@ public class Index implements Comparable { // determine if the addition of this entry would break the uniqueness // constraint. See isUnique() for some notes about uniqueness as // defined by Access. - if(isUnique() && !isNullEntry && - (((idx > 0) && - newEntry.equalsEntryBytes(_entries.get(idx - 1))) || + boolean isDupeEntry = + (((idx > 0) && + newEntry.equalsEntryBytes(_entries.get(idx - 1))) || ((idx < _entries.size()) && - newEntry.equalsEntryBytes(_entries.get(idx))))) + newEntry.equalsEntryBytes(_entries.get(idx)))); + if(isUnique() && !isNullEntry && isDupeEntry) { throw new IOException( "New row " + Arrays.asList(row) + " violates uniqueness constraint for index " + this); } + + if(!isDupeEntry) { + ++_uniqueEntryCount; + } _entries.add(idx, newEntry); return true; diff --git a/src/java/com/healthmarketscience/jackcess/Table.java b/src/java/com/healthmarketscience/jackcess/Table.java index d850349..96926cc 100644 --- a/src/java/com/healthmarketscience/jackcess/Table.java +++ b/src/java/com/healthmarketscience/jackcess/Table.java @@ -950,10 +950,11 @@ public class Table _freeSpacePages = UsageMap.read(getDatabase(), pageNum, rowNum, false); for (int i = 0; i < _indexCount; i++) { - Index index = new Index(this); - _indexes.add(index); - index.setRowCount(tableBuffer.getInt(getFormat().OFFSET_INDEX_DEF_BLOCK + - i * getFormat().SIZE_INDEX_DEFINITION + 4)); + int uniqueEntryCountOffset = + (getFormat().OFFSET_INDEX_DEF_BLOCK + + (i * getFormat().SIZE_INDEX_DEFINITION) + 4); + int uniqueEntryCount = tableBuffer.getInt(uniqueEntryCountOffset); + _indexes.add(new Index(this, uniqueEntryCount, uniqueEntryCountOffset)); } int colOffset = getFormat().OFFSET_INDEX_DEF_BLOCK + @@ -1202,9 +1203,12 @@ public class Table // write any index changes Iterator indIter = _indexes.iterator(); for (int i = 0; i < _indexes.size(); i++) { - tdefPage.putInt(getFormat().OFFSET_INDEX_DEF_BLOCK + - (i * getFormat().SIZE_INDEX_DEFINITION) + 4, _rowCount); Index index = indIter.next(); + // write the unique entry count for the index to the table definition + // page + tdefPage.putInt(index.getUniqueEntryCountOffset(), + index.getUniqueEntryCount()); + // write the entry page for the index index.update(); }