2

我想搜索日期列的值 > '2014/03/01' 的所有行。

怎么能做到这一点?如果没有 Date value = '2014/03/01'` 的行,如何在没有完整扫描表的情况下定位光标?

Table table = db.getTable("Facture");
IndexCursor cursor = CursorBuilder.createCursor(table.getIndex("DateIndex"));

Date search_date = Date('2014/03/01');
for(Row row : cursor.newEntryIterable(search_date)) { ... }
4

1 回答 1

1

您已经在创建一个IndexCursor,因此不需要进行表扫描。只需使用IndexCursor#findClosestRowByEntry进行>=搜索,然后跳过完全匹配(如果有),如下所示:

Table table = db.getTable("Members");
String dateColumnName = "DateJoined";
Column dateColumn = table.getColumn(dateColumnName);
IndexCursor cursor = CursorBuilder.createCursor(table.getIndex(dateColumnName));

String searchDateAsString = "2014/03/01";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
Date search_date = sdf.parse(searchDateAsString);
cursor.findClosestRowByEntry(search_date);
if (cursor.isAfterLast()) {
    System.out.println(String.format("There are no rows with %s >= %s", dateColumnName, searchDateAsString));
}
else {
    // we want strictly greater than, so skip over the rows that are equal
    while (search_date.equals(cursor.getCurrentRowValue(dateColumn))) {
        if (!cursor.moveToNextRow()) break;
    }
    if (cursor.isAfterLast()) {
        System.out.println(String.format("There are no rows with %s > %s", dateColumnName, searchDateAsString));
    }
}
// now iterate over the remaining rows
while (!cursor.isAfterLast()) {
    System.out.println(sdf.format(cursor.getCurrentRowValue(dateColumn)));
    cursor.moveToNextRow();
}
于 2014-09-17T11:15:31.693 回答