Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement fully working StorageBackendMemory #1036

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion hive/lib/hive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,39 @@ export 'package:hive/src/box_collection/box_collection.dart';

export 'src/backend/js/web_worker/web_worker_stub.dart'
if (dart.library.html) 'src/backend/js/web_worker/web_worker.dart';
export 'src/backend/storage_backend_memory.dart';
export 'src/backend/stub/storage_backend_memory.dart';
export 'src/object/hive_object.dart' show HiveObject, HiveObjectMixin;

part 'src/annotations/hive_field.dart';

part 'src/annotations/hive_type.dart';

part 'src/binary/binary_reader.dart';

part 'src/binary/binary_writer.dart';

part 'src/box/box.dart';

part 'src/box/box_base.dart';

part 'src/box/lazy_box.dart';

part 'src/crypto/hive_aes_cipher.dart';

part 'src/crypto/hive_cipher.dart';

part 'src/hive.dart';

part 'src/hive_error.dart';

part 'src/object/hive_collection.dart';

part 'src/object/hive_list.dart';

part 'src/object/hive_storage_backend_preference.dart';

part 'src/registry/type_adapter.dart';

part 'src/registry/type_registry.dart';

/// Global constant to access Hive.
Expand Down
73 changes: 0 additions & 73 deletions hive/lib/src/backend/storage_backend_memory.dart

This file was deleted.

100 changes: 100 additions & 0 deletions hive/lib/src/backend/stub/random_access_buffer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import 'dart:typed_data';

import 'package:hive/hive.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';
import 'package:hive/src/binary/frame_helper.dart';
import 'package:hive/src/box/keystore.dart';

/// Implementation of [RandomAccessBuffer]
///
/// Provides simplified access to raw [Uint8List] for storing
/// [Frame] data. For main usage in [StorageBackendMemory].
class RandomAccessBuffer {
final ReadWriteSync _sync;

final FrameHelper _frameHelper;

Uint8List _bytes;

int _writeOffset;

RandomAccessBuffer(Uint8List? bytes, {int initialOffset = 0})
: _bytes = bytes ?? Uint8List(0),
_sync = ReadWriteSync(),
_writeOffset = initialOffset,
_frameHelper = FrameHelper();

/// Get the current length in bytes
int get length => _bytes.lengthInBytes;

/// Get the current write offset
int get writeOffset => _writeOffset;

/// Read a [Frame] from the [RandomAccessBuffer]
Future<dynamic> read(
Frame frame, TypeRegistry typeRegistry, HiveCipher? cipher) {
return _sync.syncRead(() async {
var bytes = _bytes.sublist(frame.offset, frame.offset + frame.length!);
var reader = BinaryReaderImpl(bytes, typeRegistry);
var readFrame = await reader.readFrame(cipher: cipher, lazy: false);

if (readFrame == null) {
throw HiveError('Could not read value from box. '
'Maybe your box is corrupted.');
}

return readFrame.value;
});
}

/// Write a list of [Frame] to the [RandomAccessBuffer]
Future<void> write(
List<Frame> frames, TypeRegistry typeRegistry, HiveCipher? cipher) {
return _sync.syncWrite(() async {
var writer = BinaryWriterImpl(typeRegistry);

for (var frame in frames) {
frame.length = await writer.writeFrame(frame, cipher: cipher);
}

var b = BytesBuilder();
b.add(_bytes);
b.add(writer.toBytes());
_bytes = b.toBytes();

for (var frame in frames) {
frame.offset = _writeOffset;
_writeOffset += frame.length!;
}
});
}

/// Perform the recovery check
/// Throws an [HiveError] on box corruption
Future<void> recoveryCheck(
TypeRegistry typeRegistry, Keystore? keystore, HiveCipher? cipher) async {
var recoveryOffset = await _frameHelper.framesFromBytes(
_bytes,
keystore,
typeRegistry,
cipher,
);

if (recoveryOffset != -1) {
throw HiveError('Wrong checksum in bytes. Box may be corrupted.');
}

_writeOffset = _bytes.offsetInBytes;
}

/// Clear the buffer and reset the write offset
Future<void> clear() async {
return _sync.syncReadWrite(() async {
_bytes = Uint8List(0);
_writeOffset = 0;
});
}
}
79 changes: 79 additions & 0 deletions hive/lib/src/backend/stub/storage_backend_memory.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import 'dart:typed_data';

import 'package:hive/hive.dart';
import 'package:hive/src/backend/storage_backend.dart';
import 'package:hive/src/backend/stub/random_access_buffer.dart';
import 'package:hive/src/binary/frame.dart';
import 'package:hive/src/box/keystore.dart';

/// In-memory storage backend
///
/// This storage memory uses the [RandomAccessBuffer] which is an abstraction
/// for managing data in [Uint8List] as a mean of storage. Data will not be
/// persisted and is lost on close. Data is not stored, is highly volatile
/// and does not survive a page refresh.
class StorageBackendMemory extends StorageBackend {
late final RandomAccessBuffer _randomAccessBuffer;

final HiveCipher? _cipher;

TypeRegistry? _typeRegistry;

/// Default constructor for the [StorageBackendMemory]
///
/// If needed, provide a [Uint8List] with already existing bytes and the
/// corresponding [initialOffset]. For encryption, provide a [_cipher].
StorageBackendMemory(Uint8List? bytes, this._cipher,
[int initialOffset = 0]) {
_randomAccessBuffer =
RandomAccessBuffer(bytes, initialOffset: initialOffset);
}

/// An in-memory storage has no path
@override
final path = null;

@override
final bool supportsCompaction = false;

@override
Future<void> initialize(
TypeRegistry registry, Keystore keystore, bool lazy) async {
_typeRegistry = registry;
await _randomAccessBuffer.recoveryCheck(registry, keystore, _cipher);
}

@override
Future<dynamic> readValue(Frame frame) async {
return _randomAccessBuffer.read(frame, _typeRegistry!, _cipher);
}

@override
Future<void> writeFrames(List<Frame> frames) {
return _randomAccessBuffer.write(frames, _typeRegistry!, _cipher);
}

@override
Future<void> compact(Iterable<Frame> frames) {
throw UnsupportedError('This operation is unsupported for memory boxes.');
}

@override
Future<void> clear() {
return _randomAccessBuffer.clear();
}

@override
Future<void> close() {
return _randomAccessBuffer.clear();
}

@override
Future<void> deleteFromDisk() {
return _randomAccessBuffer.clear();
}

/// Nothing to flush to disk as we act directly in-memory
@override
Future<void> flush() => Future.value();
}
73 changes: 73 additions & 0 deletions hive/test/tests/backend/memory/random_access_buffer_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import 'package:hive/src/backend/stub/random_access_buffer.dart';
import 'package:hive/src/binary/binary_writer_impl.dart';
import 'package:hive/src/binary/frame.dart';
import 'package:hive/src/registry/type_registry_impl.dart';
import 'package:test/test.dart';


void main() {
group('RandomAccessBuffer', () {
test('empty random access buffer', () {
final randomAccessBuffer = RandomAccessBuffer(null);
expect(randomAccessBuffer.length, 0);
expect(randomAccessBuffer.writeOffset, 0);
});

test('write() writes to random access buffer', () async {
final typeRegistry = TypeRegistryImpl.nullImpl;

final randomAccessBuffer = RandomAccessBuffer(null);
randomAccessBuffer.recoveryCheck(typeRegistry, null, null);

final frame = Frame("abc", "123");
await randomAccessBuffer.write([frame], typeRegistry, null);

expect(randomAccessBuffer.length, getFrameBytes([frame]));
expect(randomAccessBuffer.writeOffset, getFrameBytes([frame]));
});

test('read() reads data from random access buffer', () async {
final typeRegistry = TypeRegistryImpl.nullImpl;

final randomAccessBuffer = RandomAccessBuffer(null);
randomAccessBuffer.recoveryCheck(typeRegistry, null, null);

final saveFrame = Frame("abc", "123", offset: 0, length: 3);
await randomAccessBuffer.write([saveFrame], typeRegistry, null);

expect(randomAccessBuffer.length, getFrameBytes([saveFrame]));
expect(randomAccessBuffer.writeOffset, getFrameBytes([saveFrame]));

final storedValue = await randomAccessBuffer.read(
Frame("abc", "abc", offset: 0, length: 21), typeRegistry, null);

expect(storedValue, "123");
});

test('clear() resets random access buffer', () async {
final typeRegistry = TypeRegistryImpl.nullImpl;

final randomAccessBuffer = RandomAccessBuffer(null);
randomAccessBuffer.recoveryCheck(typeRegistry, null, null);

final frame = Frame("abc", "123");
await randomAccessBuffer.write([frame], typeRegistry, null);

expect(randomAccessBuffer.length, getFrameBytes([frame]));
expect(randomAccessBuffer.writeOffset, getFrameBytes([frame]));

await randomAccessBuffer.clear();

expect(randomAccessBuffer.length, 0);
expect(randomAccessBuffer.writeOffset, 0);
});
});
}

int getFrameBytes(Iterable<Frame> frames) {
var writer = BinaryWriterImpl(TypeRegistryImpl.nullImpl);
for (var frame in frames) {
writer.writeFrame(frame);
}
return writer.toBytes().lengthInBytes;
}
Loading