Skip to content

Commit

Permalink
fix(libs): fix OHW decorator counter, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zhibirc committed Dec 14, 2023
1 parent 26a42fd commit 4ad8da2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
3 changes: 2 additions & 1 deletion fifo-cache/spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it } from 'node:test';
import assert from 'node:assert';
import assert from 'node:assert/strict';
import FifoCache from './index';

describe('Test FIFO cache API', () => {
it('Create FIFO cache instance', () => {
Expand Down
34 changes: 18 additions & 16 deletions libs/decorators/detect-one-hit-wonder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,34 @@ const TYPE_FILTER_CUCKOO = Symbol('TYPE_FILTER_CUCKOO');
const detectOneHitWonder = (() => {
let filter;

return (threshold: number = 1, filterType: symbol = TYPE_FILTER_MAP) => {
return (filterType: symbol = TYPE_FILTER_MAP) => {
return function (originalMethod: any, _context: ClassMethodDecoratorContext) {
function replacementMethod(this: any, key: any, value?: any) {
const { capacity } = this.stats;
if (!filter) {
const { capacity } = this.stats;

switch (filterType) {
case TYPE_FILTER_MAP:
filter = new Map();
break;
case TYPE_FILTER_BLOOM:
filter = new BloomFilter(capacity);
break;
case TYPE_FILTER_CUCKOO:
filter = new CuckooFilter(capacity);
break;
switch (filterType) {
case TYPE_FILTER_MAP:
filter = new Map();
break;
case TYPE_FILTER_BLOOM:
filter = new BloomFilter(capacity);
break;
case TYPE_FILTER_CUCKOO:
filter = new CuckooFilter(capacity);
break;
}
}
let n = filter.get(key) ?? 0;

filter.set(key, ++n);

switch (originalMethod.name) {
case 'read':
filter.set(key, 1);
return originalMethod.call(this, key);
case 'add':
if (n > threshold) {
if (filter.get(key)) {
return originalMethod.call(this, key, value);
} else {
filter.set(key, 1);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion libs/get-typed-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function getTypedArray (n: number) {
case n - 1 <= MAX_32BIT_UINT:
return new Uint32Array(n);
default:
return new Array(n);
return new Float64Array(n);
}
}

Expand Down
3 changes: 2 additions & 1 deletion rr-cache/spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it } from 'node:test';
import assert from 'node:assert';
import assert from 'node:assert/strict';
import RRCache from './index';

describe('Test RR cache API', () => {
it('Create RR cache instance', () => {
Expand Down

0 comments on commit 4ad8da2

Please sign in to comment.