Author Topic: Berkeley DB Overview  (Read 23231 times)


  • Administrator
  • *****
  • Posts: 19
    • View Profile
Berkeley DB Overview
« on: April 01, 2006, 07:05:19 PM »
DISCONTINUED support in ScriptBasic 3.0

Berkely DB

Berkeley DB is a database engine that provides developers with fast, reliable, local persistence with zero administration. Berkeley DB is a library that links directly into your application. Your application makes simple function calls, rather than sending messages to a remote server, eliminating the performance penalty of client-server architectures. Berkeley DB stores data in application native format, as simple key/value pairs, eliminating the need for translation or mapping. Berkeley DB eliminates the overhead of SQL query processing, enabling applications with predictable access patterns to run faster. Berkeley DB is the ideal choice for static queries over dynamic data, while traditional relational databases are well suited for dynamic queries over static data.

Berkeley DB delivers the same robust data storage features as traditional, relational database systems, such as ACID transactions and recovery; locking, multiple processes and multi-threading for high concurrency; hot and cold backup; and single-master replication for high availability applications. Berkeley DB can manage databases in memory, on disk or both. Berkeley DB is designed to run in a completely unattended fashion, so all runtime administration is programmatically controlled by the application, not by a human administrator. It has been designed to be simple, fast, small and reliable.

Berkeley DB supports replication over multiple systems, enabling applications to scale massively with low latency and provide fault tolerance for high availability solutions. This technique works by having all updates go to a designated master, which distributes changes automatically to a set of replicas. The read workload can be spread across the replicas, and new replicas can join the group at any time to scale the system. If any replica fails, the remaining replicas can take over for it. If the master fails, the application can call for an election or simply designate a new master. Once the new master has been chosen, all of the replicas synchronize with the new master and move forward with normal processing with no interruption in service.

Berkeley DB is very flexible and puts developers in control of many aspects of its behavior, which allows it to be used across a wide range of applications and as a replacement for custom, home-grown solutions. For example, developers can control how resources are allocated, the amount of memory dedicated to caching records, the on-disk storage structure used for individual tables, durability and isolation guarantees, and replication policies. It includes full source code for easier porting, integration, debugging and optimization.

Berkeley DB provides very fast, reliable and scalable persistence for applications that need to store data locally, run unattended without interruption, and access data in a predictable fashion.

Open - opens a database and returns a handle
       Type - Btree, Hash, Recno, Queue, Unknown
       Flags - Create, NoMap, RdOnly, Thread, Trunc

       DB = bdb::Open(DataBase,type,flags,unixmode)

Close - closes a database


Put - put a key-value pair into the database
      Append - use with Recno databases
      NoOverWrite - error if record exists


Get - get the value associated with the key

      value = bdb::Get(DB,key)

First - retrieves the first key/value pair or first match if a key (full/partial) is given

        value = bdb::First(DB,key)

Last - position the cursor to the last record and return the value if required

       value = bdb::Last(DB)

Next - returns next record value - returns undef if cursor not set or at end of file

       value = bdb::Next(DB)

Previous - returns previous record value - returns undef if cursor not set or at begininng of file

           value = bdb::Previous(DB)

Key - return the key of the last accessed record

      key = bdb::Key(DB)

Update - updates the value of the last accessed record


DeleteRecord - deletes the last accessed record


DeleteAll - deletes all records with the given key



This command starts a transaction. This transaction lasts until a CommitTransaction, EndTrasaction, AbortTransaction command is executed or the execution of the BASIC program is finished. Any transaction not finished using CommitTransaction or EndTransactionis aborted.

Transactions provide database integrity. Operations accessing the database in a transaction get exclusive access to the records they are accessing. When a program reads a record in a transaction the module assumes that the program wants to alter the value of the record later and does not allow any other program to read the value until the transaction is finished.

When a program tries to access a locked record the program execution is suspended until the record is unlocked. If there are more programs waiting for the same record one of them gets access to the record and the others remain waiting.

When a program alters some data in a transaction either all alterations are done in a single transaction or none of the alterations are performed.



This command commits the transaction started using the command bdb::BeginTransaction. When this command is executed all the alterations are done on the database that the program performed since the transaction begin and all locks are released, so other programs may get access to the records.


EndTransaction - calls bdb::CommitTransaction - added for syntax compatibility



This command aborts a transaction. This means that the alterations performed during the transaction are not performed and the locked records are released. This command is automatically executed when a database with an active transaction is closed or when the interpreter finishes the program execution and the database remained unclosed with an open transaction.



This command deletes a database. The name of the database should be specified. Whenever you want to delete a database programmatically use this command instead of deleting the database file directly. This command takes care of the configuration and should be more compatible with future releases of the module than just deleting the physical file.


Code: [Select]
INCLUDE bdb.bas

db = bdb::Open("test.db",bdb::BTree,bdb::Create,0)

PRINT "Transaction Started\n"
counter = bdb::Get(db,"COUNTER")
PRINT "Counter is ",counter,"\n"

IF IsDefined(counter) THEN
  counter = counter + 1
  print "Record Updated\n "
  counter = 1
  PRINT "Record Created\n "
PRINT "Transaction Completed\n"


' bdb::Drop("test.db")

Original Berkeley DB Documentation
« Last Edit: August 14, 2010, 10:05:35 PM by support »