YUKI Hiroshi
null+****@clear*****
Wed Oct 22 19:17:53 JST 2014
YUKI Hiroshi 2014-10-22 19:17:53 +0900 (Wed, 22 Oct 2014) New Revision: b871d28495427f7398c0f9d50d576ab06e2c2cda https://github.com/droonga/express-droonga/commit/b871d28495427f7398c0f9d50d576ab06e2c2cda Message: Add codes to update hostNames from remote catalog (WIP) Added files: lib/catalog/index.js lib/catalog/v2.js Modified files: lib/droonga-protocol/connection-pool.js Added: lib/catalog/index.js (+12 -0) 100644 =================================================================== --- /dev/null +++ lib/catalog/index.js 2014-10-22 19:17:53 +0900 (5b54baa) @@ -0,0 +1,12 @@ +var CatalogV2 = require('./v2').CatalogV2; + +function Catalog(raw) { + switch (raw.version) + case 2: + return new CatalogV2(raw); + default: + throw new Error('catalog version ' + raw.version + ' is not supported.'); + } +}; + +exports.Catalog = Catalog; Added: lib/catalog/v2.js (+90 -0) 100644 =================================================================== --- /dev/null +++ lib/catalog/v2.js 2014-10-22 19:17:53 +0900 (5b78749) @@ -0,0 +1,90 @@ +function CatalogV2(raw) { + this._raw = raw; +} +CatalogV2.prototype = { + get allHostNames() { + if (this._allHostNames) + return this._allHostNames; + var uniqueHostNames = {}; + this.datasets.forEach(function(dataset) { + dataset.allHostNames.forEach(function(hostName) { + uniqueHostNames[hostName] = true; + }); + }); + return this._allHostNames = Object.keys(uniqueHostNames); + }, + get datasets() { + if (this._datasets) + return this._datasets; + return this._datasets = Object.keys(this._raw.datasets) + .map((function(datasetName) { + return new Dataset(datasetName, + this._raw.datasets[datasetName]); + }).bind(this)); + } +}; + +function Dataset(name, raw) { + this.name = name; + this._raw = raw; +} +Dataset.prototype = { + get allHostNames() { + if (this._allHostNames) + return this._allHostNames; + var uniqueHostNames = {}; + this.replicas.forEach(function(replica) { + replica.allHostNames.forEach(function(hostName) { + uniqueHostNames[hostName] = true; + }); + }); + return this._allHostNames = Object.keys(uniqueHostNames); + }, + get replicas() { + if (this._replicas) + return this._replicas; + return this._replicas = this._raw.replicas.map(function(replica) { + return new Replica(replica); + }); + } +}; + +function Replica(raw) { + this._raw = raw; +} +Replica.prototype = { + get allHostNames() { + if (this._allHostNames) + return this._allHostNames; + var uniqueHostNames = {}; + this.slices.forEach(function(slice) { + slice.allHostNames.forEach(function(hostName) { + uniqueHostNames[hostName] = true; + }); + }); + return this._allHostNames = Object.keys(uniqueHostNames); + }, + get slices() { + if (this._slices) + return this._slices; + return this._slices = this._raw.slices.map(function(slice) { + return new Slice(slice); + }); + } +}; + +function Slice(raw) { + this._raw = raw; +} +Slice.prototype = { + ADDRESS_PATTERN: /^([^:]+)(?::(\d+))?\/([^\.]+)\.(.+)$/, // hostName, port, tag, path + get allHostNames() { + if (this._allHostNames) + return this._allHostNames; + var address = this._raw.volume.address; + var match = address.match(this.ADDRESS_PATTERN); + return this._allHostNames = [match[0]]; + } +}; + +exports.CatalogV2 = CatalogV2; Modified: lib/droonga-protocol/connection-pool.js (+28 -0) =================================================================== --- lib/droonga-protocol/connection-pool.js 2014-10-22 18:05:03 +0900 (39c3445) +++ lib/droonga-protocol/connection-pool.js 2014-10-22 19:17:53 +0900 (6c6af25) @@ -11,6 +11,7 @@ var Connection = require('./connection').Connection; var ConsoleLogger = require('../console-logger').ConsoleLogger; +var Catalog = require('../catalog').Catalog; function ConnectionPool(params) { this._params = params || {}; @@ -100,6 +101,33 @@ ConnectionPool.prototype = { get count() { return this._hostNames.length; + }, + + updateHostNamesFromCatalog: function() { + return this.getHostNamesFromCatalog() + .then((function(hostNames) { + this.hostNames = hostNames; + }).bind(this)); + }, + + getHostNamesFromCatalog: function() { + return this.fetchCatalog() + .then(function(catalog) { + return catalog.allHostNames; + }); + }, + + fetchCatalog: function() { + return this.get().thenableEmitMessage('catalog.fetch') + .then(this._handleFetchedCatalog.bind(this)); + }, + _handleFetchedCatalog: function(result) { + if (result.errorCode) { + this._params.logger.error(new Error('ConnectionPool: failed to fetch catalog')); + return; + } + var catalog = result.response.body; + return new Catalog(catalog); } }; -------------- next part -------------- HTML����������������������������...下载