github.com/mit-dci/lit@v0.0.0-20221102210550-8c3d3b49f2ce/btcutil/database/doc.go (about)

     1  // Copyright (c) 2015-2016 The btcsuite developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  /*
     6  Package database provides a block and metadata storage database.
     7  
     8  Overview
     9  
    10  As of Feb 2016, there are over 400,000 blocks in the Bitcoin block chain and
    11  and over 112 million transactions (which turns out to be over 60GB of data).
    12  This package provides a database layer to store and retrieve this data in a
    13  simple and efficient manner.
    14  
    15  The default backend, ffldb, has a strong focus on speed, efficiency, and
    16  robustness.  It makes use leveldb for the metadata, flat files for block
    17  storage, and strict checksums in key areas to ensure data integrity.
    18  
    19  A quick overview of the features database provides are as follows:
    20  
    21   - Key/value metadata store
    22   - Bitcoin block storage
    23   - Efficient retrieval of block headers and regions (transactions, scripts, etc)
    24   - Read-only and read-write transactions with both manual and managed modes
    25   - Nested buckets
    26   - Supports registration of backend databases
    27   - Comprehensive test coverage
    28  
    29  Database
    30  
    31  The main entry point is the DB interface.  It exposes functionality for
    32  transactional-based access and storage of metadata and block data.  It is
    33  obtained via the Create and Open functions which take a database type string
    34  that identifies the specific database driver (backend) to use as well as
    35  arguments specific to the specified driver.
    36  
    37  Namespaces
    38  
    39  The Namespace interface is an abstraction that provides facilities for obtaining
    40  transactions (the Tx interface) that are the basis of all database reads and
    41  writes.  Unlike some database interfaces that support reading and writing
    42  without transactions, this interface requires transactions even when only
    43  reading or writing a single key.
    44  
    45  The Begin function provides an unmanaged transaction while the View and Update
    46  functions provide a managed transaction.  These are described in more detail
    47  below.
    48  
    49  Transactions
    50  
    51  The Tx interface provides facilities for rolling back or committing changes that
    52  took place while the transaction was active.  It also provides the root metadata
    53  bucket under which all keys, values, and nested buckets are stored.  A
    54  transaction can either be read-only or read-write and managed or unmanaged.
    55  
    56  Managed versus Unmanaged Transactions
    57  
    58  A managed transaction is one where the caller provides a function to execute
    59  within the context of the transaction and the commit or rollback is handled
    60  automatically depending on whether or not the provided function returns an
    61  error.  Attempting to manually call Rollback or Commit on the managed
    62  transaction will result in a panic.
    63  
    64  An unmanaged transaction, on the other hand, requires the caller to manually
    65  call Commit or Rollback when they are finished with it.  Leaving transactions
    66  open for long periods of time can have several adverse effects, so it is
    67  recommended that managed transactions are used instead.
    68  
    69  Buckets
    70  
    71  The Bucket interface provides the ability to manipulate key/value pairs and
    72  nested buckets as well as iterate through them.
    73  
    74  The Get, Put, and Delete functions work with key/value pairs, while the Bucket,
    75  CreateBucket, CreateBucketIfNotExists, and DeleteBucket functions work with
    76  buckets.  The ForEach function allows the caller to provide a function to be
    77  called with each key/value pair and nested bucket in the current bucket.
    78  
    79  Metadata Bucket
    80  
    81  As discussed above, all of the functions which are used to manipulate key/value
    82  pairs and nested buckets exist on the Bucket interface.  The root metadata
    83  bucket is the upper-most bucket in which data is stored and is created at the
    84  same time as the database.  Use the Metadata function on the Tx interface
    85  to retrieve it.
    86  
    87  Nested Buckets
    88  
    89  The CreateBucket and CreateBucketIfNotExists functions on the Bucket interface
    90  provide the ability to create an arbitrary number of nested buckets.  It is
    91  a good idea to avoid a lot of buckets with little data in them as it could lead
    92  to poor page utilization depending on the specific driver in use.
    93  */
    94  package database