From ea16391beffca62e3dc602452c0adc29c18c32e9 Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Wed, 7 Feb 2024 12:21:12 -0800 Subject: [PATCH] WIP: global block cache for carstore --- carstore/bs.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/carstore/bs.go b/carstore/bs.go index 0095c9fd7..efddcc442 100644 --- a/carstore/bs.go +++ b/carstore/bs.go @@ -45,6 +45,8 @@ type CarStore struct { lscLk sync.Mutex lastShardCache map[models.Uid]*CarShard + + globalCache map[cid.Cid]blockformat.Block } func NewCarStore(meta *gorm.DB, root string) (*CarStore, error) { @@ -68,6 +70,7 @@ func NewCarStore(meta *gorm.DB, root string) (*CarStore, error) { meta: meta, rootDir: root, lastShardCache: make(map[models.Uid]*CarShard), + globalCache: make(map[cid.Cid]blockformat.Block), }, nil } @@ -138,6 +141,10 @@ func unpackCids(b []byte) ([]cid.Cid, error) { return out, nil } +func (cs *CarStore) SetGlobalCacheEntry(k cid.Cid, v blockformat.Block) { + cs.globalCache[k] = v +} + type userView struct { cs *CarStore user models.Uid @@ -594,6 +601,11 @@ func (ds *DeltaSession) Get(ctx context.Context, c cid.Cid) (blockformat.Block, return b, nil } + gcb, ok := ds.cs.globalCache[c] + if ok { + return gcb, nil + } + return ds.base.Get(ctx, c) } @@ -603,6 +615,11 @@ func (ds *DeltaSession) Has(ctx context.Context, c cid.Cid) (bool, error) { return true, nil } + _, ok = ds.cs.globalCache[c] + if ok { + return true, nil + } + return ds.base.Has(ctx, c) } @@ -616,6 +633,11 @@ func (ds *DeltaSession) GetSize(ctx context.Context, c cid.Cid) (int, error) { return len(b.RawData()), nil } + gcb, ok := ds.cs.globalCache[c] + if ok { + return len(gcb.RawData()), nil + } + return ds.base.GetSize(ctx, c) }