[Persistence] Update Elastic persistence

Update ElasticSearch persistence provider to use ElasticSearch's
API, WTD-1033.
This commit is contained in:
Victor Woeltjen
2015-03-19 17:04:16 -07:00
parent 37310443e6
commit f2f9b8bbee
4 changed files with 35 additions and 69 deletions

View File

@ -13,7 +13,7 @@
"platform/features/scrolling", "platform/features/scrolling",
"platform/forms", "platform/forms",
"platform/persistence/cache", "platform/persistence/cache",
"platform/persistence/couch", "platform/persistence/elastic",
"example/generator" "example/generator"
] ]

View File

@ -6,8 +6,8 @@
{ {
"provides": "persistenceService", "provides": "persistenceService",
"type": "provider", "type": "provider",
"implementation": "CouchPersistenceProvider.js", "implementation": "ElasticPersistenceProvider.js",
"depends": [ "$http", "$q", "PERSISTENCE_SPACE", "COUCHDB_PATH" ] "depends": [ "$http", "$q", "PERSISTENCE_SPACE", "ELASTIC_ROOT", "ELASTIC_PATH" ]
} }
], ],
"constants": [ "constants": [
@ -16,22 +16,26 @@
"value": "mct" "value": "mct"
}, },
{ {
"key": "COUCHDB_PATH", "key": "ELASTIC_ROOT",
"value": "/couch/openmct" "value": "http://localhost:9200"
}, },
{ {
"key": "COUCHDB_INDICATOR_INTERVAL", "key": "ELASTIC_PATH",
"value": "mct/domain_object"
},
{
"key": "ELASTIC_INDICATOR_INTERVAL",
"value": 15000 "value": 15000
} }
], ],
"indicators": [ "indicators": [
{ {
"implementation": "CouchIndicator.js", "implementation": "ElasticIndicator.js",
"depends": [ "depends": [
"$http", "$http",
"$interval", "$interval",
"COUCHDB_PATH", "ELASTIC_ROOT",
"COUCHDB_INDICATOR_INTERVAL" "ELASTIC_INDICATOR_INTERVAL"
] ]
} }
] ]

View File

@ -1,41 +0,0 @@
/*global define*/
define(
[],
function () {
"use strict";
/**
* A CouchDocument describes domain object model in a format
* which is easily read-written to CouchDB. This includes
* Couch's _id and _rev fields, as well as a sseparate
* metadata field which contains a subset of information found
* in the model itself (to support search optimization with
* CouchDB views.)
* @constructor
* @param {string} id the id under which to store this mode
* @param {object} model the model to store
* @param {string} rev the revision to include (or undefined,
* if no revision should be noted for couch)
* @param {boolean} whether or not to mark this documnet as
* deleted (see CouchDB docs for _deleted)
*/
function CouchDocument(id, model, rev, markDeleted) {
return {
"_id": id,
"_rev": rev,
"_deleted": markDeleted,
"metadata": {
"category": "domain object",
"type": model.type,
"owner": "admin",
"name": model.name,
"created": Date.now()
},
"model": model
};
}
return CouchDocument;
}
);

View File

@ -1,37 +1,39 @@
/*global define*/ /*global define*/
define( define(
["./CouchDocument"], [],
function (CouchDocument) { function () {
'use strict'; 'use strict';
// JSLint doesn't like dangling _'s, but CouchDB uses these, so // JSLint doesn't like underscore-prefixed properties,
// hide this behind variables. // so hide them here.
var REV = "_rev", var SRC = "_source",
REV = "_version",
ID = "_id"; ID = "_id";
/** /**
* The CouchPersistenceProvider reads and writes JSON documents * The ElasticPersistenceProvider reads and writes JSON documents
* (more specifically, domain object models) to/from a CouchDB * (more specifically, domain object models) to/from an ElasticSearch
* instance. * instance.
* @constructor * @constructor
*/ */
function ElasticPersistenceProvider($http, $q, SPACE, PATH) { function ElasticPersistenceProvider($http, $q, SPACE, ROOT, PATH) {
var spaces = [ SPACE ], var spaces = [ SPACE ],
revs = {}; revs = {};
// Convert a subpath to a full path, suitable to pass // Convert a subpath to a full path, suitable to pass
// to $http. // to $http.
function url(subpath) { function url(subpath) {
return PATH + '/' + subpath; return ROOT + '/' + PATH + '/' + subpath;
} }
// Issue a request using $http; get back the plain JS object // Issue a request using $http; get back the plain JS object
// from the expected JSON response // from the expected JSON response
function request(subpath, method, value) { function request(subpath, method, value, params) {
return $http({ return $http({
method: method, method: method,
url: url(subpath), url: url(subpath),
params: params,
data: value data: value
}).then(function (response) { }).then(function (response) {
return response.data; return response.data;
@ -44,8 +46,11 @@ define(
function get(subpath) { function get(subpath) {
return request(subpath, "GET"); return request(subpath, "GET");
} }
function put(subpath, value) { function put(subpath, value, params) {
return request(subpath, "PUT", value); return request(subpath, "PUT", value, params);
}
function del(subpath) {
return request(subpath, "DELETE");
} }
// Pull out a list of document IDs from CouchDB's // Pull out a list of document IDs from CouchDB's
@ -56,9 +61,9 @@ define(
// Get a domain object model out of CouchDB's response // Get a domain object model out of CouchDB's response
function getModel(response) { function getModel(response) {
if (response && response.model) { if (response && response[SRC]) {
revs[response[ID]] = response[REV]; revs[response[ID]] = response[REV];
return response.model; return response[SRC];
} else { } else {
return undefined; return undefined;
} }
@ -108,8 +113,7 @@ define(
* operation * operation
*/ */
createObject: function (space, key, value) { createObject: function (space, key, value) {
return put(key, new CouchDocument(key, value)) return put(key, value).then(checkResponse);
.then(checkResponse);
}, },
/** /**
@ -135,7 +139,7 @@ define(
* operation * operation
*/ */
updateObject: function (space, key, value) { updateObject: function (space, key, value) {
return put(key, new CouchDocument(key, value, revs[key])) return put(key, value, { version: revs[key] })
.then(checkResponse); .then(checkResponse);
}, },
/** /**
@ -150,8 +154,7 @@ define(
* operation * operation
*/ */
deleteObject: function (space, key, value) { deleteObject: function (space, key, value) {
return put(key, new CouchDocument(key, value, revs[key], true)) return del(key).then(checkResponse);
.then(checkResponse);
} }
}; };