зеркало из https://github.com/microsoft/ghcrawler.git
remove obsolete eventFinder
This commit is contained in:
Родитель
4e64485df8
Коммит
ce03280b48
1
index.js
1
index.js
|
@ -4,7 +4,6 @@
|
|||
module.exports.crawler = require('./lib/crawler');
|
||||
module.exports.crawlerService = require('./lib/crawlerService');
|
||||
module.exports.crawlerFactory = require('./lib/crawlerFactory');
|
||||
module.exports.eventFinder = require('./lib/eventFinder');
|
||||
module.exports.githubFetcher = require('./providers/fetcher/githubFetcher');
|
||||
module.exports.githubProcessor = require('./providers/fetcher/githubProcessor');
|
||||
module.exports.policy = require('./lib/traversalPolicy');
|
||||
|
|
|
@ -1,47 +0,0 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
const Q = require('q');
|
||||
const qlimit = require('qlimit');
|
||||
|
||||
class EventFinder {
|
||||
constructor(requestor, eventStore) {
|
||||
this.requestor = requestor;
|
||||
this.eventStore = eventStore;
|
||||
}
|
||||
|
||||
discoverAndQueue(eventSource, eventSink) {
|
||||
if (!eventSource) {
|
||||
return Q(null);
|
||||
}
|
||||
const self = this;
|
||||
return this.getNewEvents(eventSource).then(events => {
|
||||
return self._queueEvents(events, eventSink);
|
||||
});
|
||||
}
|
||||
|
||||
_queueEvents(events, eventSink) {
|
||||
const limit = qlimit(10);
|
||||
return Q.all(events.each(limit(event => {
|
||||
eventSink.push(event.type, event.url, { payload: event });
|
||||
})));
|
||||
}
|
||||
|
||||
getNewEvents(eventSource) {
|
||||
return this.requestor.getAll(eventSource).then(this._findNew.bind(this));
|
||||
}
|
||||
|
||||
// Find the events for which we do NOT have a document.
|
||||
_findNew(events) {
|
||||
const self = this;
|
||||
return Q.all(events.map(qlimit(10)(event => {
|
||||
return self.eventStore.etag('event', event.url).then(etag => {
|
||||
return etag ? null : event;
|
||||
});
|
||||
}))).then(events => {
|
||||
return events.filter(event =>event);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = EventFinder;
|
|
@ -1,75 +0,0 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
const expect = require('chai').expect;
|
||||
const extend = require('extend');
|
||||
const finder = require('../../lib/eventFinder.js');
|
||||
const Q = require('q');
|
||||
|
||||
describe('Event Finder', () => {
|
||||
it('will find 1 document', () => {
|
||||
const events = [
|
||||
[{ url: 'http://test1' }, { url: 'http://test2' }]
|
||||
];
|
||||
const eventDocuments = [{ 'http://test2': { etag: 34 } }];
|
||||
const instance = createFinder(events, eventDocuments);
|
||||
|
||||
return instance.getNewEvents('http://test.com').then(found => {
|
||||
expect(found.length).to.be.equal(1);
|
||||
expect(found[0].url).to.be.equal('http://test1');
|
||||
});
|
||||
});
|
||||
|
||||
it('will not find any documents', () => {
|
||||
const events = [
|
||||
[{ url: 'http://test1' }, { url: 'http://test2' }]
|
||||
];
|
||||
const eventDocuments = [{ 'http://test1': { etag: 34 } }, { 'http://test2': { etag: 34 } }];
|
||||
const instance = createFinder(events, eventDocuments);
|
||||
|
||||
return instance.getNewEvents('http://test.com').then(found => {
|
||||
expect(found.length).to.be.equal(0);
|
||||
});
|
||||
});
|
||||
|
||||
it('will stop not finding at first found document', () => {
|
||||
const events = [
|
||||
[{ url: 'http://test1' }, { url: 'http://test2' }, { url: 'http://test3' }]
|
||||
];
|
||||
const eventDocuments = [{ 'http://test2': { etag: 34 } }];
|
||||
const instance = createFinder(events, eventDocuments);
|
||||
|
||||
return instance.getNewEvents('http://test.com').then(found => {
|
||||
expect(found.length).to.be.equal(2);
|
||||
expect(found[0].url).to.be.equal('http://test1');
|
||||
expect(found[1].url).to.be.equal('http://test3');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function createFinder(events, documents) {
|
||||
const eventStore = createStore(documents);
|
||||
const requestor = createRequestor(events);
|
||||
return new finder(requestor, eventStore);
|
||||
}
|
||||
|
||||
function createRequestor(pages) {
|
||||
const result = {};
|
||||
result.getAll = () => {
|
||||
return Q(pages.shift());
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
function createStore(documents) {
|
||||
const result = {};
|
||||
const hash = documents.reduce((collection, document) => {
|
||||
extend(collection, document);
|
||||
return collection;
|
||||
}, {});
|
||||
result.etag = (type, url) => {
|
||||
let result = hash[url];
|
||||
return Q(result ? result.etag : null);
|
||||
};
|
||||
return result;
|
||||
}
|
Загрузка…
Ссылка в новой задаче