Skip to content

Commit

Permalink
Expose APIs that can be used outside Dgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
mangalaman93 committed Sep 27, 2024
1 parent 01f5df3 commit a3996c2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
16 changes: 8 additions & 8 deletions edgraph/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ func validateAlterOperation(ctx context.Context, op *api.Operation) error {
if !isMutationAllowed(ctx) {
return errors.Errorf("No mutations allowed by server.")
}

if _, err := hasAdminAuth(ctx, "Alter"); err != nil {
glog.Warningf("Alter denied with error: %v\n", err)
return err
Expand Down Expand Up @@ -1579,7 +1580,7 @@ func parseRequest(ctx context.Context, qc *queryContext) error {
// parsing mutations
qc.gmuList = make([]*dql.Mutation, 0, len(qc.req.Mutations))
for _, mu := range qc.req.Mutations {
gmu, err := parseMutationObject(mu, qc)
gmu, err := ParseMutationObject(mu, qc.graphql)
if err != nil {
return err
}
Expand Down Expand Up @@ -1930,12 +1931,12 @@ func hasPoormansAuth(ctx context.Context) error {
return nil
}

// parseMutationObject tries to consolidate fields of the api.Mutation into the
// ParseMutationObject tries to consolidate fields of the api.Mutation into the
// corresponding field of the returned dql.Mutation. For example, the 3 fields,
// api.Mutation#SetJson, api.Mutation#SetNquads and api.Mutation#Set are consolidated into the
// dql.Mutation.Set field. Similarly the 3 fields api.Mutation#DeleteJson, api.Mutation#DelNquads
// and api.Mutation#Del are merged into the dql.Mutation#Del field.
func parseMutationObject(mu *api.Mutation, qc *queryContext) (*dql.Mutation, error) {
func ParseMutationObject(mu *api.Mutation, isGraphql bool) (*dql.Mutation, error) {
res := &dql.Mutation{Cond: mu.Cond}

if len(mu.SetJson) > 0 {
Expand Down Expand Up @@ -1979,7 +1980,7 @@ func parseMutationObject(mu *api.Mutation, qc *queryContext) (*dql.Mutation, err
return nil, err
}

if err := validateNQuads(res.Set, res.Del, qc); err != nil {
if err := validateNQuads(res.Set, res.Del, isGraphql); err != nil {
return nil, err
}
return res, nil
Expand Down Expand Up @@ -2015,8 +2016,7 @@ func validateForGraphql(nq *api.NQuad, isGraphql bool) error {
return nil
}

func validateNQuads(set, del []*api.NQuad, qc *queryContext) error {

func validateNQuads(set, del []*api.NQuad, isGraphql bool) error {
for _, nq := range set {
if err := validatePredName(nq.Predicate); err != nil {
return err
Expand All @@ -2031,7 +2031,7 @@ func validateNQuads(set, del []*api.NQuad, qc *queryContext) error {
if err := validateKeys(nq); err != nil {
return errors.Wrapf(err, "key error: %+v", nq)
}
if err := validateForGraphql(nq, qc.graphql); err != nil {
if err := validateForGraphql(nq, isGraphql); err != nil {
return err
}
}
Expand All @@ -2046,7 +2046,7 @@ func validateNQuads(set, del []*api.NQuad, qc *queryContext) error {
if nq.Subject == x.Star || (nq.Predicate == x.Star && !ostar) {
return errors.Errorf("Only valid wildcard delete patterns are 'S * *' and 'S P *': %v", nq)
}
if err := validateForGraphql(nq, qc.graphql); err != nil {
if err := validateForGraphql(nq, isGraphql); err != nil {
return err
}
// NOTE: we dont validateKeys() with delete to let users fix existing mistakes
Expand Down
21 changes: 14 additions & 7 deletions query/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,8 @@ func verifyUid(ctx context.Context, uid uint64) error {
}
}

// AssignUids tries to assign unique ids to each identity in the subjects and objects in the
// format of _:xxx. An identity, e.g. _:a, will only be assigned one uid regardless how many times
// it shows up in the subjects or objects
func AssignUids(ctx context.Context, gmuList []*dql.Mutation) (map[string]uint64, error) {
func ExtractBlankUIDs(ctx context.Context, gmuList []*dql.Mutation) (map[string]uint64, error) {
newUids := make(map[string]uint64)
num := &pb.Num{}
var err error
for _, gmu := range gmuList {
for _, nq := range gmu.Set {
Expand Down Expand Up @@ -192,8 +188,19 @@ func AssignUids(ctx context.Context, gmuList []*dql.Mutation) (map[string]uint64
}
}

num.Val = uint64(len(newUids))
num.Type = pb.Num_UID
return newUids, nil
}

// AssignUids tries to assign unique ids to each identity in the subjects and objects in the
// format of _:xxx. An identity, e.g. _:a, will only be assigned one uid regardless how many times
// it shows up in the subjects or objects
func AssignUids(ctx context.Context, gmuList []*dql.Mutation) (map[string]uint64, error) {
newUids, err := ExtractBlankUIDs(ctx, gmuList)
if err != nil {
return newUids, err
}

num := &pb.Num{Val: uint64(len(newUids)), Type: pb.Num_UID}
if int(num.Val) > 0 {
var res *pb.AssignedIds
// TODO: Optimize later by prefetching. Also consolidate all the UID requests into a single
Expand Down
4 changes: 2 additions & 2 deletions worker/server_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func InitServerState() {
State.FinishCh = make(chan struct{})
State.needTs = make(chan tsReq, 100)

State.initStorage()
State.InitStorage()
go State.fillTimestampRequests()

groupId, err := x.ReadGroupIdFile(Config.PostingDir)
Expand All @@ -103,7 +103,7 @@ func setBadgerOptions(opt badger.Options) badger.Options {
return opt
}

func (s *ServerState) initStorage() {
func (s *ServerState) InitStorage() {
var err error

if x.WorkerConfig.EncryptionKey != nil {
Expand Down

0 comments on commit a3996c2

Please sign in to comment.