Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: global block cache for carstore #567

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions carstore/bs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand Down
Loading