-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathmain.js
63 lines (57 loc) · 1.35 KB
/
main.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
'use strict';
const { Client, Predicates } = require('hazelcast-client');
class Comparator {
constructor() {
this.factoryId = 66;
this.classId = 103;
}
sort() {
// not necessary because actual sorting happens on the member side
}
readData() {
// no-op
}
writeData() {
// no-op
}
}
(async () => {
const client = await Client.newHazelcastClient({
serialization: {
dataSerializableFactories: {
66: (classId) => {
if (classId === 103) {
return new Comparator();
}
return null;
}
}
}
});
const map = await client.getMap('my-maps');
await map.setAll([
['one', 1],
['two', 2],
['three', 3],
['four', 4],
['five', 5],
['six', 6],
['seven', 7]
].sort(() => 0.5 - Math.random())); // shuffle entries
/**
* Output:
* [ [ 'seven', 7 ], [ 'six', 6 ], [ 'five', 5 ] ]
*/
console.log(await getTop3(map));
})();
/**
* @param {import('hazelcast-client').IMap} map
*/
async function getTop3(map) {
const predicate = Predicates.paging(
Predicates.alwaysTrue(),
3,
new Comparator()
);
return map.entrySetWithPredicate(predicate);
}