Documentation ¶
Index ¶
- Variables
- func ContentToMapString(content Content) (map[string]string, error)
- func Create(writer io.WriterAt, content Content, opts *CreateOptions) error
- func CreateFile(fname string, content Content, opts *CreateOptions) error
- func CreateInto(writer io.Writer, content Content, opts *CreateOptions) error
- type Content
- type CreateOptions
- type Database
- func (db *Database) Get(key []byte) ([]byte, error)
- func (db *Database) GetAsReader(key []byte) (io.Reader, error)
- func (db *Database) GetString(key string) (string, error)
- func (db *Database) GetStringAsReader(key string) (io.Reader, error)
- func (db *Database) Has(key []byte) (bool, error)
- func (db *Database) HasString(key string) (bool, error)
- type KeyValue
- type KeyValueMapString
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrOverflow = errors.New("overflow error") ErrOverflowUint32 = fmt.Errorf("%w: over 4GiB which fits in a 32bit unsigned integer", ErrOverflow) )
var DefaultCreateOptions = &CreateOptions{
HeadroomPerMille: 250,
}
var NotFound = errors.New("not found")
Functions ¶
func Create ¶
func Create(writer io.WriterAt, content Content, opts *CreateOptions) error
Create creates a constant database with the given content.
The data is never kept entirely in memory. However a few 32bits integer are kept in memory for each key/value pair, in order to write the hash tables at the end.
If opts are nil, the DefaultCreateOptions are used.
func CreateFile ¶
func CreateFile(fname string, content Content, opts *CreateOptions) error
CreateFile creates a constant database with the given content into the file.
func CreateInto ¶
func CreateInto(writer io.Writer, content Content, opts *CreateOptions) error
CreateInto creates a constant database with the given content into a Writer.
Because of the nature of how constant database are created, this method buffers the entire data in memory.
Types ¶
type Content ¶
Content represent a list of KeyValue.
Content is not goroutine safe, and MUST NOT be used in mutiple goroutine at the same time.
Example ¶
/* Copyright 2023, Antoine Catton * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ package main import ( "fmt" "io" ) func getContent() Content { return &KeyValueMapString{ Map: map[string]string{"foo": "bar"}, } } func main() { var content Content = getContent() for content.Next() { kv := content.KeyValue() key, err := io.ReadAll(kv.Key) if err != nil { fmt.Printf("ERROR: while reading key: %v\n", err) } value, err := io.ReadAll(kv.Value) if err != nil { fmt.Printf("ERROR: while reading value: %v\n", err) } fmt.Printf("%q=%q\n", key, value) } if err := content.Err(); err != nil { fmt.Printf("ERROR: while scanning content: %v\n", err) } }
Output: "foo"="bar"
type CreateOptions ¶
type CreateOptions struct { // Headroom in ‰ (= tenth of a percent). This is the headroom left after // the hash table is full. This number should be between 0 and 999. // Here are a few rules: // * 1000‰ headroom would lead to a division by zero. // * 0 means the hashtable is fully populated. // * if the hash table has 1000 entries, 1‰ means it will contain 1001 // slots after creation. HeadroomPerMille int }
type Database ¶
type Database struct {
// contains filtered or unexported fields
}
Database represents a read-only database.
It can be created with NewDatabase or with Open. Its goroutine safety depends on the underlying ReaderAt being used. It does not guard reads with a mutex. For files (= using Open()) or mmaps (= using golang.org/x/exp/mmap), it is goroutine safe. For other ReaderAt, please consult the documentations for these.
func NewDatabase ¶
NewDatabase instantiate a new database from a ReaderAt
This allows to instantiate a database from an mmap file, using golang.org/x/exp/mmap for example.
func Open ¶
Open opens a CDB file as a database.
The resulting database is thread safe. For better performance, it might advisable to pass the result of a mmap.Open() call to NewDatabase() by using golang.org/x/exp/mmap. This is not provided by default as an API in order to prevent dependency-creep.
Open also returns the function to close the file.
func (*Database) Get ¶
Get fetches the value as a slice of bytes.
If the key is not found it will return (nil, NotFound).
func (*Database) GetAsReader ¶
GetAsReader gets the value as a reader.
The returned reader is not goroutine specific, and MUST NOT be shared across goroutine.
If the key is not found it will return (nil, NotFound).
func (*Database) GetString ¶
Get fetches the value as string for a string key.
If the key is not found it will return ("", NotFound).
func (*Database) GetStringAsReader ¶
GetStringAsReader is the exact same as GetAsReader, but it takes a string.
type KeyValue ¶
KeyValue represent a stream Key and Value pair.
Key MUST be fully read before reading the Value reader. If not, behavior are undefined.
KeyValue is not goroutine safe, and MUST NOT be used in multiple goroutine at the same time.
type KeyValueMapString ¶
KeyValueMapString is a simple wrapper for a map[string]string, in order to provide a content interface.
KeyValueMapString MUST NOT be shared across goroutines, as it is not goroutine safe.
func (*KeyValueMapString) Err ¶
func (kv *KeyValueMapString) Err() error
func (*KeyValueMapString) KeyValue ¶
func (kv *KeyValueMapString) KeyValue() *KeyValue
func (*KeyValueMapString) Next ¶
func (kv *KeyValueMapString) Next() bool