1/* Part of SWI-Prolog 2 3 Author: Jan Wielemaker 4 E-mail: J.Wielemaker@vu.nl 5 WWW: http://www.swi-prolog.org 6 Copyright (c) 2015, University of Amsterdam 7 VU University Amsterdam 8 All rights reserved. 9 10 Redistribution and use in source and binary forms, with or without 11 modification, are permitted provided that the following conditions 12 are met: 13 14 1. Redistributions of source code must retain the above copyright 15 notice, this list of conditions and the following disclaimer. 16 17 2. Redistributions in binary form must reproduce the above copyright 18 notice, this list of conditions and the following disclaimer in 19 the documentation and/or other materials provided with the 20 distribution. 21 22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 30 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 32 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 POSSIBILITY OF SUCH DAMAGE. 34*/ 35 36:- module(bdb, 37 [ bdb_init/1, % +Options 38 bdb_init/2, % -Environment, +Options 39 bdb_close_environment/1, % +Environment 40 bdb_current_environment/1, % -Environment 41 bdb_environment_property/2, % ?Environment, ?Property 42 43 bdb_open/4, % +File, +Mode, -Handle, +Options 44 bdb_close/1, % +Handle 45 bdb_closeall/0, % 46 bdb_current/1, % -DB 47 48 bdb_put/3, % +DB, +Key, +Value 49 bdb_del/3, % +DB, +Key, ?Value 50 bdb_delall/3, % +DB, +Key, +Value 51 bdb_enum/3, % +DB, -Key, -Value 52 bdb_get/3, % +DB, +Key, -Value 53 bdb_getall/3, % +DB, +Key, -ValueList 54 55 bdb_transaction/1, % :Goal 56 bdb_transaction/2, % :Goal, +Environment 57 58 bdb_version/1 % -Version 59 ]). 60:- use_foreign_library(foreign(bdb4pl)). 61:- meta_predicate 62 bdb_transaction( ), 63 bdb_transaction( , ).
environment(+Environment)
option. If bdb_init/1 is called,
it must be called before the first call to bdb_open/4 that uses
the default environment. If bdb_init/1 is not called, the
default environment can only handle plain files and does not
support multiple threads, locking, crash recovery, etc.
Initializing a BDB environment always requires the home(+Dir)
option. If the environment contains no databases, the argument
create(true)
must be supplied as well.
The currently supported options are listed below. The name of
the boolean options are derived from the DB flags by dropping
the =DB_= prefix and using lowercase, e.g. DB_INIT_LOCK
becomes init_lock
. For details, please refer to the DB manual.
true
, create any underlying file as required. By
default, no new files are created. This option should be
set for prograns that create new databases.DB_INIT_LOCK
). Implied if transactions
are used.DB_INIT_LOG
). Logging
enables recovery of databases in case of system failure.
Normally it is used in combination with transactions.mp_size(+Size)
or
mp_mmapsize(+Size)
is specified.init_log(true)
.DB_INIT_MPOOL
). The
mp_size
option sets the memory-pool used for
caching, while the mp_mmapsize
controls the maximum size
of a DB file mapped entirely into memory.berkeley_db_svc
. Optionally additional options may be
specified:
DB_ENV->set_thread_count()
.201bdb_current_environment(Environment) :- 202 bdb_current_environment_(Environment), 203 bdb_is_open_env(Environment). 204 205bdb_current_environment_(Env) :- 206 ( var(Env) 207 -> ( Env = default 208 ; current_blob(Env, bdb_env) 209 ) 210 ; ( Env == default 211 -> true 212 ; current_blob(Env, bdb_env) 213 ) 214 ).
228bdb_environment_property(Env, Property) :- 229 bdb_current_environment_(Env), 230 ( bdb_is_open_env(Env) 231 -> ( var(Property) 232 -> env_property(Property), 233 bdb_env_property_(Env, Property) 234 ; bdb_env_property_(Env, Property) 235 ) 236 ; Property = open(false) 237 ). 238 239bdb_env_property_(Env, home(Home)) :- 240 !, 241 bdb_env_property(Env, home(Home0)), 242 prolog_to_os_filename(Home, Home0). 243bdb_env_property_(Env, Prop) :- 244 bdb_env_property(Env, Prop). 245 246env_property(open(true)). 247env_property(home(_)). 248env_property(init_lock(_)). 249env_property(init_log(_)). 250env_property(init_mpool(_)). 251env_property(init_rep(_)). 252env_property(init_txn(_)). 253env_property(recover(_)). 254env_property(recover_fatal(_)). 255env_property(use_environ(_)). 256env_property(use_environ_root(_)). 257env_property(create(_)). 258env_property(lockdown(_)). 259env_property(failchk(_)). 260env_property(private(_)). 261env_property(register(_)). 262env_property(system_mem(_)). 263env_property(thread(_)).
read
, providing
read-only access or update
, providing read/write access.
Options is a list of options. Supported options are below. The
boolean options are passed as flags to DB->open()
. The
option name is derived from the flag name by stripping the
DB_
prefix and converting to lower case. Consult the
Berkeley DB documentation for details.
create(true)
, fail if the database already
exists.375bdb_delall(DB, Key, Value) :- 376 var(Value), 377 !, 378 bdb_del(DB, Key). % this is much faster 379bdb_delall(DB, Key, Value) :- 380 ( bdb_del(DB, Key, Value), 381 fail 382 ; true 383 ).
bdb_put(DB, f(X), 42)
, we get the following query results:
bdb_get(DB, f(Y), V)
binds Value to 42
, while Y is left
unbound.bdb_get(DB, f(a), V)
fails.bdb_enum(DB, f(a), V)
succeeds, but does not perform any
indexing, i.e., it enumerates all key-value pairs and
performs the unification.
416bdb_current(DB) :-
417 current_blob(DB, bdb),
418 bdb_is_open(DB).
426bdb_closeall :- 427 close_databases, 428 close_environments. 429 430close_databases :- 431 forall(bdb_current(DB), 432 catch(bdb_close(DB), 433 E, 434 print_message(warning, E))). 435 436close_environments :- 437 forall(bdb_current_environment(DB), 438 catch(bdb_close_environment(DB), 439 E, 440 print_message(warning, E))). 441 442terminate_bdb :- 443 ( current_predicate(bdb_init/1) % library was loaded ok 444 -> bdb_closeall 445 ; true 446 ). 447 448:- at_halt(terminate_bdb).
error(package(db, deadlock), _)
This exception indicates a deadlock was raised by one of the DB predicates. Deadlocks may arise if multiple processes or threads access the same keys in a different order. The DB infra-structure causes one of the processes involved in the deadlock to abort its transaction. This process may choose to restart the transaction.
For example, a DB application may define {Goal}
to realise
transactions and restart these automatically is a deadlock is
raised:
{Goal} :- catch(bdb_transaction(Goal), E, true), ( var(E) -> true ; E = error(package(db, deadlock), _) -> {Goal} ; throw(E) ).
DB_VERSION_MAJOR*10000 + DB_VERSION_MINOR*100 + DB_VERSION_PATCH
501 /******************************* 502 * MESSAGES * 503 *******************************/ 504 505:- multifile 506 prolog:message/3. 507 508prologmessage(error(bdb(Code, Message, Obj), _)) --> 509 [ 'BDB: Error ~w on ~p: ~w'-[Code, Obj, Message] ]
Berkeley DB interface
This package realises a binding to Berkeley DB, originally by Sleepycat Software, now managed by Oracle. The DB library implements modular support for the bottom layers of a database. In can be configured for single-threaded access to a file, multi-threaded access with transactions, remote access as well as database replication.
Berkeley DB is an embedded database. This implies the library provides access to a file containing one or more database tables. The Berkeley DB database tables are always binary, mapping a key to a value. The SWI-Prolog interface to Berkeley DB allows for fast storage of arbitrary Prolog terms including cycles and constraints in the database.
Accessing a database consists of four steps:
Errors reported by the underlying database are mapped to an exception of the form
error(bdb(Code,Message,Object), _)
, where Code is an atom for well known errors and an integer for less known ones. Message is the return from thedb_strerror()
function and Object is the most related Prolog object, typically a database or database environment handle. If Code is an atom, it is the lowercase version of the associated C macro after string theDB_
prefix. Currently the following atom-typed codes are defined:lock_deadlock
,runrecovery
,notfound
,keyempty
,keyexist
,lock_notgranted
andsecondary_bad
. */