Skip to content

Commit 80db5a2

Browse files
[REFACTORING] Get rid of StatusFactory (#135)
Get rid of StatusFactory Signed-off-by: Levko Kravets <levko.ne@gmail.com>
1 parent d9c9ce5 commit 80db5a2

File tree

10 files changed

+167
-163
lines changed

10 files changed

+167
-163
lines changed

lib/DBSQLClient.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import IAuthentication from './connection/contracts/IAuthentication';
1414
import NoSaslAuthentication from './connection/auth/NoSaslAuthentication';
1515
import HttpConnection from './connection/connections/HttpConnection';
1616
import IConnectionOptions from './connection/contracts/IConnectionOptions';
17-
import StatusFactory from './factory/StatusFactory';
17+
import Status from './dto/Status';
1818
import HiveDriverError from './errors/HiveDriverError';
1919
import { buildUserAgentString, definedOrError } from './utils';
2020
import PlainHttpAuthentication from './connection/auth/PlainHttpAuthentication';
@@ -46,8 +46,6 @@ export default class DBSQLClient extends EventEmitter implements IDBSQLClient {
4646

4747
private connection: IThriftConnection | null;
4848

49-
private readonly statusFactory: StatusFactory;
50-
5149
private connectionProvider: IConnectionProvider;
5250

5351
private authProvider: IAuthentication;
@@ -60,7 +58,6 @@ export default class DBSQLClient extends EventEmitter implements IDBSQLClient {
6058
super();
6159
this.connectionProvider = new HttpConnection();
6260
this.authProvider = new NoSaslAuthentication();
63-
this.statusFactory = new StatusFactory();
6461
this.logger = options?.logger || new DBSQLLogger();
6562
this.client = null;
6663
this.connection = null;
@@ -155,7 +152,7 @@ export default class DBSQLClient extends EventEmitter implements IDBSQLClient {
155152
...getInitialNamespaceOptions(request.initialCatalog, request.initialSchema),
156153
});
157154

158-
this.statusFactory.create(response.status);
155+
Status.assert(response.status);
159156
return new DBSQLSession(driver, definedOrError(response.sessionHandle), this.logger);
160157
}
161158

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import { TOperationHandle, TStatusCode, TCloseOperationResp } from '../../thrift/TCLIService_types';
1+
import { TOperationHandle, TCloseOperationResp } from '../../thrift/TCLIService_types';
22
import HiveDriver from '../hive/HiveDriver';
3-
import StatusFactory from '../factory/StatusFactory';
43
import Status from '../dto/Status';
54

65
export default class CompleteOperationHelper {
76
private readonly driver: HiveDriver;
87

98
private readonly operationHandle: TOperationHandle;
109

11-
private readonly statusFactory = new StatusFactory();
12-
1310
public closed: boolean = false;
1411

1512
public cancelled: boolean = false;
@@ -19,38 +16,34 @@ export default class CompleteOperationHelper {
1916
this.operationHandle = operationHandle;
2017

2118
if (closeOperation) {
22-
this.statusFactory.create(closeOperation.status);
19+
Status.assert(closeOperation.status);
2320
this.closed = true;
2421
}
2522
}
2623

2724
public async cancel(): Promise<Status> {
2825
if (this.cancelled) {
29-
return this.statusFactory.create({
30-
statusCode: TStatusCode.SUCCESS_STATUS,
31-
});
26+
return Status.success();
3227
}
3328

3429
const response = await this.driver.cancelOperation({
3530
operationHandle: this.operationHandle,
3631
});
37-
const status = this.statusFactory.create(response.status);
32+
Status.assert(response.status);
3833
this.cancelled = true;
39-
return status;
34+
return new Status(response.status);
4035
}
4136

4237
public async close(): Promise<Status> {
4338
if (this.closed) {
44-
return this.statusFactory.create({
45-
statusCode: TStatusCode.SUCCESS_STATUS,
46-
});
39+
return Status.success();
4740
}
4841

4942
const response = await this.driver.closeOperation({
5043
operationHandle: this.operationHandle,
5144
});
52-
const status = this.statusFactory.create(response.status);
45+
Status.assert(response.status);
5346
this.closed = true;
54-
return status;
47+
return new Status(response.status);
5548
}
5649
}

lib/DBSQLOperation/FetchResultsHelper.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import {
44
TFetchResultsResp,
55
TOperationHandle,
66
TRowSet,
7-
TStatus,
87
} from '../../thrift/TCLIService_types';
98
import { ColumnCode, FetchType, Int64 } from '../hive/Types';
109
import HiveDriver from '../hive/HiveDriver';
11-
import StatusFactory from '../factory/StatusFactory';
10+
import Status from '../dto/Status';
1211

1312
function checkIfOperationHasMoreRows(response: TFetchResultsResp): boolean {
1413
if (response.hasMoreRows) {
@@ -43,8 +42,6 @@ export default class FetchResultsHelper {
4342

4443
private fetchOrientation: TFetchOrientation = TFetchOrientation.FETCH_FIRST;
4544

46-
private readonly statusFactory = new StatusFactory();
47-
4845
private prefetchedResults: TFetchResultsResp[] = [];
4946

5047
public hasMoreRows: boolean = false;
@@ -63,12 +60,8 @@ export default class FetchResultsHelper {
6360
});
6461
}
6562

66-
private assertStatus(responseStatus: TStatus): void {
67-
this.statusFactory.create(responseStatus);
68-
}
69-
7063
private processFetchResponse(response: TFetchResultsResp): TRowSet | undefined {
71-
this.assertStatus(response.status);
64+
Status.assert(response.status);
7265
this.fetchOrientation = TFetchOrientation.FETCH_NEXT;
7366
this.hasMoreRows = checkIfOperationHasMoreRows(response);
7467
return response.results;

lib/DBSQLOperation/OperationStatusHelper.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TOperationHandle, TOperationState, TGetOperationStatusResp } from '../../thrift/TCLIService_types';
22
import HiveDriver from '../hive/HiveDriver';
3-
import StatusFactory from '../factory/StatusFactory';
3+
import Status from '../dto/Status';
44
import { WaitUntilReadyOptions } from '../contracts/IOperation';
55
import OperationStateError from '../errors/OperationStateError';
66

@@ -17,8 +17,6 @@ export default class OperationStatusHelper {
1717

1818
private readonly operationHandle: TOperationHandle;
1919

20-
private readonly statusFactory = new StatusFactory();
21-
2220
private state: number = TOperationState.INITIALIZED_STATE;
2321

2422
// Once operation is finished or fails - cache status response, because subsequent calls
@@ -49,7 +47,7 @@ export default class OperationStatusHelper {
4947
}
5048

5149
private processOperationStatusResponse(response: TGetOperationStatusResp) {
52-
this.statusFactory.create(response.status);
50+
Status.assert(response.status);
5351

5452
this.state = response.operationState ?? this.state;
5553

lib/DBSQLOperation/SchemaHelper.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TGetResultSetMetadataResp, TOperationHandle, TSparkRowSetType } from '../../thrift/TCLIService_types';
22
import HiveDriver from '../hive/HiveDriver';
3-
import StatusFactory from '../factory/StatusFactory';
3+
import Status from '../dto/Status';
44
import IOperationResult from '../result/IOperationResult';
55
import JsonResult from '../result/JsonResult';
66
import ArrowResult from '../result/ArrowResult';
@@ -12,8 +12,6 @@ export default class SchemaHelper {
1212

1313
private readonly operationHandle: TOperationHandle;
1414

15-
private readonly statusFactory = new StatusFactory();
16-
1715
private metadata?: TGetResultSetMetadataResp;
1816

1917
constructor(driver: HiveDriver, operationHandle: TOperationHandle, metadata?: TGetResultSetMetadataResp) {
@@ -27,7 +25,7 @@ export default class SchemaHelper {
2725
const metadata = await this.driver.getResultSetMetadata({
2826
operationHandle: this.operationHandle,
2927
});
30-
this.statusFactory.create(metadata.status);
28+
Status.assert(metadata.status);
3129
this.metadata = metadata;
3230
}
3331

lib/DBSQLSession.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import IDBSQLSession, {
2323
import IOperation from './contracts/IOperation';
2424
import DBSQLOperation from './DBSQLOperation';
2525
import Status from './dto/Status';
26-
import StatusFactory from './factory/StatusFactory';
2726
import InfoValue from './dto/InfoValue';
2827
import { definedOrError } from './utils';
2928
import IDBSQLLogger, { LogLevel } from './contracts/IDBSQLLogger';
@@ -78,14 +77,11 @@ export default class DBSQLSession implements IDBSQLSession {
7877

7978
private readonly sessionHandle: TSessionHandle;
8079

81-
private readonly statusFactory: StatusFactory;
82-
8380
private readonly logger: IDBSQLLogger;
8481

8582
constructor(driver: HiveDriver, sessionHandle: TSessionHandle, logger: IDBSQLLogger) {
8683
this.driver = driver;
8784
this.sessionHandle = sessionHandle;
88-
this.statusFactory = new StatusFactory();
8985
this.logger = logger;
9086
this.logger.log(LogLevel.debug, `Session created with id: ${this.getId()}`);
9187
}
@@ -108,7 +104,7 @@ export default class DBSQLSession implements IDBSQLSession {
108104
infoType,
109105
});
110106

111-
this.assertStatus(response.status);
107+
Status.assert(response.status);
112108
return new InfoValue(response.infoValue);
113109
}
114110

@@ -305,16 +301,13 @@ export default class DBSQLSession implements IDBSQLSession {
305301
});
306302

307303
this.logger.log(LogLevel.debug, `Session closed with id: ${this.getId()}`);
308-
return this.statusFactory.create(response.status);
304+
Status.assert(response.status);
305+
return new Status(response.status);
309306
}
310307

311308
private createOperation(response: OperationResponseShape): IOperation {
312-
this.assertStatus(response.status);
309+
Status.assert(response.status);
313310
const handle = definedOrError(response.operationHandle);
314311
return new DBSQLOperation(this.driver, handle, this.logger, response.directResults);
315312
}
316-
317-
private assertStatus(responseStatus: TStatus): void {
318-
this.statusFactory.create(responseStatus);
319-
}
320313
}

lib/dto/Status.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,43 @@
1-
type StatusData = {
2-
success: boolean;
3-
executing: boolean;
4-
infoMessages: Array<string>;
5-
};
1+
import { TStatus, TStatusCode } from '../../thrift/TCLIService_types';
2+
import StatusError from '../errors/StatusError';
63

74
export default class Status {
8-
private isSuccess: boolean;
5+
private readonly status: TStatus;
96

10-
private isExecuting: boolean;
7+
constructor(status: TStatus) {
8+
this.status = status;
9+
}
10+
11+
public get isSuccess(): boolean {
12+
const { statusCode } = this.status;
13+
return statusCode === TStatusCode.SUCCESS_STATUS || statusCode === TStatusCode.SUCCESS_WITH_INFO_STATUS;
14+
}
1115

12-
private infoMessages: Array<string>;
16+
public get isExecuting(): boolean {
17+
const { statusCode } = this.status;
18+
return statusCode === TStatusCode.STILL_EXECUTING_STATUS;
19+
}
1320

14-
constructor(data: StatusData) {
15-
this.isSuccess = data.success;
16-
this.isExecuting = data.executing;
17-
this.infoMessages = data.infoMessages;
21+
public get isError(): boolean {
22+
const { statusCode } = this.status;
23+
return statusCode === TStatusCode.ERROR_STATUS || statusCode === TStatusCode.INVALID_HANDLE_STATUS;
1824
}
1925

20-
success(): boolean {
21-
return this.isSuccess;
26+
public get info(): Array<string> {
27+
return this.status.infoMessages || [];
2228
}
2329

24-
executing(): boolean {
25-
return this.isExecuting;
30+
public static assert(status: TStatus) {
31+
const statusWrapper = new Status(status);
32+
if (statusWrapper.isError) {
33+
throw new StatusError(status);
34+
}
2635
}
2736

28-
getInfo(): Array<string> {
29-
return this.infoMessages;
37+
public static success(info: Array<string> = []): Status {
38+
return new Status({
39+
statusCode: info.length > 0 ? TStatusCode.SUCCESS_WITH_INFO_STATUS : TStatusCode.SUCCESS_STATUS,
40+
infoMessages: info,
41+
});
3042
}
3143
}

lib/factory/StatusFactory.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)