-
Notifications
You must be signed in to change notification settings - Fork 1
/
entityEventsIndex.js
184 lines (141 loc) · 5.52 KB
/
entityEventsIndex.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
const FlumeviewLevel = require('flumeview-level');
const pull = require('pull-stream');
const window = require('pull-window');
const isPersistenceMessage = require('./util').isPersistenceMessage;
module.exports = (sbot, myKey) => {
const version = 2;
const index = sbot._flumeUse('entity-events-index', FlumeviewLevel(version, mapFunction));
function mapFunction(message) {
if (isPersistenceMessage(message)) {
const author = message.value.author;
const persistenceId = message.value.content.persistenceId;
const sequenceNr = message.value.content.sequenceNr;
const part = message.value.content.part || 1;
return [[author, persistenceId, sequenceNr, part]];
} else {
return [];
}
}
function eventsByPersistenceId(authorId, persistenceId, fromSequenceNumber, toSequenceNumber, live) {
const source = index.read({
keys: true,
gte: [authorId, persistenceId, fromSequenceNumber, null],
lte: [authorId, persistenceId, toSequenceNumber, undefined],
live: live,
sync: false
});
return pull(source, pull.map(msg => {
let content = msg.value.value.content
content['scuttlebuttSequence'] = msg.value.value.sequence;
return content
}), reAssemblePartsThrough()
);
}
function reAssemblePartsThrough() {
let windowing = false;
return window(function (_, cb) {
if (windowing) return;
windowing = true;
let parts = [];
return function (end, data) {
if (end && parts.length > 0) {
const lastPart = parts[parts.length - 1];
// If we haven't replicated the rest of the parts for the message,
// we act as though we haven't got any of it as we can't do anything
// with just part of
if (lastPart.part === lastPart.of) {
return cb(null, assembleParts(parts));
} else {
return cb(true, null);
}
}
else if (end) return cb(null, data);
else if (!data.part) {
cb(null, data);
windowing = false;
}
else if (data.part === data.of) {
windowing = false;
parts.push(data);
cb(null, assembleParts(parts))
}
else {
parts.push(data);
}
}
}, function (start, data) {
return data;
});
}
function assembleParts(parts) {
const payloads = parts.map(part => part.payload);
const fullPayload = payloads.join('');
const full = parts[parts.length - 1];
if (full.encrypted) {
// Encrypted payloads are base64 strings until they're decrypted later in the
// pipeline
full.payload = fullPayload;
} else {
// Make it into an object again now that the string is joined up.
full.payload = JSON.parse(fullPayload);
}
return full;
}
function highestSequenceNumber(authorId, persistenceId, cb) {
const source = eventsByPersistenceId(authorId, persistenceId, 0, undefined);
pull(source, pull.collect((err, result) => {
if (err) {
cb(err);
} else if (result.length === 0) {
cb(null, 0);
} else {
const lastItem = result[result.length - 1];
cb(null, lastItem.sequenceNr);
}
}));
}
function allEventsForAuthor(authorId, startSequenceNr) {
authorId = authorId || myKey;
const source = sbot.query.read({
query: [
{$filter: {
value: {
sequence: {$gte: startSequenceNr},
author: authorId,
content: {
type: 'akka-persistence-message'
}
}
}}
]
});
const isMidPart = ((item) => {
const isMid = item.value.content.part && (item.value.content.part < item.value.content.of);
return isMid;
});
return pull(source,
pull.filter(item => !isMidPart(item)),
pull.asyncMap((item, cb) => {
if (item.value.content.part) {
// Emit the full item
const sequenceNr = item.value.content.sequenceNr;
pull(eventsByPersistenceId(authorId, item.value.content.persistenceId, sequenceNr, sequenceNr + 1),
pull.collect((err, results) => {
// Note: 'scuttlebuttSequence' is already included by the eventsByPersistenceId source.
// It is the scuttlebutt sequence number of the last 'part' for the persisted entity event
cb(err, results[0])
})
)
} else {
const result = item.value.content;
result['scuttlebuttSequence'] = item.value.sequence;
cb(null, result);
}
}));
}
return {
allEventsForAuthor,
eventsByPersistenceId,
highestSequenceNumber
}
}