示例#1
0
bool Bdb::del(void *key, uint klen)
{
    DBT dbkey, dbval;
    memset(&dbkey, 0, sizeof(DBT));
    memset(&dbval, 0, sizeof(DBT));
    dbkey.data = key;
    dbkey.size = klen;
    dbval.flags = DB_DBT_MALLOC;
    int ret;
    // for each data
    DBC *cursor = NULL;
    ret = d_ptr->db->cursor(d_ptr->db, NULL, &cursor, 0);
    if (ret)
    {
        return false;
    }
    ret = cursor->get(cursor, &dbkey, &dbval, DB_SET);
    if (ret == DB_NOTFOUND)
    {
        return false;
    }
    cursor->del(cursor, 0);
    cursor->close(cursor);
    return true;
}
示例#2
0
void
bdb_remove(uint64_t keyno, int *notfoundp)
{
	DBC *dbc = g.dbc;
	size_t size;
	int ret;

	size = 0;
	key_gen(&keyitem, keyno);
	key.data = (void *)keyitem.data;
	key.size = (u_int32_t)keyitem.size;

	bdb_read(keyno, &value.data, &size, notfoundp);
	value.size = (u_int32_t)size;
	if (*notfoundp)
		return;

	/* Deleting a fixed-length item is the same as setting the bits to 0. */
	if (g.type == FIX)
		bdb_update(key.data, key.size, "", 1);
	else
		if ((ret = dbc->del(dbc, 0)) != 0) {
			if (ret != DB_NOTFOUND)
				bdb_die(ret, "dbc.del: {%.*s}",
				    (int)key.size, (char *)key.data);
			*notfoundp = 1;
		}
}
/**
 @brief
 @return true (k1,k2) existed, remove success
        false (k1,k2) not existed, nothing done
 */
bool kmapdset_base::remove_impl(const void* k1, const void* k2, DB_TXN* txn, const char* func)
{
	PortableDataOutput<AutoGrownMemIO> oKey1, oData;
	oData.resize(4*1024);
	save_key1(oKey1, k1);
	save_key2(oData, k2);
	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) {
			ret = curp->del(curp, 0);
		}
		curp->close(curp);
		return 0 == ret;
	}
	else
	{
		string_appender<> oss;
		oss << db_strerror(ret)
			<< "... at: " << func
			<< "\n"
			;
		throw std::runtime_error(oss.str());
	}
}
示例#4
0
static bool delete_cursor(void* k, void* d, void* c)
{
    DBC* cursor = (DBC*)c;
    if (cursor->del(cursor, 0) != 0)
    {
        trace_msg("failed to remove bdb cursor.");
    }
    return true;
}
示例#5
0
文件: cursor.c 项目: cran/RBerkeley
/* {{{ rberkeley_dbcursor_del */
SEXP rberkeley_dbcursor_del (SEXP _dbc, SEXP _flags)
{
  DBC *dbc;
  u_int32_t flags;
  int ret;

  dbc = R_ExternalPtrAddr(_dbc);
  if(R_ExternalPtrTag(_dbc) != install("DBC") || dbc == NULL)
    error("invalid 'dbc' handle");
  flags = (u_int32_t)INTEGER(_flags)[0];

  ret = dbc->del(dbc, flags);

  return ScalarInteger(ret);
}
示例#6
0
文件: kvs_bdb.c 项目: Machyne/mongo
static int
kvs_cursor_remove(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;

	/*
	 * WiredTiger's "remove" of a bitfield is really an update with a value
	 * of a single byte of zero.
	 */
	if (cursor->config_bitfield) {
		wtcursor->value.size = 1;
		wtcursor->value.data = "\0";
		return (kvs_cursor_update(wtcursor));
	}

	if ((ret = copyin_key(wtcursor)) != 0)
		return (ret);

	if ((ret = dbc->get(dbc, key, value, DB_SET)) != 0) {
		if (ret == DB_NOTFOUND || ret == DB_KEYEMPTY)
			return (WT_NOTFOUND);
		ERET(wt_api,
		    session, WT_ERROR, "DbCursor.get: %s", db_strerror(ret));
	}
	if ((ret = dbc->del(dbc, 0)) != 0)
		ERET(wt_api,
		    session, WT_ERROR, "DbCursor.del: %s", db_strerror(ret));

	return (0);
}
示例#7
0
文件: bdb.c 项目: qixin/wiredtiger
void
bdb_remove(uint64_t keyno, int *notfoundp)
{
	DBC *dbc = g.dbc;
	int ret;

	key.data = keybuf;
	key_gen(key.data, &key.size, keyno, 0);

	bdb_read(keyno, &value.data, &value.size, notfoundp);
	if (*notfoundp)
		return;

	if ((ret = dbc->del(dbc, 0)) != 0) {
		if (ret != DB_NOTFOUND)
			die(ret, "dbc.del: {%.*s}",
			    (int)key.size, (char *)key.data);
		*notfoundp = 1;
	}
}
示例#8
0
文件: bdb.c 项目: rmdir/sendmailornot
void 
bdb_zero_database(const char *dbpath, int blacklist) 
{
	DB *map;
	DBC *cursor;
	DBT key, data;
	int score;
	map = _bdb_open_database(dbpath);
	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));
	map->cursor(map, NULL, &cursor, 0);
	data.data = &score;
	data.ulen = sizeof(int);
	data.flags = DB_DBT_USERMEM;
	while(cursor->get(cursor, &key, &data, DB_NEXT) == 0) {
		if(score != blacklist)
			cursor->del(cursor, 0);
	}
	cursor->close(cursor); 
	map->close(map, 0);
}
示例#9
0
void
bdb_truncate(uint64_t start, uint64_t stop)
{
	DBC *dbc = g.dbc;
	size_t len;
	int cmp, ret, notfound;

	/* Deleting a fixed-length item is the same as setting the bits to 0. */
	if (g.type == FIX) {
		/*
		 * If we're deleting from/to the start/end of the database,
		 * correct for the number of records we have.
		 */
		if (start == 0)
			start = 1;
		if (stop == 0)
			stop = g.rows;
		for (; start <= stop; ++start)
			bdb_remove(start, &notfound);
		return;
	}

	if (start == 0) {
		ret = dbc->get(dbc, &key, &value, DB_FIRST);
		if (ret != 0 && ret != DB_NOTFOUND)
			bdb_die(ret, "%s", "dbc.get: DB_FIRST");
	} else {
		key_gen(&keyitem, start);
		key.data = (void *)keyitem.data;
		key.size = (u_int32_t)keyitem.size;
		ret = dbc->get(dbc, &key, &value, DB_SET_RANGE);
		if (ret != 0 && ret != DB_NOTFOUND)
			bdb_die(ret, "dbc.get: DB_SET: {%.*s}",
			    (int)key.size, (char *)key.data);
	}
	if (ret == DB_NOTFOUND)
		return;

	if (stop == 0) {
		do {
			ret = dbc->del(dbc, 0);
			if (ret != 0 && ret != DB_NOTFOUND)
				bdb_die(ret, "dbc.del: {%.*s}",
				    (int)key.size, (char *)key.data);
		} while ((ret = dbc->get(dbc, &key, &value, DB_NEXT)) == 0);
	} else {
		key_gen(&keyitem, stop);
		do {
			len = WT_MIN(key.size, keyitem.size);
			cmp = memcmp(key.data, keyitem.data, len);
			if (g.c_reverse) {
				if (cmp < 0 ||
				    (cmp == 0 && key.size < keyitem.size))
					break;
			} else
				if (cmp > 0 ||
				    (cmp == 0 && key.size > keyitem.size))
					break;
			ret = dbc->del(dbc, 0);
			if (ret != 0 && ret != DB_NOTFOUND)
				bdb_die(ret, "dbc.del: {%.*s}",
				    (int)key.size, (char *)key.data);
		} while ((ret = dbc->get(dbc, &key, &value, DB_NEXT)) == 0);
	}
	if (ret != 0 && ret != DB_NOTFOUND)
		bdb_die(ret, "%s", "dbc.get: DB_NEXT");
}
示例#10
0
int        
jack_remove_properties (jack_client_t* client, jack_uuid_t subject)
{
        DBT key;
        DBT data;
        DBC* cursor;
        int ret;
        char ustr[JACK_UUID_STRING_SIZE];
        int retval = 0;
        uint32_t cnt = 0;

        jack_uuid_unparse (subject, ustr);

        if (jack_property_init (NULL)) {
                return -1;
        }

        if ((ret = db->cursor (db, NULL, &cursor, 0)) != 0) {
                jack_error ("Cannot create cursor for metadata search (%s)", db_strerror (ret));
                return -1;
        }

        memset(&key, 0, sizeof(key));
	memset(&data, 0, sizeof(data));
        data.flags = DB_DBT_MALLOC;

        while ((ret = cursor->get(cursor, &key, &data, DB_NEXT)) == 0) {

                /* require 2 extra chars (data+null) for key,
                   which is composed of UUID str plus a key name
                */

                if (key.size < JACK_UUID_STRING_SIZE + 2) {
                        if (data.size > 0) {
                                free (data.data);
                        }
                        continue;
                }

                if (memcmp (ustr, key.data, JACK_UUID_STRING_SIZE) != 0) {
                        /* not relevant */
                        if (data.size > 0) {
                                free (data.data);
                        }
                        continue;
                }

                if ((ret = cursor->del (cursor, 0)) != 0) {
                        jack_error ("cannot delete property (%s)", db_strerror (ret));
                        /* don't return -1 here since this would leave things
                           even more inconsistent. wait till the cursor is finished
                        */
                        retval = -1;
                }
                cnt++;
        }

        cursor->close (cursor);

        if (cnt) {
                jack_property_change_notify (client, subject, NULL, PropertyDeleted);
        }

        if (retval) {
                return -1;
        } 

        return cnt;
}
示例#11
0
文件: update.c 项目: hfmanson/tlspool
/* Internal function to add or remove key/value pairs.  When rm is zero,
 * the entry will be added; otherwise, it will be removed.  Even when
 * adding, a matching record will be removed.
 *
 * The part of the database record to match is found by trimming both the
 * provided value and the database value to at most trimlen bytes and
 * comparing what remains.  Note that this comparison must also match
 * in size, so trimming to any size longer than the provided value will
 * cause a mismatch if the database value is larger.  Negative values
 * of trimlen are treated as infinite, and will thus take full records
 * into account, including their lengths.
 *
 * Another comparison trick is the inclusion of the trailing NUL char
 * at the end of a PKCS #11 URI or validation expression string.
 *
 * The array mask4 can be used to indicate flags for the first four bytes,
 * which is a big-endian flag value in localid and trust databases.
 * When NULL is provided, it will match everything.
 *
 * The function returns 1 on succes, 0 on failure.  Note that it is not
 * an error if a record to remove had already gone, or when a matching
 * record had to be removed before adding one.  This is considered to
 * be the result of manual overrides.  Failure of this function solely
 * refers to issues of a technical nature, such as I/O problems or
 * running out of memory.
 *
 * All operations on the database are excuted within the current
 * transaction; the call from api.c has ensured that one exists.
 */
static int update_db (struct pulleyback_tlspool *self,
				dercursor *key, dercursor *value,
				int trimlen, const uint8_t *mask4,
				int rm) {
	int ok = 1;
	int gotcrs = 0;
	int nomore = 1;
	DBC *crs;
	DBT db_key;
	DBT db_val;
	DBT db_got;
	uint8_t my_mask4 [] = { 0xff, 0xff, 0xff, 0xff };
	if (mask4 == NULL) {
		// When no mask4 provided, match everything
		mask4 = my_mask4;
	}
	if (trimlen < 0) {
		// Standardise on positive values that act as "infinite"
		trimlen = value->derlen + 1;
	}
	gotcrs =
	ok = ok && (0 == self->db->cursor (self->db, self->txn, &crs, 0));
	memset (&db_key, 0, sizeof (db_key));
	db_key.data = key->derptr;
	db_key.size = key->derlen;
	memset (&db_val, 0, sizeof (db_val));
	db_val.data = value->derptr;
	db_val.size = value->derlen;
	memset (&db_got, 0, sizeof (db_got));
	//OLD// dbt_init_fixbuf (&db_key, key  ->derptr, key  ->derlen);
	//OLD// dbt_init_fixbuf (&db_val, value->derptr, value->derlen);
	//OLD// dbt_init_empty  (&db_got);
	nomore = crs->get (crs, &db_key, &db_got, DB_SET);
	while (!nomore) {
		int match = 1;
		int i;
		for (i = 0; match && (i < trimlen); i++) {
			match = match && (i < db_val.size);
			match = match && (i < db_got.size);
			if (!match) {
				// Final decision; only match for same sizes
				match = (db_val.size == db_got.size);
				break;
			} else {
				uint8_t m, a, b;
				m = (i < 4)? mask4 [i]: 0xff;
				a = m & ((uint8_t *) db_val.data) [i];
				b = m & ((uint8_t *) db_got.data) [i];
				match = (a == b);
			}
		}
		if (match) {
			crs->del (crs, 0);
		}
		nomore = crs->get (crs, &db_key, &db_got, DB_NEXT_DUP);
	}
	ok = ok && (nomore == DB_NOTFOUND);
	if (gotcrs) {
		if (0 != crs->close (crs)) {
			fprintf (stderr, "Failed to close cursor\n");
		}
	}
	if (!rm) {
		ok = ok && (0 == self->db->put (
		                 self->db, self->txn, &db_key, &db_val, 0));
	}
	//OLD// // Not ours, so don't free // dbt_free (&db_got);
	//OLD// // Static,   so don't free // dbt_free (&db_val);
	//OLD// // Static,   so don't free // dbt_free (&db_key);
	return ok;
}