mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-11-20 11:08:24 +01:00
negentropy: refactor for allowing different types of storage.
This commit is contained in:
77
nip77/negentropy/storage/vector/vector.go
Normal file
77
nip77/negentropy/storage/vector/vector.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package vector
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"iter"
|
||||
"slices"
|
||||
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
"github.com/nbd-wtf/go-nostr/nip77/negentropy"
|
||||
)
|
||||
|
||||
type Vector struct {
|
||||
items []negentropy.Item
|
||||
sealed bool
|
||||
}
|
||||
|
||||
func New() *Vector {
|
||||
return &Vector{
|
||||
items: make([]negentropy.Item, 0, 30),
|
||||
}
|
||||
}
|
||||
|
||||
func (v *Vector) Insert(createdAt nostr.Timestamp, id string) error {
|
||||
if len(id)/2 != 32 {
|
||||
return fmt.Errorf("bad id size for added item: expected %d, got %d", 32, len(id)/2)
|
||||
}
|
||||
|
||||
item := negentropy.Item{Timestamp: createdAt, ID: id}
|
||||
v.items = append(v.items, item)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *Vector) Size() int { return len(v.items) }
|
||||
|
||||
func (v *Vector) Seal() {
|
||||
if v.sealed {
|
||||
panic("trying to seal an already sealed vector")
|
||||
}
|
||||
v.sealed = true
|
||||
slices.SortFunc(v.items, negentropy.ItemCompare)
|
||||
}
|
||||
|
||||
func (v *Vector) GetBound(idx int) negentropy.Bound {
|
||||
if idx < len(v.items) {
|
||||
return negentropy.Bound{Item: v.items[idx]}
|
||||
}
|
||||
return negentropy.InfiniteBound
|
||||
}
|
||||
|
||||
func (v *Vector) Range(begin, end int) iter.Seq2[int, negentropy.Item] {
|
||||
return func(yield func(int, negentropy.Item) bool) {
|
||||
for i := begin; i < end; i++ {
|
||||
if !yield(i, v.items[i]) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (v *Vector) FindLowerBound(begin, end int, bound negentropy.Bound) int {
|
||||
idx, _ := slices.BinarySearchFunc(v.items[begin:end], bound.Item, negentropy.ItemCompare)
|
||||
return begin + idx
|
||||
}
|
||||
|
||||
func (v *Vector) Fingerprint(begin, end int) [negentropy.FingerprintSize]byte {
|
||||
var out negentropy.Accumulator
|
||||
out.SetToZero()
|
||||
|
||||
tmp := make([]byte, 32)
|
||||
for _, item := range v.Range(begin, end) {
|
||||
hex.Decode(tmp, []byte(item.ID))
|
||||
out.AddBytes(tmp)
|
||||
}
|
||||
|
||||
return out.GetFingerprint(end - begin)
|
||||
}
|
||||
Reference in New Issue
Block a user