@@ -323,32 +323,32 @@ func (lc *LocalCache) readPostingListAt(key []byte) (*pb.PostingList, error) {
323323 return pl , err
324324}
325325
326- // GetSinglePosting retrieves the cached version of the first item in the list associated with the
327- // given key. This is used for retrieving the value of a scalar predicats.
328- func (lc * LocalCache ) GetSinglePosting (key []byte ) (* pb.PostingList , error ) {
329- // This would return an error if there is some data in the local cache, but we couldn't read it.
330- getListFromLocalCache := func () (* pb.PostingList , error ) {
331- lc .RLock ()
332-
333- pl := & pb.PostingList {}
334- if delta , ok := lc .deltas [string (key )]; ok && len (delta ) > 0 {
335- err := proto .Unmarshal (delta , pl )
336- lc .RUnlock ()
337- return pl , err
338- }
326+ func (lc * LocalCache ) GetSinglePostingFromLocalCache (key []byte ) (* pb.PostingList , error ) {
327+ lc .RLock ()
339328
340- l := lc .plists [string (key )]
329+ pl := & pb.PostingList {}
330+ if delta , ok := lc .deltas [string (key )]; ok && len (delta ) > 0 {
331+ err := proto .Unmarshal (delta , pl )
341332 lc .RUnlock ()
333+ return pl , err
334+ }
342335
343- if l != nil {
344- return l .StaticValue (lc .startTs )
345- }
336+ l := lc .plists [string (key )]
337+ lc .RUnlock ()
346338
347- return nil , nil
339+ if l != nil {
340+ return l .StaticValue (lc .startTs )
348341 }
349342
343+ return nil , nil
344+ }
345+
346+ // GetSinglePosting retrieves the cached version of the first item in the list associated with the
347+ // given key. This is used for retrieving the value of a scalar predicats.
348+ func (lc * LocalCache ) GetSinglePosting (key []byte ) (* pb.PostingList , error ) {
349+ // This would return an error if there is some data in the local cache, but we couldn't read it.
350350 getPostings := func () (* pb.PostingList , error ) {
351- pl , err := getListFromLocalCache ( )
351+ pl , err := lc . GetSinglePostingFromLocalCache ( key )
352352 // If both pl and err are empty, that means that there was no data in local cache, hence we should
353353 // read the data from badger.
354354 if pl != nil || err != nil {
@@ -381,6 +381,59 @@ func (lc *LocalCache) GetSinglePosting(key []byte) (*pb.PostingList, error) {
381381 return pl , nil
382382}
383383
384+ func (lc * LocalCache ) GetBatchSinglePosting (keys [][]byte ) ([]* pb.PostingList , error ) {
385+ results := make ([]* pb.PostingList , len (keys ))
386+ remaining_keys := make ([][]byte , 0 )
387+ for i , key := range keys {
388+ if pl , err := lc .GetSinglePostingFromLocalCache (key ); pl != nil && err != nil {
389+ results [i ] = pl
390+ } else {
391+ remaining_keys = append (remaining_keys , key )
392+ }
393+ }
394+
395+ txn := pstore .NewTransactionAt (lc .startTs , false )
396+ items , err := txn .GetBatch (remaining_keys )
397+ if err != nil {
398+ fmt .Println (err , keys )
399+ return nil , err
400+ }
401+ idx := 0
402+
403+ for i := 0 ; i < len (results ); i ++ {
404+ if results [i ] != nil {
405+ continue
406+ }
407+ pl := & pb.PostingList {}
408+ err = items [idx ].Value (func (val []byte ) error {
409+ if err := proto .Unmarshal (val , pl ); err != nil {
410+ return err
411+ }
412+ return nil
413+ })
414+ idx += 1
415+ results [i ] = pl
416+ }
417+
418+ for i := 0 ; i < len (results ); i ++ {
419+ pl := results [i ]
420+ idx := 0
421+ for _ , postings := range pl .Postings {
422+ if hasDeleteAll (postings ) {
423+ return nil , nil
424+ }
425+ if postings .Op != Del {
426+ pl .Postings [idx ] = postings
427+ idx ++
428+ }
429+ }
430+ pl .Postings = pl .Postings [:idx ]
431+ results [i ] = pl
432+ }
433+
434+ return results , err
435+ }
436+
384437// Get retrieves the cached version of the list associated with the given key.
385438func (lc * LocalCache ) Get (key []byte ) (* List , error ) {
386439 return lc .getInternal (key , true )
0 commit comments