forked from aerospike/aerospike-client-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
range_get.js
226 lines (197 loc) · 6.14 KB
/
range_get.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*******************************************************************************
* Copyright 2013-2014 Aerospike, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
/*******************************************************************************
*
* node range_get --start <start> --end <end> --skip <skip>
*
* Read records with given key range.
*
* Examples:
*
* Read records with keys in range 1-100
*
* node range_get -start 1 --end 100
*
* Read records with keys in range 1-100, skipping every fifth
*
* node range_get --start 1 --end 100 --skip 5
*
* Write records with keys in range 900-1000
*
* node range_put --start 900
*
******************************************************************************/
var fs = require('fs');
var aerospike = require('aerospike');
var yargs = require('yargs');
var Policy = aerospike.policy;
var Status = aerospike.status;
/*******************************************************************************
*
* Options parsing
*
******************************************************************************/
var argp = yargs
.usage("$0 [options]")
.options({
help: {
boolean: true,
describe: "Display this message."
},
host: {
alias: "h",
default: "127.0.0.1",
describe: "Aerospike database address."
},
port: {
alias: "p",
default: 3000,
describe: "Aerospike database port."
},
timeout: {
alias: "t",
default: 10,
describe: "Timeout in milliseconds."
},
'log-level': {
alias: "l",
default: aerospike.log.INFO,
describe: "Log level [0-5]"
},
'log-file': {
default: undefined,
describe: "Path to a file send log messages to."
},
namespace: {
alias: "n",
default: "test",
describe: "Namespace for the keys."
},
set: {
alias: "s",
default: "demo",
describe: "Set for the keys."
},
start: {
default: 1,
describe: "Start value for the key range."
},
end: {
default: 1000,
describe: "End value for the key range."
},
skip: {
default: 0,
describe: "Skip every n keys."
}
});
var argv = argp.argv;
if ( argv.help === true ) {
argp.showHelp();
process.exit(0);
}
/*******************************************************************************
*
* Configure the client.
*
******************************************************************************/
config = {
// the hosts to attempt to connect with.
hosts: [
{ addr: argv.host, port: argv.port }
],
// log configuration
log: {
level: argv['log-level'],
file: argv['log-file'] ? fs.openSync(argv['log-file'], "a") : 2
},
// default policies
policies: {
timeout: argv.timeout
}
};
/*******************************************************************************
*
* Perform the operation
*
******************************************************************************/
aerospike.client(config).connect(function (err, client) {
if ( err.code != Status.AEROSPIKE_OK ) {
console.error("Error: Aerospike server connection error. ", err.message);
process.exit(1);
}
//
// Perform the operation
//
function get_done(client, start, end, skip) {
var total = end - start + 1;
var done = 0;
var success = 0;
var notfound = 0;
var failure = 0;
var skipped = 0;
var timeLabel = "range_get @ " + total;
console.time(timeLabel);
return function(err, record, metadata, key, skippy) {
if ( skippy === true ) {
console.log("SKIP - ", key);
skipped++;
}
else {
switch ( err.code ) {
case Status.AEROSPIKE_OK:
console.log("OK - ", key, metadata, record);
success++;
break;
case Status.AEROSPIKE_ERR_RECORD_NOT_FOUND:
console.log("NOT_FOUND - ", key);
notfound++;
break;
default:
console.log("ERR - ", err, key);
failure++;
}
}
done++;
if ( done >= total ) {
console.timeEnd(timeLabel);
console.log();
console.log("RANGE: start=%d end=%d skip=%d", start, end, skip);
console.log("RESULTS: (%d completed, %d success, %d failed, %d notfound, %d skipped)", done, success, failure, notfound, skipped);
console.log();
client.close();
}
}
}
function get_start(client, start, end, skip) {
var done = get_done(client, start, end, skip);
var i = start, s = 0;
for (; i <= end; i++ ) {
var key = {
ns: argv.namespace,
set: argv.set,
key: i
};
if ( skip !== 0 && ++s >= skip ) {
s = 0;
done(null,null,null,key,true);
continue;
}
client.get(key, done);
}
}
get_start(client, argv.start, argv.end, argv.skip);
});