Skip to content

Commit 2b1a3b9

Browse files
committed
Cleanup.
1 parent 6b4f3e3 commit 2b1a3b9

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

sqlite3/lib/src/wasm/vfs/growing_buffer.dart renamed to sqlite3/lib/src/wasm/vfs/dynamic_buffer.dart

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@ import 'dart:typed_data';
22

33
/// A utility class that manages a growing byte buffer.
44
/// It dynamically increases its capacity in factors of 2 when needed.
5-
class GrowingByteBuffer {
5+
class DynamicBuffer {
66
Uint8List _buffer;
77
int _capacity;
88
int _length = 0;
99

10-
/// Creates a [GrowingByteBuffer] with an optional initial capacity.
10+
/// Creates a [DynamicBuffer] with an optional initial capacity.
1111
/// The default initial capacity is 1024 bytes.
12-
GrowingByteBuffer([int initialCapacity = 1024])
12+
DynamicBuffer([int initialCapacity = 1024])
1313
: _capacity = initialCapacity,
14-
_buffer = Uint8List(initialCapacity);
14+
_buffer = Uint8List(initialCapacity) {
15+
if (initialCapacity < 1) {
16+
throw ArgumentError("initialCapacity must be positive");
17+
}
18+
}
1519

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

3741
/// Truncates the buffer to a specific [newSize].
42+
///
43+
/// No memory is freed when using [truncate].
44+
///
3845
/// If [newSize] is less than the current length, the buffer is truncated.
3946
/// If [newSize] is greater than the current length, the buffer length is extended with zeros.
4047
void truncate(int newSize) {
@@ -49,7 +56,7 @@ class GrowingByteBuffer {
4956
_length = newSize;
5057
}
5158

52-
/// Returns a [Uint8List] containing the data up to the current length.
59+
/// Returns a [Uint8List] view containing the data up to the current length.
5360
Uint8List toUint8List() {
5461
return Uint8List.view(_buffer.buffer, 0, _length);
5562
}
@@ -63,7 +70,6 @@ class GrowingByteBuffer {
6370
newCapacity *= 2;
6471
}
6572
// Allocate a new buffer and copy existing data.
66-
print('resizing to $newCapacity');
6773
Uint8List newBuffer = Uint8List(newCapacity);
6874
newBuffer.setRange(0, _length, _buffer);
6975
_buffer = newBuffer;

sqlite3/lib/src/wasm/vfs/indexed_db.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'dart:math';
88
import 'dart:typed_data';
99

1010
import 'package:meta/meta.dart';
11-
import 'package:sqlite3/src/wasm/vfs/growing_buffer.dart';
11+
import 'package:sqlite3/src/wasm/vfs/dynamic_buffer.dart';
1212
import 'package:web/web.dart' as web;
1313

1414
import '../../constants.dart';
@@ -534,7 +534,7 @@ final class IndexedDbFileSystem extends BaseVirtualFileSystem {
534534
final name = entry.key;
535535
final fileId = entry.value;
536536

537-
final buffer = GrowingByteBuffer();
537+
final buffer = DynamicBuffer();
538538
final data = await _asynchronous.readFully(fileId);
539539
buffer.add(data);
540540

@@ -647,7 +647,7 @@ class _IndexedDbFile implements VirtualFileSystemFile {
647647
void xWrite(Uint8List buffer, int fileOffset) {
648648
vfs._checkClosed();
649649

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

653653
if (!vfs._inMemoryOnlyFiles.contains(path)) {

sqlite3/lib/src/wasm/vfs/memory.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import 'dart:math';
22
import 'dart:typed_data';
33

44
import 'package:path/path.dart' as p;
5-
import 'growing_buffer.dart';
5+
import 'dynamic_buffer.dart';
66

77
import '../../constants.dart';
88
import '../../vfs.dart';
99
import 'utils.dart';
1010

1111
final class InMemoryFileSystem extends BaseVirtualFileSystem {
12-
final Map<String, GrowingByteBuffer?> fileData = {};
12+
final Map<String, DynamicBuffer?> fileData = {};
1313

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

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

3737
if (create != 0) {
38-
fileData[pathStr] = GrowingByteBuffer();
38+
fileData[pathStr] = DynamicBuffer();
3939
} else {
4040
throw VfsException(SqlError.SQLITE_CANTOPEN);
4141
}
@@ -104,8 +104,8 @@ class _InMemoryFile extends BaseVfsFile {
104104
final file = vfs.fileData[path];
105105

106106
if (file == null) {
107-
vfs.fileData[path] = GrowingByteBuffer();
108-
// TODO: grow?
107+
vfs.fileData[path] = DynamicBuffer();
108+
vfs.fileData[path]!.truncate(size);
109109
} else {
110110
file.truncate(size);
111111
}
@@ -121,7 +121,7 @@ class _InMemoryFile extends BaseVfsFile {
121121
var file = vfs.fileData[path];
122122

123123
if (file == null) {
124-
file = GrowingByteBuffer();
124+
file = DynamicBuffer();
125125
vfs.fileData[path] = file;
126126
}
127127
file.write(buffer, fileOffset);

0 commit comments

Comments
 (0)