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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
//
// DB2_db.cc
//
// DB2_db: Implements the Berkeley B-Tree database as a Database object
// (including duplicate values to allow duplicate word entries)
//
// Part of the ht://Dig package <https://htdig.sourceforge.net/>
// Copyright (c) 1999-2004 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later
// <http://www.gnu.org/copyleft/lgpl.html>
//
// $Id: DB2_db.cc,v 1.26 2004/05/28 13:15:20 lha Exp $
//
#ifdef HAVE_CONFIG_H
#include "htconfig.h"
#endif /* HAVE_CONFIG_H */
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#ifdef HAVE_STD
#include <fstream>
#ifdef HAVE_NAMESPACES
using namespace std;
#endif
#else
#include <fstream.h>
#endif /* HAVE_STD */
#ifndef _MSC_VER /* _WIN32 */
#include <unistd.h>
#endif
#include "DB2_db.h"
#include "HtConfiguration.h"
// Default cache size in kilobytes.
// Maybe this should be an config option, just for easy testing and
// determination for best system performance
// NOTE: page size is 1KB - do not change!!
#define CACHE_SIZE_IN_KB 64
//*****************************************************************************
// DB2_db::DB2_db()
//
DB2_db::DB2_db()
{
isOpen = 0;
_compare = 0;
_prefix = 0;
}
//*****************************************************************************
// DB2_db::~DB2_db()
//
DB2_db::~DB2_db()
{
Close();
}
//*****************************************************************************
//
int
DB2_db::Open(const char *filename, int flags, int mode)
{
//
// Initialize the database environment.
//
if((dbenv = db_init((char *)NULL)) == 0) return NOTOK;
if(CDB_db_create(&dbp, dbenv, 0) != 0) return NOTOK;
if(_compare) dbp->set_bt_compare(dbp, _compare);
if(_prefix) dbp->set_bt_prefix(dbp, _prefix);
//
// Open the database.
//
if((errno = dbp->open(dbp, filename, NULL, db_type, flags, mode)) == 0)
{
//
// Acquire a cursor for the database.
//
if ((seqrc = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0)
{
seqerr = seqrc;
Close();
return NOTOK;
}
isOpen = 1;
return OK;
}
else
{
return NOTOK;
}
}
//*****************************************************************************
// int DB2_db::Close()
//
int
DB2_db::Close()
{
if(isOpen)
{
//
// Close cursor, database and clean up environment
//
(void)(dbcp->c_close)(dbcp);
(void)(dbp->close)(dbp, 0);
(void)(dbenv->close(dbenv, 0));
dbenv = 0;
}
isOpen = 0;
return OK;
}
//*****************************************************************************
// char *DB2_db::Get_Next(String &item, String &key)
//
char *
DB2_db::Get_Next(String &item, String &key)
{
if (isOpen && !seqrc)
{
//
// Return values
//
key = skey;
lkey = skey;
item = data;
//
// Search for the next record
//
DBT local_key;
DBT local_data;
memset(&local_key, 0, sizeof(DBT));
memset(&local_data, 0, sizeof(DBT));
local_key.data = skey.get();
local_key.size = skey.length();
seqrc = dbcp->c_get(dbcp, &local_key, &local_data, DB_NEXT);
seqerr = seqrc;
if(!seqrc) {
data = 0;
data.append((char*)local_data.data, (int)local_data.size);
skey = 0;
skey.append((char*)local_key.data, (int)local_key.size);
}
return lkey.get();
}
else
return 0;
}
//*****************************************************************************
// void DB2_db::Start_Seq()
//
void
DB2_db::Start_Seq(const String& key)
{
DBT local_key;
DBT local_data;
memset(&local_key, 0, sizeof(DBT));
memset(&local_data, 0, sizeof(DBT));
skey = key;
local_key.data = skey.get();
local_key.size = skey.length();
if (isOpen && dbp)
{
//
// Okay, get the first key. Use DB_SET_RANGE for finding partial
// keys also. If you set it to DB_SET, and the words book, books
// and bookstore do exists, it will find them if you specify
// book*. However if you specify boo* if will not find
// anything. Setting to DB_SET_RANGE will still find the `first'
// word after boo* (which is book).
//
seqrc = dbcp->c_get(dbcp, &local_key, &local_data, DB_SET_RANGE);
seqerr = seqrc;
if(!seqrc) {
data = 0;
data.append((char*)local_data.data, (int)local_data.size);
skey = 0;
skey.append((char*)local_key.data, (int)local_key.size);
}
}
}
//*****************************************************************************
// void DB2_db::Start_Get()
//
void
DB2_db::Start_Get()
{
DBT local_key;
DBT local_data;
memset(&local_key, 0, sizeof(DBT));
memset(&local_data, 0, sizeof(DBT));
if (isOpen && dbp)
{
//
// Okay, get the first key. Use DB_SET_RANGE for finding partial
// keys also. If you set it to DB_SET, and the words book, books
// and bookstore do exists, it will find them if you specify
// book*. However if you specify boo* if will not find
// anything. Setting to DB_SET_RANGE will still find the `first'
// word after boo* (which is book).
//
seqrc = dbcp->c_get(dbcp, &local_key, &local_data, DB_FIRST);
seqerr = seqrc;
if(!seqrc) {
data = 0;
data.append((char*)local_data.data, (int)local_data.size);
skey = 0;
skey.append((char*)local_key.data, (int)local_key.size);
}
}
}
//*****************************************************************************
// int DB2_db::Put(const String &key, const String &data)
//
int
DB2_db::Put(const String &key, const String &data)
{
DBT k, d;
memset(&k, 0, sizeof(DBT));
memset(&d, 0, sizeof(DBT));
if (!isOpen)
return NOTOK;
k.data = (char*)key.get();
k.size = key.length();
d.data = (char*)data.get();
d.size = data.length();
//
// A 0 in the flags in put means replace, if you didn't specify DB_DUP
// somewhere else...
//
return (dbp->put)(dbp, NULL, &k, &d, 0) == 0 ? OK : NOTOK;
}
//*****************************************************************************
// int DB2_db::Get(const String &key, String &data)
//
int
DB2_db::Get(const String &key, String &data)
{
DBT k, d;
memset(&k, 0, sizeof(DBT));
memset(&d, 0, sizeof(DBT));
//
// k arg of get should be const but is not. Harmless cast.
//
k.data = (char*)key.get();
k.size = key.length();
int rc = dbp->get(dbp, NULL, &k, &d, 0);
if (rc)
return NOTOK;
data = 0;
data.append((char *)d.data, d.size);
return OK;
}
//*****************************************************************************
// int DB2_db::Exists(const String &key)
//
int
DB2_db::Exists(const String &key)
{
String data;
if (!isOpen)
return 0;
return Get(key, data);
}
//*****************************************************************************
// int DB2_db::Delete(const String &key)
//
int
DB2_db::Delete(const String &key)
{
DBT k;
memset(&k, 0, sizeof(DBT));
if (!isOpen)
return 0;
k.data = (char*)key.get();
k.size = key.length();
return (dbp->del)(dbp, NULL, &k, 0);
}
//*****************************************************************************
// DB2_db *DB2_db::getDatabaseInstance()
//
DB2_db *
DB2_db::getDatabaseInstance(DBTYPE)
{
return new DB2_db();
}
//*****************************************************************************
// void Error(const char *error_prefix, char *message);
//
void Error(const char *error_prefix, char *message)
{
// We don't do anything here, it's mostly a stub so we can set a breakpoint
// for debugging purposes
fprintf(stderr, "%s: %s\n", error_prefix, message);
}
//******************************************************************************
/*
* db_init --
* Initialize the environment. Only returns a pointer
*/
DB_ENV *
DB2_db::db_init(char *home)
{
DB_ENV *dbenv;
char *progname = "DB2 problem...";
int error;
if((error = CDB_db_env_create(&dbenv, 0)) != 0) {
fprintf(stderr, "DB2_db: CDB_db_env_create %s\n", CDB_db_strerror(error));
return 0;
}
dbenv->set_errpfx(dbenv, progname);
dbenv->set_errcall(dbenv, &Error);
if((error = dbenv->open(dbenv, (const char*)home, NULL, DB_CREATE | DB_PRIVATE | DB_INIT_LOCK | DB_INIT_MPOOL, 0666)) != 0) {
dbenv->err(dbenv, error, "open %s", (home ? home : ""));
return 0;
}
return (dbenv);
}
|