@@ -6,13 +6,17 @@ import 'dart:typed_data';
6
6
import 'package:hive/hive.dart' ;
7
7
import 'package:hive/src/backend/storage_backend.dart' ;
8
8
import 'package:hive/src/binary/frame.dart' ;
9
- import 'package:hive/src/box/box_options .dart' ;
9
+ import 'package:hive/src/box/box_base .dart' ;
10
10
import 'package:hive/src/box/box_impl.dart' ;
11
+ import 'package:hive/src/box/box_options.dart' ;
12
+ import 'package:hive/src/box/keystore.dart' ;
13
+ import 'package:hive/src/box/lazy_box_impl.dart' ;
11
14
import 'package:hive/src/crypto_helper.dart' ;
12
15
import 'package:hive/src/hive_impl.dart' ;
13
16
import 'package:meta/meta.dart' ;
14
17
15
- Future <BoxImpl > openBox (HiveImpl hive, String name, BoxOptions options) async {
18
+ Future <Box > openBoxInternal (
19
+ HiveImpl hive, String name, bool lazy, BoxOptions options) async {
16
20
var db = await window.indexedDB.open (name, version: 1 , onUpgradeNeeded: (e) {
17
21
var db = e.target.result as Database ;
18
22
if (! db.objectStoreNames.contains ('box' )) {
@@ -26,7 +30,12 @@ Future<BoxImpl> openBox(HiveImpl hive, String name, BoxOptions options) async {
26
30
}
27
31
28
32
var backend = StorageBackendJs (db, crypto);
29
- var box = BoxImpl (hive, name, options, backend);
33
+ BoxBase box;
34
+ if (lazy) {
35
+ box = LazyBoxImpl (hive, name, options, backend);
36
+ } else {
37
+ box = BoxImpl (hive, name, options, backend);
38
+ }
30
39
backend._registry = box;
31
40
32
41
await box.initialize ();
@@ -47,7 +56,8 @@ class StorageBackendJs extends StorageBackend {
47
56
48
57
@visibleForTesting
49
58
dynamic encodeValue (dynamic value) {
50
- var noEncodingNeeded = value is num ||
59
+ var noEncodingNeeded = value == null ||
60
+ value is num ||
51
61
value is bool ||
52
62
value is String ||
53
63
(value is List <num > && value is ! Uint8List ) ||
@@ -105,7 +115,8 @@ class StorageBackendJs extends StorageBackend {
105
115
}
106
116
107
117
@override
108
- Future <int > initialize (Map <String , BoxEntry > entries, bool lazy) async {
118
+ Future <int > initialize (
119
+ Map <dynamic , BoxEntry > entries, bool lazy, bool crashRecovery) async {
109
120
var keys = await getKeys ();
110
121
if (! lazy) {
111
122
var values = await getValues ();
@@ -114,54 +125,50 @@ class StorageBackendJs extends StorageBackend {
114
125
}
115
126
} else {
116
127
for (var i = 0 ; i < keys.length; i++ ) {
117
- entries[keys[i]] = const BoxEntry (null , null , null );
128
+ entries[keys[i]] = BoxEntry (null );
118
129
}
119
130
}
120
131
121
132
return 0 ;
122
133
}
123
134
124
135
@override
125
- Future <dynamic > readValue (String key, int offset, int length) async {
136
+ Future <dynamic > readValue (dynamic key, int offset, int length) async {
126
137
var value = await getStore (false ).getObject (key);
127
138
return decodeValue (value);
128
139
}
129
140
130
141
@override
131
- Future <Map <String , dynamic >> readAll (Iterable <String > keys) async {
132
- return Map <String , dynamic >.fromIterables (keys, await getValues ());
142
+ Future <Map <dynamic , dynamic >> readAll () async {
143
+ var keys = await getKeys ();
144
+ var values = await getValues ();
145
+ return Map <dynamic , dynamic >.fromIterables (keys, values);
133
146
}
134
147
135
148
@override
136
- Future <BoxEntry > writeFrame (Frame frame, bool lazy ) async {
149
+ Future <void > writeFrame (Frame frame, BoxEntry entry ) async {
137
150
if (frame.deleted) {
138
151
await getStore (true ).delete (frame.key);
139
- return null ;
140
152
} else {
141
153
await getStore (true ).put (encodeValue (frame.value), frame.key);
142
- return BoxEntry (! lazy ? frame.value : null , null , null );
143
154
}
144
155
}
145
156
146
157
@override
147
- Future <List <BoxEntry >> writeFrames (List <Frame > frames, bool lazy) async {
158
+ Future <void > writeFrames (
159
+ List <Frame > frames, Iterable <BoxEntry > entries) async {
148
160
var store = getStore (true );
149
- var entries = List <BoxEntry >(frames.length);
150
- var i = 0 ;
151
161
for (var frame in frames) {
152
162
if (frame.deleted) {
153
163
await store.delete (frame.key);
154
- entries[i++ ] = null ;
155
164
} else {
156
165
await store.put (encodeValue (frame.value), frame.key);
157
- entries[i++ ] = BoxEntry (! lazy ? frame.value : null , null , null );
158
166
}
159
167
}
160
- return entries;
161
168
}
162
169
163
170
@override
164
- Future <Map <String , BoxEntry >> compact (Map <String , BoxEntry > entries) {
171
+ Future <Map <dynamic , BoxEntry >> compact (Map <dynamic , BoxEntry > entries) {
165
172
return Future .value (entries);
166
173
}
167
174
0 commit comments