@@ -31,6 +31,7 @@ export class BulkInsertOperation {
3131
3232 private _first : boolean = true ;
3333 private _operationId = - 1 ;
34+ private _nodeTag : string ;
3435
3536 private _useCompression : boolean = false ;
3637
@@ -45,6 +46,9 @@ export class BulkInsertOperation {
4546
4647 public constructor ( database : string , store : IDocumentStore ) {
4748 this . _conventions = store . conventions ;
49+ if ( StringUtil . isNullOrEmpty ( database ) ) {
50+ this . _throwNoDatabase ( ) ;
51+ }
4852 this . _requestExecutor = store . getRequestExecutor ( database ) ;
4953
5054 this . _generateEntityIdOnTheClient = new GenerateEntityIdOnTheClient ( this . _requestExecutor . conventions ,
@@ -65,8 +69,23 @@ export class BulkInsertOperation {
6569 }
6670
6771 private async _throwBulkInsertAborted ( e : Error ) {
68- const error = await this . _getExceptionFromOperation ( ) ;
69- throwError ( "BulkInsertAbortedException" , "Failed to execute bulk insert" , error || e ) ;
72+ let errorFromServer : Error ;
73+ try {
74+ errorFromServer = await this . _getExceptionFromOperation ( ) ;
75+ } catch ( ee ) {
76+ // server is probably down, will propagate the original exception
77+ }
78+
79+ if ( errorFromServer ) {
80+ throw errorFromServer ;
81+ }
82+
83+ throwError ( "BulkInsertAbortedException" , "Failed to execute bulk insert" , e ) ;
84+ }
85+
86+ private _throwNoDatabase ( ) : void {
87+ throwError ( "InvalidOperationException" , "Cannot start bulk insert operation without specifying a name of a database to operate on."
88+ + "Database name can be passed as an argument when bulk insert is being created or default database can be defined using 'DocumentStore.setDatabase' method." ) ;
7089 }
7190
7291 private async _waitForId ( ) : Promise < void > {
@@ -77,6 +96,7 @@ export class BulkInsertOperation {
7796 const bulkInsertGetIdRequest = new GetNextOperationIdCommand ( ) ;
7897 await this . _requestExecutor . execute ( bulkInsertGetIdRequest ) ;
7998 this . _operationId = bulkInsertGetIdRequest . result ;
99+ this . _nodeTag = bulkInsertGetIdRequest . nodeTag ;
80100 }
81101
82102 private static _typeCheckStoreArgs (
@@ -242,7 +262,7 @@ export class BulkInsertOperation {
242262 }
243263
244264 private async _getExceptionFromOperation ( ) : Promise < Error > {
245- const stateRequest = new GetOperationStateCommand ( this . _conventions , this . _operationId ) ;
265+ const stateRequest = new GetOperationStateCommand ( this . _conventions , this . _operationId , this . _nodeTag ) ;
246266 await this . _requestExecutor . execute ( stateRequest ) ;
247267 if ( ! stateRequest . result ) {
248268 return null ;
@@ -263,7 +283,8 @@ export class BulkInsertOperation {
263283
264284 this . _requestBodyStream = new stream . PassThrough ( ) ;
265285 const bulkCommand =
266- new BulkInsertCommand ( this . _operationId , this . _requestBodyStream , this . _useCompression ) ;
286+ new BulkInsertCommand ( this . _operationId , this . _requestBodyStream , this . _nodeTag ) ;
287+ bulkCommand . useCompression = this . _useCompression ;
267288
268289 const bulkCommandPromise = this . _requestExecutor . execute ( bulkCommand ) ;
269290
@@ -298,7 +319,7 @@ export class BulkInsertOperation {
298319 await this . _waitForId ( ) ;
299320
300321 try {
301- await this . _requestExecutor . execute ( new KillOperationCommand ( this . _operationId ) ) ;
322+ await this . _requestExecutor . execute ( new KillOperationCommand ( this . _operationId , this . _nodeTag ) ) ;
302323 } catch ( err ) {
303324 const bulkInsertError = getError ( "BulkInsertAbortedException" ,
304325 "Unable to kill bulk insert operation, because it was not found on the server." , err ) ;
@@ -365,14 +386,14 @@ export class BulkInsertCommand extends RavenCommand<void> {
365386
366387 private readonly _stream : stream . Readable ;
367388 private readonly _id : number ;
368- private _useCompression : boolean ;
389+ public useCompression : boolean ;
369390
370- public constructor ( id : number , stream : stream . Readable , useCompression : boolean ) {
391+ public constructor ( id : number , stream : stream . Readable , nodeTag : string ) {
371392 super ( ) ;
372393
373394 this . _stream = stream ;
374395 this . _id = id ;
375- this . _useCompression = useCompression ;
396+ this . _selectedNodeTag = nodeTag ;
376397 }
377398
378399 public createRequest ( node : ServerNode ) : HttpRequestParameters {
0 commit comments