sdb

package module
v0.0.0-...-c1743e4 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2024 License: MIT Imports: 17 Imported by: 1

README

SDB

This package implements a leveldb database encrypted with github.com/minio/sio (DARE).

Only values are encrypted.

Examples.

Documentation

Overview

Package sdb implements dstore.Documents backed by leveldb using data at rest encryption.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetLogger

func SetLogger(l Logger)

SetLogger sets logger for the package.

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.

func NewContextLogger

func NewContextLogger(lev LogLevel) ContextLogger

NewContextLogger ...

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) Close

func (d *DB) Close()

Close the db.

func (*DB) Collections

func (d *DB) Collections(ctx context.Context, parent string) ([]*dstore.Collection, error)

Collections ...

func (*DB) Create

func (d *DB) Create(ctx context.Context, path string, values map[string]interface{}) error

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) Delete

func (d *DB) Delete(ctx context.Context, path string) (bool, error)

Delete value at path.

func (*DB) DeleteAll

func (d *DB) DeleteAll(ctx context.Context, paths []string) error

DeleteAll paths.

func (*DB) DeleteCollection

func (d *DB) DeleteCollection(ctx context.Context, parent string) error

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) Exists

func (d *DB) Exists(ctx context.Context, path string) (bool, error)

Exists returns true if the db row exists at path

func (*DB) Get

func (d *DB) Get(ctx context.Context, path string) (*dstore.Document, error)

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) GetAll

func (d *DB) GetAll(ctx context.Context, paths []string) ([]*dstore.Document, error)

GetAll paths.

func (*DB) IsOpen

func (d *DB) IsOpen() bool

IsOpen returns true if open.

func (*DB) Last

func (d *DB) Last(ctx context.Context, prefix string) (*dstore.Document, error)

Last returns last item with key prefix.

func (*DB) Load

func (d *DB) Load(ctx context.Context, path string, v interface{}) (bool, error)

Load path into value.

func (*DB) OpenAtPath

func (d *DB) OpenAtPath(ctx context.Context, path string, key SecretKey) error

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

func (*DB) SetClock

func (d *DB) SetClock(clock tsutil.Clock)

SetClock sets clock.

func (*DB) Spew

func (d *DB) Spew(prefix string, out io.Writer) error

Spew ...

type LogLevel

type LogLevel int

LogLevel ...

const (
	// DebugLevel ...
	DebugLevel LogLevel = 3
	// InfoLevel ...
	InfoLevel LogLevel = 2
	// WarnLevel ...
	WarnLevel LogLevel = 1
	// ErrLevel ...
	ErrLevel LogLevel = 0
)

type Logger

type Logger interface {
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Warningf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})
}

Logger interface used in this package.

func NewLogger

func NewLogger(lev LogLevel) Logger

NewLogger ...

type SecretKey

type SecretKey *[32]byte

SecretKey for database.

Jump to

Keyboard shortcuts

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