~drizzle-trunk/drizzle/development

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
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2009 - 2010 Toru Maesaka
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"
#include "ha_blitz.h"

char *BlitzCursor::first_key(int *key_len) {
  if (!tcbdbcurfirst(cursor))
    return NULL;

  return (char *)tcbdbcurkey(cursor, key_len);
}

char *BlitzCursor::final_key(int *key_len) {
  if (!tcbdbcurlast(cursor))
    return NULL;

  return (char *)tcbdbcurkey(cursor, key_len);
}

/* It's possible that the cursor had been implicitly moved
   forward. If so, we obtain the key at the current position
   and return that. This can happen in delete_key(). */
char *BlitzCursor::next_key(int *key_len) {
  if (!moved) {
    if (!tcbdbcurnext(cursor))
      return NULL;
  } else
    moved = false;

  return (char *)tcbdbcurkey(cursor, key_len);
}

char *BlitzCursor::prev_key(int *key_len) {
  if (!tcbdbcurprev(cursor))
    return NULL;

  return (char *)tcbdbcurkey(cursor, key_len);
}

char *BlitzCursor::next_logical_key(int *key_len) {
  int origin_len, a_cmp_len, b_cmp_len;
  char *rv, *origin;

  /* Get the key to scan away from. */
  if ((origin = (char *)tcbdbcurkey(cursor, &origin_len)) == NULL)
    return NULL;

  rv = NULL;

  /* Traverse the tree from the cursor position until we hit
     a key greater than the origin or EOF. */
  while (1) {
    /* Move the cursor and get the next key. */
    if (!tcbdbcurnext(cursor))
      break;
  
    rv = (char *)tcbdbcurkey(cursor, key_len);

    /* Compare the fetched key with origin. */
    if (packed_key_cmp(this->tree, rv, origin, &a_cmp_len, &b_cmp_len) > 0)
      break;

    free(rv);
  }

  free(origin);
  return rv;
}

char *BlitzCursor::prev_logical_key(int *key_len) {
  int origin_len, a_cmp_len, b_cmp_len;
  char *rv, *origin;

  /* Get the key to scan away from. */
  if ((origin = (char *)tcbdbcurkey(cursor, &origin_len)) == NULL)
    return NULL;

  rv = NULL;

  while (1) {
    if (!tcbdbcurprev(cursor))
      break;
  
    rv = (char *)tcbdbcurkey(cursor, key_len);

    /* Compare the fetched key with origin. */
    if (packed_key_cmp(this->tree, rv, origin, &a_cmp_len, &b_cmp_len) < 0)
      break;

    free(rv);
  }

  free(origin);
  return rv;
}

/* A cursor based lookup on a B+Tree doesn't guarantee that the
   returned key is identical. This is because it would return the
   next logical key if one exists. */
char *BlitzCursor::find_key(const int search_mode, const char *key,
                            const int klen, int *rv_len) {

  if (!tcbdbcurjump(cursor, (void *)key, klen))
    return NULL;

  char *rv = (char *)tcbdbcurkey(cursor, rv_len);

  if (rv == NULL)
    return NULL;

  int cmp, a_cmp_len, b_cmp_len;
  cmp = packed_key_cmp(this->tree, rv, key, &a_cmp_len, &b_cmp_len);

  switch (search_mode) {
  case drizzled::HA_READ_KEY_EXACT:
    if (cmp != 0) {
      free(rv);
      rv = NULL;
    }
    break;
  case drizzled::HA_READ_AFTER_KEY:
    if (cmp > 0) {
      break;
    } else if (cmp == 0) {
      free(rv);
      rv = this->next_logical_key(rv_len);
    } else {
      free(rv);
      rv = NULL;
    }
    break;
  case drizzled::HA_READ_BEFORE_KEY:
    free(rv);
    rv = this->prev_logical_key(rv_len);
    break;
  //case drizzled::HA_READ_KEY_OR_NEXT:
  //case drizzled::HA_READ_KEY_OR_PREV:
  //case drizzled::HA_READ_PREFIX:
  //case drizzled::HA_READ_PREFIX_LAST:
  //case drizzled::HA_READ_PREFIX_LAST_OR_PREV:
  default:
     break;
  }

  return rv;
}

int BlitzCursor::delete_position(void) {
  if (!tcbdbcurout(cursor))
    return -1;

  moved = true;
  return 0;
}