Skip to content

Commit

Permalink
refactor: primitive.Object -> Value
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Nov 29, 2023
1 parent 7452dd4 commit 841e869
Show file tree
Hide file tree
Showing 31 changed files with 241 additions and 237 deletions.
6 changes: 3 additions & 3 deletions pkg/database/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ type (

Watch(ctx context.Context, filter *Filter) (Stream, error)

InsertOne(ctx context.Context, doc *primitive.Map) (primitive.Object, error)
InsertMany(ctx context.Context, docs []*primitive.Map) ([]primitive.Object, error)
InsertOne(ctx context.Context, doc *primitive.Map) (primitive.Value, error)
InsertMany(ctx context.Context, docs []*primitive.Map) ([]primitive.Value, error)

UpdateOne(ctx context.Context, filter *Filter, patch *primitive.Map, options ...*UpdateOptions) (bool, error)
UpdateMany(ctx context.Context, filter *Filter, patch *primitive.Map, options ...*UpdateOptions) (int, error)
Expand Down Expand Up @@ -48,7 +48,7 @@ type (

Event struct {
OP eventOP
DocumentID primitive.Object
DocumentID primitive.Value
}

eventOP int
Expand Down
18 changes: 9 additions & 9 deletions pkg/database/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type (
Filter struct {
OP OP
Key string
Value primitive.Object
Value primitive.Value
Children []*Filter
}

Expand Down Expand Up @@ -45,63 +45,63 @@ func Where(key string) *filterHelper {
}
}

func (fh *filterHelper) EQ(value primitive.Object) *Filter {
func (fh *filterHelper) EQ(value primitive.Value) *Filter {
return &Filter{
OP: EQ,
Key: fh.key,
Value: value,
}
}

func (fh *filterHelper) NE(value primitive.Object) *Filter {
func (fh *filterHelper) NE(value primitive.Value) *Filter {
return &Filter{
OP: NE,
Key: fh.key,
Value: value,
}
}

func (fh *filterHelper) LT(value primitive.Object) *Filter {
func (fh *filterHelper) LT(value primitive.Value) *Filter {
return &Filter{
Key: fh.key,
OP: LT,
Value: value,
}
}

func (fh *filterHelper) LTE(value primitive.Object) *Filter {
func (fh *filterHelper) LTE(value primitive.Value) *Filter {
return &Filter{
OP: LTE,
Key: fh.key,
Value: value,
}
}

func (fh *filterHelper) GT(value primitive.Object) *Filter {
func (fh *filterHelper) GT(value primitive.Value) *Filter {
return &Filter{
OP: GT,
Key: fh.key,
Value: value,
}
}

func (fh *filterHelper) GTE(value primitive.Object) *Filter {
func (fh *filterHelper) GTE(value primitive.Value) *Filter {
return &Filter{
OP: GTE,
Key: fh.key,
Value: value,
}
}

func (fh *filterHelper) IN(slice ...primitive.Object) *Filter {
func (fh *filterHelper) IN(slice ...primitive.Value) *Filter {
return &Filter{
OP: IN,
Key: fh.key,
Value: primitive.NewSlice(slice...),
}
}

func (fh *filterHelper) NotIN(slice ...primitive.Object) *Filter {
func (fh *filterHelper) NotIN(slice ...primitive.Value) *Filter {
return &Filter{
OP: NIN,
Key: fh.key,
Expand Down
14 changes: 7 additions & 7 deletions pkg/database/memdb/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (coll *Collection) Watch(ctx context.Context, filter *database.Filter) (dat
return stream, nil
}

func (coll *Collection) InsertOne(ctx context.Context, doc *primitive.Map) (primitive.Object, error) {
func (coll *Collection) InsertOne(ctx context.Context, doc *primitive.Map) (primitive.Value, error) {
if id, err := coll.insertOne(ctx, doc); err != nil {
return nil, err
} else {
Expand All @@ -100,7 +100,7 @@ func (coll *Collection) InsertOne(ctx context.Context, doc *primitive.Map) (prim
}
}

func (coll *Collection) InsertMany(ctx context.Context, docs []*primitive.Map) ([]primitive.Object, error) {
func (coll *Collection) InsertMany(ctx context.Context, docs []*primitive.Map) ([]primitive.Value, error) {
if ids, err := coll.insertMany(ctx, docs); err != nil {
return nil, err
} else {
Expand Down Expand Up @@ -132,7 +132,7 @@ func (coll *Collection) UpdateOne(ctx context.Context, filter *database.Filter,
return false, nil
}

var id primitive.Object
var id primitive.Value
if old != nil {
id = old.GetOr(keyID, nil)
}
Expand Down Expand Up @@ -345,19 +345,19 @@ func (coll *Collection) Drop(ctx context.Context) error {
return nil
}

func (coll *Collection) insertOne(ctx context.Context, doc *primitive.Map) (primitive.Object, error) {
func (coll *Collection) insertOne(ctx context.Context, doc *primitive.Map) (primitive.Value, error) {
if ids, err := coll.insertMany(ctx, []*primitive.Map{doc}); err != nil {
return nil, err
} else {
return ids[0], nil
}
}

func (coll *Collection) insertMany(ctx context.Context, docs []*primitive.Map) ([]primitive.Object, error) {
func (coll *Collection) insertMany(ctx context.Context, docs []*primitive.Map) ([]primitive.Value, error) {
coll.dataLock.Lock()
defer coll.dataLock.Unlock()

ids := make([]primitive.Object, len(docs))
ids := make([]primitive.Value, len(docs))
for i, doc := range docs {
if id, ok := doc.Get(keyID); !ok {
return nil, errors.Wrap(errors.WithStack(ErrPKNotFound), database.ErrCodeWrite)
Expand Down Expand Up @@ -481,7 +481,7 @@ func (coll *Collection) deleteMany(ctx context.Context, docs []*primitive.Map) (
coll.dataLock.Lock()
defer coll.dataLock.Unlock()

ids := make([]primitive.Object, 0, len(docs))
ids := make([]primitive.Value, 0, len(docs))
deletes := make([]*primitive.Map, 0, len(docs))
for _, doc := range docs {
if doc == nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/database/memdb/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import (

var (
comparator = utils.Comparator(func(a, b any) int {
return primitive.Compare(a.(primitive.Object), b.(primitive.Object))
return primitive.Compare(a.(primitive.Value), b.(primitive.Value))
})
)
20 changes: 10 additions & 10 deletions pkg/database/memdb/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,55 +22,55 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool {
switch filter.OP {
case database.EQ:
return func(m *primitive.Map) bool {
if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok {
if o, ok := primitive.Pick[primitive.Value](m, parsePath(filter.Key)...); !ok {
return false
} else {
return primitive.Compare(o, filter.Value) == 0
}
}
case database.NE:
return func(m *primitive.Map) bool {
if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok {
if o, ok := primitive.Pick[primitive.Value](m, parsePath(filter.Key)...); !ok {
return false
} else {
return primitive.Compare(o, filter.Value) != 0
}
}
case database.LT:
return func(m *primitive.Map) bool {
if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok {
if o, ok := primitive.Pick[primitive.Value](m, parsePath(filter.Key)...); !ok {
return false
} else {
return primitive.Compare(o, filter.Value) < 0
}
}
case database.LTE:
return func(m *primitive.Map) bool {
if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok {
if o, ok := primitive.Pick[primitive.Value](m, parsePath(filter.Key)...); !ok {
return false
} else {
return primitive.Compare(o, filter.Value) <= 0
}
}
case database.GT:
return func(m *primitive.Map) bool {
if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok {
if o, ok := primitive.Pick[primitive.Value](m, parsePath(filter.Key)...); !ok {
return false
} else {
return primitive.Compare(o, filter.Value) > 0
}
}
case database.GTE:
return func(m *primitive.Map) bool {
if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok {
if o, ok := primitive.Pick[primitive.Value](m, parsePath(filter.Key)...); !ok {
return false
} else {
return primitive.Compare(o, filter.Value) >= 0
}
}
case database.IN:
return func(m *primitive.Map) bool {
if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok {
if o, ok := primitive.Pick[primitive.Value](m, parsePath(filter.Key)...); !ok {
return false
} else if o == nil {
return false
Expand All @@ -87,7 +87,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool {
}
case database.NIN:
return func(m *primitive.Map) bool {
if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok {
if o, ok := primitive.Pick[primitive.Value](m, parsePath(filter.Key)...); !ok {
return true
} else if o == nil {
return true
Expand All @@ -104,15 +104,15 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool {
}
case database.NULL:
return func(m *primitive.Map) bool {
if v, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok {
if v, ok := primitive.Pick[primitive.Value](m, parsePath(filter.Key)...); !ok {
return false
} else {
return v == nil
}
}
case database.NNULL:
return func(m *primitive.Map) bool {
if v, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok {
if v, ok := primitive.Pick[primitive.Value](m, parsePath(filter.Key)...); !ok {
return false
} else {
return v != nil
Expand Down
16 changes: 8 additions & 8 deletions pkg/database/memdb/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (iv *IndexView) deleteAll(_ context.Context) error {
return nil
}

func (iv *IndexView) findMany(_ context.Context, examples []*primitive.Map) ([]primitive.Object, error) {
func (iv *IndexView) findMany(_ context.Context, examples []*primitive.Map) ([]primitive.Value, error) {
iv.lock.RLock()
defer iv.lock.RUnlock()

Expand All @@ -158,7 +158,7 @@ func (iv *IndexView) findMany(_ context.Context, examples []*primitive.Map) ([]p
var i int
var k string
for i, k = range model.Keys {
if obj, ok := primitive.Pick[primitive.Object](example, k); ok {
if obj, ok := primitive.Pick[primitive.Value](example, k); ok {
visits[k] = true
if sub, ok := curr.Get(obj); ok {
if i < len(model.Keys)-1 {
Expand Down Expand Up @@ -220,9 +220,9 @@ func (iv *IndexView) findMany(_ context.Context, examples []*primitive.Map) ([]p
}
}

var uniqueIds []primitive.Object
var uniqueIds []primitive.Value
for _, v := range ids.Values() {
uniqueIds = append(uniqueIds, v.(primitive.Object))
uniqueIds = append(uniqueIds, v.(primitive.Value))
}
return uniqueIds, nil
}
Expand All @@ -242,7 +242,7 @@ func (iv *IndexView) insertOne(ctx context.Context, doc *primitive.Map) error {
}

for i, k := range model.Keys {
obj, _ := primitive.Pick[primitive.Object](doc, k)
obj, _ := primitive.Pick[primitive.Value](doc, k)

if i < len(model.Keys)-1 {
sub, ok := curr.Get(obj)
Expand Down Expand Up @@ -293,11 +293,11 @@ func (iv *IndexView) deleteOne(_ context.Context, doc *primitive.Map) error {

var nodes []containers.Container
nodes = append(nodes, curr)
var keys []primitive.Object
var keys []primitive.Value
keys = append(keys, nil)

for i, k := range model.Keys {
obj, _ := primitive.Pick[primitive.Object](doc, k)
obj, _ := primitive.Pick[primitive.Value](doc, k)

if i < len(model.Keys)-1 {
if sub, ok := curr.Get(obj); ok {
Expand All @@ -309,7 +309,7 @@ func (iv *IndexView) deleteOne(_ context.Context, doc *primitive.Map) error {
return nil
}
} else if model.Unique {
if r, ok := curr.Get(obj); ok && primitive.Compare(id, r.(primitive.Object)) == 0 {
if r, ok := curr.Get(obj); ok && primitive.Compare(id, r.(primitive.Value)) == 0 {
curr.Remove(obj)
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions pkg/database/memdb/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
func ParseSorts(sorts []database.Sort) func(i, j *primitive.Map) bool {
return func(i, j *primitive.Map) bool {
for _, s := range sorts {
x, _ := primitive.Pick[primitive.Object](i, parsePath(s.Key)...)
y, _ := primitive.Pick[primitive.Object](j, parsePath(s.Key)...)
x, _ := primitive.Pick[primitive.Value](i, parsePath(s.Key)...)
y, _ := primitive.Pick[primitive.Value](j, parsePath(s.Key)...)

if x == y {
continue
Expand Down
14 changes: 7 additions & 7 deletions pkg/database/mongodb/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (coll *Collection) Watch(ctx context.Context, filter *database.Filter) (dat
return UpgradeStream(stream), nil
}

func (coll *Collection) InsertOne(ctx context.Context, doc *primitive.Map) (primitive.Object, error) {
func (coll *Collection) InsertOne(ctx context.Context, doc *primitive.Map) (primitive.Value, error) {
raw, err := MarshalDocument(doc)
if err != nil {
return nil, err
Expand All @@ -70,14 +70,14 @@ func (coll *Collection) InsertOne(ctx context.Context, doc *primitive.Map) (prim
return nil, errors.Wrap(database.ErrWrite, err.Error())
}

var id primitive.Object
var id primitive.Value
if err := UnmarshalDocument(res.InsertedID, &id); err != nil {
return nil, err
}
return id, nil
}

func (coll *Collection) InsertMany(ctx context.Context, docs []*primitive.Map) ([]primitive.Object, error) {
func (coll *Collection) InsertMany(ctx context.Context, docs []*primitive.Map) ([]primitive.Value, error) {
var raws bson.A
for _, doc := range docs {
if raw, err := MarshalDocument(doc); err == nil {
Expand All @@ -92,9 +92,9 @@ func (coll *Collection) InsertMany(ctx context.Context, docs []*primitive.Map) (
return nil, errors.Wrap(database.ErrWrite, err.Error())
}

var ids []primitive.Object
var ids []primitive.Value
for _, insertedID := range res.InsertedIDs {
var id primitive.Object
var id primitive.Value
if err := UnmarshalDocument(insertedID, &id); err != nil {
return nil, err
}
Expand Down Expand Up @@ -182,7 +182,7 @@ func (coll *Collection) FindOne(ctx context.Context, filter *database.Filter, op
return nil, errors.Wrap(database.ErrRead, res.Err().Error())
}

var doc primitive.Object
var doc primitive.Value
var r any
if err := res.Decode(&r); err != nil {
return nil, err
Expand All @@ -206,7 +206,7 @@ func (coll *Collection) FindMany(ctx context.Context, filter *database.Filter, o

var docs []*primitive.Map
for cursor.Next(ctx) {
var doc primitive.Object
var doc primitive.Value
var r any
if err := cursor.Decode(&r); err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 841e869

Please sign in to comment.