MCPcopy Create free account
hub / github.com/apache/cassandra-nodejs-driver / HostMap

Class HostMap

lib/host.js:454–655  ·  view source on GitHub ↗

* Represents an associative-array of Host hosts that can be iterated. * It creates an internal copy when adding or removing, making it safe to iterate using the values() * method within async operations. * @extends events.EventEmitter * @constructor

Source from the content-addressed store, hash-verified

452 * @constructor
453 */
454class HostMap extends events.EventEmitter{
455 constructor() {
456 super();
457
458 this._items = new Map();
459 this._values = null;
460
461 Object.defineProperty(this, 'length', { get: () => this.values().length, enumerable: true });
462
463 /**
464 * Emitted when a host is added to the map
465 * @event HostMap#add
466 */
467 /**
468 * Emitted when a host is removed from the map
469 * @event HostMap#remove
470 */
471 }
472
473 /**
474 * Executes a provided function once per map element.
475 * @param callback
476 */
477 forEach(callback) {
478 const items = this._items;
479 for (const [ key, value ] of items) {
480 callback(value, key);
481 }
482 }
483
484 /**
485 * Gets a {@link Host host} by key or undefined if not found.
486 * @param {String} key
487 * @returns {Host}
488 */
489 get(key) {
490 return this._items.get(key);
491 }
492
493 /**
494 * Returns an array of host addresses.
495 * @returns {Array.<String>}
496 */
497 keys() {
498 return Array.from(this._items.keys());
499 }
500
501 /**
502 * Removes an item from the map.
503 * @param {String} key The key of the host
504 * @fires HostMap#remove
505 */
506 remove(key) {
507 const value = this._items.get(key);
508 if (value === undefined) {
509 return;
510 }
511

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected