Skip to content

Commit be3b646

Browse files
committed
refactor: encapsulate pooling helpers
1 parent 951207c commit be3b646

File tree

2 files changed

+39
-44
lines changed

2 files changed

+39
-44
lines changed

src/objectid.ts

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,39 @@ import { type InspectFn, defaultInspect } from './parser/utils';
44
import { ByteUtils } from './utils/byte_utils';
55
import { NumberUtils } from './utils/number_utils';
66

7-
// Settings for ObjectId Buffer pool
8-
// Disable pool by default in order to ensure compatibility
9-
// Specify larger poolSize to enable pool
10-
let currentPool: Uint8Array | null = null;
11-
let poolSize = 1; // Disable pool by default.
12-
let currentPoolOffset = 0;
7+
const ObjectIdPooling: {
8+
currentPool: Uint8Array | null;
9+
poolSize: number;
10+
currentPoolOffset: number;
11+
getPool(): { readonly currentPool: Uint8Array; readonly currentPoolOffset: number };
12+
incrementPool(): void;
13+
} = {
14+
// Settings for ObjectId Buffer pool
15+
// Disable pool by default in order to ensure compatibility
16+
// Specify larger poolSize to enable pool
17+
currentPool: null,
18+
poolSize: 1, // Disable pool by default.
19+
currentPoolOffset: 0,
20+
/**
21+
* Retrieves a ObjectId pool and offset. This function may create a new ObjectId buffer pool and reset the pool offset
22+
* @internal
23+
*/
24+
getPool() {
25+
if (!this.currentPool || this.currentPoolOffset + 12 > this.currentPool.length) {
26+
this.currentPool = ByteUtils.allocateUnsafe(this.poolSize * 12);
27+
this.currentPoolOffset = 0;
28+
}
29+
return { currentPool: this.currentPool, currentPoolOffset: this.currentPoolOffset } as const;
30+
},
1331

14-
/**
15-
* Retrieves a ObjectId pool and offset. This function may create a new ObjectId buffer pool and reset the pool offset
16-
* @internal
17-
*/
18-
function getPool(): [Uint8Array, number] {
19-
if (!currentPool || currentPoolOffset + 12 > currentPool.length) {
20-
currentPool = ByteUtils.allocateUnsafe(poolSize * 12);
21-
currentPoolOffset = 0;
32+
/**
33+
* Increments the pool offset by 12 bytes
34+
* @internal
35+
*/
36+
incrementPool(): void {
37+
this.currentPoolOffset += 12;
2238
}
23-
return [currentPool, currentPoolOffset];
24-
}
25-
26-
/**
27-
* Increments the pool offset by 12 bytes
28-
* @internal
29-
*/
30-
function incrementPool(): void {
31-
currentPoolOffset += 12;
32-
}
39+
};
3340

3441
// Unique sequence for the current process (initialized on first use)
3542
let PROCESS_UNIQUE: Uint8Array | null = null;
@@ -70,11 +77,11 @@ export class ObjectId extends BSONValue {
7077
* The size of the current ObjectId buffer pool.
7178
*/
7279
static get poolSize(): number {
73-
return poolSize;
80+
return ObjectIdPooling.poolSize;
7481
}
7582

7683
static set poolSize(size: number) {
77-
poolSize = Math.max(Math.abs(Number(size)) >>> 0, 1);
84+
ObjectIdPooling.poolSize = Math.max(Math.abs(Number(size)) >>> 0, 1);
7885
}
7986

8087
/** ObjectId buffer pool pointer @internal */
@@ -168,11 +175,13 @@ export class ObjectId extends BSONValue {
168175
let offset: number;
169176

170177
// Special case when poolSize === 1 and a 12 byte buffer is passed in - just persist buffer
171-
if (poolSize === 1 && ArrayBuffer.isView(workingId) && workingId.length === 12) {
178+
if (ObjectId.poolSize === 1 && ArrayBuffer.isView(workingId) && workingId.length === 12) {
172179
pool = ByteUtils.toLocalBufferType(workingId);
173180
offset = 0;
174181
} else {
175-
[pool, offset] = getPool();
182+
const currentPool = ObjectIdPooling.getPool();
183+
pool = currentPool.currentPool;
184+
offset = currentPool.currentPoolOffset;
176185

177186
// The following cases use workingId to construct an ObjectId
178187
if (workingId == null || typeof workingId === 'number') {
@@ -211,15 +220,10 @@ export class ObjectId extends BSONValue {
211220
// Increment pool offset once we have completed initialization
212221
this.pool = pool;
213222
// Only set offset if pool is used
214-
if (poolSize > 1) {
223+
if (ObjectId.poolSize > 1) {
215224
this.offset = offset;
216225
}
217-
incrementPool();
218-
}
219-
220-
/** ObjectId bytes @internal */
221-
get buffer(): Uint8Array {
222-
return this.id;
226+
ObjectIdPooling.incrementPool();
223227
}
224228

225229
/** ObjectId bytes @internal */

test/node/object_id.test.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,6 @@ import { expect } from 'chai';
55
import { bufferFromHexArray } from './tools/utils';
66
import { isBufferOrUint8Array } from './tools/utils';
77

8-
ObjectId.poolSize = 100;
9-
10-
declare module '../register-bson' {
11-
interface ObjectId {
12-
pool: Uint8Array;
13-
offset: number;
14-
}
15-
}
16-
178
describe('ObjectId', function () {
189
describe('static createFromTime()', () => {
1910
it('creates an objectId with user defined value in the timestamp field', function () {

0 commit comments

Comments
 (0)