kubo/thirdparty/datastore2/datastore_closer.go
Łukasz Magiera 6401a9191e gx: Update go-datastore to 1.4.0
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
2017-12-02 14:55:26 -08:00

41 lines
913 B
Go

package datastore2
import (
"io"
"gx/ipfs/QmdHG8MAuARdGHxx4rPQASLcvhz24fzjSQq7AJRAQEorq5/go-datastore"
syncds "gx/ipfs/QmdHG8MAuARdGHxx4rPQASLcvhz24fzjSQq7AJRAQEorq5/go-datastore/sync"
)
type ThreadSafeDatastoreCloser interface {
datastore.ThreadSafeDatastore
io.Closer
Batch() (datastore.Batch, error)
}
func CloserWrap(ds datastore.ThreadSafeDatastore) ThreadSafeDatastoreCloser {
return &datastoreCloserWrapper{ds}
}
func ThreadSafeCloserMapDatastore() ThreadSafeDatastoreCloser {
return CloserWrap(syncds.MutexWrap(datastore.NewMapDatastore()))
}
type datastoreCloserWrapper struct {
datastore.ThreadSafeDatastore
}
func (w *datastoreCloserWrapper) Close() error {
return nil // no-op
}
func (w *datastoreCloserWrapper) Batch() (datastore.Batch, error) {
bds, ok := w.ThreadSafeDatastore.(datastore.Batching)
if !ok {
return nil, datastore.ErrBatchUnsupported
}
return bds.Batch()
}