Skip to content

Commit a19070e

Browse files
committed
refactor: wip
1 parent 592fb89 commit a19070e

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

packages/utils/src/lib/wal.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export class WriteAheadLogFile<T> implements AppendableSink<T> {
118118
#fd: number | null = null;
119119
readonly #file: string;
120120
readonly #decode: Codec<T | InvalidEntry<string>>['decode'];
121-
readonly #encode: Codec<T | InvalidEntry<string>>['encode'];
121+
readonly #encode: Codec<T>['encode'];
122122

123123
/**
124124
* Create a new WAL file instance.
@@ -202,7 +202,7 @@ export class WriteAheadLogFile<T> implements AppendableSink<T> {
202202
console.log('Found invalid entries during WAL repack');
203203
}
204204
const recordsToWrite = hasInvalidEntries
205-
? r.records
205+
? (r.records as T[])
206206
: filterValidRecords(r.records);
207207
fs.mkdirSync(path.dirname(out), { recursive: true });
208208
fs.writeFileSync(out, `${recordsToWrite.map(this.#encode).join('\n')}\n`);

packages/utils/src/lib/wal.unit.test.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,18 @@ describe('WriteAheadLogFile', () => {
160160
vol.fromJSON({}, MEMFS_VOLUME);
161161
});
162162

163+
it('should act as WLA for any kind of data', () => {
164+
const w = wal('/test/a.log', stringCodec<object>());
165+
w.open();
166+
w.append({ id: 1, name: 'test' });
167+
w.close();
168+
expect(w.recover()).toStrictEqual({ id: 1, name: 'test' });
169+
expect(() =>
170+
w.append('{ id: 1, name:...' as unknown as object),
171+
).not.toThrow();
172+
w.expect(w.recover()).toStrictEqual({ id: 1, name: 'test' });
173+
});
174+
163175
it('should create instance with file path and codecs without opening', () => {
164176
const w = wal('/test/a.log');
165177
expect(w).toBeInstanceOf(WriteAheadLogFile);
@@ -797,8 +809,12 @@ describe('ShardedWal', () => {
797809
const files = (sw as any).shardFiles();
798810

799811
expect(files).toHaveLength(2);
800-
expect(files).toContain('/shards/wal.1.log');
801-
expect(files).toContain('/shards/wal.2.log');
812+
expect(files).toEqual(
813+
expect.arrayContaining([
814+
expect.pathToMatch('/shards/wal.1.log'),
815+
expect.pathToMatch('/shards/wal.2.log'),
816+
]),
817+
);
802818
});
803819

804820
it('should finalize empty shards to empty result', () => {

0 commit comments

Comments
 (0)