@@ -330,32 +330,32 @@ func (lc *LocalCache) readPostingListAt(key []byte) (*pb.PostingList, error) {
330330 return pl , err
331331}
332332
333- // GetSinglePosting retrieves the cached version of the first item in the list associated with the
334- // given key. This is used for retrieving the value of a scalar predicats.
335- func (lc * LocalCache ) GetSinglePosting (key []byte ) (* pb.PostingList , error ) {
336- // This would return an error if there is some data in the local cache, but we couldn't read it.
337- getListFromLocalCache := func () (* pb.PostingList , error ) {
338- lc .RLock ()
339-
340- pl := & pb.PostingList {}
341- if delta , ok := lc .deltas [string (key )]; ok && len (delta ) > 0 {
342- err := proto .Unmarshal (delta , pl )
343- lc .RUnlock ()
344- return pl , err
345- }
333+ func (lc * LocalCache ) GetSinglePostingFromLocalCache (key []byte ) (* pb.PostingList , error ) {
334+ lc .RLock ()
346335
347- l := lc .plists [string (key )]
336+ pl := & pb.PostingList {}
337+ if delta , ok := lc .deltas [string (key )]; ok && len (delta ) > 0 {
338+ err := proto .Unmarshal (delta , pl )
348339 lc .RUnlock ()
340+ return pl , err
341+ }
349342
350- if l != nil {
351- return l .StaticValue (lc .startTs )
352- }
343+ l := lc .plists [string (key )]
344+ lc .RUnlock ()
353345
354- return nil , nil
346+ if l != nil {
347+ return l .StaticValue (lc .startTs )
355348 }
356349
350+ return nil , nil
351+ }
352+
353+ // GetSinglePosting retrieves the cached version of the first item in the list associated with the
354+ // given key. This is used for retrieving the value of a scalar predicats.
355+ func (lc * LocalCache ) GetSinglePosting (key []byte ) (* pb.PostingList , error ) {
356+ // This would return an error if there is some data in the local cache, but we couldn't read it.
357357 getPostings := func () (* pb.PostingList , error ) {
358- pl , err := getListFromLocalCache ( )
358+ pl , err := lc . GetSinglePostingFromLocalCache ( key )
359359 // If both pl and err are empty, that means that there was no data in local cache, hence we should
360360 // read the data from badger.
361361 if pl != nil || err != nil {
@@ -388,6 +388,59 @@ func (lc *LocalCache) GetSinglePosting(key []byte) (*pb.PostingList, error) {
388388 return pl , nil
389389}
390390
391+ func (lc * LocalCache ) GetBatchSinglePosting (keys [][]byte ) ([]* pb.PostingList , error ) {
392+ results := make ([]* pb.PostingList , len (keys ))
393+ remaining_keys := make ([][]byte , 0 )
394+ for i , key := range keys {
395+ if pl , err := lc .GetSinglePostingFromLocalCache (key ); pl != nil && err != nil {
396+ results [i ] = pl
397+ } else {
398+ remaining_keys = append (remaining_keys , key )
399+ }
400+ }
401+
402+ txn := pstore .NewTransactionAt (lc .startTs , false )
403+ items , err := txn .GetBatch (remaining_keys )
404+ if err != nil {
405+ fmt .Println (err , keys )
406+ return nil , err
407+ }
408+ idx := 0
409+
410+ for i := 0 ; i < len (results ); i ++ {
411+ if results [i ] != nil {
412+ continue
413+ }
414+ pl := & pb.PostingList {}
415+ err = items [idx ].Value (func (val []byte ) error {
416+ if err := proto .Unmarshal (val , pl ); err != nil {
417+ return err
418+ }
419+ return nil
420+ })
421+ idx += 1
422+ results [i ] = pl
423+ }
424+
425+ for i := 0 ; i < len (results ); i ++ {
426+ pl := results [i ]
427+ idx := 0
428+ for _ , postings := range pl .Postings {
429+ if hasDeleteAll (postings ) {
430+ return nil , nil
431+ }
432+ if postings .Op != Del {
433+ pl .Postings [idx ] = postings
434+ idx ++
435+ }
436+ }
437+ pl .Postings = pl .Postings [:idx ]
438+ results [i ] = pl
439+ }
440+
441+ return results , err
442+ }
443+
391444// Get retrieves the cached version of the list associated with the given key.
392445func (lc * LocalCache ) Get (key []byte ) (* List , error ) {
393446 return lc .getInternal (key , true )
0 commit comments