/* * db_init -- * Open the database. */ int db_init( DB_ENV *dbenv, DB **dbpp, int dups, int pagesize) { DB *dbp; DB_TXN *txnp; int ret; dbp = NULL; txnp = NULL; ret = 0; if ((ret = db_create(&dbp, dbenv, 0)) != 0) { fprintf(stderr, "%s: db_create: %s\n", progname, db_strerror(ret)); return (ret); } dbp->set_errfile(dbp, stderr); dbp->set_errpfx(dbp, progname); if ((ret = dbp->set_pagesize(dbp, pagesize)) != 0) { dbp->err(dbp, ret, "set_pagesize"); goto err; } if (dups && (ret = dbp->set_flags(dbp, DB_DUP)) != 0) { dbp->err(dbp, ret, "set_flags"); goto err; } if ((ret = dbenv->txn_begin(dbenv, NULL, &txnp, 0)) != 0) goto err; if ((ret = dbp->open(dbp, txnp, "sss.db", "primary", DB_BTREE, DB_CREATE , 0664)) != 0) { dbp->err(dbp, ret, "%s: open", "sss.db"); goto err; } *dbpp = dbp; ret = txnp->commit(txnp, 0); txnp = NULL; if (ret != 0) goto err; return (0); err: if (txnp != NULL) (void)txnp->abort(0); if (dbp != NULL) (void)dbp->close(dbp, 0); return (ret); }
static bool dir_is_empty(DB_TXN *txn, const struct nfs_inode *ino) { int rc; DBC *cur = NULL; DB *dirent = srv.fsdb.dirent; DBT pkey, pval; struct fsdb_de_key key; nfsino_t rnum = 0; uint64_t db_dummy; rc = dirent->cursor(dirent, txn, &cur, 0); if (rc) { dirent->err(dirent, rc, "dirent->cursor"); return false; } key.inum = inum_encode(ino->inum); memset(&pkey, 0, sizeof(pkey)); pkey.data = &key; pkey.size = sizeof(key); pkey.flags = DB_DBT_MALLOC; memset(&pval, 0, sizeof(pval)); pval.data = &db_dummy; pval.ulen = sizeof(db_dummy); pval.flags = DB_DBT_USERMEM; rc = cur->get(cur, &pkey, &pval, DB_SET_RANGE); if (rc == 0) { struct fsdb_de_key *rkey = pkey.data; rnum = inum_decode(rkey->inum); free(rkey); } else if (rc != DB_NOTFOUND) dirent->err(dirent, rc, "dir_is_empty cur->get"); rc = cur->close(cur); if (rc) { dirent->err(dirent, rc, "dirent->cursor close"); return false; } if (rnum == ino->inum) return false; return true; }
int uriindex_get (char uri[], unsigned int *DocID, unsigned int *lastmodified, char subname[]) { DB dbpArray; DB *dbp = NULL; DBT key, data; int ret; int forreturn = 1; struct uriindexFormat uriindex; if (!uriindex_open(&dbp,subname, DB_RDONLY)) { #ifdef DEBUG fprintf(stderr,"can't open uriindex\n"); #endif return 0; } //resetter minne memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); //legger inn datane i bdb strukturen key.data = uri; key.size = strlen(uri); if ((ret = dbp->get(dbp, NULL, &key, &data, 0)) == 0) { *DocID = (*(struct uriindexFormat *)data.data).DocID; *lastmodified = (*(struct uriindexFormat *)data.data).lastmodified; forreturn = 1; } else if (ret == DB_NOTFOUND) { #ifdef DEBUG dbp->err(dbp, ret, "DBcursor->get"); printf("search for \"%s\", len %i\n",key.data,key.size); #endif forreturn = 0; } else { dbp->err(dbp, ret, "DBcursor->get"); forreturn = 0; } uriindex_close(&dbp); return forreturn; }
/* Open a Berkeley DB database */ int open_db(DB **dbpp, const char *progname, const char *file_name, DB_ENV *envp, u_int32_t extra_flags) { int ret; u_int32_t open_flags; DB *dbp; /* Initialize the DB handle */ ret = db_create(&dbp, envp, 0); if (ret != 0) { fprintf(stderr, "%s: %s\n", progname, db_strerror(ret)); return (EXIT_FAILURE); } /* Point to the memory malloc'd by db_create() */ *dbpp = dbp; if (extra_flags != 0) { ret = dbp->set_flags(dbp, extra_flags); if (ret != 0) { dbp->err(dbp, ret, "open_db: Attempt to set extra flags failed."); return (EXIT_FAILURE); } } /* Now open the database */ open_flags = DB_CREATE | /* Allow database creation */ DB_READ_UNCOMMITTED | /* Allow dirty reads */ DB_AUTO_COMMIT | /* Allow autocommit */ DB_THREAD; /* Cause the database to be free-threaded */ ret = dbp->open(dbp, /* Pointer to the database */ NULL, /* Txn pointer */ file_name, /* File name */ NULL, /* Logical db name */ DB_BTREE, /* Database type (using btree) */ open_flags, /* Open flags */ 0); /* File mode. Using defaults */ if (ret != 0) { dbp->err(dbp, ret, "Database '%s' open failed", file_name); return (EXIT_FAILURE); } return (EXIT_SUCCESS); }
/* {{{ rberkeley_db_put */ SEXP rberkeley_db_put(SEXP _dbp, SEXP _txnid, SEXP _key, SEXP _data, SEXP _flags) { DB *dbp; DBT key, data; DB_TXN *txnid; u_int32_t flags = INTEGER(_flags)[0]; int ret; memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); key.data = (unsigned char *)RAW(_key); key.size = length(_key); data.data = (unsigned char *)RAW(_data); data.size = length(_data); dbp = R_ExternalPtrAddr(_dbp); if(R_ExternalPtrTag(_dbp) != RBerkeley_DB || dbp == NULL) error("invalid 'db' handle"); if(!isNull(_txnid)) { txnid = R_ExternalPtrAddr(_txnid); } else txnid = NULL; /* Store a key/data pair. */ if ((ret = dbp->put(dbp, txnid, &key, &data, flags)) == 0) { return ScalarInteger(ret); } else { dbp->err(dbp, ret, "DB->put"); } return R_NilValue; }
int uriindex_delete (char uri[], char subname[]) { DB *dbp = NULL; DBT key; int ret; int forreturn = 1; if (!uriindex_open(&dbp,subname, DB_CREATE)) { fprintf(stderr,"can't open uriindex\n"); return 0; } //resetter minne memset(&key, 0, sizeof(DBT)); //legger inn datane i bdb strukturen key.data = uri; key.size = strlen(uri); if ((ret = dbp->del(dbp, NULL, &key, 0)) == 0) { forreturn = 1; } else { dbp->err(dbp, ret, "DBcursor->get"); forreturn = 0; } uriindex_close(&dbp); return forreturn; }
static Info* berkely_registry_get(void *self, const char* filename) { DB *dbp = NULL; int ret; dbp = get_db(self); g_assert(dbp != NULL); DBT key, data; memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); key.data = (void*) filename; key.size = strlen(filename) + 1; key.flags = DB_DBT_USERMEM; if ((ret = dbp->get(dbp, NULL, &key, &data, 0)) == 0) { g_debug("db: '%s' key retreived", (char*)key.data); } else { dbp->err(dbp, ret, "DB->get"); g_warning("DB get failed"); } return bab_info_unmarshall(data.data); }
static int ooMindMap_keys(struct ooMindMap *self, mindmap_size_t **ids, mindmap_size_t batch_size, mindmap_size_t batch_start) { DBT key, data; DB *dbp; DBC *dbcp; int ret; dbp = self->_storage; /* initialize the key/data pair */ memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); /* acquire a cursor for the database */ if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) { dbp->err(dbp, ret, "DB->cursor"); goto error; } /* TODO: batching */ /* walk through the database and print out the key/data pairs. */ while ((ret = dbcp->c_get(dbcp, &key, &data, DB_NEXT)) == 0) printf("%.*s : %.*s\n", (int)key.size, (char *)key.data, (int)data.size, (char *)data.data); if (ret != DB_NOTFOUND) { dbp->err(dbp, ret, "DBcursor->get"); goto error; } /* close everything down */ if ((ret = dbcp->c_close(dbcp)) != 0) { dbp->err(dbp, ret, "DBcursor->close"); goto error; } return oo_OK; error: (void)dbp->close(dbp, 0); return oo_FAIL; }
int main() { DB *dbp; DB_ENV *dbenv; DB_TXN *xid; DBT key, data; const unsigned int INSERT_NUM = 100; char value[22]; /* should be log INSERT_NUM */ int ret, i, t_ret; env_dir_create(); env_open(&dbenv); if ((ret = db_create(&dbp, dbenv, 0)) != 0) { fprintf(stderr, "db_create: %s\n", db_strerror(ret)); exit (1); } dbenv->txn_begin(dbenv, NULL, &xid, 0); if ((ret = dbp->open(dbp, xid, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) { dbp->err(dbp, ret, "%s", DATABASE); goto err; } memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); key.size = sizeof(int); key.data = malloc(sizeof(int)); data.data = value; for( i = 0; i < INSERT_NUM; i++ ) { *((int*)key.data) = i; data.size = sizeof(char)*strlen(data.data); sprintf(value, "value: %u\n", i); dbp->put(dbp, xid, &key, &data, 0); } xid->commit(xid, 0); dbenv->txn_begin(dbenv, NULL, &xid, 0); for( i = 0; i < INSERT_NUM; i++ ) { *((int*)key.data) = i; dbp->get(dbp, xid, &key, &data, 0); printf("db: %u: key retrieved: data was %s.\n", *((int*)key.data), (char *)data.data); } xid->abort(xid); err: if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0) ret = t_ret; return 0; }
/** * Retrieving a concept: * if it's already cached in memory, * return a reference to it, * otherwise check the MindMap DB * and initialize a new concept. */ static struct ooConcept* ooMindMap_get(struct ooMindMap *self, mindmap_size_t id) { DB *dbp; DBT key, data; int ret; struct ooConcept *conc = NULL; char *key_buffer; static mindmap_size_t idsize = sizeof(mindmap_size_t); if (id > self->num_concepts) return NULL; /* check the memory cache */ conc = (struct ooConcept*)self->concept_index[id]; if (conc) return conc; dbp = self->_storage; /* initialize the DBTs */ memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); key_buffer = malloc(idsize); if (!key_buffer) return NULL; memcpy(key_buffer, &id, idsize); /* set the search key with the Concept id */ key.data = key_buffer; key.size = idsize; /* get the record */ ret = dbp->get(dbp, NULL, &key, &data, 0); if (ret != 0) { dbp->err(dbp, ret, "Error searching for id: %ld", id); return NULL; } /* create concept */ ret = ooConcept_new(&conc); if (ret != oo_OK) return NULL; conc->numid = id; conc->bytecode = (char*)data.data; conc->bytecode_size = (size_t)data.size; conc->unpack(conc); /* update the cache */ self->concept_index[id] = conc; free(key_buffer); return conc; }
int uriindex_open(DB **dbpp, char subname[], u_int32_t flags) { DB *dbp = (*dbpp); char fileName[512]; int ret; GetFilPathForLotFile(fileName,"urls.db",1,subname); #ifdef DEBUG printf("uriindex_open: Trying to open lotfile \"%s\"\n",fileName); #endif /******************************************************************** * Opening nrOfFiles to stor the data in ********************************************************************/ /* Create and initialize database object */ if ((ret = db_create(&dbp, NULL, 0)) != 0) { fprintf(stderr, "%s: db_create: %s\n", "bbdocument", db_strerror(ret)); return 0; } //#define dbCashe 314572800 //300 mb ////setter cashe størelsen manuelt //if ((ret = dbp->set_cachesize(dbp, 0, dbCashe, dbCasheBlokes)) != 0) { // dbp->err(dbp, ret, "set_cachesize"); //} /* open the database. */ if ((ret = dbp->open(dbp, NULL, fileName, NULL, DB_BTREE, flags, 0664)) != 0) { dbp->err(dbp, ret, "%s: open", fileName); //goto err1; //dette skjer nor collection mappen ikke er opprettet enda, typisk forde vi ikke har lagret et dokument der enda #ifdef DEBUG printf("can't dbp->open(), but db_create() was sucessful!\n"); #endif dbp->close(dbp, 0); return 0; } /********************************************************************/ #ifdef DEBUG printf("uriindex_open: finished\n"); #endif (*dbpp) = dbp; return 1; }
int init_koef_db(DB **kdbp) { FILE *fp; DB *dbp; DBT key, data; struct kdb_key kdbkey; struct kdb_data kdbdata; unsigned int asdu; unsigned int ioaf; float koefu, koefi; int ret; char line[LMAX]; if ( (fp = fopen(KFILE,"r")) == NULL) return -1; if ( db_create(&dbp, NULL, 0) != 0) return -2; if ( dbp->open(dbp,NULL,NULL,NULL,DB_BTREE,DB_CREATE,0) != 0 ) return -3; /* Zero out the DBTs before using them. */ memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); while(fgets(line, LMAX, fp)!=NULL){ if (sscanf(line,"%u;%x;%f;%f\n",&asdu,&ioaf,&koefu,&koefi) < 1) continue; kdbkey.asdu = (unsigned short) asdu; kdbkey.ioa2 = (unsigned char) ioaf; kdbdata.koef_u = koefu; kdbdata.koef_i = koefi; key.data = &kdbkey; key.size = sizeof(struct kdb_key); data.data = &kdbdata; data.size = sizeof(struct kdb_data); ret = dbp->put(dbp, NULL, &key, &data, DB_NOOVERWRITE); if (ret != 0) { dbp->err(dbp, ret, "Put failed: "); } } fclose(fp); *kdbp = dbp; return 0; }
int del (void **db, void *key, int key_len, void *value, int value_len) { int ret; DB *dbp = *db; DBT db_key; memset(&db_key, 0, sizeof(db_key)); db_key.data = key; db_key.size = key_len; if (ret = dbp->del(dbp, NULL, key, 0)) { dbp->err(dbp, ret, "DB->del"); exit(1); } }
int uriindex_add (char uri[], unsigned int DocID, unsigned int lastmodified, char subname[]) { DB dbpArray; DB *dbp = NULL; DBT key, data; int ret; struct uriindexFormat uriindex; #ifdef DEBUG printf("uriindex_add: subname %s\n",subname); #endif uriindex_open(&dbp,subname, DB_CREATE); //resetter minne memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); //legger inn datane i bdb strukturen key.data = uri; key.size = strlen(uri); uriindex.DocID = DocID; uriindex.lastmodified = lastmodified; //data.data = &DocID; //data.size = sizeof(DocID); data.data = &uriindex; data.size = sizeof(uriindex); //legger til i databasen if ((ret = dbp->put(dbp, NULL, &key, &data, 0)) != 0) { dbp->err(dbp, ret, "DB->put"); //kan ikke returnere her for da blir den aldr lukket. //return (EXIT_FAILURE); } uriindex_close(&dbp); }
// ------------------------------------------------------------------ int main (int argc,char *argv[]) { cerr << "*** 開始 ***\n"; char file_db[160]; strcpy (file_db,argv[1]); cerr << "\tfile_db = " << file_db << "\n"; DB *dbp = NULL; int ret; int t_ret; ret = db_create(&dbp, NULL, 0); if (ret != 0) { fprintf(stderr, "db_create: %s\n", db_strerror(ret)); } else { // ret = dbp->open(dbp, NULL, file_db, NULL, DB_BTREE, DB_RDONLY, 0); ret = dbp->open(dbp, NULL, file_db, NULL, DB_HASH, DB_RDONLY, 0); if (ret != 0) { dbp->err(dbp, ret, "%s", file_db); } else { db_cursor_read_proc (dbp,file_db); } } /* int DB->close(DB *db, u_int32_t flags); * * DB->close は、Berkeley DB ハンドルを閉じます。*/ if (dbp) { t_ret = dbp->close(dbp, 0); if (t_ret != 0 && ret == 0) ret = t_ret; } cerr << "*** 終了 ***\n"; exit (ret); }
/*@ * Walk through the database and return the highest iid we have seen. * If this is a RECNO database this can probably be done more cheaply * but for now we will make this usable regardless of the index impl */ iid_t storage_get_max_iid(struct storage * s) { int ret; DB *dbp = s->db; DBC *dbcp; DBT key, data; iid_t max_iid = 0; /* Acquire a cursor for the database. */ if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) { dbp->err(dbp, ret, "DB->cursor"); return (1); } /* Re-initialize the key/data pair. */ memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); /* Walk through the database and print out the key/data pairs. */ while ((ret = dbcp->c_get(dbcp, &key, &data, DB_NEXT)) == 0) { assert(data.size = sizeof(iid_t)); max_iid = *(iid_t *)key.data; } if (ret != DB_NOTFOUND) { dbp->err(dbp, ret, "DBcursor->get"); return 0; } /* Close the cursor. */ if ((ret = dbcp->c_close(dbcp)) != 0) { dbp->err(dbp, ret, "DBcursor->close"); } return (max_iid); }
int load_metadata_keys(bdb_table_p _tp) { int ret,n,ci; char dbuf[MAX_ROW_SIZE]; char *s = NULL; DB *db = NULL; DBT key, data; ret = n = ci = 0; if(!_tp || !_tp->db) return -1; db = _tp->db; memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); memset(dbuf, 0, MAX_ROW_SIZE); key.data = METADATA_KEY; key.size = strlen(METADATA_KEY); data.data = dbuf; data.ulen = MAX_ROW_SIZE; data.flags = DB_DBT_USERMEM; if ((ret = db->get(db, NULL, &key, &data, 0)) != 0) { db->err(db, ret, "load_metadata_keys DB->get failed"); ERR("FAILED to find METADATA in table \n"); return ret; } s = strtok(dbuf, " "); while(s!=NULL && n< _tp->ncols) { ret = sscanf(s,"%i", &ci); if(ret != 1) return -1; if( _tp->colp[ci] ) { _tp->colp[ci]->flag = 1; _tp->nkeys++; } n++; s=strtok(NULL, " "); } return 0; }
int open_table (void **db, char *table) { int ret; DB *dbp = NULL; if ((ret = db_create(&dbp, NULL, 0)) != 0) { fprintf(stderr, "db_create: %s\n", db_strerror(ret)); exit (1); } if ((ret = dbp->open( dbp, NULL, table, NULL, DB_BTREE, DB_CREATE, 0)) != 0) { dbp->err(dbp, ret, "%s", table); exit (1); } *db = dbp; return ES_OK; }
int insert(context_t context, char*fname) { int ret; DB* dbContext = db_getContext(context); DBT key, data; char *pContext; analSet anal = {{{0}},{0}}; if(context[0]) { pContext = context; } else { pContext = g_lastcontext; } printf("(%d) [%s] Adding %s\n", ++unsafeCtr, pContext, fname); memset (&key, 0, sizeof (key)); memset (&data, 0, sizeof (data)); // analyze the file to be inserted analyze(fname, &anal); // this is the simple key value store //compress(fname, comp); key.data = fname; key.size = strlen(fname); data.data = (void*)&anal; data.size = sizeof(analSet); if ((ret = dbContext->put (dbContext, NULL, &key, &data, 0)) != 0) { dbContext->err (dbContext, ret, "(db)"); } ixPut(fname, &anal); return 0; }
static void berkely_registry_add(void *self, Info *info) { g_assert(self != NULL); g_assert(info != NULL); g_debug("Berkeley registry adding %s", info->filename); DB *dbp = NULL; int ret; dbp = get_db(self); g_assert(dbp != NULL); DBT key, data; memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); bab_info_log("Persisting", info); char* info_data = bab_info_marshall(info); key.data = info->filename; key.size = strlen(info->filename) + 1; key.flags = DB_DBT_USERMEM; data.data = info_data; data.size = bab_info_max_size(); if ((ret = dbp->put(dbp, NULL, &key, &data, 0)) == 0) { g_debug("db: '%s' key stored", (char*)key.data); } else { dbp->err(dbp, ret, "DB->put"); g_warning("DB put failed"); } }
/* opening the MindMap Database */ static int ooMindMap_open(struct ooMindMap *self, const char *dbpath) { int ret; DB *dbp = self->_storage; if (!dbp) return oo_FAIL; /* open the database */ if ((ret = dbp->open(dbp, /* DB structure pointer */ NULL, /* Transaction pointer */ dbpath, /* On-disk file that holds the database. */ NULL, /* Optional logical database name */ DB_BTREE, /* Database access method */ DB_CREATE, /* Open flags */ 0664)) /* File mode (using defaults) */ != oo_OK) { dbp->err(dbp, ret, "%s: open", dbpath); (void)dbp->close(dbp, 0); return oo_FAIL; } return 0; }
int put (void **db, void *key, int key_len, void *value, int value_len) { int ret; DBT db_key, db_data; DB *dbp = *db; memset(&db_key, 0, sizeof(db_key)); memset(&db_data, 0, sizeof(db_data)); db_key.data = key; db_key.size = key_len; db_data.data = value; db_data.size = value_len; if ((ret = dbp->put(dbp, NULL, &db_key, &db_data, 0)) == 0) printf("db: %s: key stored.\n", (char *)db_key.data); else { dbp->err(dbp, ret, "DB->put"); exit(1); } return ES_OK; }
int doloop(DB_ENV *dbenv) { DB *dbp; DBT key, data; char buf[BUFSIZE], *rbuf; int ret; u_int32_t db_flags; dbp = NULL; memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); ret = 0; for (;;) { if (dbp == NULL) { if ((ret = db_create(&dbp, dbenv, 0)) != 0) return (ret); db_flags = DB_AUTO_COMMIT | DB_CREATE; if ((ret = dbp->open(dbp, NULL, DATABASE, NULL, DB_BTREE, db_flags, 0)) != 0) { dbenv->err(dbenv, ret, "DB->open"); goto err; } } printf("QUOTESERVER> "); fflush(stdout); if (fgets(buf, sizeof(buf), stdin) == NULL) break; if (strtok(&buf[0], " \t\n") == NULL) { switch ((ret = print_stocks(dbp))) { case 0: continue; default: dbp->err(dbp, ret, "Error traversing data"); goto err; } } rbuf = strtok(NULL, " \t\n"); if (rbuf == NULL || rbuf[0] == '\0') { if (strncmp(buf, "exit", 4) == 0 || strncmp(buf, "quit", 4) == 0) break; dbenv->errx(dbenv, "Format: TICKER VALUE"); continue; } key.data = buf; key.size = (u_int32_t)strlen(buf); data.data = rbuf; data.size = (u_int32_t)strlen(rbuf); if ((ret = dbp->put(dbp, NULL, &key, &data, 0)) != 0) { dbp->err(dbp, ret, "DB->put"); if (ret != DB_KEYEXIST) goto err; } } err: if (dbp != NULL) (void)dbp->close(dbp, DB_NOSYNC); return (ret); }
int doloop(DB_ENV *dbenv) { DB *dbp; APP_DATA *app_data; DBT key, data; char buf[BUFSIZE], *rbuf; int ret; u_int32_t flags; dbp = NULL; ret = 0; memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); app_data = dbenv->app_private; for (;;) { if (dbp == NULL) { if ((ret = db_create(&dbp, dbenv, 0)) != 0) return (ret); flags = DB_AUTO_COMMIT; if (app_data->is_master) flags |= DB_CREATE; if ((ret = dbp->open(dbp, NULL, DATABASE, NULL, DB_BTREE, flags, 0)) != 0) { if (ret == ENOENT) { printf( "No stock database yet available.\n"); if ((ret = dbp->close(dbp, 0)) != 0) { dbenv->err(dbenv, ret, "DB->close"); goto err; } dbp = NULL; sleep(SLEEPTIME); continue; } dbenv->err(dbenv, ret, "DB->open"); goto err; } } printf("QUOTESERVER%s> ", app_data->is_master ? "" : " (read-only)"); fflush(stdout); if (fgets(buf, sizeof(buf), stdin) == NULL) break; if (strtok(&buf[0], " \t\n") == NULL) { switch ((ret = print_stocks(dbp))) { case 0: continue; case DB_REP_HANDLE_DEAD: (void)dbp->close(dbp, DB_NOSYNC); dbp = NULL; dbenv->errx(dbenv, "Got a dead replication handle"); continue; default: dbp->err(dbp, ret, "Error traversing data"); goto err; } } rbuf = strtok(NULL, " \t\n"); if (rbuf == NULL || rbuf[0] == '\0') { if (strncmp(buf, "exit", 4) == 0 || strncmp(buf, "quit", 4) == 0) break; dbenv->errx(dbenv, "Format: TICKER VALUE"); continue; } if (!app_data->is_master) { dbenv->errx(dbenv, "Can't update at client"); continue; } key.data = buf; key.size = (u_int32_t)strlen(buf); data.data = rbuf; data.size = (u_int32_t)strlen(rbuf); if ((ret = dbp->put(dbp, NULL, &key, &data, 0)) != 0) { dbp->err(dbp, ret, "DB->put"); goto err; } } err: if (dbp != NULL) (void)dbp->close(dbp, DB_NOSYNC); return (ret); }
icalerrorenum icalbdbset_commit(icalset *set) { DB *dbp; DBC *dbcp; DBT key, data; icalcomponent *c; char *str = NULL; int ret = 0; int reterr = ICAL_NO_ERROR; char keystore[256]; char uidbuf[256]; char datastore[1024]; char *more_mem = NULL; DB_TXN *tid = NULL; icalbdbset *bset = (icalbdbset *) set; int bad_uid_counter = 0; int retry = 0, done = 0, completed = 0, deadlocked = 0; icalerror_check_arg_re((bset != 0), "bset", ICAL_BADARG_ERROR); dbp = bset->dbp; icalerror_check_arg_re((dbp != 0), "dbp is invalid", ICAL_BADARG_ERROR); if (bset->changed == 0) { return ICAL_NO_ERROR; } memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); key.flags = DB_DBT_USERMEM; key.data = keystore; key.ulen = (u_int32_t) sizeof(keystore); data.flags = DB_DBT_USERMEM; data.data = datastore; data.ulen = (u_int32_t) sizeof(datastore); if (!ICAL_DB_ENV) { if (icalbdbset_init_dbenv(NULL, NULL) != 0) { return ICAL_INTERNAL_ERROR; } } while ((retry < MAX_RETRY) && !done) { if ((ret = ICAL_DB_ENV->txn_begin(ICAL_DB_ENV, NULL, &tid, 0)) != 0) { if (ret == DB_LOCK_DEADLOCK) { retry++; continue; } else if (ret == DB_RUNRECOVERY) { ICAL_DB_ENV->err(ICAL_DB_ENV, ret, "icalbdbset_commit: txn_begin failed"); abort(); } else { ICAL_DB_ENV->err(ICAL_DB_ENV, ret, "icalbdbset_commit"); return ICAL_INTERNAL_ERROR; } } /* first delete everything in the database, because there could be removed components */ if ((ret = dbp->cursor(dbp, tid, &dbcp, DB_DIRTY_READ)) != 0) { tid->abort(tid); if (ret == DB_LOCK_DEADLOCK) { retry++; continue; } else if (ret == DB_RUNRECOVERY) { ICAL_DB_ENV->err(ICAL_DB_ENV, ret, "curor failed"); abort(); } else { ICAL_DB_ENV->err(ICAL_DB_ENV, ret, "curor failed"); /* leave bset->changed set to true */ return ICAL_INTERNAL_ERROR; } } /* fetch the key/data pair, then delete it */ completed = 0; while (!completed && !deadlocked) { ret = dbcp->c_get(dbcp, &key, &data, DB_NEXT); if (ret == DB_NOTFOUND) { completed = 1; } else if (ret == ENOMEM) { if (more_mem) { free(more_mem); } more_mem = malloc(data.ulen + 1024); data.data = more_mem; data.ulen = data.ulen + 1024; } else if (ret == DB_LOCK_DEADLOCK) { deadlocked = 1; } else if (ret == DB_RUNRECOVERY) { tid->abort(tid); ICAL_DB_ENV->err(ICAL_DB_ENV, ret, "c_get failed."); abort(); } else if (ret == 0) { if ((ret = dbcp->c_del(dbcp, 0)) != 0) { dbp->err(dbp, ret, "cursor"); if (ret == DB_KEYEMPTY) { /* never actually created, continue onward.. */ /* do nothing - break; */ } else if (ret == DB_LOCK_DEADLOCK) { deadlocked = 1; } else { /*char *foo = db_strerror(ret); */ abort(); } } } else { /* some other non-fatal error */ dbcp->c_close(dbcp); tid->abort(tid); if (more_mem) { free(more_mem); more_mem = NULL; } return ICAL_INTERNAL_ERROR; } } if (more_mem) { free(more_mem); more_mem = NULL; } if (deadlocked) { dbcp->c_close(dbcp); tid->abort(tid); retry++; continue; /* next retry */ } deadlocked = 0; for (c = icalcomponent_get_first_component(bset->cluster, ICAL_ANY_COMPONENT); c != 0 && !deadlocked; c = icalcomponent_get_next_component(bset->cluster, ICAL_ANY_COMPONENT)) { memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); /* Note that we're always inserting into a primary index. */ if (icalcomponent_isa(c) != ICAL_VAGENDA_COMPONENT) { char *uidstr = (char *)icalcomponent_get_uid(c); if (!uidstr) { /* this shouldn't happen */ /* no uid string, we need to add one */ snprintf(uidbuf, 256, "baduid%d-%d", getpid(), bad_uid_counter++); key.data = uidbuf; } else { key.data = uidstr; } } else { char *relcalid = NULL; relcalid = (char *)icalcomponent_get_relcalid(c); if (relcalid == NULL) { snprintf(uidbuf, 256, "baduid%d-%d", getpid(), bad_uid_counter++); key.data = uidbuf; } else { key.data = relcalid; } } key.size = (u_int32_t) strlen(key.data); str = icalcomponent_as_ical_string_r(c); data.data = str; data.size = (u_int32_t) strlen(str); if ((ret = dbcp->c_put(dbcp, &key, &data, DB_KEYLAST)) != 0) { if (ret == DB_LOCK_DEADLOCK) { deadlocked = 1; } else if (ret == DB_RUNRECOVERY) { ICAL_DB_ENV->err(ICAL_DB_ENV, ret, "c_put failed."); abort(); } else { ICAL_DB_ENV->err(ICAL_DB_ENV, ret, "c_put failed %s.", str); /* continue to try to put as many icalcomponent as possible */ reterr = ICAL_INTERNAL_ERROR; } } } if (str) { free(str); } if (deadlocked) { dbcp->c_close(dbcp); tid->abort(tid); retry++; continue; } if ((ret = dbcp->c_close(dbcp)) != 0) { tid->abort(tid); if (ret == DB_LOCK_DEADLOCK) { retry++; continue; } else if (ret == DB_RUNRECOVERY) { ICAL_DB_ENV->err(ICAL_DB_ENV, ret, "c_closed failed."); abort(); } else { ICAL_DB_ENV->err(ICAL_DB_ENV, ret, "c_closed failed."); reterr = ICAL_INTERNAL_ERROR; } } if ((ret = tid->commit(tid, 0)) != 0) { tid->abort(tid); if (ret == DB_LOCK_DEADLOCK) { retry++; continue; } else if (ret == DB_RUNRECOVERY) { ICAL_DB_ENV->err(ICAL_DB_ENV, ret, "commit failed."); abort(); } else { ICAL_DB_ENV->err(ICAL_DB_ENV, ret, "commit failed."); reterr = ICAL_INTERNAL_ERROR; } } done = 1; } bset->changed = 0; return reterr; }
/* This populates a cluster with the entire contents of a database */ static icalerrorenum icalbdbset_read_database(icalbdbset *bset, char *(*pfunc) (const DBT *dbt)) { DB *dbp; DBC *dbcp; DBT key, data; char *str; int ret = EINVAL; char keystore[256]; char datastore[1024]; char *more_mem = NULL; DB_TXN *tid; _unused(pfunc); memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); if (bset->sdbp) { dbp = bset->sdbp; } else { dbp = bset->dbp; } if (!dbp) { return ICAL_FILE_ERROR; } bset->cluster = icalcomponent_new(ICAL_XROOT_COMPONENT); if ((ret = ICAL_DB_ENV->txn_begin(ICAL_DB_ENV, NULL, &tid, 0)) != 0) { /*char *foo = db_strerror(ret); */ abort(); } /* acquire a cursor for the database */ if ((ret = dbp->cursor(dbp, tid, &dbcp, 0)) != 0) { dbp->err(dbp, ret, "primary index"); goto err1; } key.flags = DB_DBT_USERMEM; key.data = keystore; key.ulen = (u_int32_t) sizeof(keystore); data.flags = DB_DBT_USERMEM; data.data = datastore; data.ulen = (u_int32_t) sizeof(datastore); /* fetch the key/data pair */ while (1) { ret = dbcp->c_get(dbcp, &key, &data, DB_NEXT); if (ret == DB_NOTFOUND) { break; } else if (ret == ENOMEM) { if (more_mem) { free(more_mem); } more_mem = malloc(data.ulen + 1024); data.data = more_mem; data.ulen = data.ulen + 1024; } else if (ret == DB_LOCK_DEADLOCK) { /*char *foo = db_strerror(ret); */ abort(); /* should retry in case of DB_LOCK_DEADLOCK */ } else if (ret) { /*char *foo = db_strerror(ret); */ /* some other weird-ass error */ dbp->err(dbp, ret, "cursor"); abort(); } else { icalcomponent *cl; /* this prevents an array read bounds error */ if ((str = (char *)calloc(data.size + 1, sizeof(char))) == NULL) { goto err2; } memcpy(str, (char *)data.data, data.size); cl = icalparser_parse_string(str); icalcomponent_add_component(bset->cluster, cl); free(str); } } if (ret != DB_NOTFOUND) { goto err2; } if (more_mem) { free(more_mem); more_mem = NULL; } if ((ret = dbcp->c_close(dbcp)) != 0) { /*char *foo = db_strerror(ret); */ abort(); /* should retry in case of DB_LOCK_DEADLOCK */ } if ((ret = tid->commit(tid, 0)) != 0) { /*char *foo = db_strerror(ret); */ abort(); } return ICAL_NO_ERROR; err2: if (more_mem) { free(more_mem); } dbcp->c_close(dbcp); abort(); /* should retry in case of DB_LOCK_DEADLOCK */ return ICAL_INTERNAL_ERROR; err1: dbp->err(dbp, ret, "cursor index"); abort(); return ICAL_FILE_ERROR; }
int main(int argc, char **argv) { DB *dbp; DB_ENV *dbenv; DB_TXN *xid; DBT key, data; db_recno_t recno; int num_trials; int ret; char c; double r; int start, end; char first_printable_ascii = ' '; char last_printable_ascii = '~'; int ascii_length = (int)(last_printable_ascii - first_printable_ascii); char *ASCII = (char *)malloc(sizeof(char) * ascii_length); char *string; for (c = 0; c < ascii_length; c++) { ASCII[(int)c] = first_printable_ascii + c; } if (argc != 2) { printf("usage: %s <num trials>\n", argv[0]); exit(-1); } env_dir_create(); env_open(&dbenv); if (db_open(dbenv, &dbp, DATABASE, 0)) return (1); memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); num_trials = atoi(argv[1]); for (recno = 1; (int)recno <= num_trials; recno++) { start = 0; end = 0; while (start == end) { r = ((double)rand()/(double)((double)RAND_MAX+1)); /* returns [0, 1)*/ r = r*ascii_length; start = (int)r; /* an int in the rand [0, ascii_length) */ r = ((double)rand()/(double)((double)RAND_MAX+1)); /* re turns [0, 1)*/ r = r*ascii_length; end = (int)r; /* an int in the rand [0, ascii_length) */ } if (end < start) { int swap = start; start = end; end = swap; } string = (char *)malloc(sizeof(char) * (end - start) + 1); strncpy(string, ASCII + start, end-start); string[end-start] = '\0'; /* make the string null terminated */ dbenv->txn_begin(dbenv, NULL, &xid, 0); key.size = sizeof(recno); key.data = &recno; data.size = strlen(string) + 1; // + 1 for the null terminator data.data = string; switch (ret = dbp->put(dbp, xid, &key, &data, 0)) { case 0: xid->commit(xid, 0); break; default: dbp->err(dbp, ret, "DB->put"); xid->abort(xid); break; } } return 0; }
int load_metadata_columns(bdb_table_p _tp) { int ret,n,len; char dbuf[MAX_ROW_SIZE]; char *s = NULL; char cn[64], ct[16]; DB *db = NULL; DBT key, data; bdb_col_p col; ret = n = len = 0; if(!_tp || !_tp->db) return -1; if(_tp->ncols!=0) return 0; db = _tp->db; memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); memset(dbuf, 0, MAX_ROW_SIZE); key.data = METADATA_COLUMNS; key.size = strlen(METADATA_COLUMNS); /*memory for the result*/ data.data = dbuf; data.ulen = MAX_ROW_SIZE; data.flags = DB_DBT_USERMEM; if ((ret = db->get(db, NULL, &key, &data, 0)) != 0) { db->err(db, ret, "load_metadata_columns DB->get failed"); ERR("FAILED to find METADATA_COLUMNS in DB \n"); return -1; } /* eg: dbuf = "bdb_table_name(str) bdb_table_version(int)" */ s = strtok(dbuf, " "); while(s!=NULL && n<MAX_NUM_COLS) { /* eg: meta[0]=table_name meta[1]=str */ sscanf(s,"%20[^(](%10[^)])[^\n]", cn, ct); /* create column*/ col = (bdb_col_p) pkg_malloc(sizeof(bdb_col_t)); if(!col) { ERR("out of private memory \n"); return -1; } /* set name*/ len = strlen( cn ); col->name.s = (char*)pkg_malloc(len * sizeof(char)); memcpy(col->name.s, cn, len ); col->name.len = len; /*set column type*/ if(strncmp(ct, "str", 3)==0) { col->type = DB_STR; } else if(strncmp(ct, "int", 3)==0) { col->type = DB_INT; } else if(strncmp(ct, "double", 6)==0) { col->type = DB_DOUBLE; } else if(strncmp(ct, "datetime", 8)==0) { col->type = DB_DATETIME; } else { col->type = DB_STR; } col->flag = 0; _tp->colp[n] = col; n++; _tp->ncols++; s=strtok(NULL, " "); } return 0; }
/************************************************************************************* * slår opp i databasen for å finne DoCID for en url *************************************************************************************/ int getDocIDFromUrl(char bdbfiledir[],char url[],unsigned int *DocID) { unsigned int crc32Value; int dbFileForUrl; int ret; DB *dbp; static char inited; static DB *dbp_store[nrOfUrlToDocIDFiles]; DBT key, data; char fileName[256]; crc32Value = crc32boitho(url); dbFileForUrl = (crc32Value % nrOfUrlToDocIDFiles); if (inited == 0) { int i; for(i = 0; i < nrOfUrlToDocIDFiles; i++) { sprintf(fileName,"%s%i.db",bdbfiledir,i); /* Create and initialize database object */ if ((ret = db_create(&dbp, NULL, 0)) != 0) { fprintf(stderr, "%s: db_create: %s\n", "getDocIDFromUrl", db_strerror(ret)); return (EXIT_FAILURE); } /* open the database. */ //if ((ret = dbp->open(dbp, NULL, fileName, NULL, DB_BTREE, DB_CREATE, 0444)) != 0) { if ((ret = dbp->open(dbp, NULL, fileName, NULL, DB_BTREE, DB_RDONLY, 0444)) != 0) { dbp->err(dbp, ret, "%s: open", fileName); //goto err1; } dbp_store[i] = dbp; } inited = 1; } dbp = dbp_store[dbFileForUrl]; //finner ut hvilken database vi skal opne //lager en has verdi slik at vi kan velge en av filene #ifdef DEBUG printf("Openig db %s\n",fileName); #endif /* Initialize the key/data pair so the flags aren't set. */ memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); key.data = url; key.size = strlen(url); /* Walk through the database and print out the key/data pairs. */ if ((ret = dbp->get(dbp, NULL, &key, &data, 0)) == 0) { //printf("%s : %u-%i \n", key.data, *(int *)data.data,rLotForDOCid(*(int *)data.data)); *DocID = *(int *)data.data; return 1; } else if (ret == DB_NOTFOUND) { #ifdef DEBUG dbp->err(dbp, ret, "DBcursor->get"); #endif return 0; } else { dbp->err(dbp, ret, "DBcursor->get"); return 0; } }
static bool bucket_list_keys(struct client *cli, const char *user, const char *bucket) { GHashTable *param; enum errcode err = InternalError; char *prefix, *marker, *maxkeys_str, *delim, *s; int maxkeys = 100, i, rc; GList *content, *tmpl; size_t pfx_len; struct bucket_list_info bli; bool rcb; DB_ENV *dbenv = tdbrep.tdb.env; DB_TXN *txn = NULL; DB *objs = tdbrep.tdb.objs; DBC *cur = NULL; DBT pkey, pval; struct db_obj_key *obj_key; size_t alloc_len; bool seen_prefix = false; int get_flags; /* verify READ access */ if (!user || !has_access(user, bucket, NULL, "READ")) { err = AccessDenied; goto err_out; } /* parse URI query string */ param = hreq_query(&cli->req); if (!param) goto err_out; /* read useful params from query string */ prefix = g_hash_table_lookup(param, "prefix"); pfx_len = prefix ? strlen(prefix) : 0; marker = g_hash_table_lookup(param, "marker"); delim = g_hash_table_lookup(param, "delimiter"); maxkeys_str = g_hash_table_lookup(param, "max-keys"); if (maxkeys_str) { i = atoi(maxkeys_str); if (i > 0 && i < maxkeys) maxkeys = i; } /* open transaction */ rc = dbenv->txn_begin(dbenv, NULL, &txn, 0); if (rc) { dbenv->err(dbenv, rc, "DB_ENV->txn_begin"); goto err_out; } /* search for (bucket, *) in object database, to see if * any objects associated with this bucket exist */ rc = objs->cursor(objs, txn, &cur, 0); if (rc) { objs->err(objs, rc, "objs->cursor"); goto err_out; } alloc_len = sizeof(*obj_key) + (marker ? strlen(marker) : pfx_len) + 1; obj_key = alloca(alloc_len); memset(obj_key, 0, alloc_len); strncpy(obj_key->bucket, bucket, sizeof(obj_key->bucket)); strcpy(obj_key->key, marker ? marker : prefix ? prefix : ""); memset(&pkey, 0, sizeof(pkey)); pkey.data = obj_key; pkey.size = alloc_len; memset(&bli, 0, sizeof(bli)); bli.prefix = prefix; bli.pfx_len = pfx_len; bli.delim = delim; bli.common_pfx = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL); bli.maxkeys = maxkeys; /* iterate through each returned data row */ get_flags = DB_SET_RANGE; while (1) { struct obj_vitals v; struct db_obj_key *tmpkey; struct db_obj_ent *obj; memset(&pval, 0, sizeof(pval)); pval.flags = DB_DBT_MALLOC; rc = cur->get(cur, &pkey, &pval, get_flags); if (rc) { if (rc != DB_NOTFOUND) objs->err(objs, rc, "bucket_list_keys iter"); break; } get_flags = DB_NEXT; tmpkey = pkey.data; obj = pval.data; if (strcmp(tmpkey->bucket, bucket)) { free(obj); break; } if (prefix) { if (strncmp(tmpkey->key, prefix, pfx_len) != 0) { free(obj); if (!seen_prefix) /* continue searching for * a record that begins with this * prefix */ continue; else /* no more records with our prefix */ break; } seen_prefix = true; } memset(&v, 0, sizeof(v)); strcpy(v.md5, obj->md5); strncpy(v.owner, obj->owner, sizeof(v.owner)-1); if (!(GUINT32_FROM_LE(obj->flags) & DB_OBJ_INLINE)) memcpy(&v.addr, &obj->d.a, sizeof(v.addr)); v.mtime = GUINT64_FROM_LE(obj->mtime); v.size = GUINT64_FROM_LE(obj->size); free(obj); if (bucket_list_iter(tmpkey->key, &v, &bli)) break; } /* close cursor, transaction */ rc = cur->close(cur); if (rc) { objs->err(objs, rc, "objs->cursor close"); goto err_out_rb; } rc = txn->commit(txn, 0); if (rc) { dbenv->err(dbenv, rc, "DB_ENV->txn_commit"); goto err_out_param; } s = g_markup_printf_escaped( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" "<ListBucketResult xmlns=\"http://indy.yyz.us/doc/2006-03-01/\">\r\n" " <Name>%s</Name>\r\n" " <MaxKeys>%d</MaxKeys>\r\n" " <IsTruncated>%s</IsTruncated>\r\n", bucket, maxkeys, bli.trunc ? "true" : "false"); content = g_list_append(NULL, s); if (prefix) { s = g_markup_printf_escaped(" <Prefix>%s</Prefix>\n", prefix); content = g_list_append(content, s); } if (marker) { s = g_markup_printf_escaped(" <Marker>%s</Marker>\n", marker); content = g_list_append(content, s); } tmpl = bli.res; while (tmpl) { char timestr[64]; struct obj_vitals *vp; vp = tmpl->data; tmpl = tmpl->next; /* * FIXME Use the vp->addr to verify that key still exists. * And if it doesn't, then what? (addr.nid can be 0 for inline) */ s = g_markup_printf_escaped( " <Contents>\r\n" " <Key>%s</Key>\r\n" " <LastModified>%s</LastModified>\r\n" " <ETag>%s</ETag>\r\n" " <Size>%llu</Size>\r\n" " <StorageClass>STANDARD</StorageClass>\r\n" " <Owner>\r\n" " <ID>%s</ID>\r\n" " <DisplayName>%s</DisplayName>\r\n" " </Owner>\r\n" " </Contents>\r\n", vp->key, hutil_time2str(timestr, sizeof(timestr), vp->mtime / 1000000), vp->md5, (unsigned long long) vp->size, vp->owner, vp->owner); content = g_list_append(content, s); free(vp->key); free(vp); } g_list_free(bli.res); content = bucket_list_pfx(content, bli.common_pfx, bli.delim); s = strdup("</ListBucketResult>\r\n"); content = g_list_append(content, s); free(bli.last_comp); g_hash_table_destroy(bli.common_pfx); g_hash_table_destroy(param); rcb = cli_resp_xml(cli, 200, content); g_list_free(content); return rcb; err_out_rb: rc = txn->abort(txn); if (rc) dbenv->err(dbenv, rc, "DB_ENV->txn_abort"); err_out_param: g_hash_table_destroy(param); err_out: return cli_err(cli, err); }