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
/* 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) {
30
if ((btree = tcbdbnew()) == NULL)
31
return HA_ERR_OUT_OF_MEM;
33
if (!tcbdbsetmutex(btree)) {
35
return HA_ERR_CRASHED_ON_USAGE;
38
if (!tcbdbsetcmpfunc(btree, blitz_keycmp_cb, this)) {
40
return HA_ERR_CRASHED_ON_USAGE;
43
snprintf(buf, FN_REFLEN, "%s_%02d%s", path, key_num, BLITZ_INDEX_EXT);
45
if (!tcbdbopen(btree, buf, mode)) {
47
return HA_ERR_CRASHED_ON_USAGE;
53
/* Similar to UNIX touch(1) but generates a TCBDB file. */
54
int BlitzTree::create(const char *path, const int key_num) {
57
if ((rv = this->open(path, key_num, (BDBOWRITER | BDBOCREAT))) != 0)
60
if ((rv = this->close()) != 0)
66
int BlitzTree::drop(const char *path, const int key_num) {
68
snprintf(buf, FN_REFLEN, "%s_%02d%s", path, key_num, BLITZ_INDEX_EXT);
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];
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);
79
return drizzled::internal::my_rename(from_buf, to_buf, MYF(0));
82
int BlitzTree::close(void) {
85
if (!tcbdbclose(btree)) {
87
return HA_ERR_CRASHED_ON_USAGE;
94
bool BlitzTree::create_cursor(BlitzCursor *bc) {
98
if ((bc->cursor = tcbdbcurnew(btree)) == NULL)
107
void BlitzTree::destroy_cursor(BlitzCursor *bc) {
111
tcbdbcurdel(bc->cursor);
115
int BlitzTree::write(const char *key, const size_t klen) {
116
return (tcbdbputdup(btree, key, klen, "", 0)) ? 0 : -1;
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;
129
int BlitzTree::delete_key(const char *key, const int klen) {
130
return (tcbdbout(btree, key, klen)) ? 0 : -1;
133
int BlitzTree::delete_all(void) {
134
return (tcbdbvanish(btree)) ? 0 : -1;
137
uint64_t BlitzTree::records(void) {
138
return tcbdbrnum(btree);