This repository has been archived by the owner on Jun 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
map-entries.js
201 lines (169 loc) · 6.3 KB
/
map-entries.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
/* eslint max-statements:[0, 99] */
'use strict';
var bufrw = require('bufrw');
var inherits = require('util').inherits;
var errors = require('./errors');
// RW a thrift map to an array of [k, v] entries
// ktype:1 vtype:1 length:4 (k... v...){length}
function MapEntriesRW(ktype, vtype) {
this.ktype = ktype;
this.vtype = vtype;
if (this.ktype.rw.width && this.vtype.rw.width) {
this.poolByteLength = this.mapFixFixbyteLength;
} else if (this.ktype.rw.width) {
this.poolByteLength = this.mapFixVarbyteLength;
} else if (this.vtype.rw.width) {
this.poolByteLength = this.mapVarFixbyteLength;
}
bufrw.Base.call(this);
}
inherits(MapEntriesRW, bufrw.Base);
MapEntriesRW.prototype.poolByteLength =
function mapVarVarByteLength(destResult, entries) {
var len = 6; // static overhead
for (var i = 0; i < entries.length; i++) {
var res = this.ktype.rw.poolByteLength(destResult, entries[i][0]);
// istanbul ignore if
if (res.err) return res;
len += res.length;
res = this.vtype.rw.poolByteLength(destResult, entries[i][1]);
// istanbul ignore if
if (res.err) return res;
len += res.length;
}
return destResult.reset(null, len);
};
MapEntriesRW.prototype.mapVarFixbyteLength =
function mapVarFixByteLength(destResult, entries) {
var len = 6 + entries.length * this.vtype.rw.width;
for (var i = 0; i < entries.length; i++) {
var res = this.ktype.rw.poolByteLength(destResult, entries[i][0]);
// istanbul ignore if
if (res.err) return res;
len += res.length;
}
return destResult.reset(null, len);
};
MapEntriesRW.prototype.mapFixVarbyteLength =
function mapFixVarByteLength(destResult, entries) {
var len = 6 + entries.length * this.ktype.rw.width;
for (var i = 0; i < entries.length; i++) {
var res = this.vtype.rw.poolByteLength(destResult, entries[i][1]);
// istanbul ignore if
if (res.err) return res;
len += res.length;
}
return destResult.reset(null, len);
};
MapEntriesRW.prototype.mapFixFixbyteLength =
function mapFixFixByteLength(destResult, entries) {
var len = 6 +
entries.length * this.ktype.rw.width +
entries.length * this.vtype.rw.width;
return destResult.reset(null, len);
};
MapEntriesRW.prototype.poolWriteInto =
function poolWriteInto(destResult, entries, buffer, offset) {
// ktype:1
var res = bufrw.UInt8.poolWriteInto(destResult, this.ktype.typeid, buffer, offset);
// istanbul ignore if
if (res.err) return res;
offset = res.offset;
// vtype:1
res = bufrw.UInt8.poolWriteInto(destResult, this.vtype.typeid, buffer, offset);
// istanbul ignore if
if (res.err) return res;
offset = res.offset;
// length:4
res = bufrw.UInt32BE.poolWriteInto(destResult, entries.length, buffer, offset);
// istanbul ignore if
if (res.err) return res;
offset = res.offset;
// (k... v...){length}
for (var i = 0; i < entries.length; i++) {
var pair = entries[i];
// k...
res = this.ktype.rw.poolWriteInto(destResult, pair[0], buffer, offset);
// istanbul ignore if
if (res.err) return res;
offset = res.offset;
// v...
res = this.vtype.rw.poolWriteInto(destResult, pair[1], buffer, offset);
// istanbul ignore if
if (res.err) return res;
offset = res.offset;
}
return destResult.reset(null, offset);
};
MapEntriesRW.prototype.poolReadFrom = function poolReadFrom(destResult, buffer, offset) {
// ktype:1
var res = bufrw.UInt8.poolReadFrom(destResult, buffer, offset);
// istanbul ignore if
if (res.err) return res;
offset = res.offset;
var ktypeid = res.value;
if (ktypeid !== this.ktype.typeid) {
return destResult.reset(errors.MapKeyTypeIdMismatch({
encoded: ktypeid,
expected: this.ktype.name,
expectedId: this.ktype.typeid
}), offset);
}
// vtype:1
res = bufrw.UInt8.poolReadFrom(destResult, buffer, offset);
// istanbul ignore if
if (res.err) return res;
offset = res.offset;
var vtypeid = res.value;
if (vtypeid !== this.vtype.typeid) {
return destResult.reset(errors.MapValTypeIdMismatch({
encoded: vtypeid,
expected: this.vtype.name,
expectedId: this.vtype.typeid
}), offset);
}
// length:4
res = bufrw.UInt32BE.poolReadFrom(destResult, buffer, offset);
// istanbul ignore if
if (res.err) return res;
offset = res.offset;
var length = res.value;
// (k... v...){length}
var entries = [];
for (var i = 0; i < length; i++) {
// k...
res = this.ktype.rw.poolReadFrom(destResult, buffer, offset);
// istanbul ignore if
if (res.err) return res;
offset = res.offset;
var key = res.value;
// v...
res = this.vtype.rw.poolReadFrom(destResult, buffer, offset);
// istanbul ignore if
if (res.err) return res;
offset = res.offset;
var val = res.value;
entries.push([key, val]);
}
return destResult.reset(null, offset, entries);
};
module.exports.MapEntriesRW = MapEntriesRW;