github.com/decred/dcrd/database (go): Package database provides a block and metadata storage database.  This package provides a database layer to store and retrieve block data and arbitrary metadata in a simple and efficient manner.  The default backend, ffldb, has a strong focus on speed, efficiency, and robustness.  It makes use leveldb for the metadata, flat files for block storage, and strict checksums in key areas to ensure data integrity.  A quick overview of the features database provides are as follows:  The main entry point is the DB interface.  It exposes functionality for transactional-based access and storage of metadata and block data.  It is obtained via the Create and Open functions which take a database type string that identifies the specific database driver (backend) to use as well as arguments specific to the specified driver.  The Namespace interface is an abstraction that provides facilities for obtaining transactions (the Tx interface) that are the basis of all database reads and writes.  Unlike some database interfaces that support reading and writing without transactions, this interface requires transactions even when only reading or writing a single key.  The Begin function provides an unmanaged transaction while the View and Update functions provide a managed transaction.  These are described in more detail below.  The Tx interface provides facilities for rolling back or committing changes that took place while the transaction was active.  It also provides the root metadata bucket under which all keys, values, and nested buckets are stored.  A transaction can either be read-only or read-write and managed or unmanaged.  A managed transaction is one where the caller provides a function to execute within the context of the transaction and the commit or rollback is handled automatically depending on whether or not the provided function returns an error.  Attempting to manually call Rollback or Commit on the managed transaction will result in a panic.  An unmanaged transaction, on the other hand, requires the caller to manually call Commit or Rollback when they are finished with it.  Leaving transactions open for long periods of time can have several adverse effects, so it is recommended that managed transactions are used instead.  The Bucket interface provides the ability to manipulate key/value pairs and nested buckets as well as iterate through them.  The Get, Put, and Delete functions work with key/value pairs, while the Bucket, CreateBucket, CreateBucketIfNotExists, and DeleteBucket functions work with buckets.  The ForEach function allows the caller to provide a function to be called with each key/value pair and nested bucket in the current bucket.  As discussed above, all of the functions which are used to manipulate key/value pairs and nested buckets exist on the Bucket interface.  The root metadata bucket is the upper-most bucket in which data is stored and is created at the same time as the database.  Use the Metadata function on the Tx interface to retrieve it.  The CreateBucket and CreateBucketIfNotExists functions on the Bucket interface provide the ability to create an arbitrary number of nested buckets.  It is a good idea to avoid a lot of buckets with little data in them as it could lead to poor page utilization depending on the specific driver in use.  This example demonstrates creating a new database and using a managed read-write transaction to store and retrieve metadata.   This example demonstrates creating a new database, using a managed read-write transaction to store a block, and using a managed read-only transaction to fetch the block.
Keywords: blockchain|cryptocurrency|dcrd|decred|decred-daemon|decred-nodes|go|golang|p2p|peer-to-peer