From 60437280ba7761ff7d19fb074208aa26b5cfc517 Mon Sep 17 00:00:00 2001 From: Nikolay Govorov Date: Wed, 17 Apr 2024 16:28:45 +0100 Subject: [PATCH] Cleanup dirs in tests, update Open signature --- chotki.go | 48 +++++++++++++++++++++----------------- chotki_test.go | 52 +++++++++++++++++++++++++++--------------- object_example_test.go | 21 ++++++----------- objects_test.go | 11 ++++----- repl/commands.go | 33 ++++++++++++--------------- 5 files changed, 87 insertions(+), 78 deletions(-) diff --git a/chotki.go b/chotki.go index e40e4aa..2439d46 100644 --- a/chotki.go +++ b/chotki.go @@ -21,10 +21,24 @@ type Packet []byte type Batch [][]byte type Options struct { + pebble.Options + + Orig uint64 + Name string RelaxedOrder bool MaxLogLen int64 } +func (o *Options) SetDefaults() { + if o.MaxLogLen == 0 { + o.MaxLogLen = 1 << 23 + } + + if o.Merger == nil { + o.Merger = &pebble.Merger{Name: "CRDT", Merge: merger} + } +} + type Hook func(cho *Chotki, id rdx.ID) error type CallHook struct { @@ -120,12 +134,6 @@ func ReplicaDirName(rno uint64) string { return fmt.Sprintf("cho%x", rno) } -func (o *Options) SetDefaults() { - if o.MaxLogLen == 0 { - o.MaxLogLen = 1 << 23 - } -} - func merger(key, value []byte) (pebble.ValueMerger, error) { /*if len(key) != 10 { return nil, nil @@ -161,55 +169,53 @@ func Exists(dirname string) (bool, error) { return desc.Exists, nil } -func Open(orig uint64, name, dirname string) (*Chotki, bool, error) { +func Open(dirname string, opts Options) (*Chotki, error) { exists, err := Exists(dirname) if err != nil { - return nil, false, err + return nil, err } - db, err := pebble.Open(dirname, &pebble.Options{ - ErrorIfExists: false, - ErrorIfNotExists: false, - Merger: &pebble.Merger{Name: "CRDT", Merge: merger}, - }) + opts.SetDefaults() // todo param + + db, err := pebble.Open(dirname, &opts.Options) if err != nil { - return nil, exists, err + return nil, err } conn := Chotki{ db: db, - src: orig, + src: opts.Orig, dir: dirname, + opts: opts, types: make(map[rdx.ID]Fields), hooks: make(map[rdx.ID][]Hook), syncs: make(map[rdx.ID]*pebble.Batch), outq: make(map[string]toyqueue.DrainCloser), } - conn.opts.SetDefaults() // todo param if !exists { - id0 := rdx.IDFromSrcSeqOff(orig, 0, 0) + id0 := rdx.IDFromSrcSeqOff(opts.Orig, 0, 0) init := append(toyqueue.Records(nil), Log0...) init = append(init, toytlv.Record('Y', toytlv.Record('I', id0.ZipBytes()), toytlv.Record('R', rdx.ID0.ZipBytes()), - toytlv.Record('S', rdx.Stlv(name)), + toytlv.Record('S', rdx.Stlv(opts.Name)), )) if err = conn.Drain(init); err != nil { - return nil, exists, err + return nil, err } } vv, err := conn.VersionVector() if err != nil { - return nil, exists, err + return nil, err } conn.last = vv.GetID(conn.src) - return &conn, exists, nil + return &conn, nil } func (cho *Chotki) OpenTCP(tcp *toytlv.TCPDepot) error { diff --git a/chotki_test.go b/chotki_test.go index 61c60c5..6d83eff 100644 --- a/chotki_test.go +++ b/chotki_test.go @@ -1,14 +1,31 @@ package chotki import ( - "github.com/drpcorg/chotki/rdx" - "github.com/learn-decentralized-systems/toyqueue" - "github.com/stretchr/testify/assert" "io" "os" "testing" + + "github.com/cockroachdb/pebble" + "github.com/drpcorg/chotki/rdx" + "github.com/learn-decentralized-systems/toyqueue" + "github.com/stretchr/testify/assert" ) +func testdirs(origs ...uint64) ([]string, func()) { + dirs := make([]string, len(origs)) + + for i, orig := range origs { + dirs[i] = ReplicaDirName(orig) + os.RemoveAll(dirs[i]) + } + + return dirs, func() { + for _, dir := range dirs { + os.RemoveAll(dir) + } + } +} + func TestChotki_Debug(t *testing.T) { oid := rdx.IDFromSrcSeqOff(0x1e, 0x1ab, 0) key := OKey(oid+1, 'I') @@ -23,14 +40,18 @@ func TestChotki_Debug(t *testing.T) { } func TestChotki_Create(t *testing.T) { - dirname := ReplicaDirName(0x1a) - _ = os.RemoveAll(dirname) - a, exists, err := Open(0x1a, "test replica", dirname) + dirs, cancel := testdirs(0x1a) + defer cancel() + + a, err := Open(dirs[0], Options{ + Orig: 0x1a, + Name: "test replica", + Options: pebble.Options{ErrorIfExists: true}, + }) assert.Nil(t, err) - assert.Equal(t, exists, false) - //a.DumpAll() + assert.NotNil(t, a) + _ = a.Close() - _ = os.RemoveAll(dirname) } type KVMerger interface { @@ -38,15 +59,12 @@ type KVMerger interface { } func TestChotki_Sync(t *testing.T) { - adir, bdir := ReplicaDirName(0xa), ReplicaDirName(0xb) - _ = os.RemoveAll(adir) - _ = os.RemoveAll(bdir) + dirs, clear := testdirs(0xa, 0xb) + defer clear() - a, _, err := Open(0xa, "test replica A", adir) + a, err := Open(dirs[0], Options{Orig: 0xa, Name: "test replica A"}) assert.Nil(t, err) - //a.DumpAll() - - b, _, err := Open(0xb, "test replica B", bdir) + b, err := Open(dirs[1], Options{Orig: 0xb, Name: "test replica B"}) assert.Nil(t, err) synca := Syncer{Host: a, Mode: SyncRW, Name: "a"} @@ -64,6 +82,4 @@ func TestChotki_Sync(t *testing.T) { _ = a.Close() _ = b.Close() - _ = os.RemoveAll("choa") - _ = os.RemoveAll("chob") } diff --git a/object_example_test.go b/object_example_test.go index c2b4eb0..7ea5325 100644 --- a/object_example_test.go +++ b/object_example_test.go @@ -2,7 +2,6 @@ package chotki import ( "io" - "os" "testing" "github.com/drpcorg/chotki/rdx" @@ -11,32 +10,26 @@ import ( ) func TestORMExample(t *testing.T) { - edir, fdir := ReplicaDirName(0x1e), ReplicaDirName(0x1f) - _ = os.RemoveAll("cho1e") - _ = os.RemoveAll("cho1f") - defer func() { - _ = os.RemoveAll("cho1e") - _ = os.RemoveAll("cho1f") - }() + dirs, clear := testdirs(0x1e, 0x1f) + defer clear() - a, _, err := Open(0x1e, "test replica", edir) + a, err := Open(dirs[0], Options{Orig: 0x1e, Name: "test replica"}) assert.Nil(t, err) - var tid, oid rdx.ID - tid, err = a.NewClass(rdx.ID0, + tid, err := a.NewClass(rdx.ID0, Field{Name: "Name", RdxType: rdx.String}, Field{Name: "Score", RdxType: rdx.Integer}, ) assert.Nil(t, err) assert.Equal(t, "1e-1", tid.String()) - oid, _ = a.NewObject(tid, "\"Ivan Petrov\"", "102") + oid, _ := a.NewObject(tid, "\"Ivan Petrov\"", "102") assert.Equal(t, "1e-2", oid.String()) //a.DumpAll() err = a.Close() assert.Nil(t, err) - a, _, err = Open(0x1e, "test replica", edir) + a, err = Open(dirs[0], Options{Orig: 0x1e, Name: "test replica"}) assert.Nil(t, err) var exa Example @@ -50,7 +43,7 @@ func TestORMExample(t *testing.T) { exa.Score = 103 // todo save the object - b, _, err := Open(0x1f, "another test replica", fdir) + b, err := Open(dirs[1], Options{Orig:0x1f, Name: "another test replica"}) assert.Nil(t, err) syncera := Syncer{Host: a, Mode: SyncRW} diff --git a/objects_test.go b/objects_test.go index 734df0d..fbd4e58 100644 --- a/objects_test.go +++ b/objects_test.go @@ -1,18 +1,18 @@ package chotki import ( + "testing" + "github.com/drpcorg/chotki/rdx" "github.com/learn-decentralized-systems/toyqueue" "github.com/stretchr/testify/assert" - "os" - "testing" ) func TestTypes(t *testing.T) { - adir := ReplicaDirName(0x1a) + dirs, clear := testdirs(0x1a) + defer clear() - _ = os.RemoveAll(adir) - a, _, err := Open(0x1a, "test replica A", adir) + a, err := Open(dirs[0], Options{Orig: 0x1a, Name: "test replica A"}) assert.Nil(t, err) var tid, oid rdx.ID @@ -70,5 +70,4 @@ func TestTypes(t *testing.T) { _ = i3.Close() _ = a.Close() - _ = os.RemoveAll("cho1a") } diff --git a/repl/commands.go b/repl/commands.go index 0498bca..f7e429d 100644 --- a/repl/commands.go +++ b/repl/commands.go @@ -3,12 +3,14 @@ package main import ( "errors" "fmt" + "os" + "time" + + "github.com/cockroachdb/pebble" "github.com/drpcorg/chotki" "github.com/drpcorg/chotki/rdx" "github.com/learn-decentralized-systems/toyqueue" "github.com/learn-decentralized-systems/toytlv" - "os" - "time" ) var HelpCreate = errors.New("create zone/1 {Name:\"Name\",Description:\"long text\"}") @@ -53,15 +55,11 @@ func (repl *REPL) CommandCreate(arg *rdx.RDX) (id rdx.ID, err error) { } dirname := chotki.ReplicaDirName(src.Src()) - exists, err := chotki.Exists(dirname) - if err != nil { - return rdx.BadId, err - } - if exists { - return rdx.BadId, errors.New("Replica already exists") - } - - repl.Host, _, err = chotki.Open(src.Src(), name, dirname) + repl.Host, err = chotki.Open(dirname, chotki.Options{ + Orig: src.Src(), + Name: name, + Options: pebble.Options{ErrorIfExists: true}, + }) if err == nil { id = repl.Host.Last() } @@ -78,14 +76,11 @@ func (repl *REPL) CommandOpen(arg *rdx.RDX) (rdx.ID, error) { src0 := rdx.IDFromText(arg.Text) dirname := chotki.ReplicaDirName(src0.Src()) - exists, err := chotki.Exists(dirname) - if err != nil { - return rdx.BadId, err - } else if !exists { - return rdx.BadId, errors.New("Replica not found") - } - - repl.Host, _, err = chotki.Open(src0.Src(), "", dirname) + var err error + repl.Host, err = chotki.Open(dirname, chotki.Options{ + Orig: src0.Src(), + Options: pebble.Options{ErrorIfNotExists: true}, + }) if err != nil { return rdx.BadId, err }