Skip to content

Commit 5dd0479

Browse files
committed
firewalldb: move bbolt kvstore logic to specific file
Here, we move the abstract kvstore interfaces to the `interfaces.go` file and then rename the `kvstores.go` file to `kvstores_kvdb.go` to indicate that it houses the kvdb implementation of the kvstore interfaces. This is in preparation for adding a `kvstores_sql.go` file.
1 parent 8bc7dd3 commit 5dd0479

File tree

2 files changed

+57
-57
lines changed

2 files changed

+57
-57
lines changed

firewalldb/interface.go

+57
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,60 @@ type DBExecutor[T any] interface {
3535
View(ctx context.Context, f func(ctx context.Context,
3636
tx T) error) error
3737
}
38+
39+
// KVStores provides an Update and View method that will allow the caller to
40+
// perform atomic read and write transactions on and of the key value stores
41+
// offered the KVStoreTx.
42+
type KVStores = DBExecutor[KVStoreTx]
43+
44+
// KVStoreTx represents a database transaction that can be used for both read
45+
// and writes of the various different key value stores offered for the rule.
46+
type KVStoreTx interface {
47+
// Global returns a persisted global, rule-name indexed, kv store. A
48+
// rule with a given name will have access to this store independent of
49+
// group ID or feature.
50+
Global() KVStore
51+
52+
// Local returns a persisted local kv store for the rule. Depending on
53+
// how the implementation is initialised, this will either be under the
54+
// group ID namespace or the group ID _and_ feature name namespace.
55+
Local() KVStore
56+
57+
// GlobalTemp is similar to the Global store except that its contents
58+
// is cleared upon restart of the database. The reason persisting the
59+
// temporary store changes instead of just keeping an in-memory store is
60+
// that we can then guarantee atomicity if changes are made to both
61+
// the permanent and temporary stores.
62+
GlobalTemp() KVStore
63+
64+
// LocalTemp is similar to the Local store except that its contents is
65+
// cleared upon restart of the database. The reason persisting the
66+
// temporary store changes instead of just keeping an in-memory store is
67+
// that we can then guarantee atomicity if changes are made to both
68+
// the permanent and temporary stores.
69+
LocalTemp() KVStore
70+
}
71+
72+
// KVStore is in interface representing a key value store. It allows us to
73+
// abstract away the details of the data storage method.
74+
type KVStore interface {
75+
// Get fetches the value under the given key from the underlying kv
76+
// store. If no value is found, nil is returned.
77+
Get(ctx context.Context, key string) ([]byte, error)
78+
79+
// Set sets the given key-value pair in the underlying kv store.
80+
Set(ctx context.Context, key string, value []byte) error
81+
82+
// Del deletes the value under the given key in the underlying kv store.
83+
Del(ctx context.Context, key string) error
84+
}
85+
86+
// RulesDB can be used to initialise a new rules.KVStores.
87+
type RulesDB interface {
88+
// GetKVStores constructs a new rules.KVStores in a namespace defined
89+
// by the rule name, group ID and feature name.
90+
GetKVStores(rule string, groupID session.ID, feature string) KVStores
91+
92+
// DeleteTempKVStores deletes all temporary kv stores.
93+
DeleteTempKVStores(ctx context.Context) error
94+
}

firewalldb/kvstores.go renamed to firewalldb/kvstores_kvdb.go

-57
Original file line numberDiff line numberDiff line change
@@ -52,63 +52,6 @@ var (
5252
featureKVStoreBucketKey = []byte("feature-kv-store")
5353
)
5454

55-
// KVStores provides an Update and View method that will allow the caller to
56-
// perform atomic read and write transactions on and of the key value stores
57-
// offered the KVStoreTx.
58-
type KVStores = DBExecutor[KVStoreTx]
59-
60-
// KVStoreTx represents a database transaction that can be used for both read
61-
// and writes of the various different key value stores offered for the rule.
62-
type KVStoreTx interface {
63-
// Global returns a persisted global, rule-name indexed, kv store. A
64-
// rule with a given name will have access to this store independent of
65-
// group ID or feature.
66-
Global() KVStore
67-
68-
// Local returns a persisted local kv store for the rule. Depending on
69-
// how the implementation is initialised, this will either be under the
70-
// group ID namespace or the group ID _and_ feature name namespace.
71-
Local() KVStore
72-
73-
// GlobalTemp is similar to the Global store except that its contents
74-
// is cleared upon restart of the database. The reason persisting the
75-
// temporary store changes instead of just keeping an in-memory store is
76-
// that we can then guarantee atomicity if changes are made to both
77-
// the permanent and temporary stores.
78-
GlobalTemp() KVStore
79-
80-
// LocalTemp is similar to the Local store except that its contents is
81-
// cleared upon restart of the database. The reason persisting the
82-
// temporary store changes instead of just keeping an in-memory store is
83-
// that we can then guarantee atomicity if changes are made to both
84-
// the permanent and temporary stores.
85-
LocalTemp() KVStore
86-
}
87-
88-
// KVStore is in interface representing a key value store. It allows us to
89-
// abstract away the details of the data storage method.
90-
type KVStore interface {
91-
// Get fetches the value under the given key from the underlying kv
92-
// store. If no value is found, nil is returned.
93-
Get(ctx context.Context, key string) ([]byte, error)
94-
95-
// Set sets the given key-value pair in the underlying kv store.
96-
Set(ctx context.Context, key string, value []byte) error
97-
98-
// Del deletes the value under the given key in the underlying kv store.
99-
Del(ctx context.Context, key string) error
100-
}
101-
102-
// RulesDB can be used to initialise a new rules.KVStores.
103-
type RulesDB interface {
104-
// GetKVStores constructs a new rules.KVStores in a namespace defined
105-
// by the rule name, group ID and feature name.
106-
GetKVStores(rule string, groupID session.ID, feature string) KVStores
107-
108-
// DeleteTempKVStores deletes all temporary kv stores.
109-
DeleteTempKVStores(ctx context.Context) error
110-
}
111-
11255
// GetKVStores constructs a new rules.KVStores backed by a bbolt db.
11356
func (db *BoltDB) GetKVStores(rule string, groupID session.ID,
11457
feature string) KVStores {

0 commit comments

Comments
 (0)