This repository has been archived by the owner on Jul 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Using create-batch-stream * minor fix * Revert modifications for testing purpose * Try to use node streams * Fix test running twice * First "working" version * Make tests pass * Make ordered reader inherit Readable * Increase message count * Use stream.on('data') * Update jest * Add set timeout so read doesnt owerwhelm the event loop * Lint * Rename to SelectiveReader * Add a test for a feed added later * Fix a test for a feed added later * Revert to while(true) iml * Clean up code * Remove consolel og * Remove commented code Co-authored-by: Martin Acosta <tinchoz49@gmail.com>
- Loading branch information
1 parent
ce2d9ad
commit 9ddb5e3
Showing
6 changed files
with
1,666 additions
and
641 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// | ||
// Copyright 2019 DXOS.org | ||
// | ||
|
||
import { Readable } from 'stream'; | ||
import createBatchStream from './create-batch-stream'; | ||
|
||
/** | ||
* Creates a multi ReadableStream for feed streams. | ||
*/ | ||
export default class SelectiveReader extends Readable { | ||
/** @type {(feedDescriptor, message) => Promise<boolean>} */ | ||
_evaluator; | ||
|
||
/** @type {Set<{ descriptor: FeedDescriptor, stream: any, buffer: any[] }>} */ | ||
_feeds = new Set(); | ||
|
||
/** @type {() => void} */ | ||
_wakeUpReader; | ||
|
||
/** @type {Promise} */ | ||
_hasData; | ||
|
||
_reading = false; | ||
|
||
constructor (evaluator) { | ||
super({ objectMode: true }); | ||
|
||
this._evaluator = evaluator; | ||
this._resetDataLock(); | ||
} | ||
|
||
_resetDataLock () { | ||
this._hasData = new Promise(resolve => { this._wakeUpReader = resolve; }); | ||
} | ||
|
||
async _read () { | ||
if (this._reading) { | ||
this._needsData = true; | ||
return; | ||
} | ||
this._reading = true; | ||
this._needsData = false; | ||
|
||
while (true) { | ||
this._resetDataLock(); | ||
|
||
for (const feed of this._feeds.values()) { | ||
if (feed.buffer.length === 0) { | ||
const messages = feed.stream.read(); | ||
if (!messages) continue; | ||
feed.buffer.push(...messages); | ||
} | ||
|
||
let message; | ||
while ((message = feed.buffer.shift())) { | ||
if (await this._evaluator(feed.descriptor, message)) { | ||
process.nextTick(() => this._wakeUpReader()); | ||
this._needsData = false; | ||
if (!this.push(message)) { | ||
this._reading = false; | ||
return; | ||
} | ||
} else { | ||
feed.buffer.unshift(message); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
await new Promise(resolve => setTimeout(resolve, 0)); // yield so that other tasks can be processed | ||
|
||
if (this._needsData && Array.from(this._feeds.values()).some(x => x.buffer.length > 0)) { | ||
continue; | ||
} | ||
await this._hasData; | ||
} | ||
} | ||
|
||
async addInitialFeedStreams (descriptors) { | ||
for (const descriptor of descriptors) { | ||
this.addFeedStream(descriptor); | ||
} | ||
} | ||
|
||
/** | ||
* Adds a feed stream and stream the block data, seq, key and metadata. | ||
* | ||
* @param {FeedDescriptor} descriptor | ||
*/ | ||
async addFeedStream (descriptor) { | ||
const stream = createBatchStream(descriptor.feed, { live: true }); | ||
|
||
stream.on('readable', () => { | ||
this._wakeUpReader(); | ||
this._read(); | ||
}); | ||
|
||
this._feeds.add({ descriptor, stream, buffer: [] }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// | ||
// Copyright 2019 DXOS.org | ||
// | ||
|
||
import pify from 'pify'; | ||
import ram from 'random-access-memory'; | ||
import waitForExpect from 'wait-for-expect'; | ||
import { FeedStore } from './feed-store'; | ||
|
||
function append (feed, message) { | ||
return pify(feed.append.bind(feed))(message); | ||
} | ||
|
||
async function generateStreamData (feedStore, maxMessages = 200) { | ||
const [feed1, feed2] = await Promise.all([ | ||
feedStore.openFeed('/feed1'), | ||
feedStore.openFeed('/feed2') | ||
]); | ||
|
||
const messages = []; | ||
for (let i = 0; i < maxMessages; i++) { | ||
messages.push(append(feed1, `feed1/message${i}`)); | ||
messages.push(append(feed2, `feed2/message${i}`)); | ||
} | ||
|
||
await Promise.all(messages); | ||
|
||
return [feed1, feed2]; | ||
} | ||
|
||
describe('SelectiveReader', () => { | ||
test('two feeds', async () => { | ||
const feedStore = await FeedStore.create(ram, { feedOptions: { valueEncoding: 'utf-8' } }); | ||
|
||
const MESSAGE_COUNT = 10; | ||
|
||
const [feed1] = await generateStreamData(feedStore, MESSAGE_COUNT); | ||
|
||
const messages = []; | ||
|
||
const allowedFeeds = new Set(['/feed1']); | ||
const stream = feedStore.createSelectiveStream( | ||
async (feedDescriptor, message) => allowedFeeds.has(feedDescriptor.path) | ||
); | ||
|
||
stream.on('data', message => { | ||
messages.push(message); | ||
if (message.data.startsWith('allow-')) { | ||
allowedFeeds.add(message.data.slice(6)); | ||
} | ||
}); | ||
|
||
// only feed1 messages should be here at this point | ||
await waitForExpect(async () => { | ||
expect(messages.length === MESSAGE_COUNT); | ||
expect(messages.every(msg => msg.data.startsWith('feed1'))); | ||
}); | ||
|
||
await append(feed1, 'allow-/feed2'); | ||
|
||
await waitForExpect(() => expect(messages.length).toBe(MESSAGE_COUNT * 2 + 1)); | ||
|
||
// TODO(marik-d): Test for sync events | ||
}); | ||
|
||
test('feed is added later', async () => { | ||
const feedStore = await FeedStore.create(ram, { feedOptions: { valueEncoding: 'utf-8' } }); | ||
|
||
const MESSAGE_COUNT = 10; | ||
|
||
await generateStreamData(feedStore, MESSAGE_COUNT); | ||
|
||
const messages = []; | ||
|
||
const allowedFeeds = new Set(['/feed1', '/feed3']); | ||
const stream = feedStore.createSelectiveStream( | ||
async (feedDescriptor, message) => allowedFeeds.has(feedDescriptor.path) | ||
); | ||
|
||
stream.on('data', message => { | ||
messages.push(message); | ||
if (message.data.startsWith('allow-')) { | ||
allowedFeeds.add(message.data.slice(6)); | ||
} | ||
}); | ||
|
||
// only feed1 messages should be here at this point | ||
await waitForExpect(async () => { | ||
expect(messages.length === MESSAGE_COUNT); | ||
expect(messages.every(msg => msg.data.startsWith('feed1'))); | ||
}); | ||
|
||
const feed = await feedStore.openFeed('/feed3'); | ||
await append(feed, 'allow-/feed2'); | ||
|
||
await waitForExpect(() => expect(messages.length).toBe(MESSAGE_COUNT * 2 + 1)); | ||
|
||
// TODO(marik-d): Test for sync events | ||
}); | ||
}); |
Oops, something went wrong.