Skip to content

Commit c42f80b

Browse files
trying potential optimization
1 parent 349b5df commit c42f80b

File tree

2 files changed

+82
-48
lines changed

2 files changed

+82
-48
lines changed

posting/lists.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,39 @@ func (lc *LocalCache) getInternal(key []byte, readFromDisk bool) (*List, error)
240240
return lc.SetIfAbsent(skey, pl), nil
241241
}
242242

243+
func (lc *LocalCache) GetSinglePosting(key []byte) (*pb.PostingList, error) {
244+
pl := &pb.PostingList{}
245+
lc.RLock()
246+
if delta, ok := lc.deltas[string(key)]; ok && len(delta) > 0 {
247+
err := pl.Unmarshal(delta)
248+
lc.RUnlock()
249+
if err != nil {
250+
return pl, nil
251+
}
252+
} else {
253+
lc.RUnlock()
254+
}
255+
256+
txn := pstore.NewTransactionAt(lc.startTs, false)
257+
item, err := txn.Get(key)
258+
if err != nil {
259+
return pl, err
260+
}
261+
262+
err = item.Value(func(val []byte) error {
263+
if err := pl.Unmarshal(val); err != nil {
264+
return err
265+
}
266+
return nil
267+
})
268+
269+
if err != nil {
270+
return pl, err
271+
}
272+
273+
return pl, nil
274+
}
275+
243276
func (lc *LocalCache) GetSingle(key []byte) (*List, error) {
244277
return lc.getSingleInternal(key, true)
245278
}

worker/task.go

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -391,69 +391,70 @@ func (qs *queryState) handleValuePostings(ctx context.Context, args funcArgs) er
391391
key := x.DataKey(q.Attr, q.UidList.Uids[i])
392392

393393
// Get or create the posting list for an entity, attribute combination.
394-
var pl *posting.List
395394
pickMultiplePostings := q.DoCount || q.ExpandAll || listType || len(q.Langs) > 0
396395

397396
var vals []types.Val
398397
fcs := &pb.FacetsList{FacetsList: make([]*pb.Facets, 0)} // TODO Figure out how it is stored
399398

400399
if !pickMultiplePostings {
401-
//fmt.Println("HERE GETTING SINGLE KEY", key)
402-
//vals, fcs, err = retrieveValuesAndFacets(args, pl, facetsTree, listType)
403-
pl, _ = qs.cache.GetSingle(key)
404-
405-
//vals1, _, _ := retrieveValuesAndFacets(args, pl1, facetsTree, listType)
406-
//fmt.Println("Here getting key", len(vals), vals, len(vals1), vals1, len(vals) != len(vals1))
407-
//if len(vals) != len(vals1) {
408-
// fmt.Println("HERE")
409-
//}
410-
//vals, fcs, err = retrieveValuesAndFacets(args, pl, facetsTree, listType)
411-
//fmt.Println("Here getting full key", len(vals), len(fcs.FacetsList), vals[0])
412-
} else {
400+
pl, _ := qs.cache.GetSinglePosting(key)
401+
for _, p := range pl.Postings {
402+
vals = append(vals, types.Val{
403+
Tid: types.TypeID(p.ValType),
404+
Value: p.Value,
405+
})
406+
407+
if q.FacetParam != nil {
408+
fcs.FacetsList = append(fcs.FacetsList, &pb.Facets{Facets: facets.CopyFacets(p.Facets, q.FacetParam)})
409+
}
410+
}
413411

414-
pl, err = qs.cache.Get(key)
415-
}
416-
if err != nil {
417-
return err
418-
}
419-
// If count is being requested, there is no need to populate value and facets matrix.
420-
if q.DoCount {
421-
count, err := countForValuePostings(args, pl, facetsTree, listType)
422-
if err != nil && err != posting.ErrNoValue {
412+
} else {
413+
pl, err := qs.cache.Get(key)
414+
if err != nil {
423415
return err
424416
}
425-
out.Counts = append(out.Counts, uint32(count))
426-
// Add an empty UID list to make later processing consistent.
427-
out.UidMatrix = append(out.UidMatrix, &pb.List{})
428-
continue
429-
}
430417

431-
if q.ExpandAll {
432-
langTags, err := pl.GetLangTags(args.q.ReadTs)
433-
if err != nil {
418+
// If count is being requested, there is no need to populate value and facets matrix.
419+
if q.DoCount {
420+
count, err := countForValuePostings(args, pl, facetsTree, listType)
421+
if err != nil && err != posting.ErrNoValue {
422+
return err
423+
}
424+
out.Counts = append(out.Counts, uint32(count))
425+
// Add an empty UID list to make later processing consistent.
426+
out.UidMatrix = append(out.UidMatrix, &pb.List{})
427+
continue
428+
}
429+
430+
vals, fcs, err = retrieveValuesAndFacets(args, pl, facetsTree, listType)
431+
432+
switch {
433+
case err == posting.ErrNoValue || (err == nil && len(vals) == 0):
434+
// This branch is taken when the value does not exist in the pl or
435+
// the number of values retrieved is zero (there could still be facets).
436+
// We add empty lists to the UidMatrix, FaceMatrix, ValueMatrix and
437+
// LangMatrix so that all these data structure have predictable layouts.
438+
out.UidMatrix = append(out.UidMatrix, &pb.List{})
439+
out.FacetMatrix = append(out.FacetMatrix, &pb.FacetsList{})
440+
out.ValueMatrix = append(out.ValueMatrix,
441+
&pb.ValueList{Values: []*pb.TaskValue{}})
442+
if q.ExpandAll {
443+
// To keep the cardinality same as that of ValueMatrix.
444+
out.LangMatrix = append(out.LangMatrix, &pb.LangList{})
445+
}
446+
continue
447+
case err != nil:
434448
return err
435449
}
436-
out.LangMatrix = append(out.LangMatrix, &pb.LangList{Lang: langTags})
437-
}
438450

439-
vals, fcs, err = retrieveValuesAndFacets(args, pl, facetsTree, listType)
440-
switch {
441-
case err == posting.ErrNoValue || (err == nil && len(vals) == 0):
442-
// This branch is taken when the value does not exist in the pl or
443-
// the number of values retrieved is zero (there could still be facets).
444-
// We add empty lists to the UidMatrix, FaceMatrix, ValueMatrix and
445-
// LangMatrix so that all these data structure have predictable layouts.
446-
out.UidMatrix = append(out.UidMatrix, &pb.List{})
447-
out.FacetMatrix = append(out.FacetMatrix, &pb.FacetsList{})
448-
out.ValueMatrix = append(out.ValueMatrix,
449-
&pb.ValueList{Values: []*pb.TaskValue{}})
450451
if q.ExpandAll {
451-
// To keep the cardinality same as that of ValueMatrix.
452-
out.LangMatrix = append(out.LangMatrix, &pb.LangList{})
452+
langTags, err := pl.GetLangTags(args.q.ReadTs)
453+
if err != nil {
454+
return err
455+
}
456+
out.LangMatrix = append(out.LangMatrix, &pb.LangList{Lang: langTags})
453457
}
454-
continue
455-
case err != nil:
456-
return err
457458
}
458459

459460
uidList := new(pb.List)

0 commit comments

Comments
 (0)