Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Datastore type #20

Merged
merged 1 commit into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import (
"github.com/syndtr/goleveldb/leveldb/util"
)

type datastore struct {
type Datastore struct {
*accessor
DB *leveldb.DB
path string
}

var _ ds.Datastore = (*datastore)(nil)
var _ ds.TxnDatastore = (*datastore)(nil)
var _ ds.Datastore = (*Datastore)(nil)
var _ ds.TxnDatastore = (*Datastore)(nil)

// Options is an alias of syndtr/goleveldb/opt.Options which might be extended
// in the future.
Expand All @@ -31,7 +31,7 @@ type Options opt.Options
// NewDatastore returns a new datastore backed by leveldb
//
// for path == "", an in memory bachend will be chosen
func NewDatastore(path string, opts *Options) (*datastore, error) {
func NewDatastore(path string, opts *Options) (*Datastore, error) {
var nopts opt.Options
if opts != nil {
nopts = opt.Options(*opts)
Expand All @@ -53,7 +53,7 @@ func NewDatastore(path string, opts *Options) (*datastore, error) {
return nil, err
}

return &datastore{
return &Datastore{
accessor: &accessor{ldb: db},
DB: db,
path: path,
Expand Down Expand Up @@ -233,7 +233,7 @@ func (a *accessor) runQuery(worker goprocess.Process, qrb *dsq.ResultBuilder) {

// DiskUsage returns the current disk size used by this levelDB.
// For in-mem datastores, it will return 0.
func (d *datastore) DiskUsage() (uint64, error) {
func (d *Datastore) DiskUsage() (uint64, error) {
if d.path == "" { // in-mem
return 0, nil
}
Expand All @@ -256,18 +256,18 @@ func (d *datastore) DiskUsage() (uint64, error) {
}

// LevelDB needs to be closed.
func (d *datastore) Close() (err error) {
func (d *Datastore) Close() (err error) {
return d.DB.Close()
}

func (d *datastore) IsThreadSafe() {}
func (d *Datastore) IsThreadSafe() {}

type leveldbBatch struct {
b *leveldb.Batch
db *leveldb.DB
}

func (d *datastore) Batch() (ds.Batch, error) {
func (d *Datastore) Batch() (ds.Batch, error) {
return &leveldbBatch{
b: new(leveldb.Batch),
db: d.DB,
Expand Down Expand Up @@ -302,7 +302,7 @@ func (t *transaction) Discard() {
t.tx.Discard()
}

func (d *datastore) NewTransaction(readOnly bool) (ds.Txn, error) {
func (d *Datastore) NewTransaction(readOnly bool) (ds.Txn, error) {
tx, err := d.DB.OpenTransaction()
if err != nil {
return nil, err
Expand Down
10 changes: 5 additions & 5 deletions ds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var testcases = map[string]string{
//
// d, close := newDS(t)
// defer close()
func newDS(t *testing.T) (*datastore, func()) {
func newDS(t *testing.T) (*Datastore, func()) {
path, err := ioutil.TempDir("/tmp", "testing_leveldb_")
if err != nil {
t.Fatal(err)
Expand All @@ -45,15 +45,15 @@ func newDS(t *testing.T) (*datastore, func()) {
}

// newDSMem returns an in-memory datastore.
func newDSMem(t *testing.T) *datastore {
func newDSMem(t *testing.T) *Datastore {
d, err := NewDatastore("", nil)
if err != nil {
t.Fatal(err)
}
return d
}

func addTestCases(t *testing.T, d *datastore, testcases map[string]string) {
func addTestCases(t *testing.T, d *Datastore, testcases map[string]string) {
for k, v := range testcases {
dsk := ds.NewKey(k)
if err := d.Put(dsk, []byte(v)); err != nil {
Expand All @@ -74,7 +74,7 @@ func addTestCases(t *testing.T, d *datastore, testcases map[string]string) {

}

func testQuery(t *testing.T, d *datastore) {
func testQuery(t *testing.T, d *Datastore) {
addTestCases(t, d, testcases)

rs, err := d.Query(dsq.Query{Prefix: "/a/"})
Expand Down Expand Up @@ -146,7 +146,7 @@ func expectMatches(t *testing.T, expect []string, actualR dsq.Results) {
}
}

func testBatching(t *testing.T, d *datastore) {
func testBatching(t *testing.T, d *Datastore) {
b, err := d.Batch()
if err != nil {
t.Fatal(err)
Expand Down