Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions src/Documents/Identity/HiloIdGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,13 @@ export class HiloIdGenerator {
}

public async nextId(): Promise<number> {

const getNextIdWithinRange = async (): Promise<number> => {
while (true) {
// local range is not exhausted yet
const range = this._range;

let id = range.increment();
if (id <= range.maxId) {
return Promise.resolve(id);
return id;
}

let acquiredSemContext: AcquiredSemaphoreContext;
Expand All @@ -73,15 +72,12 @@ export class HiloIdGenerator {
}

await this._getNextRange();
return getNextIdWithinRange();
} finally {
if (acquiredSemContext) {
acquiredSemContext.dispose();
}
}
};

return getNextIdWithinRange();
}
}

public returnUnusedRange(): Promise<void> {
Expand All @@ -91,24 +87,24 @@ export class HiloIdGenerator {
return executor.execute(new HiloReturnCommand(this._tag, range.current, range.maxId));
}

protected _getNextRange(): Promise<void> {
protected async _getNextRange(): Promise<void> {
const hiloCmd = new NextHiloCommand(
this._tag,
this._lastBatchSize,
this._lastRangeAt,
this._identityPartsSeparator,
this._range.maxId,
this._store.conventions);
return this._store.getRequestExecutor(this._dbName).execute(hiloCmd)
.then(() => {
const result: HiLoResult = hiloCmd.result;
this._prefix = result.prefix;
this._lastBatchSize = result.lastSize;
this._serverTag = result.serverTag || null;
this._lastRangeAt = result.lastRangeAt;

this._range = new HiloRangeValue(result.low, result.high);
});

await this._store.getRequestExecutor(this._dbName).execute(hiloCmd);

const result: HiLoResult = hiloCmd.result;
this._prefix = result.prefix;
this._lastBatchSize = result.lastSize;
this._serverTag = result.serverTag || null;
this._lastRangeAt = result.lastRangeAt;

this._range = new HiloRangeValue(result.low, result.high);
}

protected _assembleDocumentId(currentRangeValue: number): string {
Expand Down