YUKI Hiroshi
null+****@clear*****
Thu Oct 30 11:37:55 JST 2014
YUKI Hiroshi 2014-10-30 11:37:55 +0900 (Thu, 30 Oct 2014) New Revision: 35f349a87c9c7ad686383da962bb05f86ec03e29 https://github.com/droonga/express-droonga/commit/35f349a87c9c7ad686383da962bb05f86ec03e29 Message: Implement recursively "allHostNames" Modified files: lib/catalog/v2.js Modified: lib/catalog/v2.js (+51 -30) =================================================================== --- lib/catalog/v2.js 2014-10-22 19:18:46 +0900 (5b78749) +++ lib/catalog/v2.js 2014-10-30 11:37:55 +0900 (8355df4) @@ -1,3 +1,5 @@ +var util = require('util'); + function CatalogV2(raw) { this._raw = raw; } @@ -24,67 +26,86 @@ CatalogV2.prototype = { } }; -function Dataset(name, raw) { - this.name = name; + +function VolumeCollectionOwner(raw) { this._raw = raw; } -Dataset.prototype = { +VolumeCollectionOwner.prototype = { get allHostNames() { if (this._allHostNames) return this._allHostNames; + var uniqueHostNames = {}; + this.replicas.forEach(function(replica) { replica.allHostNames.forEach(function(hostName) { uniqueHostNames[hostName] = true; }); }); + + this.slices.forEach(function(replica) { + replica.allHostNames.forEach(function(hostName) { + uniqueHostNames[hostName] = true; + }); + }); + + if (this.ownVolume) + uniqueHostNames[this.ownVolume.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); }); + }, + + ADDRESS_PATTERN: /^([^:]+)(?::(\d+))?\/([^\.]+)\.(.+)$/, // hostName, port, tag, path + get ownVolume() { + if (typeof this._ownVolume !== 'undefined') + return this._ownVolume; + + if (this._raw.volume && this._raw.volume.address) { + var address = this._raw.volume.address; + var match = address.match(this.ADDRESS_PATTERN); + this._ownVolume = { + hostName: match[0] + }; + } + else { + this._ownVolume = null; + } + return this._ownVolume; } }; + +function Dataset(name, raw) { + this.name = name; + this._raw = raw; +} +util.inherits(Dataset, VolumeCollectionOwner); + +function Replica(raw) { + this._raw = raw; +} +util.inherits(Replica, VolumeCollectionOwner); + 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]]; - } -}; +util.inherits(Slice, VolumeCollectionOwner); + exports.CatalogV2 = CatalogV2; -------------- next part -------------- HTML����������������������������...Download