Skip to content

Commit

Permalink
update to engine & namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
jairad26 committed Jan 14, 2025
1 parent 95d75a9 commit 4e5214b
Show file tree
Hide file tree
Showing 19 changed files with 465 additions and 464 deletions.
6 changes: 3 additions & 3 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ runtimes:
lint:
enabled:
- [email protected]
- [email protected].350
- [email protected].352
- git-diff-check
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- renovate@39.92.0
- [email protected].1
- renovate@39.106.0
- [email protected].2
- [email protected]

actions:
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
- fix: alter schema on reverse edge after querying schema
[#55](https://github.com/hypermodeinc/modusDB/pull/55)

- feat: update interface to driver and db [#56](https://github.com/hypermodeinc/modusDB/pull/56)
- feat: update interface to engine and namespace
[#56](https://github.com/hypermodeinc/modusDB/pull/56)

## 2025-01-02 - Version 0.1.0

Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ import (

type User struct {
Gid uint64 `json:"gid,omitempty"`
Id string `json:"id,omitempty" db:"constraint=unique"`
Id string `json:"id,omitempty" ns:"constraint=unique"`
Name string `json:"name,omitempty"`
Age int `json:"age,omitempty"`
}

func main() {
db, err := New(NewDefaultConfig("/tmp/modusdb"))
ns, err := New(NewDefaultConfig("/tmp/modusdb"))
if err != nil {
panic(err)
}
defer db.Close()
defer ns.Close()

gid, user, err := modusdb.Upsert(db, User{
gid, user, err := modusdb.Upsert(ns, User{
Id: "123",
Name: "A",
Age: 10,
Expand All @@ -62,13 +62,13 @@ func main() {
}
fmt.Println(user)

_, queriedUser, err := modusdb.Get[User](db, gid)
_, queriedUser, err := modusdb.Get[User](ns, gid)
if err != nil {
panic(err)
}
fmt.Println(queriedUser)

_, _, err = modusdb.Delete[User](db, gid)
_, _, err = modusdb.Delete[User](ns, gid)
if err != nil {
panic(err)
}
Expand Down
92 changes: 46 additions & 46 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,52 @@ import (
"github.com/hypermodeinc/modusdb/api/structreflect"
)

func Create[T any](driver *Driver, object T, dbId ...uint64) (uint64, T, error) {
driver.mutex.Lock()
defer driver.mutex.Unlock()
if len(dbId) > 1 {
func Create[T any](engine *Engine, object T, nsId ...uint64) (uint64, T, error) {
engine.mutex.Lock()
defer engine.mutex.Unlock()
if len(nsId) > 1 {
return 0, object, fmt.Errorf("only one namespace is allowed")
}
ctx, db, err := getDefaultDB(driver, dbId...)
ctx, ns, err := getDefaultNamespace(engine, nsId...)
if err != nil {
return 0, object, err
}

gid, err := driver.z.nextUID()
gid, err := engine.z.nextUID()
if err != nil {
return 0, object, err
}

dms := make([]*dql.Mutation, 0)
sch := &schema.ParsedSchema{}
err = generateSetDqlMutationsAndSchema[T](ctx, db, object, gid, &dms, sch)
err = generateSetDqlMutationsAndSchema[T](ctx, ns, object, gid, &dms, sch)
if err != nil {
return 0, object, err
}

err = driver.alterSchemaWithParsed(ctx, sch)
err = engine.alterSchemaWithParsed(ctx, sch)
if err != nil {
return 0, object, err
}

err = applyDqlMutations(ctx, driver, dms)
err = applyDqlMutations(ctx, engine, dms)
if err != nil {
return 0, object, err
}

return getByGid[T](ctx, db, gid)
return getByGid[T](ctx, ns, gid)
}

func Upsert[T any](driver *Driver, object T, dbId ...uint64) (uint64, T, bool, error) {
func Upsert[T any](engine *Engine, object T, nsId ...uint64) (uint64, T, bool, error) {

var wasFound bool
driver.mutex.Lock()
defer driver.mutex.Unlock()
if len(dbId) > 1 {
engine.mutex.Lock()
defer engine.mutex.Unlock()
if len(nsId) > 1 {
return 0, object, false, fmt.Errorf("only one namespace is allowed")
}

ctx, db, err := getDefaultDB(driver, dbId...)
ctx, ns, err := getDefaultNamespace(engine, nsId...)
if err != nil {
return 0, object, false, err
}
Expand All @@ -82,106 +82,106 @@ func Upsert[T any](driver *Driver, object T, dbId ...uint64) (uint64, T, bool, e

dms := make([]*dql.Mutation, 0)
sch := &schema.ParsedSchema{}
err = generateSetDqlMutationsAndSchema[T](ctx, db, object, gid, &dms, sch)
err = generateSetDqlMutationsAndSchema[T](ctx, ns, object, gid, &dms, sch)
if err != nil {
return 0, object, false, err
}

err = db.driver.alterSchemaWithParsed(ctx, sch)
err = ns.engine.alterSchemaWithParsed(ctx, sch)
if err != nil {
return 0, object, false, err
}

if gid != 0 || cf != nil {
gid, err = getExistingObject[T](ctx, db, gid, cf, object)
gid, err = getExistingObject[T](ctx, ns, gid, cf, object)
if err != nil && err != apiutils.ErrNoObjFound {
return 0, object, false, err
}
wasFound = err == nil
}

if gid == 0 {
gid, err = driver.z.nextUID()
gid, err = engine.z.nextUID()
if err != nil {
return 0, object, false, err
}
}

dms = make([]*dql.Mutation, 0)
err = generateSetDqlMutationsAndSchema[T](ctx, db, object, gid, &dms, sch)
err = generateSetDqlMutationsAndSchema[T](ctx, ns, object, gid, &dms, sch)
if err != nil {
return 0, object, false, err
}

err = applyDqlMutations(ctx, driver, dms)
err = applyDqlMutations(ctx, engine, dms)
if err != nil {
return 0, object, false, err
}

gid, object, err = getByGid[T](ctx, db, gid)
gid, object, err = getByGid[T](ctx, ns, gid)
if err != nil {
return 0, object, false, err
}

return gid, object, wasFound, nil
}

func Get[T any, R UniqueField](driver *Driver, uniqueField R, dbId ...uint64) (uint64, T, error) {
driver.mutex.Lock()
defer driver.mutex.Unlock()
func Get[T any, R UniqueField](engine *Engine, uniqueField R, nsId ...uint64) (uint64, T, error) {
engine.mutex.Lock()
defer engine.mutex.Unlock()
var obj T
if len(dbId) > 1 {
if len(nsId) > 1 {
return 0, obj, fmt.Errorf("only one namespace is allowed")
}
ctx, db, err := getDefaultDB(driver, dbId...)
ctx, ns, err := getDefaultNamespace(engine, nsId...)
if err != nil {
return 0, obj, err
}
if uid, ok := any(uniqueField).(uint64); ok {
return getByGid[T](ctx, db, uid)
return getByGid[T](ctx, ns, uid)
}

if cf, ok := any(uniqueField).(ConstrainedField); ok {
return getByConstrainedField[T](ctx, db, cf)
return getByConstrainedField[T](ctx, ns, cf)
}

return 0, obj, fmt.Errorf("invalid unique field type")
}

func Query[T any](driver *Driver, queryParams QueryParams, dbId ...uint64) ([]uint64, []T, error) {
driver.mutex.Lock()
defer driver.mutex.Unlock()
if len(dbId) > 1 {
func Query[T any](engine *Engine, queryParams QueryParams, nsId ...uint64) ([]uint64, []T, error) {
engine.mutex.Lock()
defer engine.mutex.Unlock()
if len(nsId) > 1 {
return nil, nil, fmt.Errorf("only one namespace is allowed")
}
ctx, db, err := getDefaultDB(driver, dbId...)
ctx, ns, err := getDefaultNamespace(engine, nsId...)
if err != nil {
return nil, nil, err
}

return executeQuery[T](ctx, db, queryParams, true)
return executeQuery[T](ctx, ns, queryParams, true)
}

func Delete[T any, R UniqueField](driver *Driver, uniqueField R, dbId ...uint64) (uint64, T, error) {
driver.mutex.Lock()
defer driver.mutex.Unlock()
func Delete[T any, R UniqueField](engine *Engine, uniqueField R, nsId ...uint64) (uint64, T, error) {
engine.mutex.Lock()
defer engine.mutex.Unlock()
var zeroObj T
if len(dbId) > 1 {
if len(nsId) > 1 {
return 0, zeroObj, fmt.Errorf("only one namespace is allowed")
}
ctx, db, err := getDefaultDB(driver, dbId...)
ctx, ns, err := getDefaultNamespace(engine, nsId...)
if err != nil {
return 0, zeroObj, err
}
if uid, ok := any(uniqueField).(uint64); ok {
uid, obj, err := getByGid[T](ctx, db, uid)
uid, obj, err := getByGid[T](ctx, ns, uid)
if err != nil {
return 0, zeroObj, err
}

dms := generateDeleteDqlMutations(db, uid)
dms := generateDeleteDqlMutations(ns, uid)

err = applyDqlMutations(ctx, driver, dms)
err = applyDqlMutations(ctx, engine, dms)
if err != nil {
return 0, zeroObj, err
}
Expand All @@ -190,14 +190,14 @@ func Delete[T any, R UniqueField](driver *Driver, uniqueField R, dbId ...uint64)
}

if cf, ok := any(uniqueField).(ConstrainedField); ok {
uid, obj, err := getByConstrainedField[T](ctx, db, cf)
uid, obj, err := getByConstrainedField[T](ctx, ns, cf)
if err != nil {
return 0, zeroObj, err
}

dms := generateDeleteDqlMutations(db, uid)
dms := generateDeleteDqlMutations(ns, uid)

err = applyDqlMutations(ctx, driver, dms)
err = applyDqlMutations(ctx, engine, dms)
if err != nil {
return 0, zeroObj, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/structreflect/tagparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func parseJsonTag(field reflect.StructField) (string, error) {
}

func parseDbTag(field reflect.StructField) *DbTag {
dbConstraintsTag := field.Tag.Get("db")
dbConstraintsTag := field.Tag.Get("ns")
if dbConstraintsTag == "" {
return nil
}
Expand Down
20 changes: 10 additions & 10 deletions api_mutation_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/hypermodeinc/modusdb/api/structreflect"
)

func generateSetDqlMutationsAndSchema[T any](ctx context.Context, d *DB, object T,
func generateSetDqlMutationsAndSchema[T any](ctx context.Context, n *Namespace, object T,
gid uint64, dms *[]*dql.Mutation, sch *schema.ParsedSchema) error {
t := reflect.TypeOf(object)
if t.Kind() != reflect.Struct {
Expand All @@ -49,7 +49,7 @@ func generateSetDqlMutationsAndSchema[T any](ctx context.Context, d *DB, object
if tagMaps.JsonToReverseEdge[jsonName] != "" {
reverseEdgeStr := tagMaps.JsonToReverseEdge[jsonName]
typeName := strings.Split(reverseEdgeStr, ".")[0]
currSchema, err := getSchema(ctx, d)
currSchema, err := getSchema(ctx, n)
if err != nil {
return err
}
Expand All @@ -70,7 +70,7 @@ func generateSetDqlMutationsAndSchema[T any](ctx context.Context, d *DB, object
}

if !(typeFound && predicateFound) {
if err := mutations.HandleReverseEdge(jsonName, reflectValueType, d.ID(), sch,
if err := mutations.HandleReverseEdge(jsonName, reflectValueType, n.ID(), sch,
reverseEdgeStr); err != nil {
return err
}
Expand All @@ -82,17 +82,17 @@ func generateSetDqlMutationsAndSchema[T any](ctx context.Context, d *DB, object
continue
}

value, err = processStructValue(ctx, value, d)
value, err = processStructValue(ctx, value, n)
if err != nil {
return err
}

value, err = processPointerValue(ctx, value, d)
value, err = processPointerValue(ctx, value, n)
if err != nil {
return err
}

nquad, u, err := mutations.CreateNQuadAndSchema(value, gid, jsonName, t, d.ID())
nquad, u, err := mutations.CreateNQuadAndSchema(value, gid, jsonName, t, n.ID())
if err != nil {
return err
}
Expand All @@ -111,7 +111,7 @@ func generateSetDqlMutationsAndSchema[T any](ctx context.Context, d *DB, object
}

sch.Types = append(sch.Types, &pb.TypeUpdate{
TypeName: apiutils.AddNamespace(d.ID(), t.Name()),
TypeName: apiutils.AddNamespace(n.ID(), t.Name()),
Fields: sch.Preds,
})

Expand All @@ -120,7 +120,7 @@ func generateSetDqlMutationsAndSchema[T any](ctx context.Context, d *DB, object
return err
}
typeNquad := &api.NQuad{
Namespace: d.ID(),
Namespace: n.ID(),
Subject: fmt.Sprint(gid),
Predicate: "dgraph.type",
ObjectValue: val,
Expand All @@ -134,11 +134,11 @@ func generateSetDqlMutationsAndSchema[T any](ctx context.Context, d *DB, object
return nil
}

func generateDeleteDqlMutations(d *DB, gid uint64) []*dql.Mutation {
func generateDeleteDqlMutations(n *Namespace, gid uint64) []*dql.Mutation {
return []*dql.Mutation{{
Del: []*api.NQuad{
{
Namespace: d.ID(),
Namespace: n.ID(),
Subject: fmt.Sprint(gid),
Predicate: x.Star,
ObjectValue: &api.Value{
Expand Down
Loading

0 comments on commit 4e5214b

Please sign in to comment.