~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/blitzdb/blitzindex.cc

  • Committer: Monty Taylor
  • Date: 2010-06-18 17:03:01 UTC
  • mfrom: (1239.3.124 merger)
  • Revision ID: mordred@inaugust.com-20100618170301-2rr20efiqgi1zdme
Merged in BlitzDB.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2009 - 2010 Toru Maesaka
 
5
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 */
 
19
 
 
20
#include <config.h>
 
21
#include "ha_blitz.h"
 
22
 
 
23
/* Unlike the data dictionary, don't tune the btree by default
 
24
   since the default configuration satisfies BlitzDB's default
 
25
   performance requirements. Tuning parameters will be made dynamic
 
26
   in the upcoming releases. */
 
27
int BlitzTree::open(const char *path, const int key_num, int mode) {
 
28
  char buf[FN_REFLEN];
 
29
 
 
30
  if ((btree = tcbdbnew()) == NULL)
 
31
    return HA_ERR_OUT_OF_MEM;
 
32
 
 
33
  if (!tcbdbsetmutex(btree)) {
 
34
    tcbdbdel(btree);
 
35
    return HA_ERR_CRASHED_ON_USAGE;
 
36
  }
 
37
 
 
38
  if (!tcbdbsetcmpfunc(btree, blitz_keycmp_cb, this)) {
 
39
    tcbdbdel(btree);
 
40
    return HA_ERR_CRASHED_ON_USAGE;
 
41
  }
 
42
 
 
43
  snprintf(buf, FN_REFLEN, "%s_%02d%s", path, key_num, BLITZ_INDEX_EXT);
 
44
 
 
45
  if (!tcbdbopen(btree, buf, mode)) {
 
46
    tcbdbdel(btree);
 
47
    return HA_ERR_CRASHED_ON_USAGE;
 
48
  }
 
49
 
 
50
  return 0;
 
51
}
 
52
 
 
53
/* Similar to UNIX touch(1) but generates a TCBDB file. */
 
54
int BlitzTree::create(const char *path, const int key_num) {
 
55
  int rv;
 
56
 
 
57
  if ((rv = this->open(path, key_num, (BDBOWRITER | BDBOCREAT))) != 0)
 
58
    return rv;
 
59
 
 
60
  if ((rv = this->close()) != 0)
 
61
    return rv;
 
62
 
 
63
  return rv;
 
64
}
 
65
 
 
66
int BlitzTree::drop(const char *path, const int key_num) {
 
67
  char buf[FN_REFLEN];
 
68
  snprintf(buf, FN_REFLEN, "%s_%02d%s", path, key_num, BLITZ_INDEX_EXT);
 
69
  return unlink(buf);
 
70
}
 
71
 
 
72
int BlitzTree::rename(const char *from, const char *to, const int key_num) {
 
73
  char from_buf[FN_REFLEN];
 
74
  char to_buf[FN_REFLEN];
 
75
 
 
76
  snprintf(from_buf, FN_REFLEN, "%s_%02d%s", from, key_num, BLITZ_INDEX_EXT);
 
77
  snprintf(to_buf, FN_REFLEN, "%s_%02d%s", to, key_num, BLITZ_INDEX_EXT);
 
78
 
 
79
  return drizzled::internal::my_rename(from_buf, to_buf, MYF(0));
 
80
}
 
81
 
 
82
int BlitzTree::close(void) {
 
83
  assert(btree);
 
84
 
 
85
  if (!tcbdbclose(btree)) {
 
86
    tcbdbdel(btree);
 
87
    return HA_ERR_CRASHED_ON_USAGE;
 
88
  }
 
89
 
 
90
  tcbdbdel(btree);
 
91
  return 0;
 
92
}
 
93
 
 
94
bool BlitzTree::create_cursor(BlitzCursor *bc) {
 
95
  if (bc == NULL)
 
96
    return false;
 
97
 
 
98
  if ((bc->cursor = tcbdbcurnew(btree)) == NULL)
 
99
    return false;
 
100
 
 
101
  bc->tree = this;
 
102
  bc->active = true;
 
103
  bc->moved = false;
 
104
  return true;
 
105
}
 
106
 
 
107
void BlitzTree::destroy_cursor(BlitzCursor *bc) {
 
108
  if (bc == NULL)
 
109
    return;
 
110
 
 
111
  tcbdbcurdel(bc->cursor);
 
112
  return;
 
113
}
 
114
 
 
115
int BlitzTree::write(const char *key, const size_t klen) {
 
116
  return (tcbdbputdup(btree, key, klen, "", 0)) ? 0 : -1;
 
117
}
 
118
 
 
119
int BlitzTree::write_unique(const char *key, const size_t klen) {
 
120
  if (!tcbdbputkeep(btree, key, klen, "", 0)) {
 
121
    if (tcbdbecode(btree) == TCEKEEP) {
 
122
      errno = HA_ERR_FOUND_DUPP_KEY;
 
123
      return HA_ERR_FOUND_DUPP_KEY;
 
124
    }
 
125
  }
 
126
  return 0;
 
127
}
 
128
 
 
129
int BlitzTree::delete_key(const char *key, const int klen) {
 
130
  return (tcbdbout(btree, key, klen)) ? 0 : -1;
 
131
}
 
132
 
 
133
int BlitzTree::delete_all(void) {
 
134
  return (tcbdbvanish(btree)) ? 0 : -1;
 
135
}
 
136
 
 
137
uint64_t BlitzTree::records(void) {
 
138
  return tcbdbrnum(btree);
 
139
}