diff --git a/lib/metadata/data-collection.js b/lib/metadata/data-collection.js deleted file mode 100644 index e7f463fc1..000000000 --- a/lib/metadata/data-collection.js +++ /dev/null @@ -1,161 +0,0 @@ -"use strict"; -const events = require("events"); -/** - * Describes a table or a view - * @alias module:metadata~DataCollection - * @abstract - */ -class DataCollection extends events.EventEmitter { - /** - * Creates a new instance of DataCollection - * @param {String} name Name of the data object. - */ - constructor(name) { - super(); - this.setMaxListeners(0); - // private - Object.defineProperty(this, "loading", { - value: false, - enumerable: false, - writable: true, - }); - Object.defineProperty(this, "loaded", { - value: false, - enumerable: false, - writable: true, - }); - /** - * Name of the object - * @type {String} - */ - this.name = name; - /** - * False-positive probability for SSTable Bloom filters. - * @type {number} - */ - this.bloomFilterFalsePositiveChance = 0; - /** - * Level of caching: all, keys_only, rows_only, none - * @type {String} - */ - this.caching = null; - /** - * A human readable comment describing the table. - * @type {String} - */ - this.comment = null; - /** - * Specifies the time to wait before garbage collecting tombstones (deletion markers) - * @type {number} - */ - this.gcGraceSeconds = 0; - /** - * Compaction strategy class used for the table. - * @type {String} - */ - this.compactionClass = null; - /** - * Associative-array containing the compaction options keys and values. - * @type {Object} - */ - this.compactionOptions = null; - /** - * Associative-array containing the compression options. - * @type {Object} - */ - this.compression = null; - /** - * Specifies the probability of read repairs being invoked over all replicas in the current data center. - * @type {number} - */ - this.localReadRepairChance = 0; - /** - * Specifies the probability with which read repairs should be invoked on non-quorum reads. The value must be - * between 0 and 1. - * @type {number} - */ - this.readRepairChance = 0; - /** - * An associative Array containing extra metadata for the table. - * - * For Apache Cassandra versions prior to 3.0.0, this method always returns `null`. - * @type {Object} - */ - this.extensions = null; - /** - * When compression is enabled, this option defines the probability - * with which checksums for compressed blocks are checked during reads. - * The default value for this options is 1.0 (always check). - * - * For Apache Cassandra versions prior to 3.0.0, this method always returns `null`. - * @type {Number|null} - */ - this.crcCheckChance = null; - /** - * Whether the populate I/O cache on flush is set on this table. - * @type {Boolean} - */ - this.populateCacheOnFlush = false; - /** - * Returns the default TTL for this table. - * @type {Number} - */ - this.defaultTtl = 0; - /** - * * Returns the speculative retry option for this table. - * @type {String} - */ - this.speculativeRetry = "NONE"; - /** - * Returns the minimum index interval option for this table. - * - * Note: this option is available in Apache Cassandra 2.1 and above, and will return `null` for - * earlier versions. - * @type {Number|null} - */ - this.minIndexInterval = 128; - /** - * Returns the maximum index interval option for this table. - * - * Note: this option is available in Apache Cassandra 2.1 and above, and will return `null` for - * earlier versions. - * @type {Number|null} - */ - this.maxIndexInterval = 2048; - /** - * Array describing the table columns. - * @type {Array} - */ - this.columns = null; - /** - * An associative Array of columns by name. - * @type {Object} - */ - this.columnsByName = null; - /** - * Array describing the columns that are part of the partition key. - * @type {Array} - */ - this.partitionKeys = []; - /** - * Array describing the columns that form the clustering key. - * @type {Array} - */ - this.clusteringKeys = []; - /** - * Array describing the clustering order of the columns in the same order as the clusteringKeys. - * @type {Array} - */ - this.clusteringOrder = []; - /** - * An associative Array containing nodesync options for this table. - * - * For DSE versions prior to 6.0.0, this method always returns {@code null}. If nodesync - * was not explicitly configured for this table this method will also return {@code null}. - * @type {Object} - */ - this.nodesync = null; - } -} - -module.exports = DataCollection; diff --git a/lib/metadata/index.d.ts b/lib/metadata/index.d.ts index 854f42432..bf8c9a090 100644 --- a/lib/metadata/index.d.ts +++ b/lib/metadata/index.d.ts @@ -39,6 +39,18 @@ export interface ColumnInfo { type: DataTypeInfo; } +export enum ColumnKind { + Regular = 0, + Static, + Clustering, + PartitionKey, + } + +export interface ColumnMetadata { + type: string; + kind: ColumnKind; + } + export enum IndexKind { custom = 0, keys, @@ -58,48 +70,15 @@ export interface Index { isKeysKind(): boolean; } -export interface DataCollection { - bloomFilterFalsePositiveChance: number; - caching: string; - clusteringKeys: ColumnInfo[]; - clusteringOrder: string[]; - columns: ColumnInfo[]; - columnsByName: { [key: string]: ColumnInfo }; - comment: string; - compactionClass: string; - compactionOptions: { [option: string]: any }; - compression: { - class?: string; - [option: string]: any; - }; - crcCheckChange?: number; - defaultTtl: number; - extensions: { [option: string]: any }; - gcGraceSeconds: number; - localReadRepairChance: number; - maxIndexInterval?: number; - minIndexInterval?: number; - name: string; - partitionKeys: ColumnInfo[]; - populateCacheOnFlush: boolean; - readRepairChance: number; - speculativeRetry: string; - } - -export interface MaterializedView extends DataCollection { +export interface MaterializedView extends TableMetadata { tableName: string; - whereClause: string; - includeAllColumns: boolean; } -export interface TableMetadata extends DataCollection { - indexes: Index[]; - indexInterval?: number; - isCompact: boolean; - memtableFlushPeriod: number; - replicateOnWrite: boolean; - cdc?: boolean; - virtual: boolean; +export interface TableMetadata { + columns: { [name: string]: ColumnMetadata }; + partitionKey: string[]; + clusteringKey: string[]; + partitioner: string | null; } export interface QueryTrace { @@ -135,6 +114,27 @@ export interface Udt { fields: ColumnInfo[]; } +export enum StrategyKind { + SimpleStrategy = 0, + NetworkTopologyStrategy, + LocalStrategy, + Other, + } + +export type Strategy = + | { kind: StrategyKind.SimpleStrategy; replicationFactor: number } + | { kind: StrategyKind.NetworkTopologyStrategy; datacenterRepfactors: { [datacenter: string]: number } } + | { kind: StrategyKind.LocalStrategy } + | { kind: StrategyKind.Other; name: string; data: { [key: string]: string } }; + +export interface KeyspaceMetadata { + strategy: Strategy; + durableWrites: boolean; + tables: { [name: string]: TableMetadata }; + views: { [name: string]: MaterializedView }; + userDefinedTypes: { [name: string]: Udt }; + } + export interface Metadata { keyspaces: { [name: string]: { name: string; strategy: string } }; diff --git a/lib/metadata/keyspace-metadata.js b/lib/metadata/keyspace-metadata.js new file mode 100644 index 000000000..d69397f55 --- /dev/null +++ b/lib/metadata/keyspace-metadata.js @@ -0,0 +1,121 @@ +"use strict"; + +const { _TableMetadata } = require("./table-metadata"); + +/** + * Identifies the replication strategy variant. + * @readonly + * @enum {number} + * @alias module:metadata~StrategyKind + */ +const StrategyKind = { + /** + * Deprecated in ScyllaDB. + * + * **Use only for a single datacenter and one rack.** + * + * Places the first replica on a node determined by the partitioner. + * Additional replicas are placed on the next nodes clockwise in the ring + * without considering topology (rack or datacenter location). + */ + SimpleStrategy: 0, + /** + * Use this strategy when you have (or plan to have) your cluster deployed across + * multiple datacenters. This strategy specifies how many replicas you want in each + * datacenter. + * + * `NetworkTopologyStrategy` places replicas in the same datacenter by walking the ring + * clockwise until reaching the first node in another rack. It attempts to place replicas + * on distinct racks because nodes in the same rack (or similar physical grouping) often + * fail at the same time due to power, cooling, or network issues. + */ + NetworkTopologyStrategy: 1, + /** + * Used for internal purposes, e.g. for system tables. + */ + LocalStrategy: 2, + /** + * Unknown other strategy, which is not supported by the driver. + */ + Other: 3, +}; + +StrategyKind.S; + +/** + * Describes the replication strategy used by a keyspace. + * @alias module:metadata~Strategy + */ +class Strategy { + /** + * Identifies which strategy variant this is. + * @type {StrategyKind} + */ + kind; + + /** + * Replication factor, i.e. how many replicas of each piece of data there are. + * (only set when {@link kind} is {@link StrategyKind.SimpleStrategy}). + * @type {number?} + */ + replicationFactor; + + /** + * Replication factors of datacenters with given names, i.e. how many replicas of each piece + * of data there are in each datacenter. + * (only set when {@link kind} is {@link StrategyKind.NetworkTopologyStrategy}). + * @type {Object.?} + */ + datacenterRepfactors; + + /** + * Name of the strategy (only set when {@link kind} is {@link StrategyKind.Other}). + * @type {String?} + */ + name; + + /** + * Additional parameters of the strategy, which the driver does not understand. + * (only set when {@link kind} is {@link StrategyKind.Other}). + * @type {Object.?} + */ + data; +} + +/** + * Describes a keyspace in the cluster. + * @alias module:metadata~KeyspaceMetadata + */ +class KeyspaceMetadata { + /** + * Replication strategy used by the keyspace. + * @type {Strategy} + */ + strategy; + + /** + * Whether the keyspace has durable writes enabled. + * @type {Boolean} + */ + durableWrites; + + /** + * Tables in the keyspace, keyed by table name. + * @type {Object.} + */ + tables; + + /** + * Materialized views in the keyspace, keyed by view name. + * @type {Object.} + */ + views; + + /** + * User-defined types in the keyspace, keyed by type name. + * @type {Object.} + */ + userDefinedTypes; +} + +module.exports = { KeyspaceMetadata, Strategy, StrategyKind }; diff --git a/lib/metadata/materialized-view.js b/lib/metadata/materialized-view.js index 764bc351e..78d189a8a 100644 --- a/lib/metadata/materialized-view.js +++ b/lib/metadata/materialized-view.js @@ -1,33 +1,16 @@ "use strict"; -const DataCollection = require("./data-collection"); +const { TableMetadata } = require("./table-metadata"); /** * Describes a CQL materialized view. * @alias module:metadata~MaterializedView - * @augments {module:metadata~DataCollection} + * @extends TableMetadata */ -class MaterializedView extends DataCollection { +class MaterializedView extends TableMetadata { /** - * Creates a new MaterializedView. - * @param {String} name Name of the View. + * Name of the table. + * @type {String} */ - constructor(name) { - super(name); - /** - * Name of the table. - * @type {String} - */ - this.tableName = null; - /** - * View where clause. - * @type {String} - */ - this.whereClause = null; - /** - * Determines if all the table columns where are included in the view. - * @type {boolean} - */ - this.includeAllColumns = false; - } + tableName; } module.exports = MaterializedView; diff --git a/lib/metadata/table-metadata.js b/lib/metadata/table-metadata.js index bfe8ee50e..c4a8d6e19 100644 --- a/lib/metadata/table-metadata.js +++ b/lib/metadata/table-metadata.js @@ -1,61 +1,71 @@ "use strict"; -const DataCollection = require("./data-collection"); /** - * Describes a table - * @augments {module:metadata~DataCollection} + * Some columns have a specific meaning in the context of a table, + * and this meaning is represented by the {@link ColumnKind} enum. + * @readonly + * @enum {number} + * @alias module:metadata~ColumnKind + */ +const ColumnKind = { + /** Just a regular column. */ + Regular: 0, + /** Column that has the same value for all rows in a partition. */ + Static: 1, + /** Column that is part of the clustering key. */ + Clustering: 2, + /** Column that is part of the partition key. */ + PartitionKey: 3, +}; + +/** + * Describes a column of the table. + * @alias module:metadata~ColumnMetadata + */ +class ColumnMetadata { + /** + * CQL type that the value stored in this column has. + * @type {type} + */ + type; + + /** + * Describes role of the column in the table. + * @type {ColumnKind} + */ + kind; +} + +/** + * Describes a table in the cluster. * @alias module:metadata~TableMetadata */ -class TableMetadata extends DataCollection { +class TableMetadata { + /** + * Columns that constitute the table, keyed by column name. + * @type {Object.} + */ + columns; + + /** + * Names of the columns that constitute the partition key. + * All names are guaranteed to be present in {@link columns}. + * @type {Array.} + */ + partitionKey; + + /** + * Names of the columns that constitute the clustering key. + * All names are guaranteed to be present in {@link columns}. + * @type {Array.} + */ + clusteringKey; + /** - * Creates a new instance of TableMetadata - * @param {String} name Name of the Table + * Name of the partitioner used by the table, or null if not set. + * @type {String|null} */ - constructor(name) { - super(name); - /** - * Applies only to counter tables. - * When set to true, replicates writes to all affected replicas regardless of the consistency level specified by - * the client for a write request. For counter tables, this should always be set to true. - * @type {Boolean} - */ - this.replicateOnWrite = true; - /** - * Returns the memtable flush period (in milliseconds) option for this table. - * @type {Number} - */ - this.memtableFlushPeriod = 0; - /** - * Returns the index interval option for this table. - * - * Note: this option is only available in Apache Cassandra 2.0. It is deprecated in Apache Cassandra 2.1 and - * above, and will therefore return `null` for 2.1 nodes. - * @type {Number|null} - */ - this.indexInterval = null; - /** - * Determines whether the table uses the COMPACT STORAGE option. - * @type {Boolean} - */ - this.isCompact = false; - /** - * - * @type {Array.} - */ - this.indexes = null; - - /** - * Determines whether the Change Data Capture (CDC) flag is set for the table. - * @type {Boolean|null} - */ - this.cdc = null; - - /** - * Determines whether the table is a virtual table or not. - * @type {Boolean} - */ - this.virtual = false; - } + partitioner; } -module.exports = TableMetadata; +module.exports = { TableMetadata, ColumnMetadata, ColumnKind };