Skip to content

Commit

Permalink
Fix check condition bug of field index in hive generator
Browse files Browse the repository at this point in the history
Ensure HiveLists are treated as non-nullable in hive_generator (isar#728)

* Replaced ?. operator with . operator

Resolves error "The argument type 'HiveList<T>?' can't be assigned to the parameter type 'HiveList<T>'.

Closes issue isar#664.

* Ensuring nullable HiveLists are properly generated

Co-authored-by: Misir Jafarov <misir.ceferov@gmail.com>

Updated hive_generator dependencies (isar#765)

* Updated dependencies

* Changed version dependencies

Bump up hive_generator to v1.1.1

Get indexDB selectively based on window property (isar#802)

Co-authored-by: Wenhao Wu <wenhao.wu@signanthealth.com>

Add missing path parameter to HiveInterface methods (isar#776)

Fixes broken CI (isar#806)

* Update pubspec.yaml

* Migrated from mockito to mocktail

* Test on dart 2.14

Don't loose track of box objects if init crashes (isar#846)

feat: Add flush method to boxes (isar#852)

bump: Update to v2.0.5

Updated hive_generator analyzer dependency (isar#858)

bump up hive_generator to v1.1.2

Change pedantic to linter and fix warnings (isar#876)
  • Loading branch information
MaGaroo authored and mrdavidrees committed Jan 16, 2022
1 parent b110329 commit ec6c78a
Show file tree
Hide file tree
Showing 45 changed files with 443 additions and 1,628 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
matrix:
test-platform: [vm, chrome]
dart-channel: ["2.12.0", "2.13.0"]
dart-channel: ["2.12.0", "2.13.0", "2.14.0"]
steps:
- uses: actions/checkout@v1
- uses: dart-lang/setup-dart@v1
Expand Down
12 changes: 12 additions & 0 deletions hive/CHANGELOG.md
@@ -1,3 +1,15 @@
# 2.0.5

### Enhancements

- Get IndexedDB selectively based on window property - [#802](https://github.com/hivedb/hive/pull/802)
- Added `path` parameter to `boxExists` and `deleteBoxFromDisk` methods - [#776](https://github.com/hivedb/hive/pull/776)
- Added `flush` method to boxes - [#852](https://github.com/hivedb/hive/pull/852)

### Fixes

- Don't loose track of box objects if init crashes - [#846](https://github.com/hivedb/hive/pull/846)

# 2.0.4

### Enhancements
Expand Down
8 changes: 4 additions & 4 deletions hive/analysis_options.yaml
@@ -1,13 +1,13 @@
include: package:pedantic/analysis_options.yaml
include: package:lints/recommended.yaml
analyzer:
exclude:
- "**/*.g.dart"
- "example/*"
- "example/**"
strong-mode:
implicit-casts: false
errors:
invalid_use_of_protected_member: error
always_declare_return_types: error
always_declare_return_types: error
linter:
rules:
- avoid_function_literals_in_foreach_calls
Expand Down Expand Up @@ -44,4 +44,4 @@ linter:
- unnecessary_getters_setters
- unnecessary_lambdas
- unnecessary_null_aware_assignments
- unnecessary_statements
- unnecessary_statements
13 changes: 8 additions & 5 deletions hive/lib/src/backend/js/backend_manager.dart
@@ -1,17 +1,20 @@
import 'dart:html';
import 'dart:indexed_db';

import 'dart:js' as js;
import 'package:hive/hive.dart';
import 'package:hive/src/backend/js/storage_backend_js.dart';
import 'package:hive/src/backend/storage_backend.dart';

/// Opens IndexedDB databases
class BackendManager implements BackendManagerInterface {
IdbFactory? get indexedDB => js.context.hasProperty('window')
? window.indexedDB
: WorkerGlobalScope.instance.indexedDB;

@override
Future<StorageBackend> open(
String name, String? path, bool crashRecovery, HiveCipher? cipher) async {
var db =
await window.indexedDB!.open(name, version: 1, onUpgradeNeeded: (e) {
var db = await indexedDB!.open(name, version: 1, onUpgradeNeeded: (e) {
var db = e.target.result as Database;
if (!db.objectStoreNames!.contains('box')) {
db.createObjectStore('box');
Expand All @@ -23,15 +26,15 @@ class BackendManager implements BackendManagerInterface {

@override
Future<void> deleteBox(String name, String? path) {
return window.indexedDB!.deleteDatabase(name);
return indexedDB!.deleteDatabase(name);
}

@override
Future<bool> boxExists(String name, String? path) async {
// https://stackoverflow.com/a/17473952
try {
var _exists = true;
await window.indexedDB!.open(name, version: 1, onUpgradeNeeded: (e) {
await indexedDB!.open(name, version: 1, onUpgradeNeeded: (e) {
e.target.transaction!.abort();
_exists = false;
});
Expand Down
9 changes: 8 additions & 1 deletion hive/lib/src/backend/js/storage_backend_js.dart
@@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:html';
import 'dart:indexed_db';
import 'dart:js' as js;
import 'dart:js_util';
import 'dart:typed_data';

Expand Down Expand Up @@ -199,6 +200,12 @@ class StorageBackendJs extends StorageBackend {

@override
Future<void> deleteFromDisk() {
return window.indexedDB!.deleteDatabase(_db.name!);
final indexDB = js.context.hasProperty('window')
? window.indexedDB
: WorkerGlobalScope.instance.indexedDB;
return indexDB!.deleteDatabase(_db.name!);
}

@override
Future<void> flush() => Future.value();
}
3 changes: 3 additions & 0 deletions hive/lib/src/backend/storage_backend.dart
Expand Up @@ -34,6 +34,9 @@ abstract class StorageBackend {

/// Clear database and delete from disk
Future<void> deleteFromDisk();

/// Flush all changes to disk
Future<void> flush();
}

/// Abstract database manager
Expand Down
3 changes: 3 additions & 0 deletions hive/lib/src/backend/storage_backend_memory.dart
Expand Up @@ -67,4 +67,7 @@ class StorageBackendMemory extends StorageBackend {
Future<void> deleteFromDisk() {
throw UnsupportedError('This operation is unsupported for memory boxes.');
}

@override
Future<void> flush() => Future.value();
}
9 changes: 8 additions & 1 deletion hive/lib/src/backend/vm/storage_backend_vm.dart
Expand Up @@ -2,8 +2,8 @@ import 'dart:async';
import 'dart:io';

import 'package:hive/hive.dart';
import 'package:hive/src/backend/vm/read_write_sync.dart';
import 'package:hive/src/backend/storage_backend.dart';
import 'package:hive/src/backend/vm/read_write_sync.dart';
import 'package:hive/src/binary/binary_reader_impl.dart';
import 'package:hive/src/binary/binary_writer_impl.dart';
import 'package:hive/src/binary/frame.dart';
Expand Down Expand Up @@ -226,4 +226,11 @@ class StorageBackendVm extends StorageBackend {
await _file.delete();
});
}

@override
Future<void> flush() {
return _sync.syncWrite(() async {
await writeRaf.flush();
});
}
}
8 changes: 8 additions & 0 deletions hive/lib/src/binary/frame.dart
Expand Up @@ -96,6 +96,14 @@ class Frame {
'length: $length, offset: $offset)';
}
}

@override
int get hashCode =>
runtimeType.hashCode ^
key.hashCode ^
value.hashCode ^
length.hashCode ^
deleted.hashCode;
}

/// Possible Key types
Expand Down
6 changes: 6 additions & 0 deletions hive/lib/src/box/box_base.dart
Expand Up @@ -21,6 +21,9 @@ class BoxEvent {
}
return false;
}

@override
int get hashCode => runtimeType.hashCode ^ key.hashCode ^ value.hashCode;
}

/// Boxes contain all of your data. In the browser, each box has its own
Expand Down Expand Up @@ -124,4 +127,7 @@ abstract class BoxBase<E> {
///
/// In the browser, the IndexedDB database is being removed.
Future<void> deleteFromDisk();

/// Flushes all pending changes of the box to disk.
Future<void> flush();
}
3 changes: 3 additions & 0 deletions hive/lib/src/box/box_base_impl.dart
Expand Up @@ -254,4 +254,7 @@ class _NullBoxBase<E> implements BoxBase<E> {

@override
Never watch({key}) => throw UnimplementedError();

@override
Never flush() => throw UnimplementedError();
}
5 changes: 5 additions & 0 deletions hive/lib/src/box/box_impl.dart
Expand Up @@ -103,4 +103,9 @@ class BoxImpl<E> extends BoxBaseImpl<E> implements Box<E> {
}
return map;
}

@override
Future<void> flush() async {
await backend.flush();
}
}
7 changes: 6 additions & 1 deletion hive/lib/src/box/lazy_box_impl.dart
Expand Up @@ -5,8 +5,8 @@ import 'package:hive/hive.dart';
import 'package:hive/src/backend/storage_backend.dart';
import 'package:hive/src/binary/frame.dart';
import 'package:hive/src/box/box_base_impl.dart';
import 'package:hive/src/object/hive_object.dart';
import 'package:hive/src/hive_impl.dart';
import 'package:hive/src/object/hive_object.dart';

/// Not part of public API
class LazyBoxImpl<E> extends BoxBaseImpl<E> implements LazyBox<E> {
Expand Down Expand Up @@ -92,4 +92,9 @@ class LazyBoxImpl<E> extends BoxBaseImpl<E> implements LazyBox<E> {

await performCompactionIfNeeded();
}

@override
Future<void> flush() async {
await backend.flush();
}
}
8 changes: 4 additions & 4 deletions hive/lib/src/hive.dart
Expand Up @@ -19,7 +19,7 @@ abstract class HiveInterface implements TypeRegistry {
bool crashRecovery = true,
String? path,
Uint8List? bytes,
@deprecated List<int>? encryptionKey,
@Deprecated('Use encryptionCipher instead') List<int>? encryptionKey,
});

/// Opens a lazy box.
Expand All @@ -33,7 +33,7 @@ abstract class HiveInterface implements TypeRegistry {
CompactionStrategy compactionStrategy = defaultCompactionStrategy,
bool crashRecovery = true,
String? path,
@deprecated List<int>? encryptionKey,
@Deprecated('Use encryptionCipher instead') List<int>? encryptionKey,
});

/// Returns a previously opened box.
Expand All @@ -51,7 +51,7 @@ abstract class HiveInterface implements TypeRegistry {
/// Removes the file which contains the box and closes the box.
///
/// In the browser, the IndexedDB database is being removed.
Future<void> deleteBoxFromDisk(String name);
Future<void> deleteBoxFromDisk(String name, {String? path});

/// Deletes all currently open boxes from disk.
///
Expand All @@ -62,7 +62,7 @@ abstract class HiveInterface implements TypeRegistry {
List<int> generateSecureKey();

/// Checks if a box exists
Future<bool> boxExists(String name);
Future<bool> boxExists(String name, {String? path});
}

///
Expand Down
8 changes: 4 additions & 4 deletions hive/lib/src/hive_impl.dart
Expand Up @@ -12,8 +12,8 @@ import 'package:hive/src/box/box_impl.dart';
import 'package:hive/src/box/default_compaction_strategy.dart';
import 'package:hive/src/box/default_key_comparator.dart';
import 'package:hive/src/box/lazy_box_impl.dart';
import 'package:hive/src/util/extensions.dart';
import 'package:hive/src/registry/type_registry_impl.dart';
import 'package:hive/src/util/extensions.dart';
import 'package:meta/meta.dart';

import 'backend/storage_backend.dart';
Expand Down Expand Up @@ -107,8 +107,8 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface {
newBox = BoxImpl<E>(this, name, comparator, compaction, backend);
}

await newBox.initialize();
_boxes[name] = newBox;
await newBox.initialize();

completer.complete();
return newBox;
Expand All @@ -131,7 +131,7 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface {
bool crashRecovery = true,
String? path,
Uint8List? bytes,
@deprecated List<int>? encryptionKey,
@Deprecated('Use encryptionCipher instead') List<int>? encryptionKey,
}) async {
if (encryptionKey != null) {
encryptionCipher = HiveAesCipher(encryptionKey);
Expand All @@ -148,7 +148,7 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface {
CompactionStrategy compactionStrategy = defaultCompactionStrategy,
bool crashRecovery = true,
String? path,
@deprecated List<int>? encryptionKey,
@Deprecated('Use encryptionCipher instead') List<int>? encryptionKey,
}) async {
if (encryptionKey != null) {
encryptionCipher = HiveAesCipher(encryptionKey);
Expand Down
2 changes: 1 addition & 1 deletion hive/lib/src/object/hive_object.dart
@@ -1,8 +1,8 @@
library hive_object_internal;

import 'package:hive/hive.dart';
import 'package:meta/meta.dart';
import 'package:hive/src/object/hive_list_impl.dart';
import 'package:meta/meta.dart';

part 'hive_object_internal.dart';

Expand Down
11 changes: 5 additions & 6 deletions hive/pubspec.yaml
@@ -1,6 +1,6 @@
name: hive
description: Lightweight and blazing fast key-value database written in pure Dart. Strongly encrypted using AES-256.
version: 2.0.4
version: 2.0.5
homepage: https://github.com/hivedb/hive/tree/master/hive
documentation: https://docs.hivedb.dev/

Expand All @@ -12,10 +12,9 @@ dependencies:
crypto: ^3.0.0

dev_dependencies:
test: ^1.15.7
# TODO: Fix & update mockito version
mockito: 5.0.0-nullsafety.5
pedantic: ^1.9.0
test: ^1.17.12
mocktail: ^0.2.0
lints: ^1.0.0
path: ^1.7.0
pointycastle: ^3.0.1
build_runner: ^1.10.0
build_runner: ^2.1.2
4 changes: 3 additions & 1 deletion hive/test/integration/hive_list_test.dart
@@ -1,7 +1,6 @@
import 'package:hive/hive.dart';
import 'package:hive/src/hive_impl.dart';
import 'package:hive/src/object/hive_list_impl.dart';
import 'package:hive/src/object/hive_object.dart';
import 'package:test/test.dart';

import 'integration.dart';
Expand All @@ -15,6 +14,9 @@ class _TestObject extends HiveObject {

@override
bool operator ==(dynamic other) => other is _TestObject && other.name == name;

@override
int get hashCode => runtimeType.hashCode ^ name.hashCode;
}

class _TestObjectAdapter extends TypeAdapter<_TestObject> {
Expand Down
3 changes: 3 additions & 0 deletions hive/test/integration/hive_object_test.dart
Expand Up @@ -11,6 +11,9 @@ class _TestObject with HiveObjectMixin {

@override
bool operator ==(dynamic other) => other is _TestObject && other.name == name;

@override
int get hashCode => runtimeType.hashCode ^ name.hashCode;
}

class _TestObjectAdapter extends TypeAdapter<_TestObject> {
Expand Down
2 changes: 1 addition & 1 deletion hive/test/integration/recovery_test.dart
Expand Up @@ -5,8 +5,8 @@ import 'dart:io';

import 'package:hive/src/box/keystore.dart';
import 'package:hive/src/hive_impl.dart';
import 'package:test/test.dart';
import 'package:path/path.dart' as path;
import 'package:test/test.dart';

import '../tests/backend/vm/storage_backend_vm_test.dart';
import '../tests/common.dart';
Expand Down

0 comments on commit ec6c78a

Please sign in to comment.