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

delayed: implement io.Closer and export datastore type. #108

Merged
merged 2 commits into from
Dec 27, 2018
Merged
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
44 changes: 33 additions & 11 deletions delayed/delayed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,78 @@
package delayed

import (
"io"

ds "github.com/ipfs/go-datastore"
dsq "github.com/ipfs/go-datastore/query"
delay "github.com/ipfs/go-ipfs-delay"
)

// New returns a new delayed datastore.
func New(ds ds.Datastore, delay delay.D) ds.Datastore {
return &delayed{ds: ds, delay: delay}
func New(ds ds.Datastore, delay delay.D) *Delayed {
return &Delayed{ds: ds, delay: delay}
}

type delayed struct {
// Delayed is an adapter that delays operations on the inner datastore.
type Delayed struct {
ds ds.Datastore
delay delay.D
}

func (dds *delayed) Put(key ds.Key, value []byte) (err error) {
var _ ds.Batching = (*Delayed)(nil)
var _ io.Closer = (*Delayed)(nil)

// Put implements the ds.Datastore interface.
func (dds *Delayed) Put(key ds.Key, value []byte) (err error) {
dds.delay.Wait()
return dds.ds.Put(key, value)
}

func (dds *delayed) Get(key ds.Key) (value []byte, err error) {
// Get implements the ds.Datastore interface.
func (dds *Delayed) Get(key ds.Key) (value []byte, err error) {
dds.delay.Wait()
return dds.ds.Get(key)
}

func (dds *delayed) Has(key ds.Key) (exists bool, err error) {
// Has implements the ds.Datastore interface.
func (dds *Delayed) Has(key ds.Key) (exists bool, err error) {
dds.delay.Wait()
return dds.ds.Has(key)
}

func (dds *delayed) GetSize(key ds.Key) (size int, err error) {
// GetSize implements the ds.Datastore interface.
func (dds *Delayed) GetSize(key ds.Key) (size int, err error) {
dds.delay.Wait()
return dds.ds.GetSize(key)
}

func (dds *delayed) Delete(key ds.Key) (err error) {
// Delete implements the ds.Datastore interface.
func (dds *Delayed) Delete(key ds.Key) (err error) {
dds.delay.Wait()
return dds.ds.Delete(key)
}

func (dds *delayed) Query(q dsq.Query) (dsq.Results, error) {
// Query implements the ds.Datastore interface.
func (dds *Delayed) Query(q dsq.Query) (dsq.Results, error) {
dds.delay.Wait()
return dds.ds.Query(q)
}

func (dds *delayed) Batch() (ds.Batch, error) {
// Batch implements the ds.Batching interface.
func (dds *Delayed) Batch() (ds.Batch, error) {
return ds.NewBasicBatch(dds), nil
}

func (dds *delayed) DiskUsage() (uint64, error) {
// DiskUsage implements the ds.PersistentDatastore interface.
func (dds *Delayed) DiskUsage() (uint64, error) {
dds.delay.Wait()
return ds.DiskUsage(dds.ds)
}

// Close closes the inner datastore (if it implements the io.Closer interface).
func (dds *Delayed) Close() error {
if closer, ok := dds.ds.(io.Closer); ok {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, if I understand correctly, this will be forward compatible with a Close method that D is not implementing at the moment, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for the underlying datastore, not the delay. It allows us to wrap e.g. badger without hiding the Close method.

return closer.Close()
}
return nil
}