@@ -4,8 +4,12 @@ import 'dart:convert';
4
4
import 'package:crypto/crypto.dart' ;
5
5
import 'package:hive/hive.dart' ;
6
6
7
- class BoxCollection {
7
+ import 'box_collection_stub.dart' as implementation;
8
+
9
+ class BoxCollection implements implementation.BoxCollection {
10
+ @override
8
11
final String name;
12
+ @override
9
13
final Set <String > boxNames;
10
14
HiveCipher ? _cipher;
11
15
@@ -34,9 +38,11 @@ class BoxCollection {
34
38
return collection;
35
39
}
36
40
41
+ @override
37
42
Future <CollectionBox <V >> openBox <V >(String name,
38
43
{bool preload = false ,
39
- CollectionBox <V > Function (String , BoxCollection )? boxCreator}) async {
44
+ implementation.CollectionBox <V > Function (String , BoxCollection )?
45
+ boxCreator}) async {
40
46
if (! boxNames.contains (name)) {
41
47
throw Exception (
42
48
'Box with name $name is not in the known box names of this collection.' );
@@ -46,17 +52,22 @@ class BoxCollection {
46
52
return _openBoxes[i] as CollectionBox <V >;
47
53
}
48
54
final boxIdentifier = '${this .name }_$name ' ;
49
- final box = boxCreator? .call (boxIdentifier, this ) ??
55
+ final box = boxCreator? .call (boxIdentifier, this ) as CollectionBox < V > ? ??
50
56
CollectionBox <V >(boxIdentifier, this );
51
57
if (preload) {
52
- box._cachedBox = await Hive .openBox (box.name, encryptionCipher: _cipher);
58
+ box._cachedBox = await Hive .openBox (
59
+ box.name,
60
+ encryptionCipher: _cipher,
61
+ collection: name,
62
+ );
53
63
}
54
64
_openBoxes.add (box);
55
65
return box;
56
66
}
57
67
58
68
final List <CollectionBox > _openBoxes = [];
59
69
70
+ @override
60
71
Future <void > transaction (
61
72
Future <void > Function () action, {
62
73
List <String >? boxNames,
@@ -80,20 +91,24 @@ class BoxCollection {
80
91
});
81
92
}
82
93
94
+ @override
83
95
void close () {
84
96
for (final box in _openBoxes) {
85
97
box._cachedBox? .close ();
86
98
}
87
99
}
88
100
101
+ @override
89
102
Future <void > deleteFromDisk () => Future .wait (
90
103
boxNames.map (Hive .deleteBoxFromDisk),
91
104
);
92
105
}
93
106
94
107
/// represents a [Box] being part of a [BoxCollection]
95
- class CollectionBox <V > {
108
+ class CollectionBox <V > implements implementation.CollectionBox <V > {
109
+ @override
96
110
final String name;
111
+ @override
97
112
final BoxCollection boxCollection;
98
113
99
114
static final transactionBoxes = < Zone , Set <String >> {};
@@ -104,6 +119,7 @@ class CollectionBox<V> {
104
119
return _cachedBox ?? = await Hive .openLazyBox <V >(
105
120
name,
106
121
encryptionCipher: boxCollection._cipher,
122
+ collection: boxCollection.name,
107
123
);
108
124
}
109
125
@@ -120,6 +136,7 @@ class CollectionBox<V> {
120
136
}
121
137
}
122
138
139
+ @override
123
140
Future <List <String >> getAllKeys () async {
124
141
final box = await _getBox ();
125
142
return box.keys
@@ -134,6 +151,7 @@ class CollectionBox<V> {
134
151
.toList ();
135
152
}
136
153
154
+ @override
137
155
Future <Map <String , V >> getAllValues () async {
138
156
final box = await _getBox ();
139
157
final keys = box.keys.toList ();
@@ -149,13 +167,15 @@ class CollectionBox<V> {
149
167
.map ((k, v) => MapEntry (Uri .decodeComponent (k.toString ()), v as V ));
150
168
}
151
169
170
+ @override
152
171
Future <V ?> get (String key) async {
153
172
key = _toHiveKey (key);
154
173
final box = await _getBox ();
155
174
if (box is LazyBox ) return await box.get (key) as V ? ;
156
175
return (box as Box ).get (key) as V ? ;
157
176
}
158
177
178
+ @override
159
179
Future <List <V ?>> getAll (
160
180
List <String > keys,
161
181
) async {
@@ -172,7 +192,8 @@ class CollectionBox<V> {
172
192
return values;
173
193
}
174
194
175
- Future <void > put (String key, V val) async {
195
+ @override
196
+ Future <void > put (String key, V val, [Object ? transaction]) async {
176
197
if (val == null ) {
177
198
return delete (key);
178
199
}
@@ -181,25 +202,29 @@ class CollectionBox<V> {
181
202
await _flushOrMark ();
182
203
}
183
204
205
+ @override
184
206
Future <void > delete (String key) async {
185
207
final box = await _getBox ();
186
208
await box.delete (_toHiveKey (key));
187
209
await _flushOrMark ();
188
210
}
189
211
212
+ @override
190
213
Future <void > deleteAll (List <String > keys) async {
191
214
final hiveKeys = keys.map (_toHiveKey);
192
215
final box = await _getBox ();
193
216
await box.deleteAll (hiveKeys);
194
217
await _flushOrMark ();
195
218
}
196
219
220
+ @override
197
221
Future <void > clear () async {
198
222
final box = await _getBox ();
199
223
await box.deleteAll (box.keys);
200
224
await _flushOrMark ();
201
225
}
202
226
227
+ @override
203
228
Future <void > flush () async {
204
229
final box = await _getBox ();
205
230
// we do *not* await the flushing here. That makes it so that we can execute
0 commit comments