Skip to content

Fix orm #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions chotki.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ type Chotki struct {

lock sync.Mutex
db *pebble.DB
orm *ORM
net *protocol.Net
dir string
opts Options
Expand Down Expand Up @@ -202,8 +201,6 @@ func Open(dirname string, opts Options) (*Chotki, error) {
}
})

cho.orm = NewORM(&cho, db.NewSnapshot())

if !exists {
id0 := rdx.IDFromSrcSeqOff(opts.Src, 0, 0)

Expand Down Expand Up @@ -243,15 +240,6 @@ func (cho *Chotki) Close() error {

cho.net = nil
}

if cho.orm != nil {
if err := cho.orm.Close(); err != nil {
cho.log.Error("couldn't close ORM", "err", err)
}

cho.orm = nil
}

if cho.db != nil {
if err := cho.db.Close(); err != nil {
cho.log.Error("couldn't close Pebble", "err", err)
Expand Down Expand Up @@ -336,7 +324,7 @@ func (cho *Chotki) Directory() string {
}

func (cho *Chotki) ObjectMapper() *ORM {
return cho.orm
return NewORM(cho, cho.db.NewSnapshot())
}

func (cho *Chotki) RestoreNet(ctx context.Context) error {
Expand Down
35 changes: 0 additions & 35 deletions chotki_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,38 +129,3 @@ func (k *Test) Store(off uint64, rdt byte, old []byte, clock rdx.Clock) (bare []
}
return
}

func TestOrmCreateThanEdit(t *testing.T) {
dirs, clear := testdirs(0xa)
defer clear()

a, err := Open(dirs[0], Options{Src: 0xa, Name: "test replica A"})
assert.NoError(t, err)
id, err := a.NewClass(rdx.ID0, Schema...)
assert.NoError(t, err)
obj := &Test{
Test: 10,
}
err = a.orm.New(id, obj)
assert.NoError(t, err)

objId := a.orm.FindID(obj)
assert.NotEqual(t, rdx.BadId, objId)

loadedObj, err := a.orm.Load(objId, &Test{})
assert.NoError(t, err)

result := loadedObj.(*Test)
assert.Equal(t, &Test{Test: 10}, result)

result.Test = 100
err = a.orm.Save(result)
assert.NoError(t, err)

loadedObj, err = a.orm.Load(objId, &Test{})
assert.NoError(t, err)
result = loadedObj.(*Test)

assert.Equal(t, &Test{Test: 100}, result)

}
2 changes: 0 additions & 2 deletions orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ func (orm *ORM) New(cid rdx.ID, objs ...NativeObject) (err error) {
if err == nil {
orm.ids.Store(obj, id)
orm.objects.Store(id, obj)
orm.Snap.Close()
orm.Snap = orm.Host.Database().NewSnapshot()
}
}
return nil
Expand Down
15 changes: 15 additions & 0 deletions repl/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ func (repl *REPL) CommandOpen(arg *rdx.RDX) (rdx.ID, error) {
return repl.Host.Last(), nil
}

func (repl *REPL) CommandOpenDir(dirname string) (rdx.ID, error) {

var err error
repl.Host, err = chotki.Open(dirname, chotki.Options{
Src: 0xa,
Options: pebble.Options{ErrorIfNotExists: true},
})
if err != nil {
return rdx.BadId, err
}
go repl.Host.KeepAliveLoop()

return repl.Host.Last(), nil
}

var HelpCheckpoint = errors.New("cp \"monday\"")

func (repl *REPL) CommandCheckpoint(arg *rdx.RDX) (rdx.ID, error) {
Expand Down
4 changes: 3 additions & 1 deletion repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (repl *REPL) REPL(line string) (id rdx.ID, err error) {
}
var arg *rdx.RDX
arg, err = rdx.ParseRDX([]byte(line))
if err != nil {
if err != nil && cmd != "opendir" {
return rdx.ID0, err
}
if repl.snap != nil {
Expand All @@ -128,6 +128,8 @@ func (repl *REPL) REPL(line string) (id rdx.ID, err error) {
id, err = repl.CommandCreate(arg)
case "open":
id, err = repl.CommandOpen(arg)
case "opendir":
id, err = repl.CommandOpenDir(line)
case "checkpoint", "cp":
id, err = repl.CommandCheckpoint(arg)
case "close":
Expand Down