cdb64 package - github.com/chrislusf/cdb64 - Go Packages (original) (raw)
Package cdb64 provides a native implementation of cdb, a fast constant key/value database, but without the 4GB size limitation.
For more information on cdb, see the original design doc at http://cr.yp.to/cdb.html.
This is based on the code from https://github.com/colinmarc/cdb
writer, err := Create("/tmp/example.cdb") if err != nil { log.Fatal(err) }
// Write some key/value pairs to the database. writer.Put([]byte("Alice"), []byte("Practice")) writer.Put([]byte("Bob"), []byte("Hope")) writer.Put([]byte("Charlie"), []byte("Horse"))
// Freeze the database, and open it for reads. db, err := writer.Freeze() if err != nil { log.Fatal(err) }
// Fetch a value. v, err := db.Get([]byte("Alice")) if err != nil { log.Fatal(err) }
fmt.Println(string(v))
Output: Practice
This section is empty.
This section is empty.
CDB represents an open CDB database. It can only be used for reads; to create a database, use Writer.
db, err := Open("./test/test.cdb") if err != nil { log.Fatal(err) }
// Fetch a value. v, err := db.Get([]byte("foo")) if err != nil { log.Fatal(err) }
fmt.Println(string(v))
Output: bar
New opens a new CDB instance for the given io.ReaderAt. It can only be used for reads; to create a database, use Writer.
If hasher is nil, it will default to the CDB hash function. If a database was created with a particular hash function, that same hash function must be passed to New, or the database will return incorrect results.
Open opens an existing CDB database at the given path.
Close closes the database to further reads.
Get returns the value for a given key, or nil if it can't be found.
func (cdb *CDB) Iter() *Iterator
Iter creates an Iterator that can be used to iterate the database.
Iterator represents a sequential iterator over a CDB database.
db, err := Open("./test/test.cdb") if err != nil { log.Fatal(err) }
// Create an iterator for the database. iter := db.Iter() for iter.Next() { // Do something with iter.Key()/iter.Value() }
if err := iter.Err(); err != nil { log.Fatal(err) }
Err returns the current error.
func (iter *Iterator) Key() []byte
Key returns the current key.
func (iter *Iterator) Next() bool
Next reads the next key/value pair and advances the iterator one record. It returns false when the scan stops, either by reaching the end of the database or an error. After Next returns false, the Err method will return any error that occurred while iterating.
func (iter *Iterator) Value() []byte
Value returns the current value.
Writer provides an API for creating a CDB database record by record.
Close or Freeze must be called to finalize the database, or the resulting file will be invalid.
writer, err := Create("/tmp/example.cdb") if err != nil { log.Fatal(err) }
// Write some key/value pairs to the database. writer.Put([]byte("Alice"), []byte("Practice")) writer.Put([]byte("Bob"), []byte("Hope")) writer.Put([]byte("Charlie"), []byte("Horse"))
// It's important to call Close or Freeze when you're finished writing // records. writer.Close()
Create opens a CDB database at the given path. If the file exists, it will be overwritten.
NewWriter opens a CDB database for the given io.WriteSeeker.
If hasher is nil, it will default to the CDB hash function.
Close finalizes the database, then closes it to further writes.
Close or Freeze must be called to finalize the database, or the resulting file will be invalid.
func (cdb Writer) Freeze() (CDB, error)
Freeze finalizes the database, then opens it for reads. If the stream cannot be converted to a io.ReaderAt, Freeze will return os.ErrInvalid.
Close or Freeze must be called to finalize the database, or the resulting file will be invalid.
Put adds a key/value pair to the database.