Class: Bdb::Db

Inherits:
Object show all
Defined in:
ext/bdb.c

Defined Under Namespace

Classes: Cursor, Stat

Constant Summary collapse

BTREE =
INT2FIX((DBTYPE)(DB_BTREE))
HASH =
INT2FIX((DBTYPE)(DB_HASH))
RECNO =
INT2FIX((DBTYPE)(DB_RECNO))
QUEUE =
INT2FIX((DBTYPE)(DB_QUEUE))
UNKNOWN =
INT2FIX((DBTYPE)(DB_UNKNOWN))

Instance Method Summary collapse

Constructor Details

#initializeObject


225
226
227
228
# File 'ext/bdb.c', line 225

VALUE db_initialize(VALUE obj)
{
  return db_init_aux(obj,NULL);
}

Instance Method Details

#[]Object

db -> data

array ref style data retrieval


773
774
775
776
# File 'ext/bdb.c', line 773

VALUE db_aget(VALUE obj, VALUE vkey)
{
  return db_get(obj,Qnil,vkey,Qnil,Qnil);
}

#[]=Object

db=data

array ref style data storage


784
785
786
787
# File 'ext/bdb.c', line 784

VALUE db_aset(VALUE obj, VALUE vkey, VALUE vdata)
{
  return db_put(obj,Qnil,vkey,vdata,Qnil);
}

#associateObject

db.associate(txn,sec_db,flags,proc)

associate a secondary index(database) with this (primary) database. The proc can be nil if the database is only opened DB_RDONLY.

call back proc has signature: proc(secdb,key,value)


1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
# File 'ext/bdb.c', line 1270

VALUE db_associate(VALUE obj, VALUE vtxn, VALUE osecdb,
		   VALUE vflags, VALUE cb_proc)
{
  t_dbh *sdbh,*pdbh;
  int rv;
  u_int32_t flags,flagsp,flagss;
  //int fdp;
  t_txnh *txn=NOTXN;

  flags=NUM2UINT(vflags);
  Data_Get_Struct(obj,t_dbh,pdbh);
  if (!pdbh->db)
    raise(0, "db is closed");
  Data_Get_Struct(osecdb,t_dbh,sdbh);
  if (!sdbh->db)
    raise(0, "sdb is closed");

  if ( ! NIL_P(vtxn) ) {
    Data_Get_Struct(vtxn,t_txnh,txn);
    if (!txn->txn)
        raise(0, "txn is closed");
  }

  if ( cb_proc == Qnil ) {
    rb_warning("db_associate: no association may be applied");
    pdbh->db->get_open_flags(pdbh->db,&flagsp);
    sdbh->db->get_open_flags(sdbh->db,&flagss);
    if ( flagsp & DB_RDONLY & flagss ) {
      rv=pdbh->db->associate(pdbh->db,txn?txn->txn:NULL,
			     sdbh->db,NULL,flags);
      if (rv)
	raise_error(rv,"db_associate: %s",db_strerror(rv));
      return Qtrue;
    } else {
      raise_error(0,"db_associate empty associate only available when both DBs opened with DB_RDONLY");
    }
  } else if ( rb_obj_is_instance_of(cb_proc,rb_cProc) != Qtrue ) {
    raise_error(0, "db_associate proc required");
  }
  
  sdbh->aproc=cb_proc;

  rv=pdbh->db->associate(pdbh->db,txn?txn->txn:NULL,sdbh->db,assoc_callback,flags);
#ifdef DEBUG_DB
  fprintf(stderr,"file is %d\n",fdp);
  fprintf(stderr,"assoc done 0x%x\n",sdbh);
#endif
  if (rv != 0) {
    raise_error(rv, "db_associate failure: %s",db_strerror(rv));
  }
  return Qtrue;
}

#btree_compare=Object

db.btree_compare = proc

set the btree key comparison function to the callback proc.

callback proc has signature: proc(db,key1,key2)


1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
# File 'ext/bdb.c', line 1363

VALUE db_btree_compare_set(VALUE obj, VALUE cb_proc)
{
  t_dbh *dbh;
  int rv;

  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise(0, "db is closed");

  if ( rb_obj_is_instance_of(cb_proc,rb_cProc) != Qtrue ) {
    raise_error(0, "db_associate proc required");
  }
  
  dbh->sproc=cb_proc;
  rv=dbh->db->set_bt_compare(dbh->db,bt_compare_callback);

#ifdef DEBUG_DB
  fprintf(stderr,"btree_compare set 0x%x\n",dbh);
#endif
  if (rv != 0) {
    raise_error(rv, "db_btree_compare_set failure: %s",db_strerror(rv));
  }
  return Qtrue;
}

#closeObject

db.close(flags) -> value

close a database handle. Will close open cursors.


543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'ext/bdb.c', line 543

VALUE db_close(VALUE obj, VALUE vflags)
{
  t_dbh *dbh;
  int rv;
  u_int32_t flags;
  VALUE cur;

  flags=NUM2UINT(vflags);
  Data_Get_Struct(obj,t_dbh,dbh);
  if ( dbh->db==NULL )
    return Qnil;

  if (! NIL_P(dbh->adbc) && RARRAY_LEN(dbh->adbc) > 0 ) {
    rb_warning("%s/%d %s",__FILE__,__LINE__,
	       "cursor handles still open");
    while ( (cur=rb_ary_pop(dbh->adbc)) != Qnil ) {
      dbc_close(cur);
    }
  }

  if ( RTEST(ruby_debug) )
    rb_warning("%s/%d %s 0x%p %s",__FILE__,__LINE__,"db_close!", (void*)dbh,
	       (dbh->filename==NULL||*(dbh->filename)=='0') ? (char*)"unknown" : dbh->filename);

  rv = dbh->db->close(dbh->db,flags);
  dbh->db=NULL;
  dbh->aproc=Qnil;
  dbh->sproc=Qnil;
  if ( dbh->env ) {
  	if ( RTEST(ruby_debug) )
    	rb_warning("%s/%d %s 0x%p",__FILE__,__LINE__,"db_close! removing",(void*)obj);
    rb_ary_delete(dbh->env->adb,obj);
    dbh->env = NULL;
  }
  if ( rv != 0 ) {
    raise_error(rv, "db_close failure: %s",db_strerror(rv));
  }
	dbh->db_opened = 0;
  return obj;
}

#compactObject

db.compact(txn,start_key,stop_key,compact_opts,flags) -> end_key

compact the database (4.4 an up). start and stop keys limit the range of compaction in BTREE types. compact_opts is currently ignored. Call returns the last key compacted (could be fed into a subsequent call as the start_key).


841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
# File 'ext/bdb.c', line 841

VALUE db_compact(VALUE obj, VALUE vtxn, VALUE vstart_key,
		 VALUE vstop_key, VALUE db_compact,
		 VALUE vflags)
{
  t_dbh *dbh;
  u_int32_t flags;
  t_txnh *txn=NULL;
  DBT start_key, stop_key, end_key;
  int rv;

  flags=NUM2UINT(vflags);
  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise(0, "db is closed");

  memset(&start_key,0,sizeof(DBT));
  memset(&stop_key,0,sizeof(DBT));
  memset(&end_key,0,sizeof(DBT));

  if ( ! NIL_P(vstart_key) ) {
    StringValue(vstart_key);
    start_key.data=RSTRING_PTR(vstart_key);
    start_key.size=RSTRING_LEN(vstart_key);
    start_key.flags= LMEMFLAG;
  }
  if ( ! NIL_P(vstop_key) ) {
    StringValue(vstop_key);
    stop_key.data=RSTRING_PTR(vstop_key);
    stop_key.size=RSTRING_LEN(vstop_key);
    stop_key.flags= LMEMFLAG;
  }
  if ( ! NIL_P(vtxn) ) {
    Data_Get_Struct(vtxn,t_txnh,txn);
    if (!txn->txn)
        raise(0, "txn is closed");
  }

  rv=dbh->db->compact(dbh->db,txn?txn->txn:NULL,
		      &start_key,
		      &stop_key,
		      NULL,
		      flags,
		      &end_key);
  if (rv)
    raise_error(rv,"db_compact failure: %s",db_strerror(rv));

  return rb_str_new(end_key.data,end_key.size);
  
}

#cursorObject

db.cursor(txn,flags)

open a cursor


1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
# File 'ext/bdb.c', line 1394

VALUE db_cursor(VALUE obj, VALUE vtxn, VALUE vflags)
{
  t_dbh *dbh;
  int rv;
  u_int32_t flags;
  //DBC *dbc;
  t_txnh *txn=NOTXN;
  VALUE c_obj;
  t_dbch *dbch;

  flags=NUM2UINT(vflags);
  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise(0, "db is closed");

  c_obj=Data_Make_Struct(cCursor, t_dbch, dbc_mark, dbc_free, dbch);

  if ( ! NIL_P(vtxn) ) {
    Data_Get_Struct(vtxn,t_txnh,txn);
    if (!txn->txn)
        raise(0, "txn is closed");
  }

  rv=dbh->db->cursor(dbh->db,txn?txn->txn:NULL,&(dbch->dbc),flags);
  if (rv)
    raise_error(rv,"db_cursor: %s",db_strerror(rv));

  filename_dup(dbch->filename,dbh->filename);
  dbch->db=dbh;
  rb_ary_push(dbch->db->adbc,c_obj);
  rb_obj_call_init(c_obj,0,NULL);
  return c_obj;
}

#delObject

db.del(txn,key,flags) -> true/nil

delete records with the key given. DUP values will removed as a group. true if deleted extant records. NIL if the operation resulted in DB indicating DB_NOTFOUND.


1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
# File 'ext/bdb.c', line 1141

VALUE db_del(VALUE obj, VALUE vtxn, VALUE vkey, VALUE vflags)
{
  t_dbh *dbh;
  int rv;
  u_int32_t flags;
  DBT key;
  //VALUE str;
  t_txnh *txn=NULL;

  memset(&key,0,sizeof(DBT));

  if ( ! NIL_P(vtxn) ) {
    Data_Get_Struct(vtxn,t_txnh,txn);
    if (!txn->txn)
        raise(0, "txn is closed");
  }

  flags=NUM2UINT(vflags);
  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise(0, "db is closed");

  StringValue(vkey);
  key.data = RSTRING_PTR(vkey);
  key.size = RSTRING_LEN(vkey);
  key.flags = LMEMFLAG;

  rv = dbh->db->del(dbh->db,txn?txn->txn:NULL,&key,flags);
  if ( rv == DB_NOTFOUND ) {
    return Qnil;
  } else if (rv != 0) {
    raise_error(rv, "db_del failure: %s",db_strerror(rv));
  }
  return Qtrue;
}

#set_encrypt(db, passwd) ⇒ Object

Set encrypt


1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
# File 'ext/bdb.c', line 1067

VALUE db_set_encrypt(VALUE obj, VALUE vpasswd) {
    int rv;
    t_dbh *dbh;
    const char *passwd;
 
    passwd = StringValueCStr(vpasswd);
    Data_Get_Struct(obj,t_dbh,dbh);
    u_int32_t flags=0x00000001; //DB_ENCRYPT_AES
       
    rv = dbh->db->set_encrypt(dbh->db, passwd, flags);
    
    if ( rv != 0 )
        raise_error(rv, "set_encrypt failure: %s",db_strerror(rv));
       return vpasswd;
}

#flagsObject

db.flags -> value

get database flags. see www.sleepycat.com/docs/api_c/db_get_flags.html


370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'ext/bdb.c', line 370

VALUE db_flags_get(VALUE obj)
{
  t_dbh *dbh;
  int rv;
  u_int32_t flags;

  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise_error(0,"db is closed");

  rv = dbh->db->get_flags(dbh->db,&flags);
  if ( rv != 0 ) {
    raise_error(rv, "db_flag_get failure: %s",db_strerror(rv));
  }
  return INT2NUM(flags);
}

#flags=Object

db.flags=value

set database flags based on DB constants. see www.sleepycat.com/docs/api_c/db_set_flags.html


344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'ext/bdb.c', line 344

VALUE db_flags_set(VALUE obj, VALUE vflags)
{
  t_dbh *dbh;
  int rv;
  u_int32_t flags;

  flags=NUM2UINT(vflags);
  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise_error(0,"db is closed");

  rv = dbh->db->set_flags(dbh->db,flags);
  if ( rv != 0 ) {
    raise_error(rv, "db_flag_set failure: %s",db_strerror(rv));
  }
  return vflags;
}

#getObject

db.get(txn,key,data,flags) -> String(data)

get a key/data pair from database. data as a string.


651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
# File 'ext/bdb.c', line 651

VALUE db_get(VALUE obj, VALUE vtxn, VALUE vkey, VALUE vdata, VALUE vflags)
{
  t_dbh *dbh;
  int rv;
  u_int32_t flags=0;
  DBT key,data;
  VALUE str;
  t_txnh *txn=NULL;

  memset(&key,0,sizeof(DBT));
  memset(&data,0,sizeof(DBT));

  if ( ! NIL_P(vtxn) ) {
    Data_Get_Struct(vtxn,t_txnh,txn);
    if (!txn->txn)
        raise(0, "txn is closed");
  }

  if ( ! NIL_P(vflags) ) {
		flags=NUM2UINT(vflags);
	}
  
  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise_error(0,"db is closed");

  StringValue(vkey);

  key.data  = RSTRING_PTR(vkey);
  key.size  = RSTRING_LEN(vkey);
  key.flags = LMEMFLAG;  

  if ( ! NIL_P(vdata) ) {
    StringValue(vdata);
    data.data = RSTRING_PTR(vdata);
    data.size = RSTRING_LEN(vdata);
  }
  data.flags = DB_DBT_MALLOC;

  rv = dbh->db->get(dbh->db,txn?txn->txn:NULL,&key,&data,flags);
  if ( rv == 0 ) {
    str = rb_str_new(data.data,data.size);
    if (data.data) free(data.data);
    return str;
  } else if (rv == DB_NOTFOUND) {
    return Qnil;
  } else {
    raise_error(rv, "db_get failure: %s",db_strerror(rv));
  }
  return Qnil;
}

#get_byteswappedObject

db.get_byteswapped -> true/false

true if database is running in swapped mode. This happens when the db is created on a machine with a different endian.


899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
# File 'ext/bdb.c', line 899

VALUE db_get_byteswapped(VALUE obj)
{
  t_dbh *dbh;
  int rv;
  int is_swapped;

  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise(0, "db is closed");
  rv=dbh->db->get_byteswapped(dbh->db,&is_swapped);
  if (rv)
    raise_error(rv,"db_get_byteswapped failed: %s",db_strerror(rv));
  if (is_swapped)
    return Qtrue;
  else
    return Qfalse;
}

#get_typeObject

db.get_type -> Fixnum(type)

an integer indicating the type of db (BTREE, HASH, etc).


923
924
925
926
927
928
929
930
931
932
933
934
935
936
# File 'ext/bdb.c', line 923

VALUE db_get_type(VALUE obj)
{
  t_dbh *dbh;
  int rv;
  DBTYPE dbtype;

  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise(0, "db is closed");
  rv=dbh->db->get_type(dbh->db,&dbtype);
  if (rv)
    raise_error(rv,"db_get_type failed: %s",db_strerror(rv));
  return INT2FIX(dbtype);
}

#h_ffactorObject

db.h_ffactor

get hash db fill factor formula:

(pagesize - 32) / (average_key_size + average_data_size + 8)

see www.sleepycat.com/docs/api_c/db_set_flags.html


472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
# File 'ext/bdb.c', line 472

VALUE db_h_ffactor(VALUE obj)
{
  t_dbh *dbh;
  int rv;
  u_int32_t cint;

  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise_error(0,"db is closed");
  rv = dbh->db->get_h_ffactor(dbh->db,&cint);
  if ( rv != 0 ) {
    raise_error(rv, "db_h_ffactor failure: %s",db_strerror(rv));
  }
  return INT2NUM(cint);
}

#h_ffactor=Object

db.h_ffactor=value

get hash db fill factor formula:

(pagesize - 32) / (average_key_size + average_data_size + 8)

445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'ext/bdb.c', line 445

VALUE db_h_ffactor_set(VALUE obj, VALUE vint)
{
  t_dbh *dbh;
  int rv;
  u_int32_t cint;

  cint=NUM2INT(vint);
  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise_error(0,"db is closed");
  rv = dbh->db->set_h_ffactor(dbh->db,cint);
  if ( rv != 0 ) {
    raise_error(rv, "db_h_ffactor_set failure: %s",db_strerror(rv));
  }
  return vint;
}

#h_nelemObject

db.h_nelem

get estimate number of element in the hash table see www.sleepycat.com/docs/api_c/db_set_flags.html


520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'ext/bdb.c', line 520

VALUE db_h_nelem(VALUE obj)
{
  t_dbh *dbh;
  int rv;
  u_int32_t nelem;

  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise_error(0,"db is closed");
  rv = dbh->db->get_h_nelem(dbh->db,&nelem);
  if ( rv != 0 ) {
    raise_error(rv, "db_h_nelem failure: %s",db_strerror(rv));
  }
  return INT2NUM(nelem);
}

#h_nelem=Object

db.h_nelem=value

set estimate number of elements in hash table see www.sleepycat.com/docs/api_c/db_set_flags.html


495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'ext/bdb.c', line 495

VALUE db_h_nelem_set(VALUE obj, VALUE vint)
{
  t_dbh *dbh;
  int rv;
  u_int32_t cint;

  cint=NUM2INT(vint);
  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise_error(0,"db is closed");
  rv = dbh->db->set_h_nelem(dbh->db,cint);
  if ( rv != 0 ) {
    raise_error(rv, "db_h_nelem_set failure: %s",db_strerror(rv));
  }
  return vint;
}

#joinObject

db.join(,flags) -> join_cursor

create a join cursor from an array of cursors. The input cursors will usually be set_range and, if duplicate data items are allowed the should be DUP_SORT or the performance will be abysmal.


798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
# File 'ext/bdb.c', line 798

VALUE db_join(VALUE obj, VALUE vacurs, VALUE vflags)
{
  t_dbh *dbh;
  t_dbch *dbch;
  u_int32_t flags;
  DBC **curs;
  int i,rv;
  VALUE jcurs;

  flags=NUM2UINT(vflags);
  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise(0, "db is closed");

  curs = ALLOCA_N(DBC *,RARRAY_LEN(vacurs));
  for (i=0; i<RARRAY_LEN(vacurs); i++) {
    Data_Get_Struct(RARRAY_PTR(vacurs)[i],t_dbch,dbch);
    /* cursor is closed? */
    curs[i]=dbch->dbc;
  }
  curs[i]=NULL;
  jcurs=Data_Make_Struct(cCursor,t_dbch,dbc_mark,dbc_free,dbch);
  rv =  dbh->db->join(dbh->db,curs,&(dbch->dbc),flags);
  if (rv) {
    raise_error(rv, "db_join: %s",db_strerror(rv));
  }
  dbch->db=dbh;
  rb_ary_push(dbch->db->adbc,jcurs);
  rb_obj_call_init(jcurs,0,NULL);
  return jcurs;
}

#key_rangeObject

db.key_range(txn,vkey,flags) -> [#less,#same,#greater]

calculate position of key within database. returns the counts of keys less, same and greater than the given key as an array of Fixnum-s


1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
# File 'ext/bdb.c', line 1091

VALUE db_key_range(VALUE obj, VALUE vtxn, VALUE vkey, VALUE vflags)
{
  t_dbh *dbh;
  t_txnh *txn=NULL;
  DBT key;
  u_int32_t flags = 0;
  int rv;
  DB_KEY_RANGE key_range;
  VALUE result;

  if ( ! NIL_P(vtxn) ) {
    Data_Get_Struct(vtxn,t_txnh,txn);
    if (!txn->txn)
        raise(0, "txn is closed");
  }

  if ( ! NIL_P(vflags) )
    flags=NUM2UINT(vflags);

  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise(0, "db is closed");

  memset(&key,0,sizeof(DBT));
  StringValue(vkey);
  key.data = RSTRING_PTR(vkey);
  key.size = RSTRING_LEN(vkey);
  key.flags = LMEMFLAG;

  rv=dbh->db->key_range(dbh->db,txn?txn->txn:NULL,&key,
			&key_range,flags);
  if (rv)
    raise_error(rv,"db_key_range: %s",db_strerror(rv));

  result=rb_ary_new3(3,
		     rb_float_new(key_range.less),
		     rb_float_new(key_range.equal),
		     rb_float_new(key_range.greater));
  return result;
}

#openObject

db.open(txn_object,disk_file,logical_db,db_type,flags,mode) -> value

open a database. disk file is file path. logical_db is a named database within that file, which will be created under conditions noted by DB.

db_type is one of the constants: Bdb::DB::BTREE Bdb::DB::HASH Bdb::DB::RECNO Bdb::DB::QUEUE Bdb::DB::UNKNOWN

unknown will open an already existing db in the mode created


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
# File 'ext/bdb.c', line 247

VALUE db_open(VALUE obj, VALUE vtxn, VALUE vdisk_file,
	      VALUE vlogical_db,
	      VALUE vdbtype, VALUE vflags, VALUE vmode)
{
  t_dbh *dbh;
  int rv;
  t_txnh *txn=NOTXN;
  u_int32_t flags=0;
  DBTYPE dbtype=DB_UNKNOWN;
  char *logical_db=NULL;
  //long len;
  int mode=0;

  if ( ! NIL_P(vflags) )
    flags=NUM2UINT(vflags);

  if ( ! NIL_P(vtxn) ) {
    Data_Get_Struct(vtxn,t_txnh,txn);
    if (!txn->txn)
        raise(0, "txn is closed");
  }

  if ( TYPE(vlogical_db)==T_STRING && RSTRING_LEN(vlogical_db) > 0 )
    logical_db=StringValueCStr(vlogical_db);
    
  if ( FIXNUM_P(vdbtype) ) {
    dbtype=NUM2INT(vdbtype);
    if ( dbtype < DB_BTREE || dbtype > DB_UNKNOWN ) {
      raise_error(0,"db_open Bad access type: %d",dbtype);
      return Qnil;
    }
  }

  if ( TYPE(vdisk_file)!=T_STRING || RSTRING_LEN(vdisk_file) < 1 ) {
    raise_error(0,"db_open Bad disk file name");
    return Qnil;
  }

  if ( ! NIL_P(vmode) )
    mode=NUM2INT(vmode);

  Data_Get_Struct(obj,t_dbh,dbh);
  if ( ! NIL_P(dbh->adbc) )
    raise_error(0,"db handle already opened");
  
  dbh->db->app_private=dbh;
  rv = dbh->db->open(dbh->db,txn?txn->txn:NULL,
                     StringValueCStr(vdisk_file),
                     logical_db,
                     dbtype,flags,mode);
  if (rv != 0) {
    raise_error(rv,"db_open failure: %s(%d)",db_strerror(rv),rv);
  }
  filename_copy(dbh->filename,vdisk_file)
  dbh->adbc=rb_ary_new();
	dbh->db_opened = 1;
  return obj;
}

#pagesizeObject

db.pagesize

set database flags based on DB constants. see www.sleepycat.com/docs/api_c/db_set_flags.html


420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'ext/bdb.c', line 420

VALUE db_pagesize(VALUE obj)
{
  t_dbh *dbh;
  int rv;
  u_int32_t pagesize;

  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise_error(0,"db is closed");
  rv = dbh->db->get_pagesize(dbh->db,&pagesize);
  if ( rv != 0 ) {
    raise_error(rv, "db_pagesize_get failure: %s",db_strerror(rv));
  }
  return INT2NUM(pagesize);
}

#pagesize=Object

db.pagesize=value

set database flags based on DB constants. see www.sleepycat.com/docs/api_c/db_set_flags.html


395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'ext/bdb.c', line 395

VALUE db_pagesize_set(VALUE obj, VALUE vpagesize)
{
  t_dbh *dbh;
  int rv;
  u_int32_t pagesize;

  pagesize=NUM2INT(vpagesize);
  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise_error(0,"db is closed");
  rv = dbh->db->set_pagesize(dbh->db,pagesize);
  if ( rv != 0 ) {
    raise_error(rv, "db_pagesize_set failure: %s",db_strerror(rv));
  }
  return vpagesize;
}

#pgetObject

db.pget(txn,key,data,flags) -> [pkey,data]

get a key/data pair from database using a secondary index. returns an array with a primary key and the data element.


711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
# File 'ext/bdb.c', line 711

VALUE db_pget(VALUE obj, VALUE vtxn, VALUE vkey, VALUE vdata, VALUE vflags)
{
  t_dbh *dbh;
  int rv;
  u_int32_t flags=0;
  DBT key,data,pkey;
  //VALUE str;
  t_txnh *txn=NULL;

  memset(&key,0,sizeof(DBT));
  memset(&data,0,sizeof(DBT));
  memset(&pkey,0,sizeof(DBT));

  if ( ! NIL_P(vtxn) ) {
    Data_Get_Struct(vtxn,t_txnh,txn);
    if (!txn->txn)
        raise(0, "txn is closed");
  }

  if ( ! NIL_P(vflags) ) {
    flags=NUM2UINT(vflags);
  }
  
  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise(0, "db is closed");

  StringValue(vkey);

  key.data = RSTRING_PTR(vkey);
  key.size = RSTRING_LEN(vkey);
  key.flags = LMEMFLAG;

  if ( ! NIL_P(vdata) ) {
    StringValue(vdata);
    data.data = RSTRING_PTR(vdata);
    data.size = RSTRING_LEN(vdata);
    data.flags = LMEMFLAG;
  }

  rv = dbh->db->pget(dbh->db,txn?txn->txn:NULL,&key,&pkey,&data,flags);
  if ( rv == 0 ) {
    return 
      rb_ary_new3(2,
		  rb_str_new(pkey.data,pkey.size),
		  rb_str_new(data.data,data.size));

  } else if (rv == DB_NOTFOUND) {
    return Qnil;
  } else {
    raise_error(rv, "db_pget failure: %s",db_strerror(rv));
  }
  return Qnil;
}

#putObject

db.put(txn,key,data,flags) -> self

put a key/data pair into the database. returns db. Will raise an error on DB_KEYEXIST but error.code will indicate so it can be easily caught.


592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
# File 'ext/bdb.c', line 592

VALUE db_put(VALUE obj, VALUE vtxn, VALUE vkey, VALUE vdata, VALUE vflags)
{
  t_dbh *dbh;
  int rv;
  u_int32_t flags=0;
  DBT key,data;
  t_txnh *txn=NULL;

  memset(&key,0,sizeof(DBT));
  memset(&data,0,sizeof(DBT));

  if ( ! NIL_P(vtxn) ) {
    Data_Get_Struct(vtxn,t_txnh,txn);
    if (!txn->txn)
        raise(0, "txn is closed");
  }

  if ( ! NIL_P(vflags) )
    flags=NUM2UINT(vflags);

  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise_error(0,"db is closed");

	key.data = RSTRING_PTR(vkey);
	key.size = RSTRING_LEN(vkey);
	key.flags = LMEMFLAG;

  StringValue(vdata);
  data.data = RSTRING_PTR(vdata);
  data.size = RSTRING_LEN(vdata);
  data.flags = LMEMFLAG;

  rv = dbh->db->put(dbh->db,txn?txn->txn:NULL,&key,&data,flags);
  /*
  if (rv == DB_KEYEXIST)
    return Qnil;
  */
  if (rv != 0) {
    raise_error(rv, "db_put fails: %s",db_strerror(rv));
  }

	if ( flags & DB_APPEND == DB_APPEND ) {
    VALUE str = rb_str_new(key.data,key.size);
    if (key.data) free(key.data);
    return str;
	}

  return obj;
}

#re_lenObject


324
325
326
327
328
329
330
331
332
333
334
# File 'ext/bdb.c', line 324

VALUE db_get_re_len( VALUE obj) {
	u_int32_t re_len;
	t_dbh *dbh;
	Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise_error(0,"db isn't created");
	int rv = dbh->db->get_re_len(dbh->db,&re_len);
  if ( rv != 0 )
    raise_error(rv, "db_get_re_len failure: %s",db_strerror(rv));
	return UINT2NUM(re_len);
}

#set_re_len(db, re_len) ⇒ Object

Set record-length


312
313
314
315
316
317
318
319
320
321
322
# File 'ext/bdb.c', line 312

VALUE db_set_re_len(VALUE obj, VALUE re_len) {
	int rv;
	t_dbh *dbh;
	Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise_error(0,"db isn't created");
	rv = dbh->db->set_re_len(dbh->db,NUM2UINT(re_len));
  if ( rv != 0 )
    raise_error(rv, "set_re_len failure: %s",db_strerror(rv));
	return re_len;
}

#removeObject

db.remove(disk_file,logical_db,flags) -> true

removes a whole database file, or just a logical_db within that file, i.e. if file and logical are both specified, only the logical will be removed. the Bdb::Db instance cannot have been previously used for anything and cannot be used after.


947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
# File 'ext/bdb.c', line 947

VALUE db_remove(VALUE obj, VALUE vdisk_file,
		VALUE vlogical_db, VALUE vflags)
{
  t_dbh *dbh;
  int rv;
  u_int32_t flags=0;
  //char *logical_db=NULL;

  if ( ! NIL_P(vflags) )
    flags=NUM2UINT(vflags);

  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise(0, "db is closed");
  rv=dbh->db->remove(dbh->db,
		     NIL_P(vdisk_file)?NULL:StringValueCStr(vdisk_file),
		     NIL_P(vlogical_db)?NULL:StringValueCStr(vlogical_db),
		     flags);
  /* handle cannot be accessed again per docs */
  dbh->db=NULL;
  if (rv)
    raise_error(rv,"db_remove failed: %s",db_strerror(rv));
  return Qtrue;
}

#renameObject

db.rename(file,logical,newname,flags) -> true

rename a file or logical db to newname.


978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
# File 'ext/bdb.c', line 978

VALUE db_rename(VALUE obj, VALUE vdisk_file,
		VALUE vlogical_db, VALUE newname, VALUE vflags)
{
  t_dbh *dbh;
  int rv;
  u_int32_t flags=0;
  //char *disk_file=NULL;
  //char *logical_db=NULL;

  if ( ! NIL_P(vflags) )
    flags=NUM2UINT(vflags);

  if ( NIL_P(newname) )
    raise_error(0,"db_rename newname must be specified");

  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise(0, "db is closed");
  rv=dbh->db->rename(dbh->db,
		     NIL_P(vdisk_file)?NULL:StringValueCStr(vdisk_file),
		     NIL_P(vlogical_db)?NULL:StringValueCStr(vlogical_db),
		     StringValueCStr(newname),
		     flags);

  if (rv) {
    raise_error(rv,"db_rename failed: %s",db_strerror(rv));
	}
  return Qtrue;
}

#statObject

db.stat(txn,flags) -> Bdb::Db::Stat

get database status. Returns a Bdb::Db::Stat object that is specialized to the db_type and only responds to [] to retrieve status values. All values are stored in instance variables, so singleton classes can be created and instance_eval will work.


2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
# File 'ext/bdb.c', line 2100

VALUE db_stat(VALUE obj, VALUE vtxn, VALUE vflags)
{
  u_int32_t flags=0;
  int rv;
  t_dbh *dbh;
  t_txnh *txn=NULL;
  DBTYPE dbtype;
  union {
    void *stat;
    DB_HASH_STAT *hstat;
    DB_BTREE_STAT *bstat;
    DB_QUEUE_STAT *qstat;
  } su;
  VALUE s_obj;

  if ( ! NIL_P(vflags) )
    flags=NUM2UINT(vflags);
  if ( ! NIL_P(vtxn) ) {
    Data_Get_Struct(vtxn,t_txnh,txn);
    if (!txn->txn)
        raise(0, "txn is closed");
  }

  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise(0, "db is closed");

  rv=dbh->db->get_type(dbh->db,&dbtype);
  if (rv)
    raise_error(rv,"db_stat %s",db_strerror(rv));
#if DB_VERSION_MAJOR == 5 || DB_VERSION_MINOR > 2
  rv=dbh->db->stat(dbh->db,txn?txn->txn:NULL,&(su.stat),flags);
#else
  rv=dbh->db->stat(dbh->db,&(su.stat),flags);
#endif
  if (rv)
    raise_error(rv,"db_stat %s",db_strerror(rv));

  s_obj=rb_class_new_instance(0,NULL,cDbStat);
  rb_iv_set(s_obj,"@dbtype",INT2FIX(dbtype));

  switch(dbtype) {

#define hs_int(field)			      \
  rb_iv_set(s_obj,"@" #field,INT2FIX(su.hstat->field))

  case DB_HASH:
    hs_int(hash_magic);
    hs_int(hash_version);		/* Version number. */
    hs_int(hash_metaflags);	/* Metadata flags. */
    hs_int(hash_nkeys);		/* Number of unique keys. */
    hs_int(hash_ndata);		/* Number of data items. */
    hs_int(hash_pagesize);	/* Page size. */
    hs_int(hash_ffactor);		/* Fill factor specified at create. */
    hs_int(hash_buckets);		/* Number of hash buckets. */
    hs_int(hash_free);		/* Pages on the free list. */
    hs_int(hash_bfree);		/* Bytes free on bucket pages. */
    hs_int(hash_bigpages);	/* Number of big key/data pages. */
    hs_int(hash_big_bfree);	/* Bytes free on big item pages. */
    hs_int(hash_overflows);	/* Number of overflow pages. */
    hs_int(hash_ovfl_free);	/* Bytes free on ovfl pages. */
    hs_int(hash_dup);		/* Number of dup pages. */
    hs_int(hash_dup_free);	/* Bytes free on duplicate pages. */
    break;

#define bs_int(field)					\
    rb_iv_set(s_obj,"@" #field,INT2FIX(su.bstat->field))
    
  case DB_BTREE:
  case DB_RECNO:
    bs_int(bt_magic);		/* Magic number. */
    bs_int(bt_version);		/* Version number. */
    bs_int(bt_metaflags);		/* Metadata flags. */
    bs_int(bt_nkeys);		/* Number of unique keys. */
    bs_int(bt_ndata);		/* Number of data items. */
    bs_int(bt_pagesize);		/* Page size. */
#if DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR < 4
    bs_int(bt_maxkey);		/* Maxkey value. */
#endif
    bs_int(bt_minkey);		/* Minkey value. */
    bs_int(bt_re_len);		/* Fixed-length record length. */
    bs_int(bt_re_pad);		/* Fixed-length record pad. */
    bs_int(bt_levels);		/* Tree levels. */
    bs_int(bt_int_pg);		/* Internal pages. */
    bs_int(bt_leaf_pg);		/* Leaf pages. */
    bs_int(bt_dup_pg);		/* Duplicate pages. */
    bs_int(bt_over_pg);		/* Overflow pages. */
#if DB_VERSION_MAJOR == 5 || DB_VERSION_MINOR > 2
    bs_int(bt_empty_pg);		/* Empty pages. */
#endif
    bs_int(bt_free);		/* Pages on the free list. */
    bs_int(bt_int_pgfree);	/* Bytes free in internal pages. */
    bs_int(bt_leaf_pgfree);	/* Bytes free in leaf pages. */
    bs_int(bt_dup_pgfree);	/* Bytes free in duplicate pages. */
    bs_int(bt_over_pgfree);	/* Bytes free in overflow pages. */
    
    break;

#define qs_int(field)					\
    rb_iv_set(s_obj,"@" #field,INT2FIX(su.qstat->field))
    
  case DB_QUEUE:
    qs_int(qs_magic);		/* Magic number. */
    qs_int(qs_version);		/* Version number. */
    qs_int(qs_metaflags);		/* Metadata flags. */
    qs_int(qs_nkeys);		/* Number of unique keys. */
    qs_int(qs_ndata);		/* Number of data items. */
    qs_int(qs_pagesize);		/* Page size. */
    qs_int(qs_extentsize);	/* Pages per extent. */
    qs_int(qs_pages);		/* Data pages. */
    qs_int(qs_re_len);		/* Fixed-length record length. */
    qs_int(qs_re_pad);		/* Fixed-length record pad. */
    qs_int(qs_pgfree);		/* Bytes free in data pages. */
    qs_int(qs_first_recno);	/* First not deleted record. */
    qs_int(qs_cur_recno);		/* Next available record number. */
    
    break;
	case DB_UNKNOWN:
		break;
  }

  free(su.stat);
  return s_obj;
}

#syncObject

db.sync -> true

sync the database out to storage.


1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
# File 'ext/bdb.c', line 1014

VALUE db_sync(VALUE obj)
{
  t_dbh *dbh;
  int rv;

  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise(0, "db is closed");
  rv=dbh->db->sync(dbh->db,NOFLAGS);

  if (rv)
    raise_error(rv,"db_sync failed: %s",db_strerror(rv));
  return Qtrue;
}

#truncateObject

db.truncate(txn) -> Fixnum(record_count)

truncate, i.e. remove all records, purge, trash.


1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
# File 'ext/bdb.c', line 1036

VALUE db_truncate(VALUE obj, VALUE vtxn)
{
  t_dbh *dbh;
  t_txnh *txn=NULL;
  int rv;
  //VALUE result;
  u_int32_t count;

  if ( ! NIL_P(vtxn) ) {
    Data_Get_Struct(vtxn,t_txnh,txn);
    if (!txn->txn)
        raise(0, "txn is closed");
  }

  Data_Get_Struct(obj,t_dbh,dbh);
  if (!dbh->db)
    raise(0, "db is closed");

  rv=dbh->db->truncate(dbh->db,txn?txn->txn:NULL,&count,NOFLAGS);
  if (rv)
    raise_error(rv,"db_truncate: %s",db_strerror(rv));

  return INT2FIX(count);
}