@@ -18,17 +18,13 @@ package posting
1818
1919import (
2020 "bytes"
21- "context"
2221 "fmt"
2322 "sync"
24- "time"
2523
26- ostats "go.opencensus.io/stats"
2724 "google.golang.org/protobuf/proto"
2825
2926 "github.com/dgraph-io/badger/v4"
3027 "github.com/dgraph-io/dgo/v240/protos/api"
31- "github.com/dgraph-io/ristretto/v2"
3228 "github.com/dgraph-io/ristretto/v2/z"
3329 "github.com/hypermodeinc/dgraph/v24/protos/pb"
3430 "github.com/hypermodeinc/dgraph/v24/tok/index"
@@ -42,41 +38,15 @@ const (
4238var (
4339 pstore * badger.DB
4440 closer * z.Closer
45- lCache * ristretto.Cache [[]byte , * List ]
4641)
4742
4843// Init initializes the posting lists package, the in memory and dirty list hash.
49- func Init (ps * badger.DB , cacheSize int64 ) {
44+ func Init (ps * badger.DB , cacheSize int64 , keepUpdates bool ) {
5045 pstore = ps
5146 closer = z .NewCloser (1 )
5247 go x .MonitorMemoryMetrics (closer )
5348
54- // Initialize cache.
55- if cacheSize == 0 {
56- return
57- }
58-
59- var err error
60- lCache , err = ristretto.NewCache [[]byte , * List ](& ristretto.Config [[]byte , * List ]{
61- // Use 5% of cache memory for storing counters.
62- NumCounters : int64 (float64 (cacheSize ) * 0.05 * 2 ),
63- MaxCost : int64 (float64 (cacheSize ) * 0.95 ),
64- BufferItems : 64 ,
65- Metrics : true ,
66- Cost : func (val * List ) int64 {
67- return 0
68- },
69- })
70- x .Check (err )
71- go func () {
72- m := lCache .Metrics
73- ticker := time .NewTicker (10 * time .Second )
74- defer ticker .Stop ()
75- for range ticker .C {
76- // Record the posting list cache hit ratio
77- ostats .Record (context .Background (), x .PLCacheHitRatio .M (m .Ratio ()))
78- }
79- }()
49+ memoryLayer = initMemoryLayer (cacheSize , keepUpdates )
8050}
8151
8252func UpdateMaxCost (maxCost int64 ) {
@@ -145,7 +115,7 @@ func (vc *viLocalCache) GetWithLockHeld(key []byte) (rval index.Value, rerr erro
145115func (vc * viLocalCache ) GetValueFromPostingList (pl * List ) (rval index.Value , rerr error ) {
146116 value := pl .findStaticValue (vc .delegate .startTs )
147117
148- if value == nil {
118+ if value == nil || len ( value . Postings ) == 0 {
149119 return nil , ErrNoValue
150120 }
151121
@@ -314,8 +284,9 @@ func (lc *LocalCache) getInternal(key []byte, readFromDisk bool) (*List, error)
314284 }
315285 } else {
316286 pl = & List {
317- key : key ,
318- plist : new (pb.PostingList ),
287+ key : key ,
288+ plist : new (pb.PostingList ),
289+ mutationMap : newMutableLayer (),
319290 }
320291 }
321292
@@ -421,8 +392,6 @@ func (lc *LocalCache) UpdateDeltasAndDiscardLists() {
421392 }
422393
423394 for key , pl := range lc .plists {
424- //pk, _ := x.Parse([]byte(key))
425- //fmt.Printf("{TXN} Closing %v\n", pk)
426395 data := pl .getMutation (lc .startTs )
427396 if len (data ) > 0 {
428397 lc .deltas [key ] = data
0 commit comments