Skip to content

Commit

Permalink
go 1.23 and replace orm map with native one for memmory efficienct
Browse files Browse the repository at this point in the history
  • Loading branch information
Termina1 committed Dec 5, 2024
1 parent 72efd39 commit 32cbb5b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/drpcorg/chotki

go 1.21.4
go 1.23.3

require (
github.com/cockroachdb/pebble v1.1.0
Expand Down
23 changes: 11 additions & 12 deletions orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/cockroachdb/pebble"
"github.com/drpcorg/chotki/protocol"
"github.com/drpcorg/chotki/rdx"
"github.com/puzpuzpuz/xsync/v3"
)

type NativeObject interface {
Expand All @@ -22,7 +21,7 @@ type NativeObject interface {
type ORM struct {
Host *Chotki
Snap *pebble.Snapshot
objects *xsync.MapOf[rdx.ID, NativeObject]
objects *sync.Map
ids sync.Map // xsync sometimes does not correctly work with pointers
lock sync.Mutex
}
Expand All @@ -32,7 +31,7 @@ func NewORM(host *Chotki, snap *pebble.Snapshot) *ORM {
Host: host,
Snap: snap,

objects: xsync.NewMapOf[rdx.ID, NativeObject](),
objects: &sync.Map{},
}
}

Expand Down Expand Up @@ -114,8 +113,8 @@ func (orm *ORM) Save(ctx context.Context, objs ...NativeObject) (err error) {
// SaveAll the changed fields; this will scan the objects and
// their database records.
func (orm *ORM) SaveAll(ctx context.Context) (err error) {
orm.objects.Range(func(_ rdx.ID, obj NativeObject) bool {
err = orm.Save(ctx, obj)
orm.objects.Range(func(_ any, obj any) bool {
err = orm.Save(ctx, obj.(NativeObject))
return err == nil
})

Expand Down Expand Up @@ -178,8 +177,8 @@ func (orm *ORM) UpdateObject(obj NativeObject, snap *pebble.Snapshot) error {
// UpdateAll the registered objects to the new db state
func (orm *ORM) UpdateAll() (err error) {
snap := orm.Host.Database().NewSnapshot()
orm.objects.Range(func(_ rdx.ID, obj NativeObject) bool {
err = orm.UpdateObject(obj, snap)
orm.objects.Range(func(_ any, obj any) bool {
err = orm.UpdateObject(obj.(NativeObject), snap)
return err == nil
})
orm.lock.Lock()
Expand All @@ -206,7 +205,7 @@ func (orm *ORM) SyncAll(ctx context.Context) (err error) {
func (orm *ORM) Load(id rdx.ID, blanc NativeObject) (obj NativeObject, err error) {
pre, ok := orm.objects.Load(id)
if ok {
return pre, nil
return pre.(NativeObject), nil
}
fro, til := ObjectKeyRange(id)
io := pebble.IterOptions{
Expand All @@ -225,17 +224,17 @@ func (orm *ORM) Load(id rdx.ID, blanc NativeObject) (obj NativeObject, err error
_ = it.Close()
pre, ok = orm.objects.Load(id)
if ok {
return pre, nil
return pre.(NativeObject), nil
}
orm.objects.Store(id, blanc)
orm.ids.Store(blanc, id)
return blanc, nil
}

// Find a registered object given its id. nil if none.
func (orm *ORM) Object(id rdx.ID) (obj NativeObject) {
obj, _ = orm.objects.Load(id)
return // todo
func (orm *ORM) Object(id rdx.ID) NativeObject {
objV, _ := orm.objects.Load(id)
return objV.(NativeObject)
}

// Find the ID of the registered object's.
Expand Down
15 changes: 8 additions & 7 deletions repl/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,12 @@ func (repl *REPL) CommandDump(arg *rdx.RDX) (id rdx.ID, err error) {
case "vv":
repl.Host.DumpVV(os.Stderr)
case "all":
repl.Host.DumpAll(os.Stderr)
file, err := os.OpenFile("test.log", os.O_RDWR|os.O_CREATE, 0660)
if err != nil {
panic(err)
}
repl.Host.DumpAll(file)
file.Close()
default:
return rdx.BadId, HelpDump
}
Expand Down Expand Up @@ -420,9 +425,7 @@ func (repl *REPL) CommandListen(arg *rdx.RDX) (id rdx.ID, err error) {
return rdx.BadId, HelpListen
}
addr := rdx.Snative(rdx.Sparse(string(arg.Text)))
if err == nil {
err = repl.Host.Listen(addr)
}
err = repl.Host.Listen(addr)
return
}

Expand All @@ -433,9 +436,7 @@ func (repl *REPL) CommandConnect(arg *rdx.RDX) (id rdx.ID, err error) {
return rdx.BadId, HelpConnect
}
addr := rdx.Snative(rdx.Sparse(string(arg.Text)))
if err == nil {
err = repl.Host.Connect(addr)
}
err = repl.Host.Connect(addr)
return
}

Expand Down

0 comments on commit 32cbb5b

Please sign in to comment.