Skip to content
Permalink
Browse files

Fix compaction bug

  • Loading branch information
leisim committed Dec 2, 2019
1 parent edffb58 commit dfdecd1d2b00f10184debaa1de4ff11681251616
@@ -15,6 +15,7 @@
### Fixes
- HiveObjects have not been initialized correctly in lazy boxes
- Fixed bug where uppercase box name resulted in uppercase file name
- Fixed compaction bug which caused corrupted boxes

### More
- Changed type of `encryptionKey` from `Uint8List` to `List<int>`
File renamed without changes.
@@ -2,7 +2,7 @@ import 'dart:async';
import 'dart:io';

import 'package:hive/hive.dart';
import 'package:hive/src/backend/read_write_sync.dart';
import 'package:hive/src/backend/vm/read_write_sync.dart';
import 'package:hive/src/backend/storage_backend.dart';
import 'package:hive/src/binary/binary_reader_impl.dart';
import 'package:hive/src/binary/binary_writer_impl.dart';
@@ -39,6 +39,8 @@ class StorageBackendVm extends StorageBackend {
@visibleForTesting
TypeRegistry registry;

bool compactionScheduled = false;

StorageBackendVm(this.file, this.lockFile, this.crashRecovery, this.crypto)
: frameHelper = FrameIoHelper(),
_sync = ReadWriteSync();
@@ -129,6 +131,9 @@ class StorageBackendVm extends StorageBackend {

@override
Future<void> compact(Iterable<Frame> frames) {
if (compactionScheduled) return Future.value();
compactionScheduled = true;

return _sync.syncReadWrite(() async {
await readRaf.setPosition(0);
var reader = BufferedFileReader(readRaf);
@@ -175,6 +180,7 @@ class StorageBackendVm extends StorageBackend {
frame.offset = offset;
offset += frame.length;
}
compactionScheduled = false;
});
}

@@ -4,7 +4,7 @@ import '../util/is_browser.dart';
import 'integration.dart';

Future _performTest(bool lazy) async {
var amount = isBrowser ? 1000 : 20000;
var amount = isBrowser ? 500 : 20000;
var box = await openBox(lazy);
var entries = <String, dynamic>{};
for (var i = 0; i < amount; i++) {
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:test/test.dart';

import '../util/is_browser.dart';
@@ -6,12 +8,24 @@ import 'integration.dart';
Future _performTest(bool lazy) async {
var amount = isBrowser ? 5 : 100;
var box = await openBox(lazy);

for (var i = 0; i < amount; i++) {
for (var n = 0; n < 100; n++) {
await box.put('string$i', 'test$n');
await box.put('int$i', n);
await box.put('bool$i', n % 2 == 0);
await box.put('null$i', null);
var completer = Completer();
scheduleMicrotask(() async {
await box.put('string$i', 'test$n');
await box.put('int$i', n);
await box.put('bool$i', n % 2 == 0);
await box.put('null$i', null);

expect(await getFromBox(box, 'string$i'), 'test$n');
expect(await getFromBox(box, 'int$i'), n);
expect(await getFromBox(box, 'bool$i'), n % 2 == 0);
expect(await getFromBox(box, 'null$i', defaultValue: 0), null);

completer.complete();
});
await completer.future;
}
}

@@ -4,7 +4,7 @@ import 'dart:io';
import 'dart:typed_data';

import 'package:hive/hive.dart';
import 'package:hive/src/backend/read_write_sync.dart';
import 'package:hive/src/backend/vm/read_write_sync.dart';
import 'package:hive/src/backend/vm/storage_backend.dart';
import 'package:hive/src/binary/binary_writer_impl.dart';
import 'package:hive/src/binary/frame.dart';

0 comments on commit dfdecd1

Please sign in to comment.
You can’t perform that action at this time.