Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add write and mWrite #1302

Merged
merged 17 commits into from
May 28, 2019
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions features/kuzzle.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
Feature: Kuzzle functional tests
Scenario: Bulk mWrite
When I create a collection "kuzzle-test-index":"kuzzle-collection-test"
When I use bulk:mWrite action with
"""
{
"documents": [
{ "body": { "name": "Maedhros" } },
{ "body": { "name": "Maglor" } },
{ "body": { "name": "Celegorm" } },
{ "body": { "name": "Caranthis" } },
{ "body": { "name": "Curufin" } },
{ "body": { "name": "Amrod" } },
{ "body": { "name": "Amras" } }
]
}
"""
Then I count 7 documents
And The documents does not have kuzzle metadata

Scenario: Bulk write
When I use bulk:write action with '{ "name": "Feanor", "_kuzzle_info": { "author": "Tolkien" } }'
Then I count 1 documents
And The documents have the following kuzzle metadata '{ "author": "Tolkien" }'

Scenario: Create a collection
When I create a collection "kuzzle-test-index":"my-collection1"
Then The mapping properties field of "kuzzle-test-index":"my-collection1" is "the default value"
Expand Down
33 changes: 33 additions & 0 deletions features/step_definitions/bulk.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const
should = require('should'),
{
Then,
When
Expand Down Expand Up @@ -116,3 +117,35 @@ When(/^I do a global bulk import$/, function (callback) {
});
});

When('I use bulk:mWrite action with', function (bodyRaw) {
const body = JSON.parse(bodyRaw);

return this.api.bulkMWrite(this.index, this.collection, body);
});

When('I use bulk:write action with {string}', function (bodyRaw) {
const body = JSON.parse(bodyRaw);

return this.api.bulkWrite(this.index, this.collection, body);
});


Then('The documents does not have kuzzle metadata', function () {
return this.api.search({}, this.index, this.collection, { size: 100 })
.then(({ result }) => {
for (const hit of result.hits) {
should(hit._source._kuzzle_info).be.undefined();
}
});
});

Then('The documents have the following kuzzle metadata {string}', function (metadatRaw) {
const metadata = JSON.parse(metadatRaw);

return this.api.search({}, this.index, this.collection, { size: 100 })
.then(({ result }) => {
for (const hit of result.hits) {
should(hit._source._kuzzle_info).match(metadata);
}
});
});
26 changes: 26 additions & 0 deletions features/support/api/apiBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,32 @@ class ApiBase {
return this.send(msg);
}

bulkMWrite (index, collection, body) {
const
msg = {
controller: 'bulk',
collection: collection || this.world.fakeCollection,
index: index || this.world.fakeIndex,
action: 'mWrite',
body
};

return this.send(msg);
}

bulkWrite (index, collection, body) {
const
msg = {
controller: 'bulk',
collection: collection || this.world.fakeCollection,
index: index || this.world.fakeIndex,
action: 'write',
body
};

return this.send(msg);
}

collectionExists (index, collection) {
return this.send({
index,
Expand Down
20 changes: 20 additions & 0 deletions features/support/api/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,26 @@ class HttpApi {
return this.callApi(options);
}

bulkMWrite (index, collection, body) {
const options = {
url: this.apiPath(this.util.getIndex(index) + '/' + this.util.getCollection(collection) + '/_mWrite'),
method: 'POST',
body
};

return this.callApi(options);
}

bulkWrite (index, collection, body) {
const options = {
url: this.apiPath(this.util.getIndex(index) + '/' + this.util.getCollection(collection) + '/_write'),
method: 'POST',
body
};

return this.callApi(options);
}

/**
* @param options
* @return {Promise.<IncomingMessage>}
Expand Down
73 changes: 70 additions & 3 deletions lib/api/controllers/bulkController.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,27 @@

const
BaseController = require('./controller'),
{errors: {PartialError}} = require('kuzzle-common-objects'),
{assertHasBody, assertBodyHasAttribute} = require('../../util/requestAssertions');
{ PartialError } = require('kuzzle-common-objects').errors,
Aschen marked this conversation as resolved.
Show resolved Hide resolved
{
assertHasBody,
assertBodyHasAttribute,
assertHasIndexAndCollection,
assertBodyAttributeType,
assertIdStartsNotUnderscore
} = require('../../util/requestAssertions');

/**
* @class BulkController
* @param {Kuzzle} kuzzle
*/
class BulkController extends BaseController {
constructor (kuzzle) {
super(kuzzle, ['import']);
super(kuzzle, [
'import',
'write',
'mWrite'
]);

this.engine = kuzzle.services.list.storageEngine;
}

Expand All @@ -55,6 +66,62 @@ class BulkController extends BaseController {
return response;
});
}

/**
* Write a document without adding metadata or performing data validation.
* @param {Request} request
* @returns {Promise<Object>}
*/
write(request) {
assertHasBody(request);
assertHasIndexAndCollection(request);
assertIdStartsNotUnderscore(request);

const notify = this.ensureBooleanFlag(request, 'input.args.notify');

return this.engine.createOrReplace(request, false)
.then(response => {
this.kuzzle.indexCache.add(request.input.resource.index, request.input.resource.collection);

if (notify && response.created) {
this.kuzzle.notifier.notifyDocumentCreate(request, response);
}
else if (notify) {
Aschen marked this conversation as resolved.
Show resolved Hide resolved
this.kuzzle.notifier.notifyDocumentReplace(request);
}

return response;
});
}

/**
* Write several documents without adding metadata or performing data validation.
*
* @param {Request} request
* @returns {Promise<Object>}
*/
mWrite(request) {
assertHasBody(request);
assertBodyHasAttribute(request, 'documents');
assertBodyAttributeType(request, 'documents', 'array');
assertHasIndexAndCollection(request);

const notify = this.ensureBooleanFlag(request, 'input.args.notify');

return this.engine.mcreateOrReplace(request, false)
.then(response => {
if (response.error.length > 0) {
request.setError(new PartialError('Some document creations failed', response.error));
}

if (notify) {
this.kuzzle.notifier.notifyDocumentMChanges(request, response.result, true);
}

return { hits: response.result, total: response.result.length };
});
}

}

module.exports = BulkController;
3 changes: 3 additions & 0 deletions lib/api/controllers/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ class BaseController {
const booleanValue = flagValue !== undefined ? true : false;

_.set(request, flagPath, booleanValue);

return booleanValue;
}

return Boolean(flagValue);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not ensureBooleanFlag anymore, since you're now retrieving the value

another name must now be found, for instance tryGetBoolean?

also, the jsdoc is now outdated

}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/api/core/plugins/pluginContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class PluginContext {
const Kuzzle = require('../../kuzzle');

this.accessors = {};
this.config = kuzzle.config;
this.config = JSON.parse(JSON.stringify(kuzzle.config));
this.constructors = deprecateProperties({
RequestContext,
RequestInput,
Expand Down
7 changes: 5 additions & 2 deletions lib/config/httpRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ module.exports = [
{verb: 'post', url: '/credentials/:strategy/_me/_create', controller: 'auth', action: 'createMyCredentials'},
{verb: 'post', url: '/credentials/:strategy/_me/_validate', controller: 'auth', action: 'validateMyCredentials'},

/* DEPRECATED - will be removed in v2 */
/* DEPRECATED - will be removed in v2 */
{verb: 'post', url: '/_validateSpecifications', controller: 'collection', action: 'validateSpecifications'},

{verb: 'post', url: '/:index/:collection/_validateSpecifications', controller: 'collection', action: 'validateSpecifications'},
Expand All @@ -163,6 +163,9 @@ module.exports = [
{verb: 'post', url: '/:index/_bulk', controller: 'bulk', action: 'import'},
{verb: 'post', url: '/:index/:collection/_bulk', controller: 'bulk', action: 'import'},

{verb: 'post', url: '/:index/:collection/_mWrite', controller: 'bulk', action: 'mWrite'},
{verb: 'post', url: '/:index/:collection/_write', controller: 'bulk', action: 'write'},

/* DEPRECATED - will be removed in v2 */
{verb: 'post', url: '/_getStats', controller: 'server', action: 'getStats'},

Expand Down Expand Up @@ -296,7 +299,7 @@ module.exports = [

/* DEPRECATED - will be removed in v2 */
{verb: 'put', url: '/_specifications', controller: 'collection', action: 'updateSpecifications'},

{verb: 'put', url: '/:index/:collection/_specifications', controller: 'collection', action: 'updateSpecifications'},

{verb: 'put', url: '/:index/:collection/:_id', controller: 'document', action: 'createOrReplace'},
Expand Down
73 changes: 56 additions & 17 deletions lib/services/elasticsearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,21 @@ class ElasticSearch extends Service {
* Create a new document to ElasticSearch, or replace it if it already exist
*
* @param {Request} request
* @param {boolean} injectKuzzleMeta
* @returns {Promise} resolve an object that contains _id
*/
createOrReplace(request) {
createOrReplace(request, injectKuzzleMeta = true) {
const
esRequest = initESRequest(request, ['refresh']),
userId = this._getUserId(request);
userId = this._getUserId(request),
kuzzleMeta = {
author: userId,
createdAt: Date.now(),
updatedAt: Date.now(),
updater: userId,
active: true,
deletedAt: null
};

esRequest.id = request.input.resource._id;
esRequest.body = request.input.body;
Expand All @@ -359,14 +368,37 @@ class ElasticSearch extends Service {
}

// Add metadata
esRequest.body._kuzzle_info = {
author: userId,
createdAt: Date.now(),
updatedAt: Date.now(),
updater: userId,
active: true,
deletedAt: null
};
if (injectKuzzleMeta) {
esRequest.body._kuzzle_info = kuzzleMeta;
}

return this.client.index(esRequest)
.then(result => this.refreshIndexIfNeeded(esRequest, _.extend(result, {_source: request.input.body})))
.catch(error => Bluebird.reject(this.esWrapper.formatESError(error)));
});
}

/**
* Create a new document to ElasticSearch, or replace it if it already exist.
* This method does not adds additional metadata.
*
* @param {Request} request
* @returns {Promise} resolve an object that contains _id
*/
writeDocument(request) {
const esRequest = initESRequest(request, ['refresh']);

esRequest.id = request.input.resource._id;
esRequest.body = request.input.body;

assertNoRouting(esRequest);
assertWellFormedRefresh(esRequest);

return this.kuzzle.indexCache.exists(esRequest.index, esRequest.type)
.then(exists => {
if (! exists) {
throw new PreconditionError(`Index '${esRequest.index}' and/or collection '${esRequest.type}' does not exist`);
}

return this.client.index(esRequest)
.then(result => this.refreshIndexIfNeeded(esRequest, _.extend(result, {_source: request.input.body})))
Expand Down Expand Up @@ -1076,12 +1108,14 @@ class ElasticSearch extends Service {
* Create or replace multiple documents at once.
*
* @param {Request} request - Kuzzle API request
* @param {boolean} injectKuzzleMeta
* @return {Promise}
*/
mcreateOrReplace(request) {
const
esRequest = initESRequest(request, ['consistency', 'refresh', 'timeout', 'fields']),
extracted = extractMDocuments(request, {
mcreateOrReplace(request, injectKuzzleMeta = true) {
let kuzzleMeta = {};

if (injectKuzzleMeta) {
kuzzleMeta = {
_kuzzle_info: {
active: true,
author: this._getUserId(request),
Expand All @@ -1090,12 +1124,17 @@ class ElasticSearch extends Service {
deletedAt: null,
createdAt: Date.now()
}
});
};
}

const
esRequest = initESRequest(request, ['consistency', 'refresh', 'timeout', 'fields']),
extracted = extractMDocuments(request, kuzzleMeta);

esRequest.body = [];

let i; // NOSONAR
Aschen marked this conversation as resolved.
Show resolved Hide resolved
for(i = 0; i < extracted.documents.length; i++) {
for (i = 0; i < extracted.documents.length; i++) {
Aschen marked this conversation as resolved.
Show resolved Hide resolved
esRequest.body.push({
index: {
_index: request.input.resource.index,
Expand Down Expand Up @@ -1300,7 +1339,7 @@ class ElasticSearch extends Service {
}

/**
* Execute an ES request prepared by mcreate, mupdate, mreplace or mdelete
* Execute an ES request prepared by mcreate, mupdate, mreplace, mdelete or mwriteDocuments
* Returns a standardized ES response object, containing the list of
* successfully performed operations, and the rejected ones
*
Expand Down
Loading