Skip to content

Instantly share code, notes, and snippets.

@imsodin
Created May 13, 2020 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imsodin/196fd15ea893d0c5791ea2c9a11d519c to your computer and use it in GitHub Desktop.
Save imsodin/196fd15ea893d0c5791ea2c9a11d519c to your computer and use it in GitHub Desktop.
diff --git a/lib/db/keyer.go b/lib/db/keyer.go
index bbd2efa29..591d02230 100644
--- a/lib/db/keyer.go
+++ b/lib/db/keyer.go
@@ -325,6 +325,11 @@ func (k defaultKeyer) GenerateFolderMetaKey(key, folder []byte) (folderMetaKey,
return key, nil
}
+type hashKey interface {
+ // Hash returns whatever hash is stored in the particular key implementing this.
+ Hash() []byte
+}
+
type blockListKey []byte
func (k defaultKeyer) GenerateBlockListKey(key []byte, hash []byte) blockListKey {
@@ -334,7 +339,7 @@ func (k defaultKeyer) GenerateBlockListKey(key []byte, hash []byte) blockListKey
return key
}
-func (k blockListKey) BlocksHash() []byte {
+func (k blockListKey) Hash() []byte {
return k[keyPrefixLen:]
}
@@ -347,7 +352,7 @@ func (k defaultKeyer) GenerateVersionKey(key []byte, hash []byte) versionKey {
return key
}
-func (k versionKey) VersionHash() []byte {
+func (k versionKey) Hash() []byte {
return k[keyPrefixLen:]
}
diff --git a/lib/db/lowlevel.go b/lib/db/lowlevel.go
index 1b8e041b6..6c44c2869 100644
--- a/lib/db/lowlevel.go
+++ b/lib/db/lowlevel.go
@@ -632,16 +632,8 @@ func (db *Lowlevel) gcIndirect(ctx context.Context) error {
if db.gcKeyCount > capacity {
capacity = db.gcKeyCount
}
- blockFilter := blobloom.NewOptimized(blobloom.Config{
- Capacity: uint64(capacity),
- FPRate: indirectGCBloomFalsePositiveRate,
- MaxBits: 8 * indirectGCBloomMaxBytes,
- })
- versionFilter := blobloom.NewOptimized(blobloom.Config{
- Capacity: uint64(capacity),
- FPRate: indirectGCBloomFalsePositiveRate,
- MaxBits: 8 * indirectGCBloomMaxBytes,
- })
+ blockFilter := newBloom(capacity)
+ versionFilter := newBloom(capacity)
// Iterate the FileInfos, unmarshal the block and version hashes and
// add them to the filter.
@@ -663,10 +655,10 @@ func (db *Lowlevel) gcIndirect(ctx context.Context) error {
return err
}
if len(hashes.BlocksHash) > 0 {
- blockFilter.Add(bloomHash(hashes.BlocksHash))
+ blockFilter.Add(bloomHash(blockListKey(hashes.BlocksHash)))
}
if len(hashes.VersionHash) > 0 {
- versionFilter.Add(bloomHash(hashes.VersionHash))
+ versionFilter.Add(bloomHash(versionKey(hashes.VersionHash)))
}
}
it.Release()
@@ -691,7 +683,7 @@ func (db *Lowlevel) gcIndirect(ctx context.Context) error {
}
key := blockListKey(it.Key())
- if blockFilter.Has(bloomHash(key.BlocksHash())) {
+ if blockFilter.Has(bloomHash(key)) {
matchedBlocks++
continue
}
@@ -713,8 +705,14 @@ func (db *Lowlevel) gcIndirect(ctx context.Context) error {
}
matchedVersions := 0
for it.Next() {
+ select {
+ case <-ctx.Done():
+ return ctx.Err()
+ default:
+ }
+
key := versionKey(it.Key())
- if versionFilter.Has(bloomHash(key.VersionHash())) {
+ if versionFilter.Has(bloomHash(key)) {
matchedVersions++
continue
}
@@ -740,10 +738,18 @@ func (db *Lowlevel) gcIndirect(ctx context.Context) error {
return db.Compact()
}
+func newBloom(capacity int) *blobloom.Filter {
+ return blobloom.NewOptimized(blobloom.Config{
+ Capacity: uint64(capacity),
+ FPRate: indirectGCBloomFalsePositiveRate,
+ MaxBits: 8 * indirectGCBloomMaxBytes,
+ })
+}
+
// Hash function for the bloomfilter: first eight bytes of the SHA-256.
// Big or little-endian makes no difference, as long as we're consistent.
-func bloomHash(key []byte) uint64 {
- return binary.BigEndian.Uint64(key)
+func bloomHash(key hashKey) uint64 {
+ return binary.BigEndian.Uint64(key.Hash())
}
// CheckRepair checks folder metadata and sequences for miscellaneous errors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment