import { isVersion3 } from '@/services/datastores/cassandra/lib/utils/cluster-utils' ; import { ICachingOptions , ICompressionOptions , ITableColumn , ITableSchema , IKeyQuery , } from '@/services/datastores/cassandra/typings/cassandra' ; import { Client } from 'cassandra-driver' ; import _ from 'lodash' ; import { escapeValueString } from './statement-utils' ; param fullyQualifiedClass returns export function getClassName ( fullyQualifiedClass ) { const classNamePattern = new RegExp ( '.*\\.(.*)' ) ; const match = classNamePattern . exec ( fullyQualifiedClass ) ; return match && match . length === 2 ? match [ 1 ] : '' ; } export function getCachingDetails ( caching ) { try { const result = JSON . parse ( caching . replace ( /'/g , '"' ) ) as { keys : string ; rows_per_partition : string ; } ; return { keys : result . keys as 'ALL' | 'NONE' , rows : result . rows_per_partition as 'ALL' | 'NONE' , } ; } catch ( err ) { throw new Error ( `Invalid caching string: "${ caching }"` ) ; } } export function getCompressionDetails ( compressionInfo ) { const patternMap = { 'class|sstable_compression' : 'class' , '^chunk_length_(in_)?kb$' : 'chunk_length_kb' , } as { [ pattern ] : string ; } ; const compressionDetails = { } as { [ key ] : any ; class : string ; } ; Object . keys ( compressionInfo ) . forEach ( ( key ) => { const patterns = Object . keys ( patternMap ) ; let alias = undefined ; for ( const pattern of patterns ) { if ( new RegExp ( pattern ) . test ( key ) ) { alias = patternMap [ pattern ] ; break ; } } compressionDetails [ alias || key ] = compressionInfo [ key ] ; } ) ; compressionDetails . class = getClassName ( compressionDetails . class ) ; return compressionDetails ; } param row return { { strategy : String , options : Object } } export function extractCompaction ( client , row ) { let strategy ; let options ; if ( isVersion3 ( client ) ) { strategy = row . compaction . class ; options = _ . omit ( row . compaction , 'class' ) ; } else { strategy = row . compaction_strategy_class ; options = row . compaction_strategy_options ; } return { strategy : getClassName ( strategy ) , options } ; } param schema return export function getAdditionalQueryColumns ( schema ) { const partitionKeyNameSet = new Set ( schema . partitionKeys . map ( ( col ) => col . name ) ) ; const clusteringKeyNameSet = new Set ( schema . clusteringKeys . map ( ( col ) => col . name ) ) ; const additionalColumns = schema . columns . filter ( ( col ) => { const isCollection = col . type . match ( /.*<.*>/ ) ; return ( ! isCollection && ! partitionKeyNameSet . has ( col . name ) && ! clusteringKeyNameSet . has ( col . name ) ) ; } ) ; const ttlColumns = additionalColumns . map ( ( col ) => `TTL("${ col . name }")` ) ; const writeTimeColumns = additionalColumns . map ( ( col ) => `writetime("${ col . name }")` ) ; return new Array < string > ( ) . concat ( ttlColumns , writeTimeColumns ) ; } param schema return export function getAdditionalQueryColumnNames ( schema ) { const { partitionKeys , clusteringKeys , columns } = schema ; const partitionKeyNameSet = new Set ( partitionKeys . map ( ( col ) => col . name ) ) ; const clusteringKeyNameSet = new Set ( clusteringKeys . map ( ( col ) => col . name ) ) ; return columns . filter ( ( col ) => { const isCollection = col . type . match ( /.*<.*>/ ) ; return ( ! isCollection && ! partitionKeyNameSet . has ( col . name ) && ! clusteringKeyNameSet . has ( col . name ) ) ; } ) . map ( ( column ) => column . name ) ; } param schema param primaryKey return export function getWhereClause ( schema , query ) { const columnMap = { } as { [ columnName ] : ITableColumn ; } ; schema . columns . forEach ( ( colDef ) => { columnMap [ colDef . name ] = colDef ; } ) ; const whereClauses = new Array < string > ( ) ; const { primaryKey , options } = query ; Object . keys ( primaryKey ) . forEach ( ( keyName ) => { const details = primaryKey [ keyName ] ; const { value } = details ; const column = columnMap [ keyName ] ; const { needsQuotes , type } = column ; let keyValue ; if ( type === 'blob' && options . encoding !== 'hex' ) { if ( options . encoding === 'base64' ) { const utf8Data = Buffer . from ( value , 'base64' ) . toString ( 'utf-8' ) ; const escapedValue = escapeValueString ( utf8Data as string ) ; keyValue = `textAsBlob('${ escapedValue }')` ; } else { const escapedValue = escapeValueString ( value as string ) ; keyValue = `textAsBlob('${ escapedValue }')` ; } } else if ( needsQuotes ) { keyValue = `'${ value }'` ; } else { keyValue = value ; } whereClauses . push ( `"${ keyName }"=${ keyValue }` ) ; } ) ; return whereClauses ; } /**
 * Checks the column flags for a v3 schema as stored in the "flags" column
 * of the "system_schema.tables" table.
 */ export function isThriftV3 ( columnFlags ) { const flags = new Set ( columnFlags ) ; return flags . has ( 'super' ) || flags . has ( 'dense' ) || ! flags . has ( 'compound' ) ; } export function isThriftV2 ( isDense , comparator ) { const isComposite = comparator . indexOf ( 'org.apache.cassandra.db.marshal.CompositeType' ) !== - 1 ; return isDense || ! isComposite ; } export function getBlobColumnNames ( schema , primaryKeyOnly ) { const { columns , primaryKey } = schema ; const includeColumns = primaryKeyOnly ? primaryKey : columns ; return includeColumns . reduce ( ( set , curr ) => { const { type , name } = curr ; if ( type . includes ( 'blob' ) ) { set . add ( name ) ; } return set ; } , new Set < string > ( ) ) ; }