aboutsummaryrefslogtreecommitdiff
path: root/object/store/packed/delta_cache.go
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-03-28 04:19:44 +0000
committerGravatar Runxi Yu2026-03-28 04:20:29 +0000
commit402ef2733813d128631ca4aea18c2908c74340d5 (patch)
treee03a90b6f41411bd62e7339390802c5c50082850 /object/store/packed/delta_cache.go
parentobject/store: Rename from object/storer (diff)
signatureNo signature
object/store: Rename back from storer; rename Store to ReadingStore v0.1.118
Diffstat (limited to 'object/store/packed/delta_cache.go')
-rw-r--r--object/store/packed/delta_cache.go61
1 files changed, 61 insertions, 0 deletions
diff --git a/object/store/packed/delta_cache.go b/object/store/packed/delta_cache.go
new file mode 100644
index 00000000..3bf3a035
--- /dev/null
+++ b/object/store/packed/delta_cache.go
@@ -0,0 +1,61 @@
+package packed
+
+import (
+ "codeberg.org/lindenii/furgit/internal/lru"
+ objecttype "codeberg.org/lindenii/furgit/object/type"
+)
+
+const defaultDeltaCacheMaxBytes = 32 << 20
+
+// deltaBaseKey identifies one base object by pack location.
+type deltaBaseKey struct {
+ packName string
+ offset uint64
+}
+
+// deltaBaseValue stores one cached base object body.
+type deltaBaseValue struct {
+ ty objecttype.Type
+ content []byte
+}
+
+// deltaCache wraps a weighted LRU for resolved delta bases.
+type deltaCache struct {
+ lru *lru.Cache[deltaBaseKey, deltaBaseValue]
+}
+
+// newDeltaCache creates a delta base cache with a byte budget.
+func newDeltaCache(maxBytes int64) *deltaCache {
+ return &deltaCache{
+ lru: lru.New(
+ maxBytes,
+ func(_ deltaBaseKey, value deltaBaseValue) int64 {
+ return int64(len(value.content))
+ },
+ nil,
+ ),
+ }
+}
+
+// get returns a cloned cached base object value.
+func (cache *deltaCache) get(key deltaBaseKey) (objecttype.Type, []byte, bool) {
+ value, ok := cache.lru.Get(key)
+ if !ok {
+ return objecttype.TypeInvalid, nil, false
+ }
+
+ return value.ty, append([]byte(nil), value.content...), true
+}
+
+// add stores a cloned base object value.
+func (cache *deltaCache) add(key deltaBaseKey, ty objecttype.Type, content []byte) {
+ cache.lru.Add(key, deltaBaseValue{
+ ty: ty,
+ content: append([]byte(nil), content...),
+ })
+}
+
+// clear removes all cached entries.
+func (cache *deltaCache) clear() {
+ cache.lru.Clear()
+}