-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpersistenceIdsIndex.js
214 lines (176 loc) · 6.7 KB
/
persistenceIdsIndex.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
const FlumeviewLevel = require('flumeview-level');
const pull = require('pull-stream');
const takeRange = require('./util').takeRange;
const isPersistenceMessage = require('./util').isPersistenceMessage;
module.exports = (ssb, myKey, keysIndex) => {
const indexVersion = 2;
const index = ssb._flumeUse('akka-persistence-index',
FlumeviewLevel(
indexVersion,
flumeMapFunction)
)
function flumeMapFunction(msg) {
if (isPersistenceMessage(msg)) {
const author = msg.value.author;
const persistenceId = msg.value.content.persistenceId;
const sequenceNr = msg.value.content.sequenceNr;
const isEncrypted = msg.value.content.encrypted || false;
// We only index the first item, as otherwise we would get repeats for live streams since old values
// would be overrwritten to point to the latest message.
if (sequenceNr == 1) {
return [[author, isEncrypted, persistenceId], [persistenceId, isEncrypted, author]];
}
else {
return [];
}
} else {
return [];
}
}
function persistenceIdsQuery(author, live) {
return pull(index.read({
gte: [author, null],
lte: [author, undefined],
live
}), pull.map(value => {
return value.value.value.content.persistenceId;
}), pull.unique());
}
return {
myCurrentPersistenceIds: (opts) => {
opts = opts || {};
const stream = persistenceIdsQuery(myKey, false);
if (opts.start >= 0) {
return takeRange(stream, opts.start, opts.end);
} else {
return stream;
}
},
myCurrentPersistenceIdsAsync: (cb) => {
pull(persistenceIdsQuery(myKey, false), pull.collect(cb));
},
myLivePersistenceIds: (opts) => {
opts = opts || {};
const source = persistenceIdsQuery(myKey, true);
if (opts.start >= 0) {
return takeRange(source, opts.start, opts.end);
} else {
return source;
}
},
authorsForPersistenceId: (persistenceId, opts) => {
opts = opts || {};
const source = pull(
index.read({
gte: [persistenceId, null, null],
lte: [persistenceId, undefined, undefined],
live: opts.live,
reverse: opts.reverse,
keys: true
}),
pull.asyncMap((result, cb) => {
const data = result.key;
const persistenceId = data[0];
const isEncrypted = data[1];
const author = data[2];
if (!isEncrypted) {
cb(null, {
data: data,
isEncrypted: false,
keys: []
});
} else {
// Get our keys for the entity for the given author (if any.)
keysIndex.getAllKeysFor(persistenceId, author).then(
keys => {
return {
data: data,
isEncrypted: true,
keys: keys
}
}
).asCallback(cb);
}
}),
// Filter out any persistenceIds that are private and we don't have the keys for.
pull.filter(item => item.isEncrypted === false || item.keys.length > 0),
pull.map(result => {
return result.data[2];
}))
if (opts.start >= 0) {
return takeRange(source, opts.start, opts.end);
} else {
return source;
}
},
persistenceIdsForAuthor: (authorId, opts) => {
authorId = authorId || myKey;
opts = opts || {};
const source = pull(
index.read({
gte: [authorId, null, null],
lte: [authorId, undefined, undefined],
live: opts.live,
keys: true
}),
pull.asyncMap((data, cb) => {
const isEncrypted = data.key[1] || false;
if (!isEncrypted) {
cb(null, {
data: data.key,
isEncrypted: false,
keys: []
})
} else {
const persistenceId = data.key[2];
const authorId = data.key[0];
keysIndex.getAllKeysFor(persistenceId, authorId).then(
keys => {
return {
data: data.key,
isEncrypted: true,
keys: keys
}
}
).asCallback(cb);
}
}),
pull.filter(item => {
return !item.isEncrypted || (item.keys.length > 0)
}),
pull.map( item => {
const persistenceId = item.data[2];
return persistenceId;
})
);
if (opts.start >= 0) {
return takeRange(source, opts.start, opts.end);
} else {
return source;
}
},
allOtherAuthors: (opts) => {
opts = opts || {};
const source = pull(
index.read({
gte: [null, null, null],
lte: [undefined, undefined, undefined],
live: opts.live,
keys: true
}),
pull.filter(item => {
return item && item.key && item.key[0].startsWith('@') && item.key[0] !== myKey;
}),
pull.map(item => {
return item.key[0];
}),
pull.unique()
);
if (opts.start >= 0) {
return takeRange(source, opts.start, opts.end);
} else {
return source;
}
}
}
}