Skip to content

Commit c0d6775

Browse files
committed
filterdb: add FilterData type
Add a new FilterData type that encapsulates the filter, blockhash and type associated with a filter. This is done in order to prepare for a commit that will enable batch writes of these filters.
1 parent cdd05ae commit c0d6775

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

filterdb/db.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,27 @@ const (
3434
RegularFilter FilterType = iota
3535
)
3636

37+
// FilterData holds all the info about a filter required to store it.
38+
type FilterData struct {
39+
// Filter is the actual filter to be stored.
40+
Filter *gcs.Filter
41+
42+
// BlockHash is the block header hash of the block associated with the
43+
// Filter.
44+
BlockHash *chainhash.Hash
45+
46+
// Type is the filter type.
47+
Type FilterType
48+
}
49+
3750
// FilterDatabase is an interface which represents an object that is capable of
3851
// storing and retrieving filters according to their corresponding block hash
3952
// and also their filter type.
4053
//
4154
// TODO(roasbeef): similar interface for headerfs?
4255
type FilterDatabase interface {
43-
// PutFilter stores a filter with the given hash and type to persistent
44-
// storage.
45-
PutFilter(*chainhash.Hash, *gcs.Filter, FilterType) error
56+
// PutFilter stores a filter to persistent storage.
57+
PutFilter(*FilterData) error
4658

4759
// FetchFilter attempts to fetch a filter with the given hash and type
4860
// from persistent storage. In the case that a filter matching the
@@ -155,30 +167,22 @@ func putFilter(bucket walletdb.ReadWriteBucket, hash *chainhash.Hash,
155167
// storage.
156168
//
157169
// NOTE: This method is a part of the FilterDatabase interface.
158-
func (f *FilterStore) PutFilter(hash *chainhash.Hash,
159-
filter *gcs.Filter, fType FilterType) error {
160-
170+
func (f *FilterStore) PutFilter(filterData *FilterData) error {
161171
return walletdb.Update(f.db, func(tx walletdb.ReadWriteTx) error {
162172
filters := tx.ReadWriteBucket(filterBucket)
163173

164174
var targetBucket walletdb.ReadWriteBucket
165-
switch fType {
175+
switch filterData.Type {
166176
case RegularFilter:
167177
targetBucket = filters.NestedReadWriteBucket(regBucket)
168178
default:
169-
return fmt.Errorf("unknown filter type: %v", fType)
179+
return fmt.Errorf("unknown filter type: %v",
180+
filterData.Type)
170181
}
171182

172-
if filter == nil {
173-
return targetBucket.Put(hash[:], nil)
174-
}
175-
176-
bytes, err := filter.NBytes()
177-
if err != nil {
178-
return err
179-
}
180-
181-
return targetBucket.Put(hash[:], bytes)
183+
return putFilter(
184+
targetBucket, filterData.BlockHash, filterData.Filter,
185+
)
182186
})
183187
}
184188

filterdb/db_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,13 @@ func TestFilterStorage(t *testing.T) {
8686
// type for the block hash generate above.
8787
regFilter := genRandFilter(t, 100)
8888

89-
err = database.PutFilter(&randHash, regFilter, RegularFilter)
89+
filter := &FilterData{
90+
Filter: regFilter,
91+
BlockHash: &randHash,
92+
Type: RegularFilter,
93+
}
94+
95+
err = database.PutFilter(filter)
9096
require.NoError(t, err)
9197

9298
// With the filter stored, we should be able to retrieve the filter

query.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -534,9 +534,13 @@ func (q *cfiltersQuery) handleResponse(req, resp wire.Message,
534534
}
535535

536536
if q.cs.persistToDisk {
537-
err = q.cs.FilterDB.PutFilter(
538-
&response.BlockHash, filter, dbFilterType,
539-
)
537+
filterData := &filterdb.FilterData{
538+
Filter: filter,
539+
BlockHash: &response.BlockHash,
540+
Type: dbFilterType,
541+
}
542+
543+
err = q.cs.FilterDB.PutFilter(filterData)
540544
if err != nil {
541545
log.Warnf("Couldn't write filter to filterDB: %v", err)
542546
}

0 commit comments

Comments
 (0)