From: James Ahlborn Date: Tue, 26 Sep 2006 15:01:38 +0000 (+0000) Subject: read index entries on demand X-Git-Tag: rel_1_1_7~3 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=584802a0b811edc57fc5f423dc07f258cdede362;p=jackcess.git read index entries on demand git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@126 f203690c-595d-4dc9-a70b-905162fa7fd2 --- diff --git a/src/java/com/healthmarketscience/jackcess/Index.java b/src/java/com/healthmarketscience/jackcess/Index.java index 602bd5f..3f18a33 100644 --- a/src/java/com/healthmarketscience/jackcess/Index.java +++ b/src/java/com/healthmarketscience/jackcess/Index.java @@ -189,7 +189,7 @@ public class Index implements Comparable { /** Number of rows in the index */ private int _rowCount; private JetFormat _format; - private SortedSet _entries = new TreeSet(); + private SortedSet _entries; /** Map of columns to flags */ private Map _columns = new LinkedHashMap(); private PageChannel _pageChannel; @@ -199,6 +199,9 @@ public class Index implements Comparable { private String _name; /** is this index a primary key */ private boolean _primaryKey; + /** true if the index entries have been initialized, + false otherwise */ + private boolean _initialized; /** FIXME, for now, we can't write multi-page indexes or indexes using the funky primary key compression scheme */ boolean _readOnly; @@ -238,7 +241,44 @@ public class Index implements Comparable { return Collections.unmodifiableCollection(_columns.keySet()); } + /** + * Returns the number of index entries in the index. Only called by unit + * tests. + *

+ * Forces index initialization. + */ + int getEntryCount() + throws IOException + { + initialize(); + return _entries.size(); + } + + public boolean isInitialized() { + return _initialized; + } + + /** + * Forces initialization of this index (actual parsing of index pages). + * normally, the index will not be initialized until the entries are + * actually needed. + */ + public void initialize() throws IOException { + if(!_initialized) { + readIndexEntries(); + _initialized = true; + } + } + + /** + * Writes the current index state to the database. + *

+ * Forces index initialization. + */ public void update() throws IOException { + // make sure we've parsed the entries + initialize(); + if(_readOnly) { throw new UnsupportedOperationException( "FIXME cannot write indexes of this type yet"); @@ -287,12 +327,12 @@ public class Index implements Comparable { } /** - * Read this index in from a tableBuffer + * Read the index info from a tableBuffer * @param tableBuffer table definition buffer to read from initial info * @param availableColumns Columns that this index may use */ public void read(ByteBuffer tableBuffer, List availableColumns) - throws IOException + throws IOException { for (int i = 0; i < MAX_COLUMNS; i++) { short columnNumber = tableBuffer.getShort(); @@ -304,6 +344,16 @@ public class Index implements Comparable { tableBuffer.getInt(); //Forward past Unknown _pageNumber = tableBuffer.getInt(); tableBuffer.position(tableBuffer.position() + 10); //Forward past other stuff + } + + /** + * Reads the actual index entries. + */ + private void readIndexEntries() + throws IOException + { + _entries = new TreeSet(); + ByteBuffer indexPage = _pageChannel.createPageBuffer(); // find first leaf page @@ -340,7 +390,6 @@ public class Index implements Comparable { break; } } - } /** @@ -437,6 +486,9 @@ public class Index implements Comparable { /** * Add a row to this index + *

+ * Forces index initialization. + * * @param row Row to add * @param pageNumber Page number on which the row is stored * @param rowNumber Row number at which the row is stored @@ -444,6 +496,9 @@ public class Index implements Comparable { public void addRow(Object[] row, int pageNumber, byte rowNumber) throws IOException { + // make sure we've parsed the entries + initialize(); + _entries.add(new Entry(row, pageNumber, rowNumber)); } @@ -455,6 +510,7 @@ public class Index implements Comparable { rtn.append("\n\tPage number: " + _pageNumber); rtn.append("\n\tIs Primary Key: " + _primaryKey); rtn.append("\n\tColumns: " + _columns); + rtn.append("\n\tInitialized: " + _initialized); rtn.append("\n\tEntries: " + _entries); rtn.append("\n\n"); return rtn.toString(); diff --git a/test/src/java/com/healthmarketscience/jackcess/IndexTest.java b/test/src/java/com/healthmarketscience/jackcess/IndexTest.java index 33967bb..d8307a9 100644 --- a/test/src/java/com/healthmarketscience/jackcess/IndexTest.java +++ b/test/src/java/com/healthmarketscience/jackcess/IndexTest.java @@ -43,7 +43,6 @@ public class IndexTest extends TestCase { } public void testByteCodeComparator() { - // FIXME, writeme byte[] b0 = null; byte[] b1 = new byte[]{(byte)0x00}; byte[] b2 = new byte[]{(byte)0x00, (byte)0x00}; @@ -99,7 +98,10 @@ public class IndexTest extends TestCase { File origFile = new File("test/data/compIndexTest.mdb"); Database db = Database.open(origFile); Table t = db.getTable("Table1"); + Index index = t.getIndexes().get(0); + assertFalse(index.isInitialized()); assertEquals(512, countRows(t)); + assertEquals(512, index.getEntryCount()); db.close(); // copy to temp file and attemp to edit