2019-08-16 00:57:43 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-06-23 13:51:00 +00:00
|
|
|
"context"
|
2019-08-16 00:57:43 +00:00
|
|
|
|
2020-03-24 20:00:54 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
2020-06-23 13:51:00 +00:00
|
|
|
"go.opencensus.io/trace"
|
2019-08-16 00:57:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2019-08-20 00:34:53 +00:00
|
|
|
// attestations and we have an index `[]byte("5")` under the shard indices bucket,
|
2019-08-16 00:57:43 +00:00
|
|
|
// we might find roots `0x23` and `0x45` stored under that index. We can then
|
|
|
|
// do a batch read for attestations corresponding to those roots.
|
2020-06-23 13:51:00 +00:00
|
|
|
func lookupValuesForIndices(ctx context.Context, indicesByBucket map[string][]byte, tx *bolt.Tx) [][][]byte {
|
2021-12-07 17:52:39 +00:00
|
|
|
_, span := trace.StartSpan(ctx, "BeaconDB.lookupValuesForIndices")
|
2020-06-23 13:51:00 +00:00
|
|
|
defer span.End()
|
2020-07-09 15:50:58 +00:00
|
|
|
values := make([][][]byte, 0, len(indicesByBucket))
|
2019-08-20 15:09:10 +00:00
|
|
|
for k, v := range indicesByBucket {
|
|
|
|
bkt := tx.Bucket([]byte(k))
|
|
|
|
roots := bkt.Get(v)
|
2020-07-09 15:50:58 +00:00
|
|
|
splitRoots := make([][]byte, 0, len(roots)/32)
|
2019-08-16 00:57:43 +00:00
|
|
|
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.
|
2020-06-23 13:51:00 +00:00
|
|
|
func updateValueForIndices(ctx context.Context, indicesByBucket map[string][]byte, root []byte, tx *bolt.Tx) error {
|
2021-12-07 17:52:39 +00:00
|
|
|
_, span := trace.StartSpan(ctx, "BeaconDB.updateValueForIndices")
|
2020-06-23 13:51:00 +00:00
|
|
|
defer span.End()
|
2019-08-20 15:09:10 +00:00
|
|
|
for k, idx := range indicesByBucket {
|
|
|
|
bkt := tx.Bucket([]byte(k))
|
2019-08-16 00:57:43 +00:00
|
|
|
valuesAtIndex := bkt.Get(idx)
|
|
|
|
if valuesAtIndex == nil {
|
|
|
|
if err := bkt.Put(idx, root); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2019-11-26 19:26:35 +00:00
|
|
|
// Do not save duplication in indices bucket
|
|
|
|
for i := 0; i < len(valuesAtIndex); i += 32 {
|
|
|
|
if bytes.Equal(valuesAtIndex[i:i+32], root) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2019-08-16 00:57:43 +00:00
|
|
|
if err := bkt.Put(idx, append(valuesAtIndex, root...)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleteValueForIndices clears a root stored at each index.
|
2020-06-23 13:51:00 +00:00
|
|
|
func deleteValueForIndices(ctx context.Context, indicesByBucket map[string][]byte, root []byte, tx *bolt.Tx) error {
|
2021-12-07 17:52:39 +00:00
|
|
|
_, span := trace.StartSpan(ctx, "BeaconDB.deleteValueForIndices")
|
2020-06-23 13:51:00 +00:00
|
|
|
defer span.End()
|
2019-08-20 15:09:10 +00:00
|
|
|
for k, idx := range indicesByBucket {
|
|
|
|
bkt := tx.Bucket([]byte(k))
|
2019-08-16 00:57:43 +00:00
|
|
|
valuesAtIndex := bkt.Get(idx)
|
|
|
|
if valuesAtIndex != nil {
|
|
|
|
start := bytes.Index(valuesAtIndex, root)
|
2020-06-01 19:00:47 +00:00
|
|
|
// If the root was not found inside the values at index slice, we continue.
|
|
|
|
// Root must be correctly aligned to avoid matching to subsequences of adjacent values.
|
|
|
|
if start == -1 || start%len(root) != 0 {
|
2019-08-16 00:57:43 +00:00
|
|
|
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].
|
2019-08-26 14:00:40 +00:00
|
|
|
|
|
|
|
valuesStart := make([]byte, len(valuesAtIndex[:start]))
|
|
|
|
copy(valuesStart, valuesAtIndex[:start])
|
|
|
|
|
|
|
|
valuesEnd := make([]byte, len(valuesAtIndex[start+len(root):]))
|
|
|
|
copy(valuesEnd, valuesAtIndex[start+len(root):])
|
|
|
|
|
|
|
|
valuesAtIndex = append(valuesStart, valuesEnd...)
|
2020-07-18 18:05:04 +00:00
|
|
|
|
|
|
|
// If this removes the last value, delete the whole key/value entry.
|
|
|
|
if len(valuesAtIndex) == 0 {
|
|
|
|
if err := bkt.Delete(idx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2019-08-16 00:57:43 +00:00
|
|
|
if err := bkt.Put(idx, valuesAtIndex); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|