Documentation ¶
Index ¶
- func SetLogger(l Logger)
- type ContextLogger
- type DB
- func (d *DB) Close()
- func (d *DB) Collections(ctx context.Context, parent string) (ds.CollectionIterator, error)
- func (d *DB) Count(ctx context.Context, prefix string, contains string) (int, error)
- func (d *DB) Create(ctx context.Context, path string, b []byte) error
- func (d *DB) Delete(ctx context.Context, path string) (bool, error)
- func (d *DB) DeleteAll(ctx context.Context, paths []string) error
- func (d *DB) Documents(ctx context.Context, parent string, opt ...ds.DocumentsOption) (ds.DocumentIterator, error)
- func (d *DB) Exists(ctx context.Context, path string) (bool, error)
- func (d *DB) Get(ctx context.Context, path string) (*ds.Document, error)
- func (d *DB) GetAll(ctx context.Context, paths []string) ([]*ds.Document, error)
- func (d *DB) Increment(ctx context.Context, path string) (string, error)
- func (d *DB) Last(ctx context.Context, prefix string) (*ds.Document, error)
- func (d *DB) Now() time.Time
- func (d *DB) OpenAtPath(ctx context.Context, path string, key SecretKey) error
- func (d *DB) Set(ctx context.Context, path string, b []byte) error
- func (d *DB) SetTimeNow(nowFn func() time.Time)
- type LogLevel
- type Logger
- type SecretKey
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ContextLogger ¶
type ContextLogger interface { Debugf(ctx context.Context, format string, args ...interface{}) Infof(ctx context.Context, format string, args ...interface{}) Warningf(ctx context.Context, format string, args ...interface{}) Errorf(ctx context.Context, format string, args ...interface{}) }
ContextLogger interface used in this package with request context.
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is leveldb implementation of ds.DocumentStore.
func (*DB) Collections ¶
Collections ...
func (*DB) Count ¶
Count returns number of docs in a collection with prefix and filter. This iterates over the prefixed docs to count them.
func (*DB) Create ¶
Create entry.
Example ¶
package main import ( "context" "log" "os" "path/filepath" "github.com/keys-pub/keys" "github.com/keys-pub/keys-ext/db" ) func main() { d := db.New() defer d.Close() key := keys.Rand32() ctx := context.TODO() path := filepath.Join(os.TempDir(), "example-db-create.db") if err := d.OpenAtPath(ctx, path, key); err != nil { log.Fatal(err) } if err := d.Create(context.TODO(), "/test/1", []byte{0x01, 0x02, 0x03}); err != nil { log.Fatal(err) } }
Output:
func (*DB) Documents ¶
func (d *DB) Documents(ctx context.Context, parent string, opt ...ds.DocumentsOption) (ds.DocumentIterator, error)
Documents ...
Example ¶
package main import ( "context" "fmt" "log" "os" "path/filepath" "github.com/keys-pub/keys" "github.com/keys-pub/keys-ext/db" "github.com/keys-pub/keys/ds" ) func main() { d := db.New() defer d.Close() key := keys.Rand32() ctx := context.TODO() path := filepath.Join(os.TempDir(), "example-db-documents.db") if err := d.OpenAtPath(ctx, path, key); err != nil { log.Fatal(err) } // Don't remove db in real life defer os.RemoveAll(path) if err := d.Set(ctx, ds.Path("collection1", "doc1"), []byte("hi")); err != nil { log.Fatal(err) } iter, err := d.Documents(ctx, ds.Path("collection1")) if err != nil { log.Fatal(err) } for { doc, err := iter.Next() if err != nil { log.Fatal(err) } if doc == nil { break } fmt.Printf("%s: %s\n", doc.Path, string(doc.Data)) } }
Output: /collection1/doc1: hi
func (*DB) Get ¶
Get entry at path.
Example ¶
package main import ( "context" "fmt" "log" "os" "path/filepath" "github.com/keys-pub/keys" "github.com/keys-pub/keys-ext/db" "github.com/keys-pub/keys/ds" ) func main() { d := db.New() defer d.Close() key := keys.Rand32() ctx := context.TODO() path := filepath.Join(os.TempDir(), "example-db-get.db") if err := d.OpenAtPath(ctx, path, key); err != nil { log.Fatal(err) } // Don't remove db in real life defer os.RemoveAll(path) if err := d.Set(ctx, ds.Path("collection1", "doc1"), []byte("hi")); err != nil { log.Fatal(err) } doc, err := d.Get(ctx, ds.Path("collection1", "doc1")) if err != nil { log.Fatal(err) } fmt.Printf("Got %s\n", string(doc.Data)) }
Output: Got hi
func (*DB) Increment ¶
Increment returns the current increment as an orderable string. => 000000000000001, 000000000000002 ... This is batched. When the increment runs out for the current batch, it gets a new batch. The increment value is saved in the database at the specified path. There may be large gaps between increments (of batch size).
func (*DB) OpenAtPath ¶
OpenAtPath opens db located at path
Example ¶
package main import ( "context" "log" "os" "path/filepath" "github.com/keys-pub/keys" "github.com/keys-pub/keys-ext/db" ) func main() { d := db.New() defer d.Close() key := keys.Rand32() ctx := context.TODO() path := filepath.Join(os.TempDir(), "example-db-open.db") if err := d.OpenAtPath(ctx, path, key); err != nil { log.Fatal(err) } }
Output:
func (*DB) Set ¶
Set saves document to the db at key.
Example ¶
package main import ( "context" "fmt" "log" "os" "path/filepath" "github.com/keys-pub/keys" "github.com/keys-pub/keys-ext/db" "github.com/keys-pub/keys/ds" ) func main() { d := db.New() defer d.Close() key := keys.Rand32() ctx := context.TODO() path := filepath.Join(os.TempDir(), "example-db-set.db") if err := d.OpenAtPath(ctx, path, key); err != nil { log.Fatal(err) } // Don't remove db in real life defer os.RemoveAll(path) if err := d.Set(ctx, ds.Path("collection1", "doc1"), []byte("hi")); err != nil { log.Fatal(err) } doc, err := d.Get(ctx, ds.Path("collection1", "doc1")) if err != nil { log.Fatal(err) } fmt.Printf("Got %s\n", string(doc.Data)) }
Output: Got hi