mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-11-10 06:07:16 +01:00
etcd: add kvdb.Prefetch
This commit extends the kvdb interface in a backwards compatible way such that we'll be able to prefetch all keys in a bucket in one go reducing the number of roundtrips.
This commit is contained in:
@@ -371,3 +371,37 @@ func (b *readWriteBucket) Sequence() uint64 {
|
||||
|
||||
return num
|
||||
}
|
||||
|
||||
func flattenMap(m map[string]struct{}) []string {
|
||||
result := make([]string, len(m))
|
||||
i := 0
|
||||
|
||||
for key := range m {
|
||||
result[i] = key
|
||||
i++
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// Prefetch will prefetch all keys in the passed paths as well as all bucket
|
||||
// keys along the paths.
|
||||
func (b *readWriteBucket) Prefetch(paths ...[]string) {
|
||||
keys := make(map[string]struct{})
|
||||
ranges := make(map[string]struct{})
|
||||
|
||||
for _, path := range paths {
|
||||
parent := b.id
|
||||
for _, bucket := range path {
|
||||
bucketKey := makeBucketKey(parent, []byte(bucket))
|
||||
keys[string(bucketKey[:])] = struct{}{}
|
||||
|
||||
id := makeBucketID(bucketKey)
|
||||
parent = id[:]
|
||||
}
|
||||
|
||||
ranges[string(parent)] = struct{}{}
|
||||
}
|
||||
|
||||
b.tx.stm.Prefetch(flattenMap(keys), flattenMap(ranges))
|
||||
}
|
||||
|
||||
@@ -41,6 +41,13 @@ func rootBucket(tx *readWriteTx) *readWriteBucket {
|
||||
return newReadWriteBucket(tx, tx.rootBucketID[:], tx.rootBucketID[:])
|
||||
}
|
||||
|
||||
// RootBucket will return a handle to the root bucket. This is not a real handle
|
||||
// but just a wrapper around the root bucket ID to allow derivation of child
|
||||
// keys.
|
||||
func (tx *readWriteTx) RootBucket() walletdb.ReadBucket {
|
||||
return rootBucket(tx)
|
||||
}
|
||||
|
||||
// ReadBucket opens the root bucket for read only access. If the bucket
|
||||
// described by the key does not exist, nil is returned.
|
||||
func (tx *readWriteTx) ReadBucket(key []byte) walletdb.ReadBucket {
|
||||
|
||||
Reference in New Issue
Block a user