diff options
| author | Fuwn <[email protected]> | 2026-02-27 09:26:06 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-02-27 09:26:06 -0800 |
| commit | 8980607ef8e7426b601f942f26ae2cd4c4f3edff (patch) | |
| tree | 60bdb4bbbe5755223c3387179ee7406432d084ab /internal/storage | |
| parent | fix: make mirror replay lossless with strict seq accounting and trace (diff) | |
| download | plutia-test-8980607ef8e7426b601f942f26ae2cd4c4f3edff.tar.xz plutia-test-8980607ef8e7426b601f942f26ae2cd4c4f3edff.zip | |
feat: add thin mode for on-demand verified PLC resolution
Diffstat (limited to 'internal/storage')
| -rw-r--r-- | internal/storage/pebble_store.go | 74 | ||||
| -rw-r--r-- | internal/storage/store.go | 5 |
2 files changed, 79 insertions, 0 deletions
diff --git a/internal/storage/pebble_store.go b/internal/storage/pebble_store.go index b0a8e01..11e2dad 100644 --- a/internal/storage/pebble_store.go +++ b/internal/storage/pebble_store.go @@ -71,6 +71,27 @@ func (p *PebbleStore) PutState(state types.StateV1) error { return p.db.Set(didKey(state.DID), b, pebble.Sync) } +func (p *PebbleStore) DeleteState(did string) error { + batch := p.db.NewBatch() + defer batch.Close() + + if err := batch.Delete(didKey(did), nil); err != nil { + return err + } + + if err := batch.Delete(chainKey(did), nil); err != nil { + return err + } + + prefix := []byte("didop:" + did + ":") + upper := append(append([]byte(nil), prefix...), 0xFF) + if err := batch.DeleteRange(prefix, upper, nil); err != nil { + return err + } + + return batch.Commit(pebble.Sync) +} + func (p *PebbleStore) ApplyOperationBatch(state types.StateV1, ref *types.BlockRefV1, includeOpRef bool) error { var opRef *types.BlockRefV1 @@ -141,6 +162,57 @@ func (p *PebbleStore) ForEachState(fn func(types.StateV1) error) error { return nil } +func (p *PebbleStore) PutThinCacheMeta(meta types.ThinCacheMetaV1) error { + b, err := json.Marshal(meta) + if err != nil { + return fmt.Errorf("marshal thin cache meta: %w", err) + } + + return p.db.Set(thinMetaKey(meta.DID), b, pebble.Sync) +} + +func (p *PebbleStore) GetThinCacheMeta(did string) (types.ThinCacheMetaV1, bool, error) { + b, ok, err := p.getBytes(thinMetaKey(did)) + if err != nil || !ok { + return types.ThinCacheMetaV1{}, ok, err + } + + var meta types.ThinCacheMetaV1 + if err := json.Unmarshal(b, &meta); err != nil { + return types.ThinCacheMetaV1{}, false, fmt.Errorf("unmarshal thin cache meta: %w", err) + } + + return meta, true, nil +} + +func (p *PebbleStore) ListThinCacheMeta() ([]types.ThinCacheMetaV1, error) { + iter, err := p.db.NewIter(&pebble.IterOptions{LowerBound: []byte("thinmeta:"), UpperBound: []byte("thinmeta;")}) + if err != nil { + return nil, fmt.Errorf("new iterator: %w", err) + } + defer iter.Close() + + out := make([]types.ThinCacheMetaV1, 0) + for iter.First(); iter.Valid(); iter.Next() { + var meta types.ThinCacheMetaV1 + if err := json.Unmarshal(iter.Value(), &meta); err != nil { + return nil, fmt.Errorf("unmarshal thin cache meta: %w", err) + } + + out = append(out, meta) + } + + if err := iter.Error(); err != nil { + return nil, fmt.Errorf("iterate thin cache meta: %w", err) + } + + return out, nil +} + +func (p *PebbleStore) DeleteThinCacheMeta(did string) error { + return p.db.Delete(thinMetaKey(did), pebble.Sync) +} + func (p *PebbleStore) ApplyOperationsBatch(ops []OperationMutation, blockHashes []BlockHashEntry) error { if len(ops) == 0 && len(blockHashes) == 0 { return nil @@ -484,6 +556,8 @@ func didOpKey(did string, seq uint64) []byte { return append([]byte("didop:"+did+":"), u64bytes(seq)...) } +func thinMetaKey(did string) []byte { return []byte("thinmeta:" + did) } + func u64bytes(v uint64) []byte { b := make([]byte, 8) diff --git a/internal/storage/store.go b/internal/storage/store.go index 5267386..456e758 100644 --- a/internal/storage/store.go +++ b/internal/storage/store.go @@ -19,9 +19,14 @@ type Store interface { GetGlobalSeq() (uint64, error) SetGlobalSeq(seq uint64) error PutState(state types.StateV1) error + DeleteState(did string) error GetState(did string) (types.StateV1, bool, error) ListStates() ([]types.StateV1, error) ForEachState(fn func(types.StateV1) error) error + PutThinCacheMeta(meta types.ThinCacheMetaV1) error + GetThinCacheMeta(did string) (types.ThinCacheMetaV1, bool, error) + ListThinCacheMeta() ([]types.ThinCacheMetaV1, error) + DeleteThinCacheMeta(did string) error ApplyOperationsBatch(ops []OperationMutation, blockHashes []BlockHashEntry) error SetChainHead(did string, seq uint64) error GetChainHead(did string) (uint64, bool, error) |