Skip to content

Commit 3164f2a

Browse files
committed
sync with java: 4c961f143020b34aec8edd04963b599b3475c365
1 parent e7caacb commit 3164f2a

35 files changed

+1725
-43
lines changed

src/Documents/BulkInsertOperation.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,3 +826,8 @@ export class BulkInsertCommand extends RavenCommand<void> {
826826
}
827827

828828
}
829+
830+
export interface BulkInsertOptions {
831+
useCompression?: boolean;
832+
skipOverwriteIfUnchanged?: boolean;
833+
}

src/Documents/Commands/Batches/ClusterWideBatchCommand.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ export class ClusterWideBatchCommand extends SingleNodeBatchCommand implements I
2727
let options = super._appendOptions();
2828

2929
if (TypeUtil.isNullOrUndefined(this._disableAtomicDocumentWrites)) {
30-
return ;
30+
return "";
3131
}
3232

3333
options
3434
+= "&disableAtomicDocumentWrites=" + (this._disableAtomicDocumentWrites ? "true" : "false");
3535

3636
return options;
3737
}
38-
}
38+
}

src/Documents/Commands/GetDocumentsCommand.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ export class GetDocumentsCommand extends RavenCommand<GetDocumentsResult> {
353353
compareExchangeValueIncludes: ObjectUtil.mapCompareExchangeToLocalObject(json.CompareExchangeValueIncludes),
354354
timeSeriesIncludes: ObjectUtil.mapTimeSeriesIncludesToLocalObject(json.TimeSeriesIncludes),
355355
counterIncludes: ObjectUtil.mapCounterIncludesToLocalObject(json.CounterIncludes),
356+
revisionIncludes: json.RevisionIncludes,
356357
nextPageStart: json.NextPageStart
357358
};
358359
}

src/Documents/Commands/QueryCommand.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ export class QueryCommand extends RavenCommand<QueryResult> {
184184
includedCounterNames: json.IncludedCounterNames,
185185
timeSeriesIncludes: ObjectUtil.mapTimeSeriesIncludesToLocalObject(json.TimeSeriesIncludes),
186186
compareExchangeValueIncludes: ObjectUtil.mapCompareExchangeToLocalObject(json.CompareExchangeValueIncludes),
187+
revisionIncludes: json.RevisionIncludes,
187188
timeSeriesFields: json.TimeSeriesFields,
188189
timings: QueryCommand._mapTimingsToLocalObject(json.Timings)
189190
}

src/Documents/Identity/HiloIdGenerator.ts

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { HiloReturnCommand } from "./Commands/HiloReturnCommand";
88
import { NextHiloCommand, HiLoResult } from "./Commands/NextHiloCommand";
99
import { HiloRangeValue } from "./HiloRangeValue";
1010
import { DocumentConventions } from "../Conventions/DocumentConventions";
11+
import { Lazy } from "../Lazy";
1112

1213
export class HiloIdGenerator {
1314
private _store: IDocumentStore;
@@ -20,7 +21,8 @@ export class HiloIdGenerator {
2021
private _prefix?: string = null;
2122
private _lastBatchSize: number = 0;
2223
private _serverTag: string = null;
23-
private _generatorLock = semaphore();
24+
25+
private _nextRangeTask: Lazy<void>;
2426

2527
constructor(tag: string, store: IDocumentStore, dbName: string, identityPartsSeparator: string) {
2628
this._lastRangeAt = DateUtil.zeroDate();
@@ -44,6 +46,8 @@ export class HiloIdGenerator {
4446

4547
public async nextId(): Promise<number> {
4648
while (true) {
49+
const current = this._nextRangeTask;
50+
4751
// local range is not exhausted yet
4852
const range = this._range;
4953

@@ -52,28 +56,34 @@ export class HiloIdGenerator {
5256
return id;
5357
}
5458

55-
let acquiredSemContext: SemaphoreAcquisitionContext;
5659
try {
57-
//local range is exhausted , need to get a new range
58-
acquiredSemContext = acquireSemaphore(this._generatorLock, {
59-
contextName: `${this.constructor.name}_${this._tag}`
60-
});
61-
62-
await acquiredSemContext.promise;
63-
64-
const maybeNewRange = this._range;
65-
if (maybeNewRange !== range) {
66-
id = maybeNewRange.increment();
67-
if (id <= maybeNewRange.maxId) {
68-
return id;
69-
}
60+
// let's try to call the existing task for next range
61+
await current.getValue();
62+
if (range !== this._range) {
63+
continue;
7064
}
65+
} catch (e) {
66+
// previous task was faulted, we will try to replace it
67+
}
7168

72-
await this._getNextRange();
73-
} finally {
74-
if (acquiredSemContext) {
75-
acquiredSemContext.dispose();
76-
}
69+
// local range is exhausted , need to get a new range
70+
const maybeNextTask = new Lazy(() => this._getNextRange());
71+
let changed = false;
72+
if (this._nextRangeTask === current) {
73+
changed = true;
74+
this._nextRangeTask = maybeNextTask;
75+
}
76+
77+
if (changed) {
78+
await maybeNextTask.getValue();
79+
continue;
80+
}
81+
82+
try {
83+
// failed to replace, let's wait on the previous task
84+
await this._nextRangeTask.getValue();
85+
} catch (e) {
86+
// previous task was faulted, we will try again
7787
}
7888
}
7989
}

src/Documents/Identity/MultiDatabaseHiLoIdGenerator.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { DocumentStore } from "../DocumentStore";
33
import { IRavenObject } from "../../Types/IRavenObject";
44
import { DocumentStoreBase } from "../DocumentStoreBase";
55
import { IHiLoIdGenerator } from "./IHiLoIdGenerator";
6-
import { DocumentType } from "../DocumentAbstractions";
76
import { TypeUtil } from "../../Utility/TypeUtil";
87
import { ObjectTypeDescriptor } from "../../Types";
98

@@ -53,7 +52,7 @@ export class MultiDatabaseHiLoIdGenerator implements IHiLoIdGenerator {
5352
return this._generateNextIdFor(database, collectionName);
5453
}
5554

56-
private _generateNextIdFor(database: string, collectionName: string): Promise<number> {
55+
private async _generateNextIdFor(database: string, collectionName: string): Promise<number> {
5756
database = this._store.getEffectiveDatabase(database);
5857

5958
if (!(database in this._generators)) {

src/Documents/Identity/MultiTypeHiLoIdGenerator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ export class MultiTypeHiLoIdGenerator {
112112
} finally {
113113
acquiredSem.dispose();
114114
}
115+
116+
return value.nextId();
115117
}
116118

117119
protected _createGeneratorFor(tag: string): HiloIdGenerator {

src/Documents/Session/DocumentSessionRevisions.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ export class DocumentSessionRevisions extends DocumentSessionRevisionsBase imple
3838
const operation = new GetRevisionOperation(this._session, id, options.start, options.pageSize);
3939

4040
const command = operation.createRequest();
41+
if (!command) {
42+
return operation.getRevisionsFor(options.documentType);
43+
}
44+
if (this._sessionInfo) {
45+
this._sessionInfo.incrementRequestCount();
46+
}
4147
await this._requestExecutor.execute(command, this._sessionInfo);
4248
operation.result = command.result;
4349
return operation.getRevisionsFor(options.documentType);
@@ -52,12 +58,19 @@ export class DocumentSessionRevisions extends DocumentSessionRevisionsBase imple
5258
} as SessionRevisionsMetadataOptions, options || {});
5359
const operation = new GetRevisionOperation(this._session, id, options.start, options.pageSize, true);
5460
const command = operation.createRequest();
61+
if (!command) {
62+
return operation.getRevisionsMetadataFor();
63+
}
64+
if (this._sessionInfo) {
65+
this._sessionInfo.incrementRequestCount();
66+
}
5567
await this._requestExecutor.execute(command, this._sessionInfo);
5668
operation.result = command.result;
5769
return operation.getRevisionsMetadataFor();
5870
}
5971

6072
public async get<TEntity extends object>(id: string, date: Date): Promise<TEntity | null>;
73+
public async get<TEntity extends object>(id: string, date: Date, documentType: DocumentType<TEntity>): Promise<TEntity | null>;
6174
public async get<TEntity extends object>(changeVector: string): Promise<TEntity | null>;
6275
public async get<TEntity extends object>(changeVector: string,
6376
documentType: DocumentType<TEntity>): Promise<TEntity | null>;
@@ -67,7 +80,8 @@ export class DocumentSessionRevisions extends DocumentSessionRevisionsBase imple
6780
: Promise<RevisionsCollectionObject<TEntity>>;
6881
public async get<TEntity extends object>(
6982
changeVectorOrVectorsOrId: string | string[],
70-
documentTypeOrDate?: DocumentType<TEntity> | Date)
83+
documentTypeOrDate?: DocumentType<TEntity> | Date,
84+
documentTypeForDateOverload?: DocumentType<TEntity>)
7185
: Promise<RevisionsCollectionObject<TEntity> | TEntity> {
7286

7387
const documentType = TypeUtil.isDocumentType(documentTypeOrDate)
@@ -76,7 +90,7 @@ export class DocumentSessionRevisions extends DocumentSessionRevisionsBase imple
7690

7791
if (TypeUtil.isDate(documentTypeOrDate)) {
7892
return this._getByIdAndDate(
79-
changeVectorOrVectorsOrId as string, documentTypeOrDate);
93+
changeVectorOrVectorsOrId as string, documentTypeOrDate, documentTypeForDateOverload);
8094
} else {
8195
return this._get(changeVectorOrVectorsOrId, documentType);
8296
}
@@ -86,6 +100,12 @@ export class DocumentSessionRevisions extends DocumentSessionRevisionsBase imple
86100
id: string, date: Date, clazz?: DocumentType<TEntity>) {
87101
const operation = new GetRevisionOperation(this._session, id, date);
88102
const command = operation.createRequest();
103+
if (!command) {
104+
return operation.getRevision(clazz);
105+
}
106+
if (this._sessionInfo) {
107+
this._sessionInfo.incrementRequestCount();
108+
}
89109
await this._requestExecutor.execute(command, this._sessionInfo);
90110
operation.result = command.result;
91111
return operation.getRevision(clazz);
@@ -97,6 +117,14 @@ export class DocumentSessionRevisions extends DocumentSessionRevisionsBase imple
97117
const operation = new GetRevisionOperation(this._session, changeVectorOrVectors as any);
98118

99119
const command = operation.createRequest();
120+
if (!command) {
121+
return TypeUtil.isArray(changeVectorOrVectors)
122+
? operation.getRevisions(documentType)
123+
: operation.getRevision(documentType);
124+
}
125+
if (this._sessionInfo) {
126+
this._sessionInfo.incrementRequestCount();
127+
}
100128
await this._requestExecutor.execute(command, this._sessionInfo);
101129
operation.result = command.result;
102130
return TypeUtil.isArray(changeVectorOrVectors)
@@ -107,6 +135,9 @@ export class DocumentSessionRevisions extends DocumentSessionRevisionsBase imple
107135
public async getCountFor(id: string): Promise<number> {
108136
const operation = new GetRevisionsCountOperation(id);
109137
const command = operation.createRequest();
138+
if (this._sessionInfo) {
139+
this._sessionInfo.incrementRequestCount();
140+
}
110141
await this._requestExecutor.execute(command, this._sessionInfo);
111142
return command.result;
112143
}

src/Documents/Session/IRevisionsSessionOperations.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ export interface IRevisionsSessionOperations {
4040
*/
4141
get<TEntity extends object>(id: string, date: Date): Promise<TEntity>;
4242

43+
/**
44+
* Returns a document revision by date.
45+
*/
46+
get<TEntity extends object>(id: string, date: Date, documentType: DocumentType<TEntity>): Promise<TEntity>;
47+
4348
/**
4449
* Returns a document revision by change vector.
4550
*/

src/Documents/Session/InMemoryDocumentSessionOperations.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,49 @@ export abstract class InMemoryDocumentSessionOperations
650650
}
651651
}
652652

653+
public registerRevisionIncludes(revisionIncludes: any[]) {
654+
if (this.noTracking) {
655+
return;
656+
}
657+
658+
if (!revisionIncludes) {
659+
return;
660+
}
661+
662+
if (!this.includeRevisionsByChangeVector) {
663+
this.includeRevisionsByChangeVector = CaseInsensitiveKeysMap.create();
664+
}
665+
666+
if (!this.includeRevisionsIdByDateTimeBefore) {
667+
this.includeRevisionsIdByDateTimeBefore = CaseInsensitiveKeysMap.create();
668+
}
669+
670+
for (const obj of revisionIncludes) {
671+
if (!obj) {
672+
continue;
673+
}
674+
675+
const json = obj;
676+
const id = json.Id;
677+
const changeVector = json.ChangeVector;
678+
const beforeAsText = json.Before;
679+
const dateTime = beforeAsText ? DateUtil.utc.parse(beforeAsText) : null;
680+
const revision = json.Revision;
681+
682+
this.includeRevisionsByChangeVector.set(changeVector, DocumentInfo.getNewDocumentInfo(revision));
683+
684+
if (dateTime && !StringUtil.isNullOrWhitespace(id)) {
685+
const map = new Map<number, DocumentInfo>();
686+
687+
this.includeRevisionsIdByDateTimeBefore.set(id, map);
688+
689+
const documentInfo = new DocumentInfo();
690+
documentInfo.document = revision;
691+
map.set(dateTime.getTime(), documentInfo);
692+
}
693+
}
694+
}
695+
653696
public registerMissingIncludes(results: object[], includes: object, includePaths: string[]): void {
654697
if (this.noTracking) {
655698
return;

0 commit comments

Comments
 (0)