Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize uid allocation in live loader. #5132

Merged
merged 1 commit into from
Apr 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion dgraph/cmd/live/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ func (l *loader) uid(val string) string {
// later to another node. It is up to the user to avoid this.
if !opt.newUids {
if uid, err := strconv.ParseUint(val, 0, 64); err == nil {
l.alloc.BumpTo(uid)
return fmt.Sprintf("%#x", uid)
}
}
Expand All @@ -214,6 +213,34 @@ func (l *loader) uid(val string) string {
return fmt.Sprintf("%#x", uint64(uid))
}

// allocateUids looks for the maximum uid value in the given NQuads and bumps the
// maximum seen uid to that value.
func (l *loader) allocateUids(nqs []*api.NQuad) {
if opt.newUids {
return
}

var maxUid uint64
for _, nq := range nqs {
sUid, err := strconv.ParseUint(nq.Subject, 0, 64)
if err != nil {
continue
}
if sUid > maxUid {
maxUid = sUid
}

oUid, err := strconv.ParseUint(nq.ObjectId, 0, 64)
if err != nil {
continue
}
if oUid > maxUid {
maxUid = oUid
}
}
l.alloc.BumpTo(maxUid)
}

// processFile forwards a file to the RDF or JSON processor as appropriate
func (l *loader) processFile(ctx context.Context, filename string) error {
fmt.Printf("Processing data file %q\n", filename)
Expand Down Expand Up @@ -281,6 +308,8 @@ func (l *loader) processLoadFile(ctx context.Context, rd *bufio.Reader, ck chunk
if len(nqs) == 0 {
continue
}

l.allocateUids(nqs)
for _, nq := range nqs {
nq.Subject = l.uid(nq.Subject)
if len(nq.ObjectId) > 0 {
Expand Down