Skip to content

Commit 6a29e56

Browse files
committed
Move more cache reading code into IndexAllocationCache.
1 parent 2404631 commit 6a29e56

File tree

2 files changed

+50
-49
lines changed

2 files changed

+50
-49
lines changed

lib/IndexAllocationCache.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ import assert from 'assert-plus';
55
import {Bitstring} from '@digitalbazaar/bitstring';
66
import {v4 as uuid} from 'uuid';
77

8-
// FIXME: add `_readBlockAssignmentDoc()`
9-
108
/* See: ListManager for more design notes.
119
12-
this.selectShard(LMD, activeBADs): Select an IAD to use for assigning indexes:
10+
this.populate(LMD "active" set items): Reads all active BADs to enable
11+
choosing an IAD.
12+
13+
1. Read all BADs associated with the given items.
14+
1.1. If any BAD does not exist, CW create it.
15+
1.2. If any BAD has an SL sequence number that is behind an LMD item,
16+
CW update it. If conflict, read the BAD.
17+
18+
this.selectShard(): Select an IAD to use for assigning indexes:
1319
1420
1. Select an IAD at random from the cached BAD blocks with unassigned indexes:
1521
1.1. Get total number of unassigned blocks and choose randomly.
@@ -30,14 +36,20 @@ this.selectShard(LMD, activeBADs): Select an IAD to use for assigning indexes:
3036
*/
3137

3238
export class IndexAllocationCache {
33-
// FIXME: expose function to read records instead of passing here
34-
constructor({edvClient, records} = {}) {
39+
constructor({edvClient, statusListConfig} = {}) {
3540
assert.object(edvClient, 'edvClient');
3641
this.edvClient = edvClient;
37-
this.records = records;//[];
42+
this.records = [];
43+
this.statusListConfig = statusListConfig;
44+
}
45+
46+
// populate this cache from the given items
47+
async populate({items} = {}) {
48+
this.records = await Promise.all(items.map(
49+
item => this._readBlockAssignmentDoc({item})));
3850
}
3951

40-
async outOfSync() {
52+
outOfSync() {
4153
// if any record has a BAD SL sequence number that is greater than the
4254
// LMD item SL sequence number, then the cache is out of sync
4355
const {records} = this;
@@ -103,12 +115,11 @@ export class IndexAllocationCache {
103115
return {...choice, indexAssignmentDoc};
104116
}
105117

106-
// FIXME: determine how to expose for use in `ListManager`
107118
// read BAD contents referenced from LM doc item into a cache record
108119
async _readBlockAssignmentDoc({item}) {
109120
const {edvClient, statusListConfig} = this;
110121

111-
// executes step 1.2.1 of `_readActiveBlockAssignmentDocs()`
122+
// executes step 1.1 of `populate()`
112123
let blockAssignmentDoc;
113124
while(!blockAssignmentDoc) {
114125
try {
@@ -144,7 +155,7 @@ export class IndexAllocationCache {
144155
}
145156
}
146157

147-
// executes step 1.2.2 of `_readActiveBlockAssignmentDocs()`
158+
// executes step 1.2 of `populate()`
148159
const cacheRecord = {blockAssignmentDoc, item};
149160
await this._syncCacheRecord(cacheRecord);
150161
return cacheRecord;
@@ -191,7 +202,7 @@ export class IndexAllocationCache {
191202

192203
// ensure cache record BAD contents are in sync with LM doc item
193204
async _syncCacheRecord(cacheRecord) {
194-
// 1.2.2. If any BAD has an SL sequence number that is behind an LMD item,
205+
// 1.2. If any BAD has an SL sequence number that is behind an LMD item,
195206
// CW update it. If conflict, read the BAD.
196207
if(cacheRecord.blockAssignmentDoc.content.slSequence >=
197208
cacheRecord.item.slSequence) {

lib/ListManager.js

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,19 @@ getShard(): Gets a list shard needed to issue a VC. This will be called by
9797
this._readActiveBlockAssignmentDocs(LMD): Reads all active BADs to enable
9898
choosing an IAD.
9999
100-
1. While active BADs cache is not read:
100+
1. While index allocation active cache is not set:
101101
1.1. Call _tryAddCapacity(LMD, target=1).
102-
1.2. Read all active BADs.
103-
1.2.1. If any BAD does not exist, CW create it.
104-
1.2.2. If any BAD has an SL sequence number that is behind an LMD item,
105-
CW update it. If conflict, read the BAD.
106-
1.2.3. If any BAD has an SL sequence number that is ahead of the
107-
LMD "active" set item, read the LMD and loop to 1.
108-
1.3. If any BADs have fully assigned blocks of indexes:
109-
1.3.1. Move BAD IDs to the "inactive" set; if all BADs are fully assigned,
102+
1.2. Read all active BADs into an index allocation cache, `cache`.
103+
1.3. If any BAD has an SL sequence number that is ahead of the
104+
LMD "active" set item, read the LMD and loop to 1.
105+
1.4. If any BADs have fully assigned blocks of indexes:
106+
1.4.1. Move BAD IDs to the "inactive" set; if all BADs are fully assigned,
110107
choose one to leave as active and update its SL ID and SL sequence number.
111-
1.3.2. CW update LMD.
112-
1.3.3. If conflict, read LMD and loop to 1.
113-
1.3.4. CW update active BAD, if one was updated.
114-
1.3.5. If conflict, read LMD and loop to 1.
115-
1.4. Set read BADs as the active cache.
108+
1.4.2. CW update LMD.
109+
1.4.3. If conflict, read LMD and loop to 1.
110+
1.4.4. CW update active BAD, if one was updated.
111+
1.4.5. If conflict, read LMD and loop to 1.
112+
1.5. Set `cache` as the active cache.
116113
117114
this._selectShard(LMD, activeCache): Select an IAD to use for assigning
118115
indexes:
@@ -262,37 +259,32 @@ export class ListManager {
262259
// latest docs are used
263260
const {documentStore: {edvClient}, statusListConfig} = this;
264261

265-
// 1. While active BADs cache not read:
262+
// 1. While index allocation active cache is not set:
266263
this.activeCache = null;
267264
while(!this.activeCache) {
268265
// FIXME: can `_tryAddCapacity` be moved after reading cache records?
269266
// 1.1. Call _tryAddCapacity(LMD, target=1).
270267
await this._tryAddCapacity({target: 1});
271-
// 1.2. Read all active BADs.
272-
// 1.2.1. If any BAD does not exist, CW create it.
273-
// 1.2.2. If any BAD has an SL sequence number that is behind an LMD item,
274-
// CW update it. If conflict, read the BAD.
268+
// 1.2. Read all active BADs into an index allocation cache, `cache`.
275269
const {blockAssignment} = this.lmDoc.content;
276-
// FIXME: create `new IndexAllocatorCache()`
277-
let cacheRecords = await Promise.all(blockAssignment.active.map(
278-
item => this._readBlockAssignmentDoc({item})));
279-
// 1.2.3. If any BAD has an SL sequence number that is ahead of the
270+
const cache = new IndexAllocationCache({edvClient, statusListConfig});
271+
await cache.populate({items: blockAssignment.active});
272+
// 1.3. If any BAD has an SL sequence number that is ahead of the
280273
// LMD "active" set item, read the LMD and loop to 1.
281-
if(cacheRecords.some(({blockAssignmentDoc: {content}, item}) =>
282-
content.slSequence > item.slSequence)) {
274+
if(cache.outOfSync()) {
283275
this.lmDoc = await edvClient.get({id: this.lmDoc.id});
284276
continue;
285277
}
286278

287279
// FIXME: can this be moved to `tryAddCapacity()` such that it will only
288280
// execute when another BAD is required?
289281

290-
// 1.3. If any BADs have fully assigned blocks of indexes:
291-
const fullyAssigned = cacheRecords.filter(
282+
// 1.4. If any BADs have fully assigned blocks of indexes:
283+
const fullyAssigned = cache.records.filter(
292284
({blockAssignmentDoc: {content}}) =>
293285
content.assignedBlockCount === content.blockCount);
294286
if(fullyAssigned.length > 0) {
295-
// 1.3.1. Move BAD IDs to the "inactive" set; if all BADs are fully
287+
// 1.4.1. Move BAD IDs to the "inactive" set; if all BADs are fully
296288
// assigned, choose one to leave as active and update its SL ID and
297289
// SL sequence number.
298290
const items = fullyAssigned.map(({item}) => item);
@@ -304,7 +296,8 @@ export class ListManager {
304296
if(blockAssignment.active.length > 0) {
305297
// "active" items remain, so we can filter cache records by those
306298
// items with remaining blocks to be assigned and get >= 1 result
307-
cacheRecords = cacheRecords.filter(
299+
// FIXME: do internally
300+
cache.records = cache.records.filter(
308301
({blockAssignmentDoc: {content}}) =>
309302
content.assignedBlockCount < content.blockCount);
310303
} else {
@@ -322,7 +315,7 @@ export class ListManager {
322315
blockAssignment.inactive = blockAssignment.inactive.filter(
323316
item => item !== toUpdate.item);
324317
blockAssignment.active = [toUpdate.item];
325-
cacheRecords = [toUpdate];
318+
cache.records = [toUpdate];
326319
// FIXME: determine how SLC IDs will be stored
327320
// toUpdate.item.slcIds = slcIds;
328321
toUpdate.item.statusListCredential = slcIds[0].id;
@@ -335,7 +328,7 @@ export class ListManager {
335328
content, slSequence: toUpdate.item.slSequence, statusListConfig
336329
});
337330
}
338-
// 1.3.2. CW update LMD.
331+
// 1.4.2. CW update LMD.
339332
try {
340333
this.lmDoc = await edvClient.update({doc: this.lmDoc});
341334
} catch(e) {
@@ -346,7 +339,7 @@ export class ListManager {
346339
this.lmDoc = await edvClient.get({id: this.lmDoc.id});
347340
continue;
348341
}
349-
// 1.3.4. CW update active BAD, if one was updated.
342+
// 1.4.4. CW update active BAD, if one was updated.
350343
if(toUpdate) {
351344
/* Note: If `continue` were just called here instead, it would work
352345
properly, but be slower because the active items all have to be
@@ -361,17 +354,14 @@ export class ListManager {
361354
if(e.name !== 'InvalidStateError') {
362355
throw e;
363356
}
364-
// 1.3.5. If conflict, read LMD and loop to 1.
357+
// 1.4.5. If conflict, read LMD and loop to 1.
365358
this.lmDoc = await edvClient.get({id: this.lmDoc.id});
366359
continue;
367360
}
368361
}
369362
}
370-
// 1.4. Set read BADs as the active cache.
371-
// FIXME: just to already instantiated cache above
372-
this.activeCache = new IndexAllocationCache({
373-
edvClient, records: cacheRecords
374-
});
363+
// 1.5. Set `cache` as the active cache.
364+
this.activeCache = cache;
375365
}
376366
}
377367

0 commit comments

Comments
 (0)