-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadall.js
54 lines (46 loc) · 1.38 KB
/
readall.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
const batch = require('./batch');
function generateBody(size) {
const randomString = Math.random().toString(36).slice(2);
return new Array(Math.ceil(size / randomString.length))
.fill(randomString)
.join('')
.slice(0, size);
}
function permuteIndex(n, options) {
if (options.random) {
// multiply by a prime number to have a somewhat randomized
// bijective mapping and read all objects exactly once
return (n * 5776357) % options.count;
}
return n;
}
function readall(options, cb) {
const obj = batch.create(options);
batch.showOptions(obj);
console.log(`
random: ${options.random ? 'yes' : 'no'}
`);
const readallOp = (s3, n, endSuccess, endError) => {
const idx = permuteIndex(n, options);
batch.getKey(obj, n, key => {
s3.getObject({
Bucket: options.bucket,
Key: key,
}, err => {
if (err) {
console.error(`error during "GET ${options.bucket}/${key}":`,
err.message);
return endError();
}
return endSuccess();
});
});
};
batch.init(obj, err => {
if (err) {
return cb(err);
}
batch.run(obj, readallOp, cb);
});
}
module.exports = readall;