# 数据库模块

• Key/value store
• Namespace support (说的就是NestedBucket)
• Allows multiple packages to have their own area in the database without
• Nested buckets
• Supports registration of backend databases
• Comprehensive test coverage
主要interface
go type ReadTx interface type ReadWriteTx interface type ReadBucket interface type ReadWriteBucket interface type ReadCursor interface type ReadWriteCursor interface type DB interface

## 关于这些interface的使用

### 1. 入口


// DB represents an ACID database.  All database access is performed through
type DB interface {

// Copy writes a copy of the database to the provided writer.  This
// call will start a read-only transaction to perform all operations.
Copy(w io.Writer) error

// Close cleanly shuts down the database and syncs all data.
Close() error
}


### 2. 操作Bucket

type ReadTx interface {
// ReadBucket opens the root bucket for read only access.  If the bucket
// described by the key does not exist, nil is returned.

// Rollback closes the transaction, discarding changes (if any) if the
// database was modified by a write transaction.
Rollback() error
}
// ReadWriteTx represents a database transaction that can be used for both reads

// bucket described by the key does not exist, nil is returned.

// CreateTopLevelBucket creates th top level bucket for a key if it
// does not exist.  The newly-created bucket it returned.

// DeleteTopLevelBucket deletes the top level bucket for a key.  This
// errors if the bucket can not be found or the key keys a single value
DeleteTopLevelBucket(key []byte) error

// Commit commits all changes that have been on the transaction's root
// buckets and all of their sub-buckets to persistent storage.
Commit() error

// OnCommit takes a function closure that will be executed when the
// transaction successfully gets committed.
OnCommit(func())
}


### 3. 读写数据

// ReadBucket represents a bucket (a hierarchical structure within the database)
// that is only allowed to perform read operations.
// NestedReadBucket retrieves a nested bucket with the given key.
// Returns nil if the bucket does not exist.

// ForEach invokes the passed function with every key/value pair in
// the bucket.  This includes nested buckets, in which case the value
// is nil, but it does not include the key/value pairs within those
// nested buckets.
//
// NOTE: The values returned by this function are only valid during a
// transaction.  Attempting to access them after a transaction has ended
// results in undefined behavior.  This constraint prevents additional
// data copies and allows support for memory-mapped database
// implementations.
ForEach(func(k, v []byte) error) error

// Get returns the value for the given key.  Returns nil if the key does
// not exist in this bucket (or nested buckets).
//
// NOTE: The value returned by this function is only valid during a
// transaction.  Attempting to access it after a transaction has ended
// results in undefined behavior.  This constraint prevents additional
// data copies and allows support for memory-mapped database
// implementations.
Get(key []byte) []byte
//这个接口和forEach有啥关系,如何使用呢?
}
// ReadWriteBucket represents a bucket (a hierarchical structure within the
// database) that is allowed to perform both read and write operations.

// NestedReadWriteBucket retrieves a nested bucket with the given key.
// Returns nil if the bucket does not exist.

// CreateBucket creates and returns a new nested bucket with the given
// key.  Returns ErrBucketExists if the bucket already exists,
// ErrBucketNameRequired if the key is empty, or ErrIncompatibleValue
// if the key value is otherwise invalid for the particular database
// implementation.  Other errors are possible depending on the
// implementation.

// CreateBucketIfNotExists creates and returns a new nested bucket with
// the given key if it does not already exist.  Returns
// ErrBucketNameRequired if the key is empty or ErrIncompatibleValue
// if the key value is otherwise invalid for the particular database
// backend.  Other errors are possible depending on the implementation.

// DeleteNestedBucket removes a nested bucket with the given key.
// Returns ErrTxNotWritable if attempted against a read-only transaction
// and ErrBucketNotFound if the specified bucket does not exist.
DeleteNestedBucket(key []byte) error

// Put saves the specified key/value pair to the bucket.  Keys that do
// overwritten.  Returns ErrTxNotWritable if attempted against a
Put(key, value []byte) error

// Delete removes the specified key from the bucket.  Deleting a key
// that does not exist does not return an error.  Returns
// ErrTxNotWritable if attempted against a read-only transaction.
Delete(key []byte) error

// Cursor returns a new cursor, allowing for iteration over the bucket's
// key/value pairs and nested buckets in forward or backward order.

// Tx returns the bucket's transaction.
}