bool Bdb::over(void* data, uint dlen, void* key, uint klen) { DBT dbkey, dbval; memset(&dbkey, 0, sizeof(DBT)); memset(&dbval, 0, sizeof(DBT)); dbkey.data = key; dbkey.size = klen; dbval.data = data; dbval.size = dlen; DBT tmpval; memset(&tmpval, 0, sizeof(DBT)); DBC* cursor = NULL; if (d_ptr->db->cursor(d_ptr->db, NULL, &cursor, 0)) return false; if (cursor->get(cursor, &dbkey, &tmpval, DB_SET)) { int ret = d_ptr->db->put(d_ptr->db, NULL, &dbkey, &dbval, DB_OVERWRITE_DUP); cursor->close(cursor); return ret == 0; } int ret = cursor->put(cursor, &dbkey, &dbval, DB_CURRENT); return ret == 0; }
/* {{{ rberkeley_dbcursor_put */ SEXP rberkeley_dbcursor_put (SEXP _dbc, SEXP _key, SEXP _data, SEXP _flags) { DBC *dbc; DBT key, data; u_int32_t flags; int ret; flags = (u_int32_t)INTEGER(_flags)[0]; if(flags != DB_AFTER && flags != DB_BEFORE && flags != DB_CURRENT && flags != DB_KEYFIRST && flags != DB_KEYLAST && flags != DB_NODUPDATA) { error("incorrect flags value"); } dbc = R_ExternalPtrAddr(_dbc); if(R_ExternalPtrTag(_dbc) != install("DBC") || dbc == NULL) error("invalid 'dbc' handle"); memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); key.data = (unsigned char *)RAW(_key); key.size = length(_key); data.data = (unsigned char *)RAW(_data); data.size = length(_data); ret = dbc->put(dbc, &key, &data, flags); return ScalarInteger(ret); }
bool Bdb::put(void* data, uint dlen, void* key, uint klen) { DBT dbkey, dbval; memset(&dbkey, 0, sizeof(DBT)); memset(&dbval, 0, sizeof(DBT)); dbkey.data = key; dbkey.size = klen; dbval.data = data; dbval.size = dlen; if (dup) { DBC *cursor = NULL; DBT tmpval; memset(&tmpval, 0, sizeof(DBT)); int ret = d_ptr->db->cursor(d_ptr->db, NULL, &cursor, 0); if (ret) return false; ret = cursor->get(cursor, &dbkey, &tmpval, DB_SET); if (ret) { ret = d_ptr->db->put(d_ptr->db, NULL, &dbkey, &dbval, DB_OVERWRITE_DUP); } else { ret = cursor->put(cursor, &dbkey, &dbval, DB_AFTER); } cursor->close(cursor); return ret == 0; } int ret = d_ptr->db->put(d_ptr->db, NULL, &dbkey, &dbval, DB_OVERWRITE_DUP); return ret == 0; }
static int kvs_cursor_update(WT_CURSOR *wtcursor) { CURSOR_SOURCE *cursor; DBC *dbc; DBT *key, *value; WT_EXTENSION_API *wt_api; WT_SESSION *session; int ret = 0; session = wtcursor->session; cursor = (CURSOR_SOURCE *)wtcursor; wt_api = cursor->wt_api; dbc = cursor->dbc; key = &cursor->key; value = &cursor->value; if ((ret = copyin_key(wtcursor)) != 0) return (ret); copyin_value(wtcursor); if ((ret = dbc->put(dbc, key, value, DB_KEYFIRST)) != 0) ERET(wt_api, session, WT_ERROR, "DbCursor.put: %s", db_strerror(ret)); return (0); }
/** @brief replace OR insert a record @note if not thrown an exception, always success @return true replace the record false insert the record @throws exception, failed */ bool kmapdset_base::replace_impl(const void* k1, const void* d, DB_TXN* txn, const char* func) { PortableDataOutput<AutoGrownMemIO> oKey1, oData; try { save_key1(oKey1, k1); save_data(oData, d); } catch (const IOException& exp) { string_appender<> oss; oss << exp.what() << "... at: " << func; throw std::runtime_error(oss.str()); } DBT tk1; memset(&tk1, 0, sizeof(DBT)); tk1.data = oKey1.begin(); tk1.size = oKey1.tell(); DBT tdd; memset(&tdd, 0, sizeof(DBT)); tdd.data = oData.begin(); tdd.size = oData.tell(); DBC* curp = NULL; int ret = m_db->cursor(m_db, txn, &curp, 0); if (0 == ret) { ret = curp->get(curp, &tk1, &tdd, DB_GET_BOTH); if (0 == ret) { tk1.data = oKey1.begin(); tk1.size = oKey1.tell(); tdd.data = oData.begin(); tdd.size = oData.tell(); ret = curp->put(curp, &tk1, &tdd, DB_CURRENT); curp->close(curp); if (0 == ret) return true; } else if (DB_NOTFOUND == ret) { ret = curp->put(curp, &tk1, &tdd, DB_NODUPDATA); curp->close(curp); if (0 == ret) return false; } } string_appender<> oss; oss << db_strerror(ret) << "... at: " << func << "\n" ; throw std::runtime_error(oss.str()); }
void bdb_insert( const void *key_data, uint32_t key_size, const void *value_data, uint32_t value_size) { DBC *dbc; key.data = (void *)key_data; key.size = key_size; value.data = (void *)value_data; value.size = value_size; dbc = g.dbc; assert(dbc->put(dbc, &key, &value, DB_KEYFIRST) == 0); }
void bdb_update(const void *arg_key, size_t arg_key_size, const void *arg_value, size_t arg_value_size) { DBC *dbc = g.dbc; int ret; key.data = (void *)arg_key; key.size = (u_int32_t)arg_key_size; value.data = (void *)arg_value; value.size = (u_int32_t)arg_value_size; if ((ret = dbc->put(dbc, &key, &value, DB_KEYFIRST)) != 0) bdb_die(ret, "dbc.put: DB_KEYFIRST: {%.*s}{%.*s}", (int)key.size, (char *)key.data, (int)value.size, (char *)value.data); }
void bdb_update(const void *arg_key, uint32_t arg_key_size, const void *arg_value, uint32_t arg_value_size, int *notfoundp) { DBC *dbc = g.dbc; int ret; key.data = (void *)arg_key; key.size = arg_key_size; value.data = (void *)arg_value; value.size = arg_value_size; *notfoundp = 0; if ((ret = dbc->put(dbc, &key, &value, DB_KEYFIRST)) != 0) { if (ret != DB_NOTFOUND) { die(ret, "dbc.put: DB_KEYFIRST: {%.*s}{%.*s}", (int)key.size, (char *)key.data, (int)value.size, (char *)value.data); } *notfoundp = 1; } }
static int kvs_cursor_insert(WT_CURSOR *wtcursor) { CURSOR_SOURCE *cursor; DB *db; DBC *dbc; DBT *key, *value; WT_EXTENSION_API *wt_api; WT_SESSION *session; int ret = 0; session = wtcursor->session; cursor = (CURSOR_SOURCE *)wtcursor; wt_api = cursor->wt_api; dbc = cursor->dbc; db = cursor->db; key = &cursor->key; value = &cursor->value; if ((ret = copyin_key(wtcursor)) != 0) return (ret); copyin_value(wtcursor); if (cursor->config_append) { /* * Berkeley DB cursors have no operation to append/create a * new record and set the cursor; use the DB handle instead * then set the cursor explicitly. * * When appending, we're allocating and returning a new record * number. */ if ((ret = db->put(db, NULL, key, value, DB_APPEND)) != 0) ERET(wt_api, session, WT_ERROR, "Db.put: %s", db_strerror(ret)); wtcursor->recno = *(db_recno_t *)key->data; if ((ret = dbc->get(dbc, key, value, DB_SET)) != 0) ERET(wt_api, session, WT_ERROR, "DbCursor.get: %s", db_strerror(ret)); } else if (cursor->config_overwrite) { if ((ret = dbc->put(dbc, key, value, DB_KEYFIRST)) != 0) ERET(wt_api, session, WT_ERROR, "DbCursor.put: %s", db_strerror(ret)); } else { /* * Berkeley DB cursors don't have a no-overwrite flag; use * the DB handle instead then set the cursor explicitly. */ if ((ret = db->put(db, NULL, key, value, DB_NOOVERWRITE)) != 0) { if (ret == DB_KEYEXIST) return (WT_DUPLICATE_KEY); ERET(wt_api, session, WT_ERROR, "Db.put: %s", db_strerror(ret)); } if ((ret = dbc->get(dbc, key, value, DB_SET)) != 0) ERET(wt_api, session, WT_ERROR, "DbCursor.get: %s", db_strerror(ret)); } return (0); }