prysm-pulse/beacon-chain/db/kv/utils.go
Raul Jordan 6ec9d7e6e2
Utilize Indices for Key Lookup and Filtering Attestations in DB (#3202)
* begin indices approach

* use shard bucket

* continue the indices approach

* eliminate the filter checkers in favor of the single loop of root lookups

* elim extraneous println statement

* continue the indices approach

* intersection for multiple filter types works, but is complex, verbose, and nearly unreadable

* remove unused code

* table drive tests for byte slice intersections

* include all table driven tests

* gazelle imports

* better abstractions

* better comments

* variadic approach working

* transform to variadic

* comments

* comments

* separate bucket for indices for faster range scans

* attestation key as hash tree root of data and different indices buckets

* test pass

* default behavior without filter

* appropriate filter criterion errors if criterion does not apply to type

* better abstractions and prune keys on deletion

* better naming

* fix build

* fix build

* rem extraneous code
2019-08-15 19:57:43 -05:00

69 lines
2.2 KiB
Go

package kv
import (
"bytes"
"github.com/boltdb/bolt"
)
// lookupValuesForIndices takes in a list of indices and looks up
// their corresponding values in the DB, returning a list of
// roots which can then be used for batch lookups of their corresponding
// objects from the DB. For example, if we are fetching
// attestations and we have an index `[]byte("shard-5")`,
// we might find roots `0x23` and `0x45` stored under that index. We can then
// do a batch read for attestations corresponding to those roots.
func lookupValuesForIndices(indices [][]byte, bkt *bolt.Bucket) [][][]byte {
values := make([][][]byte, 0)
for _, k := range indices {
roots := bkt.Get(k)
splitRoots := make([][]byte, 0)
for i := 0; i < len(roots); i += 32 {
splitRoots = append(splitRoots, roots[i:i+32])
}
values = append(values, splitRoots)
}
return values
}
// updateValueForIndices updates the value for each index by appending it to the previous
// values stored at said index. Typically, indices are roots of data that can then
// be used for reads or batch reads from the DB.
func updateValueForIndices(indices [][]byte, root []byte, bkt *bolt.Bucket) error {
for _, idx := range indices {
valuesAtIndex := bkt.Get(idx)
if valuesAtIndex == nil {
if err := bkt.Put(idx, root); err != nil {
return err
}
} else {
if err := bkt.Put(idx, append(valuesAtIndex, root...)); err != nil {
return err
}
}
}
return nil
}
// deleteValueForIndices clears a root stored at each index.
func deleteValueForIndices(indices [][]byte, root []byte, bkt *bolt.Bucket) error {
for _, idx := range indices {
valuesAtIndex := bkt.Get(idx)
if valuesAtIndex != nil {
start := bytes.Index(valuesAtIndex, root)
// if the root was not found inside the values at index slice, we continue.
if start == -1 {
continue
}
// We clear out the root from the values at index slice. For example,
// If we had [0x32, 0x33, 0x45] and we wanted to clear out 0x33, the code below
// updates the slice to [0x32, 0x45].
valuesAtIndex = append(valuesAtIndex[:start], valuesAtIndex[start+len(root):]...)
if err := bkt.Put(idx, valuesAtIndex); err != nil {
return err
}
}
}
return nil
}