Added variant of truncate that truncates all open databases.
This commit is contained in:
parent
aa24f888c0
commit
f1342dc2bd
3 changed files with 49 additions and 7 deletions
|
@ -621,8 +621,10 @@ static int bdberl_drv_control(ErlDrvData handle, unsigned int cmd,
|
||||||
// Make sure this port currently has dbref open -- if it doesn't, error out. Of note,
|
// Make sure this port currently has dbref open -- if it doesn't, error out. Of note,
|
||||||
// if it's in our list, we don't need to grab the RWLOCK, as we don't have to worry about
|
// if it's in our list, we don't need to grab the RWLOCK, as we don't have to worry about
|
||||||
// the underlying handle disappearing since we have a reference.
|
// the underlying handle disappearing since we have a reference.
|
||||||
if (has_dbref(d, dbref))
|
if (dbref == -1 || has_dbref(d, dbref))
|
||||||
{
|
{
|
||||||
|
memcpy(d->work_buffer, inbuf, inbuf_sz);
|
||||||
|
|
||||||
// Mark the port as busy and then schedule the appropriate async operation
|
// Mark the port as busy and then schedule the appropriate async operation
|
||||||
d->async_op = cmd;
|
d->async_op = cmd;
|
||||||
d->async_pool = G_TPOOL_GENERAL;
|
d->async_pool = G_TPOOL_GENERAL;
|
||||||
|
@ -1110,12 +1112,42 @@ static void do_async_truncate(void* arg)
|
||||||
|
|
||||||
// Get the database reference and flags from the payload
|
// Get the database reference and flags from the payload
|
||||||
int dbref = UNPACK_INT(d->work_buffer, 0);
|
int dbref = UNPACK_INT(d->work_buffer, 0);
|
||||||
|
int rc = 0;
|
||||||
|
|
||||||
|
if (dbref == -1)
|
||||||
|
{
|
||||||
|
DBG("Truncating all open databases...\r\n");
|
||||||
|
|
||||||
|
// Iterate over the whole database list skipping null entries
|
||||||
|
int i = 0; // I hate C
|
||||||
|
for ( ; i < G_DATABASES_SIZE; ++i)
|
||||||
|
{
|
||||||
|
Database* database = &G_DATABASES[i];
|
||||||
|
if (database != NULL && database->db != 0)
|
||||||
|
{
|
||||||
|
DB* db = database->db;
|
||||||
|
|
||||||
|
DBG("Truncating dbref %i\r\n", i);
|
||||||
|
|
||||||
|
u_int32_t count = 0;
|
||||||
|
rc = db->truncate(db, d->txn, &count, 0);
|
||||||
|
|
||||||
|
if (rc != 0)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
DB* db = G_DATABASES[dbref].db;
|
DB* db = G_DATABASES[dbref].db;
|
||||||
|
|
||||||
DBG("Truncating dbref %i\r\n", dbref);
|
DBG("Truncating dbref %i\r\n", dbref);
|
||||||
|
|
||||||
u_int32_t count = 0;
|
u_int32_t count = 0;
|
||||||
int rc = db->truncate(db, d->txn, &count, 0);
|
rc = db->truncate(db, d->txn, &count, 0);
|
||||||
|
}
|
||||||
|
|
||||||
// If any error occurs while we have a txn action, abort it
|
// If any error occurs while we have a txn action, abort it
|
||||||
if (d->txn && rc)
|
if (d->txn && rc)
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
get/2, get/3,
|
get/2, get/3,
|
||||||
get_r/2, get_r/3,
|
get_r/2, get_r/3,
|
||||||
update/3, update/4,
|
update/3, update/4,
|
||||||
truncate/1,
|
truncate/0, truncate/1,
|
||||||
delete_database/1,
|
delete_database/1,
|
||||||
cursor_open/1, cursor_next/0, cursor_prev/0, cursor_current/0, cursor_close/0]).
|
cursor_open/1, cursor_next/0, cursor_prev/0, cursor_current/0, cursor_close/0]).
|
||||||
|
|
||||||
|
@ -210,6 +210,9 @@ update(Db, Key, Fun, Args) ->
|
||||||
end,
|
end,
|
||||||
transaction(F).
|
transaction(F).
|
||||||
|
|
||||||
|
truncate() ->
|
||||||
|
truncate(-1).
|
||||||
|
|
||||||
truncate(Db) ->
|
truncate(Db) ->
|
||||||
Cmd = <<Db:32/native>>,
|
Cmd = <<Db:32/native>>,
|
||||||
<<Result:32/native-signed>> = erlang:port_control(get_port(), ?CMD_TRUNCATE, Cmd),
|
<<Result:32/native-signed>> = erlang:port_control(get_port(), ?CMD_TRUNCATE, Cmd),
|
||||||
|
|
|
@ -29,7 +29,8 @@ all() ->
|
||||||
data_dir_should_be_priv_dir,
|
data_dir_should_be_priv_dir,
|
||||||
delete_should_remove_file,
|
delete_should_remove_file,
|
||||||
delete_should_fail_if_db_inuse,
|
delete_should_fail_if_db_inuse,
|
||||||
truncate_should_empty_database].
|
truncate_should_empty_database,
|
||||||
|
truncate_all_should_empty_all_databases].
|
||||||
|
|
||||||
|
|
||||||
dbconfig(Config) ->
|
dbconfig(Config) ->
|
||||||
|
@ -219,3 +220,9 @@ truncate_should_empty_database(Config) ->
|
||||||
ok = bdberl:put(Db, mykey, avalue),
|
ok = bdberl:put(Db, mykey, avalue),
|
||||||
ok = bdberl:truncate(Db),
|
ok = bdberl:truncate(Db),
|
||||||
not_found = bdberl:get(Db, mykey).
|
not_found = bdberl:get(Db, mykey).
|
||||||
|
|
||||||
|
truncate_all_should_empty_all_databases(Config) ->
|
||||||
|
Db = ?config(db, Config),
|
||||||
|
ok = bdberl:put(Db, mykey, avalue),
|
||||||
|
ok = bdberl:truncate(),
|
||||||
|
not_found = bdberl:get(Db, mykey).
|
||||||
|
|
Loading…
Reference in a new issue