Skip to content

Commit ef8f513

Browse files
committed
refactor: wip
1 parent 5396322 commit ef8f513

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

packages/utils/src/lib/wal.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -377,15 +377,14 @@ export function setCoordinatorProcess(
377377
}
378378
}
379379

380-
// eslint-disable-next-line functional/no-let
381-
let shardCount = 0;
382-
383380
/**
384381
* Simple counter implementation for generating sequential IDs.
385382
*/
386-
const shardCounter: Counter = {
387-
next: () => ++shardCount,
388-
};
383+
const shardCounter: Counter = (() => {
384+
// eslint-disable-next-line functional/no-let
385+
let count = 0;
386+
return { next: () => ++count };
387+
})();
389388

390389
/**
391390
* Generates a unique sharded WAL ID based on performance time origin, process ID, thread ID, and instance count.

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ describe('stringCodec', () => {
506506
});
507507
});
508508

509-
describe('getShardId', () => {
509+
describe('getUniqueInstanceId', () => {
510510
it('should generate shard ID with readable timestamp', () => {
511511
const counter = { next: () => 1 };
512512
const result = getUniqueInstanceId(counter);
@@ -527,25 +527,30 @@ describe('getShardId', () => {
527527
});
528528

529529
it('should handle zero values', () => {
530-
const result = getShardId();
530+
const counter = { next: () => 1 };
531+
const result = getUniqueInstanceId(counter);
531532
expect(result).toStartWith('20231114-221320-000.');
532533
});
533534

534535
it('should handle negative timestamps', () => {
535-
const result = getShardId();
536+
const counter = { next: () => 1 };
537+
const result = getUniqueInstanceId(counter);
536538

537539
expect(result).toStartWith('20231114-221320-000.');
538540
});
539541

540542
it('should handle large timestamps', () => {
541-
const result = getShardId();
543+
const counter = { next: () => 1 };
544+
const result = getUniqueInstanceId(counter);
542545

543546
expect(result).toStartWith('20231114-221320-000.');
544547
});
545548

546549
it('should generate incrementing counter', () => {
547-
const result1 = getShardId();
548-
const result2 = getShardId();
550+
let count = 0;
551+
const counter = { next: () => ++count };
552+
const result1 = getUniqueInstanceId(counter);
553+
const result2 = getUniqueInstanceId(counter);
549554

550555
const parts1 = result1.split('.');
551556
const parts2 = result2.split('.');
@@ -558,7 +563,7 @@ describe('getShardId', () => {
558563
});
559564
});
560565

561-
describe('getShardedGroupId', () => {
566+
describe('getUniqueTimeId', () => {
562567
it('should work with mocked timeOrigin', () => {
563568
const result = getUniqueTimeId();
564569

@@ -824,7 +829,7 @@ describe('ShardedWal', () => {
824829
},
825830
coordinatorIdEnvVar: SHARDED_WAL_COORDINATOR_ID_ENV_VAR,
826831
});
827-
// Create the group directory (matches actual getShardedGroupId() output)
832+
// Create the group directory (matches actual getUniqueTimeId() output)
828833
vol.mkdirSync('/empty/20231114-221320-000', { recursive: true });
829834
const files = (sw as any).shardFiles();
830835
expect(files).toEqual([]);

0 commit comments

Comments
 (0)