Skip to content

Commit d79e86a

Browse files
authored
0.4.0 (#11)
1 parent 5b23e28 commit d79e86a

Some content is hidden

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

68 files changed

+3030
-1596
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)

docs/_sidebar.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
* [Encrypted box](encrypted_box.md)
55

66
* Using boxes
7-
* [Read](read.md)
87
* [Write](write.md)
8+
* [Read](read.md)
99
* [Delete](delete.md)
1010
* [Watch changes](watch_changes.md)
11-
* [Transactions](transactions.md)
1211
* [Compaction](compaction.md)
1312

1413
* TypeAdapters

docs/auto_increment.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Auto increment & indices
2+
3+
We already know that Hive supports integer keys. You can use auto increment keys if you like. This is very useful for storing and accessing multiple objects. You can use a Box like a list.
4+
5+
```dart
6+
var friends = Hive.box('friends');
7+
8+
friends.add('Lisa'); //index 0, key 0
9+
10+
friends.add('Dave'); //index 1, key 1
11+
12+
friends.put(123, 'Marco'); //index 2, key 123
13+
14+
print(friends.values); // Lisa, Dave, Marco
15+
```
16+
17+
There are also `getAt()`, `putAt()` and `deleteAt()` methods to access or change values by their index.
18+
19+
It is important to understand the difference of integer keys and indices.
20+
21+
```dart
22+
friends.putAt(2, 'Ben');
23+
24+
frinds.put(123, 'Ben');
25+
```
26+
27+
Both of these operations do the same thing. They replace the `Marco` with `Ben`. `putAt()` uses the index (in this case `2`), `put()` uses the key (in this case `123`).
28+
29+
This also works with String keys.
30+
31+
?> Even if you only use auto increment keys, you should not rely on keys and indices being the same.

docs/boxes.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Boxes can also be encrypted to store sensitive data.
1010
Before a box can be used, you have to open it:
1111

1212
```dart
13-
var box = await Hive.box('testBox');
13+
var box = await Hive.openBox('testBox');
1414
```
1515

1616
If the box is already open, it will be returned immediately. All supplied parameters will be ignored.
@@ -22,22 +22,21 @@ Once you obtained a box instance, you can read, write and delete entries.
2222
Hive stores a reference to all open boxes. If you want to get an already opened box, you can use
2323

2424
```dart
25-
var box = Hive['myBox'];
25+
var box = Hive.box('myBox');
2626
```
2727

28-
This is especially useful for Flutter apps because you don't need a `FutureBuilder` to get a box.
28+
This is especially useful for Flutter apps because you don't need to pass the box between widgets.
2929

3030
## Close box
3131

3232
If you don't need a box again, you should close it. All cached keys and values of the box will be dropped from memory and the box file will be closed after all active read and write operations finished.
3333

34-
?> It is not recommended to open and close the same box frequently because this leads to unnecessary disk access and takes time. If you need a box again in the future, just leave it open.
34+
?> It is perfectly fine to leave a box open for the runtime of the app. If you need a box again in the future, just leave it open.
3535

3636
```dart
37-
var box = await Hive.open('myBox');
37+
var box = await Hive.openBox('myBox');
3838
await box.put('hello', 'world');
3939
await box.close();
4040
```
4141

4242
Before your application exits, you should call `Hive.close()` to close all open boxes.
43-

docs/compaction.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ Hive is an append-only data store. When you change or delete a value, the change
55
It may benefit the start time of your app if you induce compaction manually before you close a box.
66

77
```dart
8+
var box = Hive.box('myBox');
89
await box.compact();
910
await box.close();
1011
```
1112

1213
You can specify your own rules for automatic compaction. Just pass the `compactionStrategy` parameter when you open a box:
1314

1415
```dart
15-
var box = await Hive.box('myBox', compactionStrategy: (entries, deletedEntries) {
16+
var box = await Hive.openBox('myBox', compactionStrategy: (entries, deletedEntries) {
1617
return deletedEntries > 50;
1718
});
1819
```

docs/create_adapter_manually.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Sometimes it might be necessary to create a custom `TypeAdapter`. You can do that by extending the `TypeAdapter` class. Make sure to specify the generic argument.
44

5-
!> Test your custom `TypeAdapter` thoroughly. If one does not work correctly, it may corrupt your box.
5+
!> Test your custom `TypeAdapter`s thoroughly. If one does not work correctly, it may corrupt your box.
66

77
It is very easy to implement a `TypeAdapter`. Here is the `DataTimeAdapter` used by Hive internally:
88

docs/delete.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# Delete key
22

3-
If you want to change an existing value, you can either override it using `put()` or delete it:
3+
If you want to change an existing value, you can either override it using for example `put()` or delete it:
44

55
```dart
6-
var deleted = await box.delete('key');
6+
var box = Hive.box('testBox');
7+
8+
box.delete('key');
79
```
810

911
If the key does not exist, no disk access is needed and the returned `Future` finishes immediately.

docs/encrypted_box.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var key = Hive.generateSecureKey();
1111
Just pass the key when you open a box:
1212

1313
```dart
14-
var encryptedBox = await Hive.box('vaultBox', encryptionKey: key);
14+
var encryptedBox = await Hive.openBox('vaultBox', encryptionKey: key);
1515
```
1616

1717
!> Make sure you store the key securely when your application is closed. With Flutter you can use the [flutter_secure_storage](https://pub.dev/packages/flutter_secure_storage) or a similar package.

docs/generate_adapter.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Person {
3030
}
3131
```
3232

33-
As you can see, each field has a **unique** number. These field numbers are used to identify the fields in the hive binary format, and should not be changed once your class is in use.
33+
As you can see, each field has a **unique** number (unique per class). These field numbers are used to identify the fields in the hive binary format, and should not be changed once your class is in use.
3434

3535
*Field numbers can be in the range 0-255*.
3636

docs/lazy_box.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ By default, the whole content of a box is stored in memory as soon as it is open
55
For larger boxes or very memory critical applications, it may be useful to load values lazily. When a lazy box is being opened, all of its keys are read and stored in memory. Once you access a value, Hive knows its exact position on the disk and gets the value.
66

77
```dart
8-
var lazyBox = await Hive.box('myLazyBox', lazy: true);
8+
var lazyBox = await Hive.openBox('myLazyBox', lazy: true) as LazyBox;
99
1010
var value = await lazyBox.get('lazyVal');
1111
```
1212

13-
You can use lazy boxes like any other box, but you can't access keys using the list access operator `[]`.
13+
To get an already opened lazy box call:
1414

1515
```dart
16-
var value = lazyBox['lazyVal']; // ERROR
16+
var lazyBox = Hive.box('myLazyBox') as LazyBox;
1717
```

docs/limitations.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Limitations
22

33
- **HIVE IS NOT READY FOR PRODUCTION YET**
4-
- Keys have to be ASCII Strings with a max length of 255 chars.
4+
- Keys have to be 32 bit unsigned integers or ASCII Strings with a max length of 255 chars.
55
- The supported integer values include all integers between -2^53 and 2^53, and some integers with larger magnitude
66
- Strings can have a maximum of 65535 **bytes**. Mind that a Unicode chars may be longer than one byte.
77
- Lists and Maps cannot have more than 65535 elements.
8+
- Objects are not allowed to contain cycles. Hive will not detect them and storing will result in an infinite loop.
89
- Only one process can access a box at any time. Bad things happen otherwise.

docs/read.md

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
# Read from box
22

3-
Reading from a box is almost like accessing a Map:
3+
Reading from a box is very straightforward:
44

55
```dart
6-
String name = box['name'];
6+
String name = box.get('name');
77
8-
DateTime birthday = box['birthday'];
9-
```
10-
11-
If your box is [lazy](lazy_box.md) the above code will not work. You have to access the box using `get()`:
12-
13-
```dart
14-
String name = await box.get('name');
15-
16-
DateTime birthday = await box.get('birthday');
8+
DateTime birthday = box.get('birthday');
179
```
1810

1911
If the key does not exist, `null` is returned. Optionally you can specify a `defaultValue` which will be returned in case the key does not exist.
2012

2113
```dart
22-
double height = await box.get('randomKey', defaultValue: 17.5);
14+
double height = box.get('randomKey', defaultValue: 17.5);
2315
```

docs/transactions.md

-37
This file was deleted.

docs/watch_changes.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ If you want to get notified about changes in a box, you can subscribe to the `St
55
This can be very useful for Flutter apps: You can rebuild widgets every time the box changes.
66

77
```dart
8-
var box = await Hive.get('myBox');
9-
box.watch().listen((key, val) {
10-
if (val == null) {
11-
print('$key has been deleted');
8+
var box = Hive.box('myBox');
9+
box.watch().listen((event) {
10+
if (event.deleted) {
11+
print('${event.key} has been deleted');
1212
} else {
13-
print('$key is now assigned to $val');
13+
print('${event.key} is now assigned to ${event.value}');
1414
}
1515
});
1616
17-
await box.put('someKey', 123); // > someKey is now assigned to 123
18-
await box.delete('someKey'); // > someKey has been deleted
17+
box.put('someKey', 123); // > someKey is now assigned to 123
18+
box.delete('someKey'); // > someKey has been deleted
1919
```
2020

2121
If you specify the `key` parameter, you will be notified about all changes of a specific key.
2222

2323
```dart
24-
box.watch(key: 'someKey').listen((key, val) {
24+
box.watch(key: 'someKey').listen((event) {
2525
// ...
2626
});
2727
```

docs/write.md

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
# Write to box
22

3-
Writing is just as easy as reading. All keys have to be ASCII Strings with a max lenght of 255 chars.
3+
Writing to a box is almost like writing to a map. All keys have to be ASCII Strings with a max lenght of 255 chars or integers.
44

55
```dart
6-
await box.put('name', 'Paul');
6+
var box = Hive.box('myBox');
77
8-
await box.put('friends', ['Dave', 'Simon', 'Lisa']);
8+
box.put('name', 'Paul');
9+
10+
box.put('friends', ['Dave', 'Simon', 'Lisa']);
11+
12+
box.put(123, 'test');
13+
14+
box.putAll({'key1': 'value1', 42: 'life'})
915
```
1016

11-
?> Writing `null` is the same as [deleting](delete.md) a value.
17+
You may wonder why writing works without async code. This is one of the main strengths of Hive.<br>
18+
The changes are written to the disk as soon as possible in the background but all listeners are notified immediately. If the async operation fails (which it should not), all listeners are notified again with the old values.
19+
20+
This does not work for [lazy](lazy_box.md) boxes. As long as the `put()` Future did not finish, `get()` will return the old values.
21+
22+
?> Writing `null` is **NOT** the same as [deleting](delete.md) a value.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
# This is a generated file; do not edit or check into version control.
3+
export "FLUTTER_ROOT=C:\Users\simon\flutter"
4+
export "FLUTTER_APPLICATION_PATH=C:\Users\simon\Documents\GitHub\hive\examples\basic_flutter"
5+
export "FLUTTER_TARGET=lib\main.dart"
6+
export "FLUTTER_BUILD_DIR=build"
7+
export "SYMROOT=${SOURCE_ROOT}/../build\ios"
8+
export "FLUTTER_FRAMEWORK_DIR=C:\Users\simon\flutter\bin\cache\artifacts\engine\ios"

examples/basic_flutter/lib/main.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class MyApp extends StatelessWidget {
1010
Future<Box> _openBox() async {
1111
final directory = await getApplicationDocumentsDirectory();
1212
Hive.init(directory.path);
13-
return await Hive.box('myBox');
13+
return await Hive.openBox('myBox');
1414
}
1515

1616
@override
@@ -40,7 +40,7 @@ class IceCreamPage extends StatefulWidget {
4040
}
4141

4242
class _IceCreamPageState extends State<IceCreamPage> {
43-
var box = Hive['myBox'];
43+
var box = Hive.box('myBox');
4444

4545
@override
4646
void initState() {
@@ -53,7 +53,7 @@ class _IceCreamPageState extends State<IceCreamPage> {
5353

5454
@override
5555
Widget build(BuildContext context) {
56-
var userLikesIceCream = box['userLikesIceCream'] ?? false;
56+
var userLikesIceCream = box.get('userLikesIceCream', defaultValue: false);
5757
return Center(
5858
child: Row(
5959
mainAxisSize: MainAxisSize.min,

0 commit comments

Comments
 (0)