Skip to content

Commit

Permalink
Change binhelper to avoid stupid user errors; now grows the buffer on…
Browse files Browse the repository at this point in the history
… demand for the specific # of bytes; fix bugs in get_data_dirs/0 (driver and erlang caller)
  • Loading branch information
Dave Smith committed Dec 17, 2008
1 parent 3b9ba44 commit 45606dd
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
23 changes: 10 additions & 13 deletions c_src/bdberl_drv.c
Expand Up @@ -122,7 +122,7 @@ static TPool* G_TPOOL_TXNS;

#define RETURN_INT(val, outbuf) { \
BinHelper bh; \
bin_helper_init(&bh, 4); \
bin_helper_init(&bh); \
bin_helper_push_int32(&bh, val); \
RETURN_BH(bh, outbuf); }

Expand Down Expand Up @@ -351,7 +351,7 @@ static int bdberl_drv_control(ErlDrvData handle, unsigned int cmd,
// Pack the status and dbref (or errno) into a binary and return it
// Outbuf is: <<Status:8, DbRef:32>>
BinHelper bh;
bin_helper_init(&bh, 5);
bin_helper_init(&bh);
bin_helper_push_byte(&bh, status);
bin_helper_push_int32(&bh, dbref);
RETURN_BH(bh, outbuf);
Expand Down Expand Up @@ -748,6 +748,7 @@ static int delete_database(const char* name)
}

// Good, database doesn't seem to be open -- attempt the delete
DBG("Attempting to delete database: %s\n", name);
int rc = G_DB_ENV->dbremove(G_DB_ENV, 0, name, 0, DB_AUTO_COMMIT);
WRITE_UNLOCK(G_DATABASES_RWLOCK);
return rc;
Expand All @@ -766,7 +767,7 @@ static void tune_system(int target, void* values, BinHelper* bh)
unsigned int bytes = 0;
int caches = 0;
int rc = G_DB_ENV->get_cachesize(G_DB_ENV, &gbytes, &bytes, &caches);
bin_helper_init(bh, 16);
bin_helper_init(bh);
bin_helper_push_int32(bh, rc);
bin_helper_push_int32(bh, gbytes);
bin_helper_push_int32(bh, bytes);
Expand All @@ -777,15 +778,15 @@ static void tune_system(int target, void* values, BinHelper* bh)
{
unsigned int timeout = UNPACK_INT(values, 0);
int rc = G_DB_ENV->set_timeout(G_DB_ENV, timeout, DB_SET_TXN_TIMEOUT);
bin_helper_init(bh, 4);
bin_helper_init(bh);
bin_helper_push_int32(bh, rc);
break;
}
case SYSP_TXN_TIMEOUT_GET:
{
unsigned int timeout = 0;
int rc = G_DB_ENV->get_timeout(G_DB_ENV, &timeout, DB_SET_TXN_TIMEOUT);
bin_helper_init(bh, 8);
bin_helper_init(bh);
bin_helper_push_int32(bh, rc);
bin_helper_push_int32(bh, timeout);
break;
Expand All @@ -794,16 +795,12 @@ static void tune_system(int target, void* values, BinHelper* bh)
{
const char** dirs = 0;
int rc = G_DB_ENV->get_data_dirs(G_DB_ENV, &dirs);
printf("DATA DIR: %d\n", rc);
bin_helper_init(bh, 64);
bin_helper_init(bh);
bin_helper_push_int32(bh, rc);
if (dirs)
while (dirs && *dirs)
{
while (*dirs != 0)
{
bin_helper_push_string(bh, *dirs);
dirs++;
}
bin_helper_push_string(bh, *dirs);
dirs++;
}
break;
}
Expand Down
10 changes: 7 additions & 3 deletions c_src/bin_helper.c
Expand Up @@ -12,15 +12,19 @@

static void bin_helper_check_size(BinHelper* bh, int space_needed)
{
if (bh->offset + space_needed > bh->bin->orig_size)
if (bh->bin && (bh->offset + space_needed > bh->bin->orig_size))
{
bh->bin = driver_realloc_binary(bh->bin, bh->offset + space_needed);
}
else if (!bh->bin)
{
bh->bin = driver_alloc_binary(space_needed);
}
}

void bin_helper_init(BinHelper* bh, unsigned int size)
void bin_helper_init(BinHelper* bh)
{
bh->bin = driver_alloc_binary(size);
bh->bin = 0;
bh->offset = 0;
}

Expand Down
2 changes: 1 addition & 1 deletion c_src/bin_helper.h
Expand Up @@ -15,7 +15,7 @@ typedef struct
unsigned int offset;
} BinHelper;

void bin_helper_init(BinHelper* bh, unsigned int size);
void bin_helper_init(BinHelper* bh);
void bin_helper_push_byte(BinHelper* bh, int value);
void bin_helper_push_int32(BinHelper* bh, int value);
void bin_helper_push_string(BinHelper* bh, const char* string);
Expand Down
2 changes: 1 addition & 1 deletion src/bdberl.erl
Expand Up @@ -239,7 +239,7 @@ cursor_close() ->
end.

delete_database(Filename) ->
Cmd = list_to_binary(Filename),
Cmd = <<(list_to_binary(Filename))/binary, 0:8>>,
<<Rc:32/native-signed>> = erlang:port_control(get_port(), ?CMD_REMOVE_DB, Cmd),
case decode_rc(Rc) of
ok ->
Expand Down
7 changes: 5 additions & 2 deletions test/bdberl_SUITE.erl
Expand Up @@ -30,9 +30,11 @@ all() ->
delete_should_remove_file,
delete_should_fail_if_db_inuse].


dbconfig(Config) ->
Cfg = [{set_data_dir, ?config(priv_dir, Config)},
{set_flags, 'DB_TXN_NOSYNC'}],
{set_flags, 'DB_TXN_NOSYNC'},
{set_log_config, 'DB_LOG_IN_MEMORY'}],
list_to_binary(lists:flatten([io_lib:format("~s ~s\n", [K,V]) || {K, V} <- Cfg])).


Expand All @@ -45,7 +47,8 @@ init_per_suite(Config) ->
end_per_suite(_Config) ->
ok.

init_per_testcase(_TestCase, Config) ->
init_per_testcase(TestCase, Config) ->
ct:print("~p", [TestCase]),
{ok, Db} = bdberl:open("api_test.db", btree, [create, exclusive]),
[{db, Db}|Config].

Expand Down

0 comments on commit 45606dd

Please sign in to comment.