Skip to content

Commit

Permalink
feat: inmem repo
Browse files Browse the repository at this point in the history
  • Loading branch information
alok committed Dec 1, 2021
1 parent fbc0370 commit a9ba8c8
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 20 deletions.
18 changes: 15 additions & 3 deletions modules/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/plexsysio/go-msuite/modules/node/locker"
"github.com/plexsysio/go-msuite/modules/repo"
"github.com/plexsysio/go-msuite/modules/repo/fsrepo"
"github.com/plexsysio/go-msuite/modules/repo/inmem"
"github.com/plexsysio/go-msuite/modules/sharedStorage"
"github.com/plexsysio/go-msuite/utils"
"github.com/plexsysio/taskmanager"
Expand All @@ -51,9 +52,20 @@ var authModule = func(c config.Config) fx.Option {
}

func New(bCfg config.Config) (core.Service, error) {
r, err := fsrepo.CreateOrOpen(bCfg)
if err != nil {
return nil, err
var (
r repo.Repo
err error
)
if bCfg.Get("RootPath", new(string)) {
r, err = fsrepo.CreateOrOpen(bCfg)
if err != nil {
return nil, err
}
} else {
r, err = inmem.CreateOrOpen(bCfg)
if err != nil {
return nil, err
}
}
svc := &impl{}

Expand Down
81 changes: 81 additions & 0 deletions modules/repo/inmem/inmem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package inmem

import (
"encoding/base64"

"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/plexsysio/gkvstore"
ipfsdsStore "github.com/plexsysio/gkvstore-ipfsds"
"github.com/plexsysio/go-msuite/modules/config"
"github.com/plexsysio/go-msuite/modules/repo"
)

type inmemRepo struct {
c config.Config
ds datastore.Batching
st gkvstore.Store
}

func initIdentity(c config.Config) error {
if c.Get("Identity", make(map[string]interface{})) {
return nil
}
sk, pk, err := crypto.GenerateKeyPair(crypto.Ed25519, 2048)
if err != nil {
return err
}
skbytes, err := crypto.MarshalPrivateKey(sk)
if err != nil {
return err
}
ident := map[string]interface{}{}
ident["PrivKey"] = base64.StdEncoding.EncodeToString(skbytes)

id, err := peer.IDFromPublicKey(pk)
if err != nil {
return err
}
ident["ID"] = id.Pretty()
c.Set("Identity", ident)
return nil
}

func CreateOrOpen(c config.Config) (repo.Repo, error) {
err := initIdentity(c)
if err != nil {
return nil, err
}
ds := datastore.NewMapDatastore()
st := ipfsdsStore.New(namespace.Wrap(ds, datastore.NewKey("/kv")))
return &inmemRepo{
c: c,
ds: ds,
st: st,
}, nil
}

func (i *inmemRepo) Config() config.Config {
return i.c
}

func (i *inmemRepo) SetConfig(c config.Config) error {
i.c = c
return nil
}

func (i *inmemRepo) Datastore() datastore.Batching {
return i.ds
}

func (i *inmemRepo) Store() gkvstore.Store {
return i.st
}

func (i *inmemRepo) Close() error {
i.ds.Close()
i.st.Close()
return nil
}
58 changes: 58 additions & 0 deletions modules/repo/inmem/inmem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package inmem_test

import (
"reflect"
"testing"

jsonConf "github.com/plexsysio/go-msuite/modules/config/json"
"github.com/plexsysio/go-msuite/modules/repo/inmem"
)

func TestInmemRepo(t *testing.T) {
t.Run("create", func(t *testing.T) {
r, err := inmem.CreateOrOpen(jsonConf.DefaultConfig())
if err != nil {
t.Fatal(err)
}

err = r.Close()
if err != nil {
t.Fatal(err)
}
})
t.Run("create with config", func(t *testing.T) {
cfg := jsonConf.DefaultConfig()
cfg.Set("Dummy1", "Val1")
cfg.Set("Dummy2", "Val2")

r, err := inmem.CreateOrOpen(cfg)
if err != nil {
t.Fatal(err)
}

cfg2 := r.Config()
if !reflect.DeepEqual(cfg, cfg2) {
t.Fatal("expected config to be the same")
}

cfg3 := jsonConf.DefaultConfig()
err = r.SetConfig(cfg3)
if err != nil {
t.Fatal(err)
}

cfg4 := r.Config()
if !reflect.DeepEqual(cfg4, cfg3) {
t.Fatal("expected config to be the same")
}

if reflect.DeepEqual(cfg4, cfg) {
t.Fatal("expected config to be different")
}

err = r.Close()
if err != nil {
t.Fatal(err)
}
})
}
9 changes: 0 additions & 9 deletions msuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package msuite
import (
"context"
"encoding/base64"
"path/filepath"
"time"

"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/mitchellh/go-homedir"
"github.com/plexsysio/go-msuite/core"
"github.com/plexsysio/go-msuite/modules/config"
jsonConf "github.com/plexsysio/go-msuite/modules/config/json"
Expand Down Expand Up @@ -166,13 +164,6 @@ func defaultOpts(c *BuildCfg) {
}
c.startupCfg.Set("Services", services)

if !c.startupCfg.Exists("RootPath") {
hd, err := homedir.Dir()
if err != nil {
panic("Unable to determine home directory")
}
c.startupCfg.Set("RootPath", filepath.Join(hd, ".msuite"))
}
if c.startupCfg.IsSet("UseP2P") || c.startupCfg.IsSet("UseTCP") || c.startupCfg.IsSet("UseHTTP") {
tmCfg := map[string]int{}
found := c.startupCfg.Get("TMWorkers", &tmCfg)
Expand Down
8 changes: 0 additions & 8 deletions msuite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ func TestBasicNew(t *testing.T) {
}

func TestTM(t *testing.T) {
defer os.RemoveAll("tmp1")
app, err := msuite.New(
msuite.WithRepositoryRoot("tmp1"),
msuite.WithTaskManager(5, 100),
)
if err != nil {
Expand Down Expand Up @@ -184,9 +182,7 @@ func TestTM(t *testing.T) {
}

func TestNode(t *testing.T) {
defer os.RemoveAll("tmp2")
app, err := msuite.New(
msuite.WithRepositoryRoot("tmp2"),
msuite.WithP2PPort(10000),
)
if err != nil {
Expand Down Expand Up @@ -249,9 +245,7 @@ func TestHTTP(t *testing.T) {
}

func TestGRPCLockerAuth(t *testing.T) {
defer os.RemoveAll("tmp4")
app, err := msuite.New(
msuite.WithRepositoryRoot("tmp4"),
msuite.WithP2PPort(10000),
msuite.WithFiles(),
msuite.WithGRPC(),
Expand Down Expand Up @@ -365,7 +359,6 @@ func TestPrivateKey(t *testing.T) {

func TestServices(t *testing.T) {
defer os.RemoveAll("tmp5")
defer os.RemoveAll("tmp6")

app, err := msuite.New(
msuite.WithServiceName("test"),
Expand All @@ -387,7 +380,6 @@ func TestServices(t *testing.T) {

app, err = msuite.New(
msuite.WithServiceName("test"),
msuite.WithRepositoryRoot("tmp6"),
msuite.WithGRPCTCPListener(10000),
msuite.WithHTTP(10001),
msuite.WithPrometheus(true),
Expand Down

0 comments on commit a9ba8c8

Please sign in to comment.