Method: Bdb::Db#associate

Defined in:
ext/bdb.c

#associate(vtxn, osecdb, vflags, cb_proc) ⇒ Object

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)



1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
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
# File 'ext/bdb.c', line 1248

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;
}