:- ensure_loaded(common).
cvs_id('$Id: db.pl,v 1.8 2004/10/15 21:57:56 sandro Exp $').
/*

  App-specific interface to db (SleepyCat Berkeley DB)

  Note that db support (called db4pl in some places) ships with SWI
  Prolog as a package (packages/db) but it is NOT built by default.
  
  I'm using it with DB 4.2.52.2, installed from source.


  pl -g '[db, argv], handle_argv'. -- --sandbox /home/sandro/semwalker/sandbox

  
*/

:- use_module(library(db)).
%:- ensure_loaded(config_data).
:- ensure_loaded(app_config).

:- dynamic(db_dir/1).

app_db_init :-        % safe to repeat!
	flag(app_db_init_done, Done, 1),
	(   Done = 0
	->  app_config_file(db_dir, DB_Dir),
	    mkdir(DB_Dir),
	    % mp_mmapsize is only for read-only db's
	    db_init([home(DB_Dir), create(true),
		     % concurrent(true),   nahhhhh
		     mp_size(100000000)]),
	    assert(db_dir(DB_Dir))
	;   true
	).

% qtests?

xqtest :-
	db_open(test, update, T,
		[key(atom), value(atom)]
	       ),
	\+ db_get(T, k, v),
	\+ db_get(T, k2, v2),
	db_put(T, k, v),
	db_put(T, k2, v2),
	db_get(T, k, v),
	db_get(T, k2, v2),
	db_del(T, k, v),
	\+ db_get(T, k, v),
	db_close(T),
	db_open(test, update, T,
		[key(atom), value(atom)]
	       ),
	\+ db_get(T, k, v),
	db_get(T, k2, v2),
	db_del(T, k2, v2),
	\+ db_get(T, k2, v2),
	db_close(T).

/* 
   db_init([home('/home/sandro/semwalker'), create(true)]).

   db_init([home('/home/sandro/semwalker/sandbox/usr/local/semwalker/database'), create(true)]).

*/