Files
mesh-delivery/modules/mesh-grants/internal/grants/bigvalues.go
T

79 lines
3.1 KiB
Go

package grants
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
)
// **The part's user list and pass result are kept as objects** (design 47 G A-373): each outgrows one value of a key-value bucket
// (live, 2026-11-13: the list 422584 bytes and the pass 479989, over the 262144 the bucket takes), so the state `user-list` keeps
// them in the part's object bucket and the keys `list` and `pass` name the object, with its digest and size. A write that cannot be
// made stays an error, as it was; a pointer that cannot be followed is an error naming the object, never an empty answer.
// BigObjects is the object bucket the keys' values are kept in: the part's own (the process's connection).
type BigObjects interface {
PutObject(name string, data []byte) error
GetObject(name string) ([]byte, error)
}
// bigKeys are the keys of StateUserList kept as objects.
var bigKeys = map[string]bool{KeyList: true, KeyPass: true}
// BigObjectName is the object a key of StateUserList is kept in.
func BigObjectName(key string) string { return "state." + StateUserList + "." + key }
// bigPointer is what the key holds in place of the value.
type bigPointer struct {
KeptIn string `json:"kept_in_object"`
SHA256 string `json:"sha256"`
Bytes int `json:"bytes"`
}
// BigValues is store with the keys `list` and `pass` of the state user-list kept in objects. With no objects (the tools') a value is
// kept in the key whole, and a key that names an object is an error saying where the value is.
func BigValues(store Store, objects BigObjects) Store {
return bigStore{Store: store, objects: objects}
}
type bigStore struct {
Store
objects BigObjects
}
func (s bigStore) Put(state, key string, value []byte) (uint64, error) {
if state != StateUserList || !bigKeys[key] || s.objects == nil {
return s.Store.Put(state, key, value)
}
name := BigObjectName(key)
if err := s.objects.PutObject(name, value); err != nil {
return 0, fmt.Errorf("the value of %s could not be put as the object %s: %w", key, name, err)
}
sum := sha256.Sum256(value)
ptr, _ := json.Marshal(bigPointer{KeptIn: name, SHA256: hex.EncodeToString(sum[:]), Bytes: len(value)})
return s.Store.Put(state, key, ptr)
}
func (s bigStore) Get(state, key string) ([]byte, uint64, bool, error) {
raw, rev, found, err := s.Store.Get(state, key)
if err != nil || !found || state != StateUserList || !bigKeys[key] {
return raw, rev, found, err
}
var p bigPointer
if json.Unmarshal(raw, &p) != nil || p.KeptIn == "" {
return raw, rev, found, nil // a value kept whole, as before
}
if s.objects == nil {
return nil, rev, true, fmt.Errorf("the value of %s is kept in the object %s of the part's object bucket, which this reader cannot open", key, p.KeptIn)
}
data, err := s.objects.GetObject(p.KeptIn)
if err != nil {
return nil, rev, true, fmt.Errorf("the object %s that %s names cannot be read: %w", p.KeptIn, key, err)
}
if sum := sha256.Sum256(data); hex.EncodeToString(sum[:]) != p.SHA256 {
return nil, rev, true, fmt.Errorf("the object %s is not the one %s names (digest differs): it was replaced while read", p.KeptIn, key)
}
return data, rev, true, nil
}