Documentation ¶
Overview ¶
Package table implements readers and writers of leveldb tables.
Tables are either opened for reading or created for writing but not both.
A reader can create iterators, which yield all key/value pairs whose keys are 'greater than or equal' to a starting key. There may be multiple key/ value pairs that have the same key.
A reader can be used concurrently. Multiple goroutines can call Find concurrently, and each iterator can run concurrently with other iterators. However, any particular iterator should not be used concurrently, and iterators should not be used once a reader is closed.
A writer writes key/value pairs in increasing key order, and cannot be used concurrently. A table cannot be read until the writer has finished.
Readers and writers can be created with various options. Passing a nil Options pointer is valid and means to use the default values.
One such option is to define the 'less than' ordering for keys. The default Comparer uses the natural ordering consistent with bytes.Compare. The same ordering should be used for reading and writing a table.
To return the value for a key:
r := table.NewReader(file, options) defer r.Close() return r.Get(key)
To count the number of entries in a table:
i, n := r.Find(nil), 0 for i.Next() { n++ } if err := i.Close(); err != nil { return 0, err } return n, nil
To write a table with three entries:
w := table.NewWriter(file, options) if err := w.Set([]byte("apple"), []byte("red")); err != nil { w.Close() return err } if err := w.Set([]byte("banana"), []byte("yellow")); err != nil { w.Close() return err } if err := w.Set([]byte("cherry"), []byte("red")); err != nil { w.Close() return err } return w.Close()
Index ¶
- type File
- type Reader
- func (r *Reader) Close() error
- func (r *Reader) Delete(key []byte, o *db.WriteOptions) error
- func (r *Reader) Find(key []byte, o *db.ReadOptions) db.Iterator
- func (r *Reader) Get(key []byte, o *db.ReadOptions) (value []byte, err error)
- func (r *Reader) Set(key, value []byte, o *db.WriteOptions) error
- type Writer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader is a table reader. It implements the DB interface, as documented in the leveldb/db package.
func NewReader ¶
NewReader returns a new table reader for the file. Closing the reader will close the file.
func (*Reader) Delete ¶
func (r *Reader) Delete(key []byte, o *db.WriteOptions) error
Delete is provided to implement the DB interface, but returns an error, as a Reader cannot write to a table.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer is a table writer. It implements the DB interface, as documented in the leveldb/db package.
func NewWriter ¶
NewWriter returns a new table writer for the file. Closing the writer will close the file.
func (*Writer) Delete ¶
func (w *Writer) Delete(key []byte, o *db.WriteOptions) error
Delete is provided to implement the DB interface, but returns an error, as a Writer can only append key/value pairs.
func (*Writer) Find ¶
Find is provided to implement the DB interface, but returns an error, as a Writer cannot read from a table.