aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/com/healthmarketscience/jackcess/Index.java
blob: 41bd0c40e3c7850cfce23f38322b7512b5335399 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
Copyright (c) 2005 Health Market Science, Inc.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
USA

You can contact Health Market Science at info@healthmarketscience.com
or at the following address:

Health Market Science
2700 Horizon Drive
Suite 200
King of Prussia, PA 19406
*/

package com.healthmarketscience.jackcess;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
import org.apache.commons.collections.bidimap.DualHashBidiMap;
import org.apache.commons.collections.BidiMap;
import org.apache.commons.lang.builder.CompareToBuilder;

/**
 * Access table index
 * @author Tim McCune
 */
public class Index implements Comparable<Index> {
  
  /** Max number of columns in an index */
  private static final int MAX_COLUMNS = 10;
  
  private static final short COLUMN_UNUSED = -1;
  
  /**
   * Map of characters to bytes that Access uses in indexes (not ASCII)
   *    (Character -> Byte)
   */
  private static BidiMap CODES = new DualHashBidiMap();
  static {
    //These values are prefixed with a '43'
    CODES.put('^', (byte) 2);
    CODES.put('_', (byte) 3);
    CODES.put('{', (byte) 9);
    CODES.put('|', (byte) 11);
    CODES.put('}', (byte) 13);
    CODES.put('~', (byte) 15);
    
    //These values aren't.
    CODES.put(' ', (byte) 7);
    CODES.put('#', (byte) 12);
    CODES.put('$', (byte) 14);
    CODES.put('%', (byte) 16);
    CODES.put('&', (byte) 18);
    CODES.put('(', (byte) 20);
    CODES.put(')', (byte) 22);
    CODES.put('*', (byte) 24);
    CODES.put(',', (byte) 26);
    CODES.put('/', (byte) 30);
    CODES.put(':', (byte) 32);
    CODES.put(';', (byte) 34);
    CODES.put('?', (byte) 36);
    CODES.put('@', (byte) 38);
    CODES.put('+', (byte) 44);
    CODES.put('<', (byte) 46);
    CODES.put('=', (byte) 48);
    CODES.put('>', (byte) 50);
    CODES.put('0', (byte) 54);
    CODES.put('1', (byte) 56);
    CODES.put('2', (byte) 58);
    CODES.put('3', (byte) 60);
    CODES.put('4', (byte) 62);
    CODES.put('5', (byte) 64);
    CODES.put('6', (byte) 66);
    CODES.put('7', (byte) 68);
    CODES.put('8', (byte) 70);
    CODES.put('9', (byte) 72);
    CODES.put('A', (byte) 74);
    CODES.put('B', (byte) 76);
    CODES.put('C', (byte) 77);
    CODES.put('D', (byte) 79);
    CODES.put('E', (byte) 81);
    CODES.put('F', (byte) 83);
    CODES.put('G', (byte) 85);
    CODES.put('H', (byte) 87);
    CODES.put('I', (byte) 89);
    CODES.put('J', (byte) 91);
    CODES.put('K', (byte) 92);
    CODES.put('L', (byte) 94);
    CODES.put('M', (byte) 96);
    CODES.put('N', (byte) 98);
    CODES.put('O', (byte) 100);
    CODES.put('P', (byte) 102);
    CODES.put('Q', (byte) 104);
    CODES.put('R', (byte) 105);
    CODES.put('S', (byte) 107);
    CODES.put('T', (byte) 109);
    CODES.put('U', (byte) 111);
    CODES.put('V', (byte) 113);
    CODES.put('W', (byte) 115);
    CODES.put('X', (byte) 117);
    CODES.put('Y', (byte) 118);
    CODES.put('Z', (byte) 120);
  }
  
  /** Page number of the index data */
  private int _pageNumber;
  private int _parentPageNumber;
  /** Number of rows in the index */
  private int _rowCount;
  private JetFormat _format;
  private SortedSet<Entry> _entries = new TreeSet<Entry>();
  /** Map of columns to order */
  private Map<Column, Byte> _columns = new LinkedHashMap<Column, Byte>();
  private PageChannel _pageChannel;
  /** 0-based index number */
  private int _indexNumber;
  /** Index name */
  private String _name;
  
  public Index(int parentPageNumber, PageChannel channel, JetFormat format) {
    _parentPageNumber = parentPageNumber;
    _pageChannel = channel;
    _format = format;
  }
  
  public void setIndexNumber(int indexNumber) {
    _indexNumber = indexNumber;
  }
  public int getIndexNumber() {
    return _indexNumber;
  }
  
  public void setRowCount(int rowCount) {
    _rowCount = rowCount;
  }
  
  public void setName(String name) {
    _name = name;
  }
  
  public void update() throws IOException {
    _pageChannel.writePage(write(), _pageNumber);
  }
  
  /**
   * Write this index out to a buffer
   */
  public ByteBuffer write() throws IOException {
    ByteBuffer buffer = _pageChannel.createPageBuffer();
    buffer.put((byte) 0x04);  //Page type
    buffer.put((byte) 0x01);  //Unknown
    buffer.putShort((short) 0); //Free space
    buffer.putInt(_parentPageNumber);
    buffer.putInt(0); //Prev page
    buffer.putInt(0); //Next page
    buffer.putInt(0); //Leaf page
    buffer.putInt(0); //Unknown
    buffer.put((byte) 0); //Unknown
    buffer.put((byte) 0); //Unknown
    buffer.put((byte) 0); //Unknown
    byte[] entryMask = new byte[_format.SIZE_INDEX_ENTRY_MASK];
    int totalSize = 0;
    Iterator iter = _entries.iterator();
    while (iter.hasNext()) {
      Entry entry = (Entry) iter.next();
      int size = entry.size();
      totalSize += size;
      int idx = totalSize  / 8;
      entryMask[idx] |= (1 << (totalSize % 8));
    }
    buffer.put(entryMask);
    iter = _entries.iterator();
    while (iter.hasNext()) {
      Entry entry = (Entry) iter.next();
      entry.write(buffer);
    }
    buffer.putShort(2, (short) (_format.PAGE_SIZE - buffer.position()));
    return buffer;
  }
  
  /**
   * Read this index in from a buffer
   * @param buffer Buffer to read from
   * @param availableColumns Columns that this index may use
   */
  public void read(ByteBuffer buffer, List<Column> availableColumns)
  throws IOException
  {
    for (int i = 0; i < MAX_COLUMNS; i++) {
      short columnNumber = buffer.getShort();
      Byte order = new Byte(buffer.get());
      if (columnNumber != COLUMN_UNUSED) {
        _columns.put(availableColumns.get(columnNumber), order);
      }
    }
    buffer.getInt(); //Forward past Unknown
    _pageNumber = buffer.getInt();
    buffer.position(buffer.position() + 10);  //Forward past other stuff
    ByteBuffer indexPage = _pageChannel.createPageBuffer();
    _pageChannel.readPage(indexPage, _pageNumber);
    indexPage.position(_format.OFFSET_INDEX_ENTRY_MASK);
    byte[] entryMask = new byte[_format.SIZE_INDEX_ENTRY_MASK];
    indexPage.get(entryMask);
    int lastStart = 0;
    for (int i = 0; i < entryMask.length; i++) {
      for (int j = 0; j < 8; j++) {
        if ((entryMask[i] & (1 << j)) != 0) {
          int length = i * 8 + j - lastStart;
          _entries.add(new Entry(indexPage));
          lastStart += length;
        }
      }
    }
  }
  
  /**
   * Add a row to this index
   * @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
   */
  public void addRow(Object[] row, int pageNumber, byte rowNumber) {
    _entries.add(new Entry(row, pageNumber, rowNumber));
  }
  
  public String toString() {
    StringBuffer rtn = new StringBuffer();
    rtn.append("\tName: " + _name);
    rtn.append("\n\tNumber: " + _indexNumber);
    rtn.append("\n\tPage number: " + _pageNumber);
    rtn.append("\n\tColumns: " + _columns);
    rtn.append("\n\tEntries: " + _entries);
    rtn.append("\n\n");
    return rtn.toString();
  }
  
  public int compareTo(Index other) {
    if (_indexNumber > other.getIndexNumber()) {
      return 1;
    } else if (_indexNumber < other.getIndexNumber()) {
      return -1;
    } else {
      return 0;
    }
  }
  
  /**
   * A single entry in an index (points to a single row)
   */
  private class Entry implements Comparable<Entry> {
    
    /** Page number on which the row is stored */
    private int _page;
    /** Row number at which the row is stored */
    private byte _row;
    /** Columns that are indexed */
    private List<EntryColumn> _entryColumns = new ArrayList<EntryColumn>();
    
    /**
     * Create a new entry
     * @param values Indexed row values
     * @param page Page number on which the row is stored
     * @param rowNumber Row number at which the row is stored
     */
    public Entry(Object[] values, int page, byte rowNumber) {
      _page = page;
      _row = rowNumber;
      Iterator iter = _columns.keySet().iterator();
      while (iter.hasNext()) {
        Column col = (Column) iter.next();
        Object value = values[col.getColumnNumber()];
        _entryColumns.add(new EntryColumn(col, (Comparable) value));
      }
    }
    
    /**
     * Read an existing entry in from a buffer
     */
    public Entry(ByteBuffer buffer) throws IOException {
      Iterator iter = _columns.keySet().iterator();
      while (iter.hasNext()) {
        _entryColumns.add(new EntryColumn((Column) iter.next(), buffer));
      }
      //3-byte int in big endian order!  Gotta love those kooky MS programmers. :)
      _page = (((int) buffer.get()) & 0xFF) << 16;
      _page += (((int) buffer.get()) & 0xFF) << 8;
      _page += (int) buffer.get();
      _row = buffer.get();
    }
    
    public List getEntryColumns() {
      return _entryColumns;
    }
    
    public int getPage() {
      return _page;
    }
    
    public byte getRow() {
      return _row;
    }
    
    public int size() {
      int rtn = 5;
      Iterator iter = _entryColumns.iterator();
      while (iter.hasNext()) {
        rtn += ((EntryColumn) iter.next()).size();
      }
      return rtn;
    }
    
    /**
     * Write this entry into a buffer
     */
    public void write(ByteBuffer buffer) throws IOException {
      Iterator iter = _entryColumns.iterator();
      while (iter.hasNext()) {
        ((EntryColumn) iter.next()).write(buffer);
      }
      buffer.put((byte) (_page >>> 16));
      buffer.put((byte) (_page >>> 8));
      buffer.put((byte) _page);
      buffer.put(_row);
    }
    
    public String toString() {
      return ("Page = " + _page + ", Row = " + _row + ", Columns = " + _entryColumns + "\n");
    }
    
    public int compareTo(Entry other) {
      if (this == other) {
        return 0;
      }
      Iterator myIter = _entryColumns.iterator();
      Iterator otherIter = other.getEntryColumns().iterator();
      while (myIter.hasNext()) {
        if (!otherIter.hasNext()) {
          throw new IllegalArgumentException(
              "Trying to compare index entries with a different number of entry columns");
        }
        EntryColumn myCol = (EntryColumn) myIter.next();
        EntryColumn otherCol = (EntryColumn) otherIter.next();
        int i = myCol.compareTo(otherCol);
        if (i != 0) {
          return i;
        }
      }
      return new CompareToBuilder().append(_page, other.getPage())
          .append(_row, other.getRow()).toComparison();
    }
    
  }
  
  /**
   * A single column value within an index Entry; encapsulates column
   * definition and column value.
   */
  private class EntryColumn implements Comparable<EntryColumn> {
    
    /** Column definition */
    private Column _column;
    /** Column value */
    private Comparable _value;
    
    /**
     * Create a new EntryColumn
     */
    public EntryColumn(Column col, Comparable value) {
      _column = col;
      _value = value;
    }
    
    /**
     * Read in an existing EntryColumn from a buffer
     */
    public EntryColumn(Column col, ByteBuffer buffer) throws IOException {
      _column = col;
      byte flag = buffer.get();
      if (flag != (byte) 0) {
        if (col.getType() == DataType.TEXT) {
          StringBuffer sb = new StringBuffer();
          byte b;
          while ( (b = buffer.get()) != (byte) 1) {
            if ((int) b == 43) {
              b = buffer.get();
            }
            Character c = (Character) CODES.getKey(new Byte(b));
            if (c != null) {
              sb.append(c.charValue());
            }
          }
          buffer.get(); //Forward past 0x00
          _value = sb.toString();
        } else {
          byte[] data = new byte[col.getType().getSize()];
          buffer.get(data);
          _value = (Comparable) col.read(data, ByteOrder.BIG_ENDIAN);
          //ints and shorts are stored in index as value + 2147483648
          if (_value instanceof Integer) {
            _value = new Integer((int) (((Integer) _value).longValue() + (long) Integer.MAX_VALUE + 1L)); 
          } else if (_value instanceof Short) {
            _value = new Short((short) (((Short) _value).longValue() + (long) Integer.MAX_VALUE + 1L));
          }
        }
      }
    }
    
    public Comparable getValue() {
      return _value;
    }
    
    /**
     * Write this entry column to a buffer
     */
    public void write(ByteBuffer buffer) throws IOException {
      buffer.put((byte) 0x7F);
      if (_column.getType() == DataType.TEXT) {
        String s = (String) _value;
        for (int i = 0; i < s.length(); i++) {
          Byte b = (Byte) CODES.get(new Character(Character.toUpperCase(s.charAt(i))));
          
          if (b == null) {
            throw new IOException("Unmapped index value: " + s.charAt(i));
          } else {
            byte bv = b.byteValue();
            //WTF is this?  No idea why it's this way, but it is. :)
            if (bv == (byte) 2 || bv == (byte) 3 || bv == (byte) 9 || bv == (byte) 11 ||
                bv == (byte) 13 || bv == (byte) 15)
            {
              buffer.put((byte) 43);  //Ah, the magic 43.
            }
            buffer.put(b.byteValue());
            if (s.equals("_")) {
              buffer.put((byte) 3);
            }
          }
        }
        buffer.put((byte) 1);
        buffer.put((byte) 0);
      } else {
        Comparable value = _value;
        if (value instanceof Integer) {
          value = new Integer((int) (((Integer) value).longValue() - ((long) Integer.MAX_VALUE + 1L)));
        } else if (value instanceof Short) {
          value = new Short((short) (((Short) value).longValue() - ((long) Integer.MAX_VALUE + 1L)));
        }
        buffer.put(_column.write(value, ByteOrder.BIG_ENDIAN));
      }
    }
    
    public int size() {
      if (_value == null) {
        return 0;
      } else if (_value instanceof String) {
        int rtn = 3;
        String s = (String) _value;
        for (int i = 0; i < s.length(); i++) {
          rtn++;
          if (s.charAt(i) == '^' || s.charAt(i) == '_' || s.charAt(i) == '{' ||
              s.charAt(i) == '|' || s.charAt(i) == '}' || s.charAt(i) == '-')
          {
            rtn++;
          }
        }
        return rtn;
      } else {
        return _column.getType().getSize();
      }
    }
    
    public String toString() {
      return String.valueOf(_value);
    }
    
    public int compareTo(EntryColumn other) {
      return new CompareToBuilder().append(_value, other.getValue())
          .toComparison();
    }
  }
  
}