Skip to content

Commit

Permalink
refactor: remove hash
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Nov 24, 2023
1 parent 99ef30e commit 015e421
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 150 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/acobaugh/osrelease v0.1.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/golang/snappy v0.0.4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ github.com/dop251/goja v0.0.0-20231027120936-b396bb4c349d h1:wi6jN5LVt/ljaBG4ue7
github.com/dop251/goja v0.0.0-20231027120936-b396bb4c349d/go.mod h1:QMWlm50DNe14hD7t24KEqZuUdC9sOTy8W6XbCU1mlw4=
github.com/dop251/goja_nodejs v0.0.0-20210225215109-d91c329300e7/go.mod h1:hn7BA7c8pLvoGndExHudxTDKZ84Pyvv+90pbBjbTz0Y=
github.com/dop251/goja_nodejs v0.0.0-20211022123610-8dd9abb0616d/go.mod h1:DngW8aVqWbuLRMHItjPUyqdj+HWPvnQe8V8y1nDpIbM=
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down
7 changes: 0 additions & 7 deletions internal/util/hash.go

This file was deleted.

63 changes: 26 additions & 37 deletions pkg/database/memdb/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"sort"
"sync"

"github.com/emirpasic/gods/maps"
"github.com/emirpasic/gods/maps/treemap"
"github.com/emirpasic/gods/utils"
"github.com/pkg/errors"
"github.com/siyul-park/uniflow/internal/pool"
"github.com/siyul-park/uniflow/internal/util"
"github.com/siyul-park/uniflow/pkg/database"
"github.com/siyul-park/uniflow/pkg/primitive"
Expand Down Expand Up @@ -43,7 +44,7 @@ var (
func NewCollection(name string) *Collection {
return &Collection{
name: name,
data: pool.GetMap(),
data: treemap.NewWith(comparator),
indexView: NewIndexView(),
dataLock: sync.RWMutex{},
streamLock: sync.RWMutex{},
Expand Down Expand Up @@ -302,12 +303,12 @@ func (coll *Collection) FindMany(ctx context.Context, filter *database.Filter, o
}

func (coll *Collection) Drop(ctx context.Context) error {
data, err := func() (*sync.Map, error) {
data, err := func() (maps.Map, error) {
coll.dataLock.Lock()
defer coll.dataLock.Unlock()

data := coll.data
coll.data = pool.GetMap()
coll.data = treemap.NewWith(comparator)

if err := coll.indexView.deleteAll(ctx); err != nil {
return nil, err
Expand All @@ -319,7 +320,7 @@ func (coll *Collection) Drop(ctx context.Context) error {
return err
}

data.Range(func(_, val any) bool {
for _, val := range data.Values() {
doc := val.(*primitive.Map)
if id, ok := doc.Get(keyID); ok {
coll.emit(fullEvent{
Expand All @@ -330,8 +331,7 @@ func (coll *Collection) Drop(ctx context.Context) error {
Document: doc,
})
}
return true
})
}

coll.streamLock.Lock()
defer coll.streamLock.Unlock()
Expand Down Expand Up @@ -362,9 +362,7 @@ func (coll *Collection) insertMany(ctx context.Context, docs []*primitive.Map) (
for i, doc := range docs {
if id, ok := doc.Get(keyID); !ok {
return nil, errors.Wrap(errors.WithStack(ErrPKNotFound), database.ErrCodeWrite)
} else if hash, err := util.Hash(id); err != nil {
return nil, errors.Wrap(err, database.ErrCodeWrite)
} else if _, ok := coll.data.Load(hash); ok {
} else if _, ok := coll.data.Get(id); ok {
return nil, errors.Wrap(errors.WithStack(ErrPKDuplicated), database.ErrCodeWrite)
} else {
ids[i] = id
Expand All @@ -375,11 +373,7 @@ func (coll *Collection) insertMany(ctx context.Context, docs []*primitive.Map) (
return nil, errors.Wrap(err, database.ErrCodeWrite)
}
for i, doc := range docs {
if hash, err := util.Hash(ids[i].Interface()); err != nil {
return nil, errors.Wrap(err, database.ErrCodeWrite)
} else {
coll.data.Store(hash, doc)
}
coll.data.Put(ids[i], doc)
}

return ids, nil
Expand Down Expand Up @@ -423,40 +417,39 @@ func (coll *Collection) findMany(ctx context.Context, filter *database.Filter, o
scanSize = -1
}

scan := map[uint64]*primitive.Map{}
scan := treemap.NewWith(utils.Comparator(func(a, b any) int {
return primitive.Compare(a.(primitive.Object), b.(primitive.Object))
}))
if examples, ok := FilterToExample(filter); ok {
if ids, err := coll.indexView.findMany(ctx, examples); err == nil {
for _, id := range ids {
if scanSize == len(scan) {
if scanSize == scan.Size() {
break
} else if hash, err := util.Hash(id.Interface()); err != nil {
return nil, errors.Wrap(err, database.ErrCodeWrite)
} else if doc, ok := coll.data.Load(hash); ok && match(doc.(*primitive.Map)) {
scan[hash] = doc.(*primitive.Map)
} else if doc, ok := coll.data.Get(id); ok && match(doc.(*primitive.Map)) {
scan.Put(id, doc)
}
}
}
}
if scanSize != len(scan) {
coll.data.Range(func(key, value any) bool {
if scanSize == len(scan) {
return false
if scanSize != scan.Size() {
for _, key := range coll.data.Keys() {
value, _ := coll.data.Get(key)
if scanSize == scan.Size() {
continue
}

if match(value.(*primitive.Map)) {
scan[key.(uint64)] = value.(*primitive.Map)
scan.Put(key, value)
}
return true
})
}
}

if skip >= len(scan) {
if skip >= scan.Size() {
return nil, nil
}

var docs []*primitive.Map
for _, doc := range scan {
docs = append(docs, doc)
for _, doc := range scan.Values() {
docs = append(docs, doc.(*primitive.Map))
}

if len(sorts) > 0 {
Expand Down Expand Up @@ -508,11 +501,7 @@ func (coll *Collection) deleteMany(ctx context.Context, docs []*primitive.Map) (
}

for _, id := range ids {
if hash, err := util.Hash(id.Interface()); err != nil {
return nil, errors.Wrap(err, database.ErrCodeWrite)
} else {
coll.data.Delete(hash)
}
coll.data.Remove(id)
}

return deletes, nil
Expand Down
12 changes: 12 additions & 0 deletions pkg/database/memdb/compare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package memdb

import (
"github.com/emirpasic/gods/utils"
"github.com/siyul-park/uniflow/pkg/primitive"
)

var (
comparator = utils.Comparator(func(a, b any) int {
return primitive.Compare(a.(primitive.Object), b.(primitive.Object))
})
)
Loading

0 comments on commit 015e421

Please sign in to comment.