Skip to content

Commit

Permalink
Cleanup dirs in tests, update Open signature
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdimidium committed Apr 17, 2024
1 parent bd5d449 commit 6043728
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 78 deletions.
48 changes: 27 additions & 21 deletions chotki.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
52 changes: 34 additions & 18 deletions chotki_test.go
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -23,30 +40,31 @@ 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 {
Merge(key, value []byte) error
}

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"}
Expand All @@ -64,6 +82,4 @@ func TestChotki_Sync(t *testing.T) {

_ = a.Close()
_ = b.Close()
_ = os.RemoveAll("choa")
_ = os.RemoveAll("chob")
}
21 changes: 7 additions & 14 deletions object_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package chotki

import (
"io"
"os"
"testing"

"github.com/drpcorg/chotki/rdx"
Expand All @@ -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
Expand All @@ -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}
Expand Down
11 changes: 5 additions & 6 deletions objects_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -70,5 +70,4 @@ func TestTypes(t *testing.T) {
_ = i3.Close()

_ = a.Close()
_ = os.RemoveAll("cho1a")
}
33 changes: 14 additions & 19 deletions repl/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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\"}")
Expand Down Expand Up @@ -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()
}
Expand All @@ -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
}
Expand Down

0 comments on commit 6043728

Please sign in to comment.