rename plugin-search-indexer to plugin-search-backend-node
Signed-off-by: Eric Peterson <ericpeterson@spotify.com>
This commit is contained in:
committed by
Eric Peterson
parent
0057692401
commit
124b93bff4
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* Copyright 2021 Spotify AB
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { registerCollator, registerDecorator } from './';
|
||||
import { Registry } from './registry';
|
||||
|
||||
describe('external api', () => {
|
||||
afterEach(() => {
|
||||
Registry.getInstance()._reset();
|
||||
});
|
||||
|
||||
describe('registerCollator', () => {
|
||||
it('registers a collator', async () => {
|
||||
const collatorSpy = jest.fn(async () => []);
|
||||
|
||||
// Register a collator.
|
||||
registerCollator({
|
||||
type: 'anything',
|
||||
defaultRefreshIntervalSeconds: 600,
|
||||
collator: collatorSpy,
|
||||
});
|
||||
|
||||
// Execute the registry and ensure the collator was invoked.
|
||||
await Registry.getInstance().execute();
|
||||
expect(collatorSpy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('registerDecorator', () => {
|
||||
it('registers a decorator', async () => {
|
||||
const mockCollator = jest.fn(async () => []);
|
||||
const decoratorSpy = jest.fn(async docs => docs);
|
||||
|
||||
// Register a collator.
|
||||
registerCollator({
|
||||
type: 'anything',
|
||||
defaultRefreshIntervalSeconds: 600,
|
||||
collator: mockCollator,
|
||||
});
|
||||
|
||||
// Register a decorator.
|
||||
registerDecorator({
|
||||
decorator: decoratorSpy,
|
||||
});
|
||||
|
||||
// Execute the registry and ensure the decorator was invoked.
|
||||
await Registry.getInstance().execute();
|
||||
expect(decoratorSpy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('registers a type-specific decorator', async () => {
|
||||
const expectedType = 'an-expected-type';
|
||||
const docFixture = {
|
||||
title: 'Test',
|
||||
text: 'Test text.',
|
||||
location: '/test/location',
|
||||
};
|
||||
const mockCollator = jest.fn(async () => [docFixture]);
|
||||
const decoratorSpy = jest.fn(async docs => docs);
|
||||
|
||||
// Register a collator.
|
||||
registerCollator({
|
||||
type: expectedType,
|
||||
defaultRefreshIntervalSeconds: 600,
|
||||
collator: mockCollator,
|
||||
});
|
||||
|
||||
// Register a decorator for the same type.
|
||||
registerDecorator({
|
||||
types: [expectedType],
|
||||
decorator: decoratorSpy,
|
||||
});
|
||||
|
||||
// Execute the registry and ensure the decorator was invoked.
|
||||
await Registry.getInstance().execute();
|
||||
expect(decoratorSpy).toHaveBeenCalled();
|
||||
expect(decoratorSpy).toHaveBeenCalledWith([docFixture]);
|
||||
});
|
||||
|
||||
it('registers a type-specific decorator that should not be called', async () => {
|
||||
const expectedType = 'an-expected-type';
|
||||
const docFixture = {
|
||||
title: 'Test',
|
||||
text: 'Test text.',
|
||||
location: '/test/location',
|
||||
};
|
||||
const mockCollator = jest.fn(async () => [docFixture]);
|
||||
const decoratorSpy = jest.fn(async docs => docs);
|
||||
|
||||
// Register a collator.
|
||||
registerCollator({
|
||||
type: expectedType,
|
||||
defaultRefreshIntervalSeconds: 600,
|
||||
collator: mockCollator,
|
||||
});
|
||||
|
||||
// Register a decorator for a different type.
|
||||
registerDecorator({
|
||||
types: ['not-the-expected-type'],
|
||||
decorator: decoratorSpy,
|
||||
});
|
||||
|
||||
// Execute the registry and ensure the decorator was not invoked.
|
||||
await Registry.getInstance().execute();
|
||||
expect(decoratorSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2021 Spotify AB
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { Registry } from './registry';
|
||||
import {
|
||||
RegisterCollatorParameters,
|
||||
RegisterDecoratorParameters,
|
||||
} from './types';
|
||||
|
||||
const registry = Registry.getInstance();
|
||||
export const registerCollator = (params: RegisterCollatorParameters) => {
|
||||
registry.addCollator(params);
|
||||
};
|
||||
export const registerDecorator = (params: RegisterDecoratorParameters) => {
|
||||
registry.addDecorator(params);
|
||||
};
|
||||
export const collateDocuments = () => {
|
||||
registry.execute();
|
||||
};
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright 2021 Spotify AB
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import {
|
||||
IndexableDocumentCollator,
|
||||
IndexableDocumentDecorator,
|
||||
} from '@backstage/search-common';
|
||||
import {
|
||||
RegisterCollatorParameters,
|
||||
RegisterDecoratorParameters,
|
||||
} from './types';
|
||||
|
||||
interface CollatorRegistryEntry {
|
||||
collate: IndexableDocumentCollator;
|
||||
refreshInterval: number;
|
||||
}
|
||||
|
||||
export class Registry {
|
||||
private collators: Record<string, CollatorRegistryEntry>;
|
||||
private decorators: Record<string, IndexableDocumentDecorator[]>;
|
||||
|
||||
private static instance: Registry;
|
||||
|
||||
private constructor() {
|
||||
this.collators = {};
|
||||
this.decorators = {};
|
||||
}
|
||||
|
||||
static getInstance(): Registry {
|
||||
if (!Registry.instance) {
|
||||
Registry.instance = new Registry();
|
||||
}
|
||||
return Registry.instance;
|
||||
}
|
||||
|
||||
addCollator({
|
||||
type,
|
||||
collator,
|
||||
defaultRefreshIntervalSeconds,
|
||||
}: RegisterCollatorParameters): void {
|
||||
this.collators[type] = {
|
||||
refreshInterval: defaultRefreshIntervalSeconds,
|
||||
collate: collator,
|
||||
};
|
||||
}
|
||||
|
||||
addDecorator({
|
||||
types = ['*'],
|
||||
decorator,
|
||||
}: RegisterDecoratorParameters): void {
|
||||
types.forEach(type => {
|
||||
if (this.decorators.hasOwnProperty(type)) {
|
||||
this.decorators[type].push(decorator);
|
||||
} else {
|
||||
this.decorators[type] = [decorator];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// TODO: But like with coordination, timing, error handling, and what have you.
|
||||
async execute() {
|
||||
return Promise.all(
|
||||
Object.keys(this.collators).map(async type => {
|
||||
const decorators: IndexableDocumentDecorator[] = (
|
||||
this.decorators['*'] || []
|
||||
).concat(this.decorators[type] || []);
|
||||
|
||||
let documents = await this.collators[type].collate();
|
||||
for (let i = 0; i < decorators.length; i++) {
|
||||
documents = await decorators[i](documents);
|
||||
}
|
||||
|
||||
// TODO: push documents to a configured search engine.
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method for tests. Do not use otherwise.
|
||||
* @private
|
||||
*/
|
||||
_reset() {
|
||||
this.collators = {};
|
||||
this.decorators = {};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Copyright 2021 Spotify AB
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export {};
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2021 Spotify AB
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import {
|
||||
IndexableDocumentCollator,
|
||||
IndexableDocumentDecorator,
|
||||
} from '@backstage/search-common';
|
||||
|
||||
/**
|
||||
* Parameters required to register a collator.
|
||||
*/
|
||||
export interface RegisterCollatorParameters {
|
||||
/**
|
||||
* The type of document to be indexed (used to name indices, to configure refresh loop, etc).
|
||||
*/
|
||||
type: string;
|
||||
|
||||
/**
|
||||
* The default interval (in seconds) that the provided collator will be called (can be overridden in config).
|
||||
*/
|
||||
defaultRefreshIntervalSeconds: number;
|
||||
|
||||
/**
|
||||
* The collator function responsible for returning all documents of the given type.
|
||||
*/
|
||||
collator: IndexableDocumentCollator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parameters required to register a decorator
|
||||
*/
|
||||
export interface RegisterDecoratorParameters {
|
||||
/**
|
||||
* The decorator function responsible for appending or modifying documents of the given type(s).
|
||||
*/
|
||||
decorator: IndexableDocumentDecorator;
|
||||
|
||||
/**
|
||||
* (Optional) An array of document types that the given decorator should apply to. If none are provided,
|
||||
* the decorator will be applied to all types.
|
||||
*/
|
||||
types?: string[];
|
||||
}
|
||||
Reference in New Issue
Block a user