Documentation ¶
Overview ¶
Package cdb provides a native implementation of cdb, a constant key/value database with some very nice properties.
For more information on cdb, see the original design doc at http://cr.yp.to/cdb.html.
Example ¶
package main import ( "fmt" "log" "github.com/colinmarc/cdb" ) func main() { writer, err := cdb.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
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrTooMuchData = errors.New("CDB files are limited to 4GB of data")
Functions ¶
This section is empty.
Types ¶
type CDB ¶
type CDB struct {
// contains filtered or unexported fields
}
CDB represents an open CDB database. It can only be used for reads; to create a database, use Writer.
Example ¶
package main import ( "fmt" "log" "github.com/colinmarc/cdb" ) func main() { db, err := cdb.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
func New ¶
New opens a new CDB instance for the given io.ReaderAt. It can only be used for reads; to create a database, use Writer. The returned CDB instance is thread-safe as long as reader is.
If hash 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.
type Iterator ¶
type Iterator struct {
// contains filtered or unexported fields
}
Iterator represents a sequential iterator over a CDB database.
Example ¶
package main import ( "log" "github.com/colinmarc/cdb" ) func main() { db, err := cdb.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) } }
Output:
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
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.
Example ¶
package main import ( "log" "github.com/colinmarc/cdb" ) func main() { writer, err := cdb.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() }
Output:
func Create ¶
Create opens a CDB database at the given path. If the file exists, it will be overwritten. The returned database is not safe for concurrent writes.
func NewWriter ¶
NewWriter opens a CDB database for the given io.WriteSeeker.
If hash is nil, it will default to the CDB hash function.
func (*Writer) Close ¶
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.