-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-range.ts
More file actions
115 lines (96 loc) · 3.71 KB
/
get-range.ts
File metadata and controls
115 lines (96 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import type { Client } from "../client";
import type { ConsumedCapacity, Direction, Filter, TimeRange } from "../pb/type";
import type { PlainBufferCell, PlainBufferRow } from "../plainbuffer";
import { Buffer } from "node:buffer";
import { buildFilter } from "../builder/filter";
import { OTS_API_NAME } from "../const";
import { builder } from "../pb/builder";
import { decodePlainBuffer, encodePlainBuffer } from "../plainbuffer";
import { fixPlainBufferCellType } from "../utils";
export const ProtoGetRangeRequest = builder.lookupType("ots.GetRangeRequest");
export const ProtoGetRangeResponse = builder.lookupType("ots.GetRangeResponse");
export interface GetRangeData {
tableName: string;
direction: typeof Direction[keyof typeof Direction];
columnsToGet?: string[];
timeRange?: TimeRange;
maxVersions?: number;
limit?: number;
inclusiveStartPrimaryKey: PlainBufferCell;
exclusiveEndPrimaryKey: PlainBufferCell;
filter?: Filter;
startColumn?: string;
endColumn?: string;
}
export interface GetRangeResponse {
consumed: ConsumedCapacity;
rows: Array<PlainBufferRow> | null;
nextStartPrimaryKey: PlainBufferRow | null;
}
export class GetRange {
public constructor(private readonly client: Client) {
}
public static async builder(options: GetRangeData) {
const payload: Record<string, any> = {
tableName: options.tableName,
direction: options.direction,
inclusiveStartPrimaryKey: Buffer.from(encodePlainBuffer([{
primaryKey: [fixPlainBufferCellType(options.inclusiveStartPrimaryKey)],
attributes: [],
}])),
exclusiveEndPrimaryKey: Buffer.from(encodePlainBuffer([{
primaryKey: [fixPlainBufferCellType(options.exclusiveEndPrimaryKey)],
attributes: [],
}])),
};
if (options.limit) {
payload.limit = options.limit;
}
if (options.columnsToGet) {
payload.columnsToGet = options.columnsToGet;
}
if (options.timeRange) {
payload.timeRange = options.timeRange;
}
if (options.maxVersions) {
payload.maxVersions = options.maxVersions;
}
if (options.filter) {
payload.filter = buildFilter(options.filter);
}
if (options.startColumn) {
payload.startColumn = options.startColumn;
}
if (options.endColumn) {
payload.endColumn = options.endColumn;
}
if (!options.maxVersions && !options.timeRange) {
options.maxVersions = 1;
}
return ProtoGetRangeRequest.encode(ProtoGetRangeRequest.create(payload)).finish();
}
public async do(data: GetRangeData) {
const body = await GetRange.builder(data);
return await this.client.request.do({
apiName: OTS_API_NAME.GetRange,
body,
});
}
public static response(data: Uint8Array): GetRangeResponse {
const ret: Record<string, any> = {
rows: null,
nextStartPrimaryKey: null,
};
const raw = ProtoGetRangeResponse.toObject(ProtoGetRangeResponse.decode(data));
ret.consumed = raw.consumed;
const rows = raw.rows as Uint8Array | undefined;
if (rows && rows.byteLength > 0) {
ret.rows = decodePlainBuffer(Buffer.from(rows).buffer);
}
const nextStartPrimaryKey = raw.nextStartPrimaryKey as Uint8Array | undefined;
if (nextStartPrimaryKey && nextStartPrimaryKey.byteLength > 0) {
ret.nextStartPrimaryKey = decodePlainBuffer(Buffer.from(nextStartPrimaryKey).buffer);
}
return ret as GetRangeResponse;
}
}