Skip to content

Commit 1dd4e73

Browse files
authored
0.4.0 (#11)
1 parent 6328aa3 commit 1dd4e73

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2941
-1513
lines changed

.travis.yml

+39-20
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,43 @@
11
language: dart
22
os:
3-
- linux
4-
sudo: false
5-
dart:
6-
- stable
7-
script:
8-
- cd hive_generator
9-
- pub get
10-
- pub run test
3+
- osx
4+
addons:
5+
chrome: stable
6+
firefox: latest
7+
jobs:
8+
include:
9+
- name: hive
10+
script:
11+
- cd hive
12+
- pub get
13+
- pub run test
1114

12-
- cd ../hive
13-
- pub get
14-
- pub run test -p "vm,chrome"
15-
16-
- dartfmt -n --set-exit-if-changed ./lib ./test ./example || travis_terminate 1
15+
- name: hive chrome
16+
script:
17+
- cd hive
18+
- pub get
19+
- pub run test -p "chrome"
20+
21+
- name: hive firefox
22+
script:
23+
- cd hive
24+
- pub get
25+
- pub run test -p "firefox"
26+
27+
- name: hive_generator
28+
script:
29+
- cd hive_generator
30+
- pub get
31+
- pub run test
32+
33+
- name: format
34+
script:
35+
- dartfmt -n --set-exit-if-changed ./lib ./test ./example || travis_terminate 1
36+
1737

18-
- pub global activate test_coverage
19-
- test_coverage --exclude "**_js_test.dart"
20-
after_success:
21-
- bash <(curl -s https://codecov.io/bash)
22-
cache:
23-
directories:
24-
- $HOME/.pub-cache
38+
- stage: coverage
39+
script:
40+
- pub global activate test_coverage
41+
- test_coverage --exclude "**_js_test.dart"
42+
after_success:
43+
- bash <(curl -s https://codecov.io/bash)

hive/CHANGELOG.md

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
## 0.4.0
2-
- Added BigInt support
2+
- Added `BigInt` support
33
- Added `compactionStrategy` parameter
4-
- Added crash recovery
4+
- Added automatic crash recovery
5+
- Added `add()` and `addAll()` for auto increment keys
6+
- Added `getAt()`, `putAt()` and `deleteAt()` for working with indices
7+
- Support for int (32 bit unsigned) keys
8+
- Non-lazy boxes now notify their listeners immediately about changes
59
- Bugfixes
6-
- More samples
710
- More tests
11+
- **Breaking:** Open boxes with `openBox()`
12+
- **Breaking:** Writing `null` is no longer equivalent to deleting a key
13+
- **Breaking:** Temporarily removed support for transactions. New API design needed. Will be coming back in a future version.
14+
- **Breaking:** Binary format changed
15+
- **Breaking:** API changes
16+
17+
*Note: This is probably the last version that breaks binary format. From version 1.0.0 onwards, there will be no breaking changes at all.*
818

919
## 0.3.0+1
1020
- Bugfix: `Hive['yourBox']` didn't work with uppercase box names

hive/README.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ In the browser you don't have to call `init()`.
4040
All of your data is stored in boxes.
4141

4242
```dart
43-
var box = await Hive.box('testBox');
43+
var box = await Hive.openBox('testBox');
4444
```
4545

4646
Just provide an `encryptionKey` to encrypt a box:
4747

4848
```dart
4949
var key = Hive.generateSecureKey();
50-
var box = await Hive.box('secureBox', encryptionKey: key);
50+
var box = await Hive.openBox('secureBox', encryptionKey: key);
5151
```
5252

5353
### Read & Write
@@ -57,9 +57,9 @@ Hive supports all primitive types, `List`, `Map`, `DateTime` and `Uint8List`. An
5757
```dart
5858
var dog = Dog(name: 'Nero', age: 4);
5959
60-
await box.put('myDog', dog);
60+
box.put('myDog', dog);
6161
62-
Dog myDog = await box.get('myDog');
62+
Dog myDog = box.get('myDog');
6363
```
6464

6565
## Hive ❤️ Flutter
@@ -70,23 +70,23 @@ Hive was written with Flutter in mind. It is a perfect fit if you need a lightwe
7070
class SettingsPage extends StatelessWidget {
7171
@override
7272
Widget build(BuildContext context) {
73-
var box = Hive['settings'];
73+
var box = Hive.box('settings');
7474
7575
return Column(
7676
children: <Widget>[
7777
SwitchListTile(
78-
value: box['darkMode'],
78+
value: box.get('darkMode'),
7979
title: Text("Dark Mode"),
80-
onChanged: (value) async {
81-
await box.put('darkMode', !box['darkMode']);
80+
onChanged: (value) {
81+
box.put('darkMode', value);
8282
setState(() {});
8383
},
8484
),
8585
SwitchListTile(
86-
value: box['pushMessages'],
86+
value: box.get('pushMessages'),
8787
title: Text('Send push messages'),
88-
onChanged: (value) async {
89-
await box.put('pushMessages', !box['pushMessages']);
88+
onChanged: (value) {
89+
box.put('pushMessages', value);
9090
setState(() {});
9191
},
9292
),

hive/analysis_options.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ analyzer:
99
include_file_not_found: ignore
1010
linter:
1111
rules:
12+
- always_declare_return_types
1213
- annotate_overrides
1314
- avoid_empty_else
1415
- avoid_function_literals_in_foreach_calls

hive/example/main.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import 'package:hive/hive.dart';
55
void main() async {
66
Hive.init(Directory.current.path);
77

8-
var box = await Hive.box('demoBox');
8+
var box = await Hive.openBox('demoBox');
99

1010
await box.put('name', 'David');
1111
await box.put('age', 27);

hive/lib/hive.dart

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ part 'src/binary/binary_writer.dart';
1515
part 'src/registry/type_registry.dart';
1616
part 'src/registry/type_adapter.dart';
1717
part 'src/box/box.dart';
18+
part 'src/box/lazy_box.dart';
1819
part 'src/hive_error.dart';
1920
part 'src/hive.dart';
2021

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
1-
import 'package:hive/src/box/box_impl.dart';
21
import 'package:hive/src/binary/frame.dart';
2+
import 'package:hive/src/box/keystore.dart';
3+
4+
export 'package:hive/src/backend/storage_backend_stub.dart'
5+
if (dart.library.io) 'package:hive/src/backend/storage_backend_vm.dart'
6+
if (dart.library.html) 'package:hive/src/backend/storage_backend_js.dart';
37

48
abstract class StorageBackend {
59
String get path;
610

7-
Future<int> initialize(Map<String, BoxEntry> entries, bool lazy);
11+
Future<int> initialize(
12+
Map<dynamic, BoxEntry> entries, bool lazy, bool crashRecovery);
813

9-
Future<dynamic> readValue(String key, int offset, int length);
14+
Future<dynamic> readValue(dynamic key, int offset, int length);
1015

11-
Future<Map<String, dynamic>> readAll(Iterable<String> keys);
16+
Future<Map<dynamic, dynamic>> readAll();
1217

13-
Future<BoxEntry> writeFrame(Frame frame, bool lazy);
18+
Future<void> writeFrame(Frame frame, BoxEntry entry);
1419

15-
Future<List<BoxEntry>> writeFrames(List<Frame> frames, bool lazy);
20+
Future<void> writeFrames(List<Frame> frames, Iterable<BoxEntry> entries);
1621

17-
Future<Map<String, BoxEntry>> compact(Map<String, BoxEntry> entries);
22+
Future<Map<dynamic, BoxEntry>> compact(Map<dynamic, BoxEntry> entries);
1823

19-
Future clear();
24+
Future<void> clear();
2025

21-
Future close();
26+
Future<void> close();
2227

23-
Future deleteFromDisk();
28+
Future<void> deleteFromDisk();
2429
}

hive/lib/src/backend/storage_backend_js.dart

+26-19
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ import 'dart:typed_data';
66
import 'package:hive/hive.dart';
77
import 'package:hive/src/backend/storage_backend.dart';
88
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';
1010
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';
1114
import 'package:hive/src/crypto_helper.dart';
1215
import 'package:hive/src/hive_impl.dart';
1316
import 'package:meta/meta.dart';
1417

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 {
1620
var db = await window.indexedDB.open(name, version: 1, onUpgradeNeeded: (e) {
1721
var db = e.target.result as Database;
1822
if (!db.objectStoreNames.contains('box')) {
@@ -26,7 +30,12 @@ Future<BoxImpl> openBox(HiveImpl hive, String name, BoxOptions options) async {
2630
}
2731

2832
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+
}
3039
backend._registry = box;
3140

3241
await box.initialize();
@@ -47,7 +56,8 @@ class StorageBackendJs extends StorageBackend {
4756

4857
@visibleForTesting
4958
dynamic encodeValue(dynamic value) {
50-
var noEncodingNeeded = value is num ||
59+
var noEncodingNeeded = value == null ||
60+
value is num ||
5161
value is bool ||
5262
value is String ||
5363
(value is List<num> && value is! Uint8List) ||
@@ -105,7 +115,8 @@ class StorageBackendJs extends StorageBackend {
105115
}
106116

107117
@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 {
109120
var keys = await getKeys();
110121
if (!lazy) {
111122
var values = await getValues();
@@ -114,54 +125,50 @@ class StorageBackendJs extends StorageBackend {
114125
}
115126
} else {
116127
for (var i = 0; i < keys.length; i++) {
117-
entries[keys[i]] = const BoxEntry(null, null, null);
128+
entries[keys[i]] = BoxEntry(null);
118129
}
119130
}
120131

121132
return 0;
122133
}
123134

124135
@override
125-
Future<dynamic> readValue(String key, int offset, int length) async {
136+
Future<dynamic> readValue(dynamic key, int offset, int length) async {
126137
var value = await getStore(false).getObject(key);
127138
return decodeValue(value);
128139
}
129140

130141
@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);
133146
}
134147

135148
@override
136-
Future<BoxEntry> writeFrame(Frame frame, bool lazy) async {
149+
Future<void> writeFrame(Frame frame, BoxEntry entry) async {
137150
if (frame.deleted) {
138151
await getStore(true).delete(frame.key);
139-
return null;
140152
} else {
141153
await getStore(true).put(encodeValue(frame.value), frame.key);
142-
return BoxEntry(!lazy ? frame.value : null, null, null);
143154
}
144155
}
145156

146157
@override
147-
Future<List<BoxEntry>> writeFrames(List<Frame> frames, bool lazy) async {
158+
Future<void> writeFrames(
159+
List<Frame> frames, Iterable<BoxEntry> entries) async {
148160
var store = getStore(true);
149-
var entries = List<BoxEntry>(frames.length);
150-
var i = 0;
151161
for (var frame in frames) {
152162
if (frame.deleted) {
153163
await store.delete(frame.key);
154-
entries[i++] = null;
155164
} else {
156165
await store.put(encodeValue(frame.value), frame.key);
157-
entries[i++] = BoxEntry(!lazy ? frame.value : null, null, null);
158166
}
159167
}
160-
return entries;
161168
}
162169

163170
@override
164-
Future<Map<String, BoxEntry>> compact(Map<String, BoxEntry> entries) {
171+
Future<Map<dynamic, BoxEntry>> compact(Map<dynamic, BoxEntry> entries) {
165172
return Future.value(entries);
166173
}
167174

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import 'package:hive/src/box/box_impl.dart';
1+
import 'package:hive/hive.dart';
22
import 'package:hive/src/box/box_options.dart';
33
import 'package:hive/src/hive_impl.dart';
44

5-
Future<BoxImpl> openBox(HiveImpl hive, String name, BoxOptions options) =>
5+
Future<Box> openBoxInternal(
6+
HiveImpl hive, String name, bool lazy, BoxOptions options) =>
67
throw UnsupportedError('Cannot create a box without dart:html or dart:io.');

0 commit comments

Comments
 (0)