Impl new driver even if i lose arr operations
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"local/dndex/config"
|
||||
"local/dndex/storage/entity"
|
||||
"os"
|
||||
"regexp"
|
||||
|
||||
@@ -13,6 +14,10 @@ import (
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
var (
|
||||
errModifiedReserved = errors.New("cannot modify reserved field " + entity.Name)
|
||||
)
|
||||
|
||||
type BoltDB struct {
|
||||
db *bolt.DB
|
||||
}
|
||||
@@ -74,12 +79,70 @@ func (bdb *BoltDB) Find(_ context.Context, namespace string, filter interface{})
|
||||
return ch, err
|
||||
}
|
||||
|
||||
func (bdb *BoltDB) Update(context.Context, string, interface{}, interface{}) error {
|
||||
return errors.New("not impl")
|
||||
func (bdb *BoltDB) Update(ctx context.Context, namespace string, filter, operator interface{}) error {
|
||||
b, err := bson.Marshal(filter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m := bson.M{}
|
||||
if err := bson.Unmarshal(b, &m); err != nil {
|
||||
return err
|
||||
}
|
||||
return bdb.db.Update(func(tx *bolt.Tx) error {
|
||||
bucket := tx.Bucket([]byte(namespace))
|
||||
if bucket == nil {
|
||||
return nil
|
||||
}
|
||||
cursor := bucket.Cursor()
|
||||
for k, v := cursor.First(); k != nil && v != nil; k, v = cursor.Next() {
|
||||
n := bson.M{}
|
||||
if err := bson.Unmarshal(v, &n); err != nil {
|
||||
return err
|
||||
}
|
||||
if matches(n, m) {
|
||||
n, err := apply(n, operator)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
v, err := bson.Marshal(n)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := bucket.Put(k, v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (bdb *BoltDB) Insert(context.Context, string, interface{}) error {
|
||||
return errors.New("not impl")
|
||||
func (bdb *BoltDB) Insert(ctx context.Context, namespace string, doc interface{}) error {
|
||||
b, err := bson.Marshal(doc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m := bson.M{}
|
||||
if err := bson.Unmarshal(b, &m); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, ok := m[entity.Name]; !ok {
|
||||
return errors.New("primary key required to insert: did not find " + entity.Name)
|
||||
} else if _, ok := m[entity.Name].(string); !ok {
|
||||
return errors.New("primary key must be a string")
|
||||
}
|
||||
return bdb.db.Update(func(tx *bolt.Tx) error {
|
||||
bucket, err := tx.CreateBucketIfNotExists([]byte(namespace))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
k := []byte(m[entity.Name].(string))
|
||||
v := bucket.Get(k)
|
||||
if len(v) > 0 {
|
||||
return errors.New("cannot insert: collision on primary key")
|
||||
}
|
||||
return bucket.Put(k, b)
|
||||
})
|
||||
}
|
||||
|
||||
func (bdb *BoltDB) Delete(_ context.Context, namespace string, filter interface{}) error {
|
||||
@@ -170,3 +233,52 @@ func matches(doc, filter bson.M) bool {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func apply(doc bson.M, operator interface{}) (bson.M, error) {
|
||||
b, err := bson.Marshal(operator)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
op := bson.M{}
|
||||
if err := bson.Unmarshal(b, &op); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, ok := op[entity.Name]; ok {
|
||||
return nil, errModifiedReserved
|
||||
}
|
||||
for k, v := range op {
|
||||
operateBM, ok := v.(bson.M)
|
||||
if !ok {
|
||||
operateM, mok := v.(map[string]interface{})
|
||||
ok = mok
|
||||
if ok {
|
||||
operateBM = bson.M(operateM)
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid apply operand: %s:%T", k, v)
|
||||
}
|
||||
var op func(bson.M, bson.M) (bson.M, error)
|
||||
switch k {
|
||||
case "$set":
|
||||
op = applySet
|
||||
default:
|
||||
return nil, errors.New("cannot apply operation " + k)
|
||||
}
|
||||
doc, err = op(doc, operateBM)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func applySet(doc, operator bson.M) (bson.M, error) {
|
||||
for k, v := range operator {
|
||||
if k == entity.Name {
|
||||
return nil, errModifiedReserved
|
||||
}
|
||||
doc[k] = v
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user