Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
rkistner committed Oct 18, 2024
1 parent 6b4f3e3 commit 2b1a3b9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ import 'dart:typed_data';

/// A utility class that manages a growing byte buffer.
/// It dynamically increases its capacity in factors of 2 when needed.
class GrowingByteBuffer {
class DynamicBuffer {
Uint8List _buffer;
int _capacity;
int _length = 0;

/// Creates a [GrowingByteBuffer] with an optional initial capacity.
/// Creates a [DynamicBuffer] with an optional initial capacity.
/// The default initial capacity is 1024 bytes.
GrowingByteBuffer([int initialCapacity = 1024])
DynamicBuffer([int initialCapacity = 1024])
: _capacity = initialCapacity,
_buffer = Uint8List(initialCapacity);
_buffer = Uint8List(initialCapacity) {
if (initialCapacity < 1) {
throw ArgumentError("initialCapacity must be positive");
}
}

/// Adds [data] to the buffer, expanding its capacity if necessary.
void add(Uint8List data) {
Expand All @@ -26,7 +30,7 @@ class GrowingByteBuffer {
if (offset < 0) {
throw ArgumentError("Offset must be non-negative");
}
int endPosition = offset + data.length;
final endPosition = offset + data.length;
_ensureCapacity(endPosition);
_buffer.setRange(offset, endPosition, data);
if (endPosition > _length) {
Expand All @@ -35,6 +39,9 @@ class GrowingByteBuffer {
}

/// Truncates the buffer to a specific [newSize].
///
/// No memory is freed when using [truncate].
///
/// If [newSize] is less than the current length, the buffer is truncated.
/// If [newSize] is greater than the current length, the buffer length is extended with zeros.
void truncate(int newSize) {
Expand All @@ -49,7 +56,7 @@ class GrowingByteBuffer {
_length = newSize;
}

/// Returns a [Uint8List] containing the data up to the current length.
/// Returns a [Uint8List] view containing the data up to the current length.
Uint8List toUint8List() {
return Uint8List.view(_buffer.buffer, 0, _length);
}
Expand All @@ -63,7 +70,6 @@ class GrowingByteBuffer {
newCapacity *= 2;
}
// Allocate a new buffer and copy existing data.
print('resizing to $newCapacity');
Uint8List newBuffer = Uint8List(newCapacity);
newBuffer.setRange(0, _length, _buffer);
_buffer = newBuffer;
Expand Down
6 changes: 3 additions & 3 deletions sqlite3/lib/src/wasm/vfs/indexed_db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'dart:math';
import 'dart:typed_data';

import 'package:meta/meta.dart';
import 'package:sqlite3/src/wasm/vfs/growing_buffer.dart';
import 'package:sqlite3/src/wasm/vfs/dynamic_buffer.dart';
import 'package:web/web.dart' as web;

import '../../constants.dart';
Expand Down Expand Up @@ -534,7 +534,7 @@ final class IndexedDbFileSystem extends BaseVirtualFileSystem {
final name = entry.key;
final fileId = entry.value;

final buffer = GrowingByteBuffer();
final buffer = DynamicBuffer();
final data = await _asynchronous.readFully(fileId);
buffer.add(data);

Expand Down Expand Up @@ -647,7 +647,7 @@ class _IndexedDbFile implements VirtualFileSystemFile {
void xWrite(Uint8List buffer, int fileOffset) {
vfs._checkClosed();

final previousContent = vfs._memory.fileData[path] ?? GrowingByteBuffer();
final previousContent = vfs._memory.fileData[path] ?? DynamicBuffer();
memoryFile.xWrite(buffer, fileOffset);

if (!vfs._inMemoryOnlyFiles.contains(path)) {
Expand Down
12 changes: 6 additions & 6 deletions sqlite3/lib/src/wasm/vfs/memory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import 'dart:math';
import 'dart:typed_data';

import 'package:path/path.dart' as p;
import 'growing_buffer.dart';
import 'dynamic_buffer.dart';

import '../../constants.dart';
import '../../vfs.dart';
import 'utils.dart';

final class InMemoryFileSystem extends BaseVirtualFileSystem {
final Map<String, GrowingByteBuffer?> fileData = {};
final Map<String, DynamicBuffer?> fileData = {};

InMemoryFileSystem({super.name = 'dart-memory', super.random});

Expand All @@ -35,7 +35,7 @@ final class InMemoryFileSystem extends BaseVirtualFileSystem {
final create = flags & SqlFlag.SQLITE_OPEN_CREATE;

if (create != 0) {
fileData[pathStr] = GrowingByteBuffer();
fileData[pathStr] = DynamicBuffer();
} else {
throw VfsException(SqlError.SQLITE_CANTOPEN);
}
Expand Down Expand Up @@ -104,8 +104,8 @@ class _InMemoryFile extends BaseVfsFile {
final file = vfs.fileData[path];

if (file == null) {
vfs.fileData[path] = GrowingByteBuffer();
// TODO: grow?
vfs.fileData[path] = DynamicBuffer();
vfs.fileData[path]!.truncate(size);
} else {
file.truncate(size);
}
Expand All @@ -121,7 +121,7 @@ class _InMemoryFile extends BaseVfsFile {
var file = vfs.fileData[path];

if (file == null) {
file = GrowingByteBuffer();
file = DynamicBuffer();
vfs.fileData[path] = file;
}
file.write(buffer, fileOffset);
Expand Down

0 comments on commit 2b1a3b9

Please sign in to comment.