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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
|
// Copyright (c) 2007 Health Market Science, Inc.
package com.healthmarketscience.jackcess;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import com.healthmarketscience.jackcess.Table.RowState;
import org.apache.commons.lang.ObjectUtils;
import static com.healthmarketscience.jackcess.PageChannel.INVALID_PAGE_NUMBER;
import static com.healthmarketscience.jackcess.RowId.INVALID_ROW_NUMBER;
/**
* Manages iteration for a Table. Different cursors provide different methods
* of traversing a table. Cursors should be fairly robust in the face of
* table modification during traversal (although depending on how the table is
* traversed, row updates may or may not be seen). Multiple cursors may
* traverse the same table simultaneously.
* <p>
* Is not thread-safe.
*
* @author james
*/
public abstract class Cursor implements Iterable<Map<String, Object>>
{
public static final int FIRST_PAGE_NUMBER = INVALID_PAGE_NUMBER;
public static final int LAST_PAGE_NUMBER = Integer.MAX_VALUE;
public static final RowId FIRST_ROW_ID = new RowId(
FIRST_PAGE_NUMBER, INVALID_ROW_NUMBER);
public static final RowId LAST_ROW_ID = new RowId(
LAST_PAGE_NUMBER, INVALID_ROW_NUMBER);
/** owning table */
protected final Table _table;
/** State used for reading the table rows */
protected final RowState _rowState;
/** the first (exclusive) row id for this iterator */
private final RowId _firstRowId;
/** the last (exclusive) row id for this iterator */
private final RowId _lastRowId;
/** the current row */
private RowId _currentRowId;
protected Cursor(Table table, RowId firstRowId, RowId lastRowId) {
_table = table;
_rowState = _table.createRowState();
_firstRowId = firstRowId;
_lastRowId = lastRowId;
_currentRowId = firstRowId;
}
/**
* Creates a normal, un-indexed cursor for the given table.
*/
public static Cursor createCursor(Table table) {
return new TableScanCursor(table);
}
public Table getTable() {
return _table;
}
public JetFormat getFormat() {
return getTable().getFormat();
}
public PageChannel getPageChannel() {
return getTable().getPageChannel();
}
public RowId getCurrentRowId() {
return _currentRowId;
}
/**
* Returns the first row id (exclusive) as defined by this cursor.
*/
protected RowId getFirstRowId() {
return _firstRowId;
}
/**
* Returns the last row id (exclusive) as defined by this cursor.
*/
protected RowId getLastRowId() {
return _lastRowId;
}
/**
* Resets this cursor for forward iteration. Calls {@link #beforeFirst}.
*/
public void reset() {
beforeFirst();
}
/**
* Resets this cursor for forward iteration (sets cursor to before the first
* row).
*/
public void beforeFirst() {
reset(true);
}
/**
* Resets this cursor for reverse iteration (sets cursor to after the last
* row).
*/
public void afterLast() {
reset(false);
}
/**
* Resets this cursor for iterating the given direction.
*/
protected void reset(boolean moveForward) {
_currentRowId = getBeginningRowId(moveForward);
_rowState.reset();
}
protected final RowId getEndRowId(boolean moveForward) {
return (moveForward ? getLastRowId() : getFirstRowId());
}
protected final RowId getBeginningRowId(boolean moveForward) {
return (moveForward ? getFirstRowId() : getLastRowId());
}
/**
* Returns {@code true} if the cursor is currently pointing at a valid row,
* {@code false} otherwise.
*/
public boolean isCurrentRowValid() {
return _currentRowId.isValidRow();
}
/**
* Calls <code>reset</code> on this table and returns a modifiable Iterator
* which will iterate through all the rows of this table. Use of the
* Iterator follows the same restrictions as a call to
* <code>getNextRow</code>.
* @throws IllegalStateException if an IOException is thrown by one of the
* operations, the actual exception will be contained within
*/
public Iterator<Map<String, Object>> iterator()
{
return iterator(null);
}
/**
* Calls <code>reset</code> on this table and returns a modifiable Iterator
* which will iterate through all the rows of this table, returning only the
* given columns. Use of the Iterator follows the same restrictions as a
* call to <code>getNextRow</code>.
* @throws IllegalStateException if an IOException is thrown by one of the
* operations, the actual exception will be contained within
*/
public Iterator<Map<String, Object>> iterator(Collection<String> columnNames)
{
return new RowIterator(columnNames);
}
/**
* Delete the current row (retrieved by a call to {@link #getNextRow}).
*/
public void deleteCurrentRow() throws IOException {
_table.deleteRow(_rowState, _currentRowId);
}
/**
* @return The next row in this table (Column name -> Column value)
*/
public Map<String, Object> getNextRow() throws IOException {
return getNextRow(null);
}
/**
* @param columnNames Only column names in this collection will be returned
* @return The next row in this table (Column name -> Column value)
*/
public Map<String, Object> getNextRow(Collection<String> columnNames)
throws IOException
{
return getNextRow(columnNames, true);
}
/**
* @return The previous row in this table (Column name -> Column value)
*/
public Map<String, Object> getPreviousRow() throws IOException {
return getPreviousRow(null);
}
/**
* @param columnNames Only column names in this collection will be returned
* @return The previous row in this table (Column name -> Column value)
*/
public Map<String, Object> getPreviousRow(Collection<String> columnNames)
throws IOException
{
return getNextRow(columnNames, false);
}
/**
* @param columnNames Only column names in this collection will be returned
* @return The next row in this table (Column name -> Column value), where
* "next" may be backwards if moveForward is {@code false}.
*/
private Map<String, Object> getNextRow(Collection<String> columnNames,
boolean moveForward)
throws IOException
{
if(moveToNextRow(moveForward)) {
return getCurrentRow(columnNames);
}
return null;
}
/**
* Moves to the next row as defined by this cursor.
* @return {@code true} if a valid next row was found, {@code false}
* otherwise
*/
public boolean moveToNextRow()
throws IOException
{
return moveToNextRow(true);
}
/**
* Moves to the previous row as defined by this cursor.
* @return {@code true} if a valid previous row was found, {@code false}
* otherwise
*/
public boolean moveToPreviousRow()
throws IOException
{
return moveToNextRow(false);
}
/**
* Moves to the next row as defined by this cursor.
* @return {@code true} if a valid next row was found, {@code false}
* otherwise
*/
private boolean moveToNextRow(boolean moveForward)
throws IOException
{
RowId endRowId = getEndRowId(moveForward);
if(_currentRowId.equals(endRowId)) {
// already at end
return false;
}
_rowState.reset();
_currentRowId = findNextRowId(_currentRowId, moveForward, endRowId);
return(!_currentRowId.equals(endRowId));
}
/**
* Moves to the first row (as defined by the cursor) where the given column
* has the given value. This may be more efficient on some cursors than
* others.
*
* @return {@code true} if a valid row was found with the given value,
* {@code false} if no row was found (and the cursor is now pointing
* past the end of the table)
*/
public boolean findRow(Column column, Object value)
throws IOException
{
beforeFirst();
while(moveToNextRow()) {
if(ObjectUtils.equals(value, getCurrentRowSingleColumn(column))) {
return true;
}
}
return false;
}
/**
* Moves to the first row (as defined by the cursor) where the given columns
* have the given values. This may be more efficient on some cursors than
* others.
*
* @return {@code true} if a valid row was found with the given values,
* {@code false} if no row was found (and the cursor is now pointing
* past the end of the table)
*/
public boolean findRow(Map<String,Object> row)
throws IOException
{
beforeFirst();
while(moveToNextRow()) {
Object value = getCurrentRow(row.keySet());
if(ObjectUtils.equals(row, value)) {
return true;
}
}
return false;
}
/**
* Skips as many rows as possible up to the given number of rows.
* @return the number of rows skipped.
*/
public int skipNextRows(int numRows)
throws IOException
{
int numSkippedRows = 0;
while((numSkippedRows < numRows) && moveToNextRow()) {
++numSkippedRows;
}
return numSkippedRows;
}
/**
* Skips as many rows as possible up to the given number of rows.
* @return the number of rows skipped.
*/
public int skipPreviousRows(int numRows)
throws IOException
{
int numSkippedRows = 0;
while((numSkippedRows < numRows) && moveToNextRow()) {
++numSkippedRows;
}
return numSkippedRows;
}
/**
* Skips as many rows as possible in the given direction up to the given
* number of rows.
* @return the number of rows skipped.
*/
private int skipRows(int numRows, boolean moveForward)
throws IOException
{
int numSkippedRows = 0;
while((numSkippedRows < numRows) && moveToNextRow(moveForward)) {
++numSkippedRows;
}
return numSkippedRows;
}
/**
* Returns the current row in this cursor (Column name -> Column value).
* @param columnNames Only column names in this collection will be returned
*/
public Map<String, Object> getCurrentRow()
throws IOException
{
return getCurrentRow(null);
}
/**
* Returns the current row in this cursor (Column name -> Column value).
* @param columnNames Only column names in this collection will be returned
*/
public Map<String, Object> getCurrentRow(Collection<String> columnNames)
throws IOException
{
return _table.getRow(_rowState, columnNames);
}
/**
* Returns the given column from the current row.
*/
public Object getCurrentRowSingleColumn(Column column)
throws IOException
{
return _table.getRowSingleColumn(_rowState, column);
}
/**
* Returns {@code true} if the row is marked as deleted, {@code false}
* otherwise. This method will not modify the rowState (it only looks at
* the "main" row, which is where the deleted flag is located).
*/
protected final boolean isCurrentRowDeleted()
throws IOException
{
ByteBuffer rowBuffer = _rowState.getFinalPage();
int rowNum = _rowState.getFinalRowNumber();
// note, we don't use findRowStart here cause we need the unmasked value
return Table.isDeletedRow(
rowBuffer.getShort(Table.getRowStartOffset(rowNum, getFormat())));
}
/**
* Returns the row count for the current page. If the page number is
* invalid or the page is not a DATA page, 0 is returned.
*/
protected final int getRowsOnCurrentDataPage(ByteBuffer rowBuffer)
throws IOException
{
int rowsOnPage = 0;
if((rowBuffer != null) && (rowBuffer.get(0) == PageTypes.DATA)) {
rowsOnPage =
rowBuffer.getShort(getFormat().OFFSET_NUM_ROWS_ON_DATA_PAGE);
}
return rowsOnPage;
}
/**
* Finds the next non-deleted row after the given row (as defined by this
* cursor) and returns the id of the row, where "next" may be backwards if
* moveForward is {@code false}. If there are no more rows, the returned
* rowId should equal the value returned by {@link #getLastRowId} if moving
* forward and {@link #getFirstRowId} if moving backward.
*/
protected abstract RowId findNextRowId(RowId currentRowId,
boolean moveForward,
RowId endRowId)
throws IOException;
/**
* Row iterator for this table, supports modification.
*/
private final class RowIterator implements Iterator<Map<String, Object>>
{
private Collection<String> _columnNames;
private boolean _hasNext = false;
private RowIterator(Collection<String> columnNames)
{
try {
reset();
_columnNames = columnNames;
_hasNext = moveToNextRow();
} catch(IOException e) {
throw new IllegalStateException(e);
}
}
public boolean hasNext() { return _hasNext; }
public void remove() {
try {
deleteCurrentRow();
} catch(IOException e) {
throw new IllegalStateException(e);
}
}
public Map<String, Object> next() {
if(!hasNext()) {
throw new NoSuchElementException();
}
try {
Map<String, Object> rtn = getCurrentRow(_columnNames);
_hasNext = moveToNextRow();
return rtn;
} catch(IOException e) {
throw new IllegalStateException(e);
}
}
}
protected abstract class DirHandler
{
public abstract RowId getBeginningRowId();
public abstract RowId getEndRowId();
}
protected abstract class ForwardDirHandler
{
public abstract RowId getBeginningRowId();
public abstract RowId getEndRowId();
}
/**
* Simple un-indexed cursor.
*/
private static class TableScanCursor extends Cursor
{
/** Iterator over the pages that this table owns */
private final UsageMap.PageIterator _ownedPagesIterator;
private TableScanCursor(Table table) {
super(table, FIRST_ROW_ID, LAST_ROW_ID);
_ownedPagesIterator = table.getOwnedPagesIterator();
}
@Override
protected void reset(boolean moveForward) {
_ownedPagesIterator.reset(moveForward);
super.reset(moveForward);
}
/**
* Position the buffer at the next row in the table
* @return a ByteBuffer narrowed to the next row, or null if none
*/
@Override
protected RowId findNextRowId(RowId currentRowId, boolean moveForward,
RowId endRowId)
throws IOException
{
// prepare to read next row
_rowState.reset();
int currentPageNumber = currentRowId.getPageNumber();
int currentRowNumber = currentRowId.getRowNumber();
int rowsOnPage = getRowsOnCurrentDataPage(
_rowState.setRow(currentPageNumber, currentRowNumber));
int inc = (moveForward ? 1 : -1);
// loop until we find the next valid row or run out of pages
while(true) {
currentRowNumber += inc;
if((currentRowNumber >= 0) && (currentRowNumber < rowsOnPage)) {
_rowState.setRow(currentPageNumber, currentRowNumber);
} else {
// load next page
currentRowNumber = INVALID_ROW_NUMBER;
currentPageNumber =
(moveForward ?
_ownedPagesIterator.getNextPage() :
_ownedPagesIterator.getPreviousPage());
ByteBuffer rowBuffer = _rowState.setRow(
currentPageNumber, currentRowNumber);
if(rowBuffer == null) {
//No more owned pages. No more rows.
return endRowId;
}
// update row count
rowsOnPage = getRowsOnCurrentDataPage(rowBuffer);
if(!moveForward) {
currentRowNumber = rowsOnPage;
}
// start again from the top
continue;
}
if(!isCurrentRowDeleted()) {
// we found a non-deleted row, return it
return new RowId(currentPageNumber, currentRowNumber);
}
}
}
}
}
|