1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 - 2010 Toru Maesaka
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
char *BlitzCursor::first_key(int *key_len) {
24
if (!tcbdbcurfirst(cursor))
27
return (char *)tcbdbcurkey(cursor, key_len);
30
char *BlitzCursor::final_key(int *key_len) {
31
if (!tcbdbcurlast(cursor))
34
return (char *)tcbdbcurkey(cursor, key_len);
37
/* It's possible that the cursor had been implicitly moved
38
forward. If so, we obtain the key at the current position
39
and return that. This can happen in delete_key(). */
40
char *BlitzCursor::next_key(int *key_len) {
42
if (!tcbdbcurnext(cursor))
47
return (char *)tcbdbcurkey(cursor, key_len);
50
char *BlitzCursor::prev_key(int *key_len) {
51
if (!tcbdbcurprev(cursor))
54
return (char *)tcbdbcurkey(cursor, key_len);
57
char *BlitzCursor::next_logical_key(int *key_len) {
58
int origin_len, a_cmp_len, b_cmp_len;
61
/* Get the key to scan away from. */
62
if ((origin = (char *)tcbdbcurkey(cursor, &origin_len)) == NULL)
67
/* Traverse the tree from the cursor position until we hit
68
a key greater than the origin or EOF. */
70
/* Move the cursor and get the next key. */
71
if (!tcbdbcurnext(cursor))
74
rv = (char *)tcbdbcurkey(cursor, key_len);
76
/* Compare the fetched key with origin. */
77
if (packed_key_cmp(this->tree, rv, origin, &a_cmp_len, &b_cmp_len) > 0)
87
char *BlitzCursor::prev_logical_key(int *key_len) {
88
int origin_len, a_cmp_len, b_cmp_len;
91
/* Get the key to scan away from. */
92
if ((origin = (char *)tcbdbcurkey(cursor, &origin_len)) == NULL)
98
if (!tcbdbcurprev(cursor))
101
rv = (char *)tcbdbcurkey(cursor, key_len);
103
/* Compare the fetched key with origin. */
104
if (packed_key_cmp(this->tree, rv, origin, &a_cmp_len, &b_cmp_len) < 0)
114
/* A cursor based lookup on a B+Tree doesn't guarantee that the
115
returned key is identical. This is because it would return the
116
next logical key if one exists. */
117
char *BlitzCursor::find_key(const int search_mode, const char *key,
118
const int klen, int *rv_len) {
120
if (!tcbdbcurjump(cursor, (void *)key, klen))
123
char *rv = (char *)tcbdbcurkey(cursor, rv_len);
128
int cmp, a_cmp_len, b_cmp_len;
129
cmp = packed_key_cmp(this->tree, rv, key, &a_cmp_len, &b_cmp_len);
131
switch (search_mode) {
132
case drizzled::HA_READ_KEY_EXACT:
138
case drizzled::HA_READ_AFTER_KEY:
141
} else if (cmp == 0) {
143
rv = this->next_logical_key(rv_len);
149
case drizzled::HA_READ_BEFORE_KEY:
151
rv = this->prev_logical_key(rv_len);
153
//case drizzled::HA_READ_KEY_OR_NEXT:
154
//case drizzled::HA_READ_KEY_OR_PREV:
155
//case drizzled::HA_READ_PREFIX:
156
//case drizzled::HA_READ_PREFIX_LAST:
157
//case drizzled::HA_READ_PREFIX_LAST_OR_PREV:
165
int BlitzCursor::delete_position(void) {
166
if (!tcbdbcurout(cursor))