db

package module
v0.0.0-...-19f2a05 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2020 License: MIT Imports: 15 Imported by: 0

README

DB

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

Only values are encrypted.

db := New()
defer db.Close()

key := keys.RandKey()
if err := db.OpenAtPath(context.TODO(), "my.db", key); err != nil {
    log.Fatal(err)
}

if err := db.Set(context.TODO(), "/collection1/doc1", []byte("hi")); err != nil {
    log.Fatal(err)
}

doc, err  := db.Get(context.TODO(), "/collection1/doc1")
if err != nil {
    log.Fatal(err)
}
fmt.Printf("%s: %s\n", doc.Path, string(doc.Data))

iter, err := db.Documents(context.TODO(), ds.Path("collection1"), nil)
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))
}

Documentation

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 leveldb implementation of ds.DocumentStore.

func New

func New() *DB

New creates a DB.

func (*DB) Close

func (d *DB) Close()

Close the db.

func (*DB) Collections

func (d *DB) Collections(ctx context.Context, parent string) (ds.CollectionIterator, error)

Collections ...

func (*DB) Count

func (d *DB) Count(ctx context.Context, prefix string, contains string) (int, error)

Count returns number of docs in a collection with prefix and filter. This iterates over the prefixed docs to count them.

func (*DB) Create

func (d *DB) Create(ctx context.Context, path string, b []byte) error

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) 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) 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) 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) (*ds.Document, error)

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

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

GetAll paths.

func (*DB) Increment

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

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

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

Last returns last item with key prefix.

func (*DB) Now

func (d *DB) Now() time.Time

Now returns current time.

func (*DB) OpenAtPath

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

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

func (d *DB) Set(ctx context.Context, path string, b []byte) error

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

func (*DB) SetTimeNow

func (d *DB) SetTimeNow(nowFn func() time.Time)

SetTimeNow sets clock.

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