Documentation ¶
Overview ¶
Package sdb implements dstore.Documents backed by leveldb using data at rest encryption.
Index ¶
- func SetLogger(l Logger)
- type ContextLogger
- type DB
- func (d *DB) Close()
- func (d *DB) Collections(ctx context.Context, parent string) ([]*dstore.Collection, error)
- func (d *DB) Create(ctx context.Context, path string, values map[string]interface{}) 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) DeleteCollection(ctx context.Context, parent string) error
- func (d *DB) DocumentIterator(ctx context.Context, parent string, opt ...dstore.Option) (dstore.Iterator, error)
- func (d *DB) Documents(ctx context.Context, parent string, opt ...dstore.Option) ([]*dstore.Document, error)
- func (d *DB) Exists(ctx context.Context, path string) (bool, error)
- func (d *DB) Get(ctx context.Context, path string) (*dstore.Document, error)
- func (d *DB) GetAll(ctx context.Context, paths []string) ([]*dstore.Document, error)
- func (d *DB) IsOpen() bool
- func (d *DB) Last(ctx context.Context, prefix string) (*dstore.Document, error)
- func (d *DB) Load(ctx context.Context, path string, v interface{}) (bool, error)
- func (d *DB) OpenAtPath(ctx context.Context, path string, key SecretKey) error
- func (d *DB) Set(ctx context.Context, path string, values map[string]interface{}, ...) error
- func (d *DB) SetClock(clock tsutil.Clock)
- func (d *DB) Spew(prefix string, out io.Writer) error
- 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 secure leveldb implementation of dstore.Documents.
func New ¶
func New() *DB
New creates a DB.
Example ¶
db := sdb.New() defer db.Close() key := keys.Rand32() dir, err := ioutil.TempDir("", "") if err != nil { log.Fatal(err) } path := filepath.Join(dir, "my.sdb") if err := db.OpenAtPath(context.TODO(), path, key); err != nil { log.Fatal(err) } type Message struct { ID string `json:"id"` Content string `json:"content"` } msg := &Message{ID: "id1", Content: "hi"} if err := db.Set(context.TODO(), dstore.Path("collection1", "doc1"), dstore.From(msg)); err != nil { log.Fatal(err) } iter, err := db.DocumentIterator(context.TODO(), dstore.Path("collection1")) if err != nil { log.Fatal(err) } defer iter.Release() for { doc, err := iter.Next() if err != nil { log.Fatal(err) } if doc == nil { break } var msg Message if err := doc.To(&msg); err != nil { log.Fatal(err) } fmt.Printf("%s: %s\n", doc.Path, msg.Content) }
Output: /collection1/doc1: hi
func (*DB) Collections ¶
Collections ...
func (*DB) Create ¶
Create entry.
Example ¶
db := sdb.New() defer db.Close() key := keys.Rand32() dir, err := ioutil.TempDir("", "") if err != nil { log.Fatal(err) } path := filepath.Join(dir, "my.sdb") if err := db.OpenAtPath(context.TODO(), path, key); err != nil { log.Fatal(err) } if err := db.Create(context.TODO(), "/test/1", dstore.Data([]byte{0x01, 0x02, 0x03})); err != nil { log.Fatal(err) }
Output:
func (*DB) DeleteCollection ¶
DeleteCollection to at paths at parent.
func (*DB) DocumentIterator ¶
func (d *DB) DocumentIterator(ctx context.Context, parent string, opt ...dstore.Option) (dstore.Iterator, error)
DocumentIterator ...
Example ¶
db := sdb.New() defer db.Close() key := keys.Rand32() dir, err := ioutil.TempDir("", "") if err != nil { log.Fatal(err) } path := filepath.Join(dir, "my.sdb") if err := db.OpenAtPath(context.TODO(), path, key); err != nil { log.Fatal(err) } // Don't remove db in real life defer os.RemoveAll(path) type Message struct { ID string `json:"id"` Content string `json:"content"` } msg := &Message{ID: "id1", Content: "hi"} if err := db.Set(context.TODO(), dstore.Path("collection1", "doc1"), dstore.From(msg)); err != nil { log.Fatal(err) } iter, err := db.DocumentIterator(context.TODO(), dstore.Path("collection1")) if err != nil { log.Fatal(err) } defer iter.Release() for { doc, err := iter.Next() if err != nil { log.Fatal(err) } if doc == nil { break } var msg Message if err := doc.To(&msg); err != nil { log.Fatal(err) } fmt.Printf("%s: %s\n", doc.Path, msg.Content) }
Output: /collection1/doc1: hi
func (*DB) Documents ¶
func (d *DB) Documents(ctx context.Context, parent string, opt ...dstore.Option) ([]*dstore.Document, error)
Documents ...
Example ¶
db := sdb.New() defer db.Close() key := keys.Rand32() dir, err := ioutil.TempDir("", "") if err != nil { log.Fatal(err) } path := filepath.Join(dir, "my.sdb") if err := db.OpenAtPath(context.TODO(), path, key); err != nil { log.Fatal(err) } // Don't remove db in real life defer os.RemoveAll(path) if err := db.Set(context.TODO(), dstore.Path("collection1", "doc1"), dstore.Data([]byte("hi"))); err != nil { log.Fatal(err) } docs, err := db.Documents(context.TODO(), dstore.Path("collection1")) if err != nil { log.Fatal(err) } for _, doc := range docs { fmt.Printf("%s: %s\n", doc.Path, string(doc.Data())) }
Output: /collection1/doc1: hi
func (*DB) Get ¶
Get entry at path.
Example ¶
db := sdb.New() defer db.Close() key := keys.Rand32() dir, err := ioutil.TempDir("", "") if err != nil { log.Fatal(err) } path := filepath.Join(dir, "my.sdb") if err := db.OpenAtPath(context.TODO(), path, key); err != nil { log.Fatal(err) } // Don't remove db in real life defer os.RemoveAll(path) if err := db.Set(context.TODO(), dstore.Path("collection1", "doc1"), dstore.Data([]byte("hi"))); err != nil { log.Fatal(err) } doc, err := db.Get(context.TODO(), dstore.Path("collection1", "doc1")) if err != nil { log.Fatal(err) } fmt.Printf("Got %s\n", string(doc.Data()))
Output: Got hi
func (*DB) OpenAtPath ¶
OpenAtPath opens db located at path.
Example ¶
db := sdb.New() defer db.Close() key := keys.Rand32() dir, err := ioutil.TempDir("", "") if err != nil { log.Fatal(err) } path := filepath.Join(dir, "my.sdb") if err := db.OpenAtPath(context.TODO(), path, key); err != nil { log.Fatal(err) }
Output:
func (*DB) Set ¶
func (d *DB) Set(ctx context.Context, path string, values map[string]interface{}, opt ...dstore.SetOption) error
Set saves document to the db at key.
Example ¶
db := sdb.New() defer db.Close() key := keys.Rand32() dir, err := ioutil.TempDir("", "") if err != nil { log.Fatal(err) } path := filepath.Join(dir, "my.sdb") if err := db.OpenAtPath(context.TODO(), path, key); err != nil { log.Fatal(err) } // Don't remove db in real life defer os.RemoveAll(path) type Message struct { ID string `msgpack:"id"` Content string `msgpack:"content"` } msg := &Message{ID: "id1", Content: "hi"} if err := db.Set(context.TODO(), dstore.Path("collection1", "doc1"), dstore.From(msg)); err != nil { log.Fatal(err) } doc, err := db.Get(context.TODO(), dstore.Path("collection1", "doc1")) if err != nil { log.Fatal(err) } var out Message if err := doc.To(&out); err != nil { log.Fatal(err) } fmt.Printf("Message: %s\n", out.Content)
Output: Message: hi
Click to show internal directories.
Click to hide internal directories.