table

package
v0.0.0-...-016f308 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2015 License: Apache-2.0, BSD-3-Clause Imports: 11 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type File

type File interface {
	io.Closer
	io.ReaderAt
	io.Writer
	Stat() (os.FileInfo, error)
}

File holds the raw bytes for a table.

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

func NewReader(f File, o *db.Options) *Reader

NewReader returns a new table reader for the file. Closing the reader will close the file.

func (*Reader) Close

func (r *Reader) Close() error

Close implements DB.Close, as documented in the leveldb/db package.

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.

func (*Reader) Find

func (r *Reader) Find(key []byte, o *db.ReadOptions) db.Iterator

Find implements DB.Find, as documented in the leveldb/db package.

func (*Reader) Get

func (r *Reader) Get(key []byte, o *db.ReadOptions) (value []byte, err error)

Get implements DB.Get, as documented in the leveldb/db package.

func (*Reader) Set

func (r *Reader) Set(key, value []byte, o *db.WriteOptions) error

Set 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

func NewWriter(f File, o *db.Options) *Writer

NewWriter returns a new table writer for the file. Closing the writer will close the file.

func (*Writer) Close

func (w *Writer) Close() (err error)

Close implements DB.Close, as documented in the leveldb/db package.

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

func (w *Writer) Find(key []byte, o *db.ReadOptions) db.Iterator

Find is provided to implement the DB interface, but returns an error, as a Writer cannot read from a table.

func (*Writer) Get

func (w *Writer) Get(key []byte, o *db.ReadOptions) ([]byte, error)

Get is provided to implement the DB interface, but returns an error, as a Writer cannot read from a table.

func (*Writer) Set

func (w *Writer) Set(key, value []byte, o *db.WriteOptions) error

Set implements DB.Set, as documented in the leveldb/db package. For a given Writer, the keys passed to Set must be in increasing order.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL