Skip to content

Commit

Permalink
Merge pull request #36 from mentos1386/add-docstore
Browse files Browse the repository at this point in the history
feat: add document store
  • Loading branch information
glouvigny authored Aug 23, 2021
2 parents e7d1ddc + d425fea commit ad349a0
Show file tree
Hide file tree
Showing 9 changed files with 921 additions and 20 deletions.
21 changes: 11 additions & 10 deletions baseorbitdb/orbitdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,16 +754,17 @@ func (o *orbitDB) createStore(ctx context.Context, storeType string, parsedDBAdd
}

store, err := storeFunc(ctx, o.IPFS(), identity, parsedDBAddress, &iface.NewStoreOptions{
AccessController: accessController,
Cache: options.Cache,
Replicate: options.Replicate,
Directory: *options.Directory,
SortFn: options.SortFn,
CacheDestroy: func() error { return o.cache.Destroy(o.directory, parsedDBAddress) },
Logger: o.logger,
Tracer: o.tracer,
IO: options.IO,
SharedKey: options.SharedKey,
AccessController: accessController,
Cache: options.Cache,
Replicate: options.Replicate,
Directory: *options.Directory,
SortFn: options.SortFn,
CacheDestroy: func() error { return o.cache.Destroy(o.directory, parsedDBAddress) },
Logger: o.logger,
Tracer: o.tracer,
IO: options.IO,
SharedKey: options.SharedKey,
StoreSpecificOpts: options.StoreSpecificOpts,
})
if err != nil {
return nil, errors.Wrap(err, "unable to instantiate store")
Expand Down
66 changes: 58 additions & 8 deletions iface/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ type CreateDBOptions struct {
SortFn ipfslog.SortFn
IO ipfslog.IO
SharedKey enc.SharedKey
StoreSpecificOpts interface{}
}

type CreateDocumentDBOptions struct {
KeyExtractor func(interface{}) (string, error)
Marshal func(interface{}) ([]byte, error)
Unmarshal func(data []byte, v interface{}) error
ItemFactory func() interface{}
}

// DetermineAddressOptions Lists the arguments used to determine a store address
Expand Down Expand Up @@ -70,7 +78,7 @@ type BaseOrbitDB interface {
// RegisterStoreType Registers a new store type
RegisterStoreType(storeType string, constructor StoreConstructor)

// RegisterStoreType Removes a store type
// UnregisterStoreType Removes a store type
UnregisterStoreType(storeType string)

// RegisterAccessControllerType Registers a new access controller type
Expand All @@ -89,13 +97,25 @@ type BaseOrbitDB interface {
Tracer() trace.Tracer
}

// OrbitDBDocumentStore An OrbitDB instance providing a Document store
type OrbitDBDocumentStore interface {
BaseOrbitDB
OrbitDBDocumentStoreProvider
}

// OrbitDBDocumentStoreProvider Exposes a method providing a document store
type OrbitDBDocumentStoreProvider interface {
// Docs Creates or opens an DocumentStore
Docs(ctx context.Context, address string, options *CreateDBOptions) (DocumentStore, error)
}

// OrbitDBKVStore An OrbitDB instance providing a KeyValue store
type OrbitDBKVStore interface {
BaseOrbitDB
OrbitDBKVStoreProvider
}

// OrbitDBLogStoreProvider Exposes a method providing a key value store
// OrbitDBKVStoreProvider Exposes a method providing a key value store
type OrbitDBKVStoreProvider interface {
// KeyValue Creates or opens an KeyValueStore
KeyValue(ctx context.Context, address string, options *CreateDBOptions) (KeyValueStore, error)
Expand All @@ -119,6 +139,7 @@ type OrbitDB interface {

OrbitDBKVStoreProvider
OrbitDBLogStoreProvider
OrbitDBDocumentStoreProvider
}

// StreamOptions Defines the parameters that can be given to the Stream function of an EventLogStore
Expand Down Expand Up @@ -152,7 +173,7 @@ type Store interface {
// Replicator Returns the Replicator object
Replicator() replicator.Replicator

// Replicator Returns the Cache object
// Cache Returns the Cache object
Cache() datastore.Datastore

// Drop Removes all the local store content
Expand Down Expand Up @@ -216,7 +237,7 @@ type EventLogStore interface {
List(ctx context.Context, options *StreamOptions) ([]operation.Operation, error)
}

// EventLogStore A type of store that provides a key value store
// KeyValueStore A type of store that provides a key value store
type KeyValueStore interface {
Store

Expand All @@ -233,6 +254,34 @@ type KeyValueStore interface {
Get(ctx context.Context, key string) ([]byte, error)
}

type DocumentStoreGetOptions struct {
CaseInsensitive bool
PartialMatches bool
}

// DocumentStore A type of store that provides a document store
type DocumentStore interface {
Store

// Put Stores the document
Put(ctx context.Context, document interface{}) (operation.Operation, error)

// Delete Clears the document for a key
Delete(ctx context.Context, key string) (operation.Operation, error)

// PutBatch Add values as multiple operations and returns the latest
PutBatch(ctx context.Context, values []interface{}) (operation.Operation, error)

// PutAll Add values as a single operation and returns it
PutAll(ctx context.Context, values []interface{}) (operation.Operation, error)

// Get Retrieves the document for a key
Get(ctx context.Context, key string, opts *DocumentStoreGetOptions) ([]interface{}, error)

// Query Finds documents using a filter function
Query(ctx context.Context, filter func(doc interface{}) (bool, error)) ([]interface{}, error)
}

// StoreIndex Index contains the state of a datastore,
// ie. what data we currently have.
//
Expand Down Expand Up @@ -272,6 +321,7 @@ type NewStoreOptions struct {
Tracer trace.Tracer
IO ipfslog.IO
SharedKey enc.SharedKey
StoreSpecificOpts interface{}
}

type DirectChannelOptions struct {
Expand All @@ -284,7 +334,7 @@ type DirectChannel interface {
// Connect Waits for the other peer to be connected
Connect(context.Context) error

// Sends Sends a message to the other peer
// Send Sends a message to the other peer
Send(context.Context, []byte) error

// Close Closes the connection
Expand Down Expand Up @@ -316,15 +366,15 @@ type PubSubTopic interface {
// WatchPeers subscribes to peers joining or leaving the topic
WatchPeers(ctx context.Context) (<-chan events.Event, error)

// WatchMessages
// WatchMessages Subscribes to new messages
WatchMessages(ctx context.Context) (<-chan *EventPubSubMessage, error)

// Returns the topic name
// Topic Returns the topic name
Topic() string
}

type PubSubInterface interface {
// Subscribe Subscribes to a topic
// TopicSubscribe Subscribes to a topic
TopicSubscribe(ctx context.Context, topic string) (PubSubTopic, error)
}

Expand Down
34 changes: 32 additions & 2 deletions orbitdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ package orbitdb
import (
"context"

coreapi "github.com/ipfs/interface-go-ipfs-core"
"github.com/pkg/errors"

"berty.tech/go-orbit-db/accesscontroller/ipfs"
"berty.tech/go-orbit-db/accesscontroller/orbitdb"
"berty.tech/go-orbit-db/accesscontroller/simple"
"berty.tech/go-orbit-db/baseorbitdb"
"berty.tech/go-orbit-db/iface"
"berty.tech/go-orbit-db/stores/documentstore"
"berty.tech/go-orbit-db/stores/eventlogstore"
"berty.tech/go-orbit-db/stores/kvstore"
coreapi "github.com/ipfs/interface-go-ipfs-core"
"github.com/pkg/errors"
)

type orbitDB struct {
Expand All @@ -30,6 +32,9 @@ type EventLogStore = iface.EventLogStore
// KeyValueStore An alias of the type defined in the iface package
type KeyValueStore = iface.KeyValueStore

// DocumentStore An alias of the type defined in the iface package
type DocumentStore = iface.DocumentStore

// StoreIndex An alias of the type defined in the iface package
type StoreIndex = iface.StoreIndex

Expand All @@ -48,6 +53,9 @@ type StreamOptions = iface.StreamOptions
// CreateDBOptions An alias of the type defined in the iface package
type CreateDBOptions = iface.CreateDBOptions

// CreateDocumentDBOptions An alias of the type defined in the iface package
type CreateDocumentDBOptions = iface.CreateDocumentDBOptions

// DetermineAddressOptions An alias of the type defined in the iface package
type DetermineAddressOptions = iface.DetermineAddressOptions

Expand All @@ -64,6 +72,7 @@ func NewOrbitDB(ctx context.Context, i coreapi.CoreAPI, options *NewOrbitDBOptio

odb.RegisterStoreType("eventlog", eventlogstore.NewOrbitDBEventLogStore)
odb.RegisterStoreType("keyvalue", kvstore.NewOrbitDBKeyValue)
odb.RegisterStoreType("docstore", documentstore.NewOrbitDBDocumentStore)

_ = odb.RegisterAccessControllerType(ipfs.NewIPFSAccessController)
_ = odb.RegisterAccessControllerType(orbitdb.NewOrbitDBAccessController)
Expand Down Expand Up @@ -123,4 +132,25 @@ func (o *orbitDB) KeyValue(ctx context.Context, address string, options *CreateD
return kvStore, nil
}

func (o *orbitDB) Docs(ctx context.Context, address string, options *CreateDBOptions) (DocumentStore, error) {
if options == nil {
options = &CreateDBOptions{}
}

options.Create = boolPtr(true)
options.StoreType = stringPtr("docstore")

store, err := o.Open(ctx, address, options)
if err != nil {
return nil, errors.Wrap(err, "unable to open database")
}

documentStore, ok := store.(DocumentStore)
if !ok {
return nil, errors.New("unable to cast store to document")
}

return documentStore, nil
}

var _ OrbitDB = (*orbitDB)(nil)
2 changes: 2 additions & 0 deletions stores/documentstore/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// documentstore a document store for OrbitDB
package documentstore // import "berty.tech/go-orbit-db/stores/documentstore"
Loading

0 comments on commit ad349a0

Please sign in to comment.