delayed.go 1.24 KB
Newer Older
Hector Sanjuan's avatar
Hector Sanjuan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Package delayed wraps a datastore allowing to artificially
// delay all operations.
package delayed

import (
	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}
}

type delayed struct {
	ds    ds.Datastore
	delay delay.D
}

21
func (dds *delayed) Put(key ds.Key, value []byte) (err error) {
Hector Sanjuan's avatar
Hector Sanjuan committed
22 23 24 25
	dds.delay.Wait()
	return dds.ds.Put(key, value)
}

26
func (dds *delayed) Get(key ds.Key) (value []byte, err error) {
Hector Sanjuan's avatar
Hector Sanjuan committed
27 28 29 30 31 32 33 34 35
	dds.delay.Wait()
	return dds.ds.Get(key)
}

func (dds *delayed) Has(key ds.Key) (exists bool, err error) {
	dds.delay.Wait()
	return dds.ds.Has(key)
}

Steven Allen's avatar
Steven Allen committed
36 37 38 39 40
func (dds *delayed) GetSize(key ds.Key) (size int, err error) {
	dds.delay.Wait()
	return dds.ds.GetSize(key)
}

Hector Sanjuan's avatar
Hector Sanjuan committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
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) {
	dds.delay.Wait()
	return dds.ds.Query(q)
}

func (dds *delayed) Batch() (ds.Batch, error) {
	return ds.NewBasicBatch(dds), nil
}

func (dds *delayed) DiskUsage() (uint64, error) {
	dds.delay.Wait()
	return ds.DiskUsage(dds.ds)
}