All files / src/utils normalizer.js

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 2571x 1x   1x   1x 381x 174x       1x 114x 114x 114x 105x     1x   2x 2x 4x 2x 2x   2x   2x       96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x 96x       74x       18x 4x     18x 18x 14x 14x 2x       14x           18x 120x 78x     120x 2x 2x     118x 20x         98x     118x 10x     118x 40x 20x   20x       118x 1x 117x 1x           18x 14x     18x 8x   8x 1x       8x 8x 8x 3x   8x 3x     8x         8x 16x 8x 14x 5x     8x 3x         8x 2x     8x   8x 4x   8x 1x           18x 16x   18x 18x 18x 10x 10x 6x   8x     18x       18x         18x 2x 2x                       2x   16x     18x       18x 18x       18x 77x 25x       18x 18x 18x 18x 18x   18x                                             1x  
const _ = require('lodash');
const util = require('util');
 
const parser = require('./parser');
 
const arraySort = (a, b) => {
  if (a > b) return 1;
  Eif (a < b) return -1;
  return 0;
};
 
const normalizeTypeDef = (typeDef) => {
  const formattedTypeDef = typeDef.replace(/[\s]/g, '').replace(/varchar/g, 'text').replace(/frozen/ig, 'frozen');
  const frozenMatch = formattedTypeDef.match(/frozen</g);
  if (frozenMatch && frozenMatch.length) return formattedTypeDef.replace(/frozen</g, '').slice(0, -1 * frozenMatch.length);
  return formattedTypeDef;
};
 
const normalizer = {
  normalize_replication_option(replicationOptions) {
    const normalizedReplicationOptions = replicationOptions;
    Object.keys(normalizedReplicationOptions).forEach((key) => {
      if (key === 'class') {
        normalizedReplicationOptions[key] = normalizedReplicationOptions[key].replace('org.apache.cassandra.locator.', '');
        return;
      }
      normalizedReplicationOptions[key] = parseInt(normalizedReplicationOptions[key], 10);
    });
    return normalizedReplicationOptions;
  },
 
  normalize_query_option(options) {
    const queryOptions = { prepare: options.prepare };
    Iif (options.consistency) queryOptions.consistency = options.consistency;
    if (options.fetchSize) queryOptions.fetchSize = options.fetchSize;
    Iif (options.autoPage) queryOptions.autoPage = options.autoPage;
    Iif (options.hints) queryOptions.hints = options.hints;
    Iif (options.pageState) queryOptions.pageState = options.pageState;
    Iif (options.retry) queryOptions.retry = options.retry;
    Iif (options.serialConsistency) queryOptions.serialConsistency = options.serialConsistency;
    Iif (options.customPayload) queryOptions.customPayload = options.customPayload;
    Iif (options.isIdempotent) queryOptions.isIdempotent = options.isIdempotent;
    Iif (options.readTimeout) queryOptions.readTimeout = options.readTimeout;
    Iif (options.retry) queryOptions.retry = options.retry;
    Iif (options.retryOnTimeout) queryOptions.retryOnTimeout = options.retryOnTimeout;
    Iif (options.routingIndexes) queryOptions.routingIndexes = options.routingIndexes;
    Iif (options.routingKey) queryOptions.routingKey = options.routingKey;
    Iif (options.routingNames) queryOptions.routingNames = options.routingNames;
    Iif (options.timestamp) queryOptions.timestamp = options.timestamp;
    return queryOptions;
  },
 
  normalize_user_defined_type(fieldType) {
    return normalizeTypeDef(fieldType);
  },
 
  normalize_primary_key(outputSchema) {
    if (outputSchema.key && typeof outputSchema.key[0] === 'string') {
      outputSchema.key[0] = [outputSchema.key[0]];
    }
 
    Eif (outputSchema.key && outputSchema.key.length) {
      for (let i = 1; i < outputSchema.key.length; i++) {
        if (!outputSchema.clustering_order) outputSchema.clustering_order = {};
        if (!outputSchema.clustering_order[outputSchema.key[i]]) {
          outputSchema.clustering_order[outputSchema.key[i]] = 'ASC';
        }
 
        // eslint-disable-next-line max-len
        outputSchema.clustering_order[outputSchema.key[i]] = outputSchema.clustering_order[outputSchema.key[i]].toUpperCase();
      }
    }
  },
 
  normalize_fields(modelSchema, outputSchema) {
    Object.keys(outputSchema.fields).forEach((fieldName) => {
      if (typeof (outputSchema.fields[fieldName]) === 'string') {
        outputSchema.fields[fieldName] = { type: outputSchema.fields[fieldName] };
      }
 
      if (fieldName === 'solr_query' || outputSchema.fields[fieldName].virtual) {
        delete outputSchema.fields[fieldName];
        return;
      }
 
      if (outputSchema.fields[fieldName].typeDef) {
        outputSchema.fields[fieldName] = {
          type: outputSchema.fields[fieldName].type,
          typeDef: outputSchema.fields[fieldName].typeDef,
        };
      } else {
        outputSchema.fields[fieldName] = { type: outputSchema.fields[fieldName].type };
      }
 
      if (outputSchema.fields[fieldName].type === 'varchar') {
        outputSchema.fields[fieldName].type = 'text';
      }
 
      if (['map', 'list', 'set', 'frozen'].includes(outputSchema.fields[fieldName].type)) {
        if (modelSchema.typeMaps && modelSchema.typeMaps[fieldName]) {
          outputSchema.fields[fieldName].typeDef = normalizeTypeDef(modelSchema.typeMaps[fieldName]);
        } else {
          outputSchema.fields[fieldName].typeDef = normalizeTypeDef(outputSchema.fields[fieldName].typeDef);
        }
      }
 
      if (modelSchema.staticMaps && modelSchema.staticMaps[fieldName] === true) {
        outputSchema.fields[fieldName].static = true;
      } else if (modelSchema.fields[fieldName].static) {
        outputSchema.fields[fieldName].static = true;
      }
    });
  },
 
  normalize_materialized_views(outputSchema) {
    if (!outputSchema.materialized_views) {
      outputSchema.materialized_views = {};
    }
 
    Object.keys(outputSchema.materialized_views).forEach((materializedViewName) => {
      const outputMView = outputSchema.materialized_views[materializedViewName];
      // make parition key an array
      if (outputMView.key && typeof outputMView.key[0] === 'string') {
        outputMView.key[0] = [outputMView.key[0]];
      }
 
      // add clustering_order for all clustering keys
      Eif (outputMView.key && outputMView.key.length) {
        for (let i = 1; i < outputMView.key.length; i++) {
          if (!outputMView.clustering_order) {
            outputMView.clustering_order = {};
          }
          if (!outputMView.clustering_order[outputMView.key[i]]) {
            outputMView.clustering_order[outputMView.key[i]] = 'ASC';
          }
          // eslint-disable-next-line max-len
          outputMView.clustering_order[outputMView.key[i]] = outputMView.clustering_order[outputMView.key[i]].toUpperCase();
        }
      }
 
      // add all non existent primary key items to select and sort them
      for (let pkeyIndex = 0; pkeyIndex < outputMView.key.length; pkeyIndex++) {
        if (pkeyIndex === 0) {
          for (let partitionIndex = 0; partitionIndex < outputMView.key[pkeyIndex].length; partitionIndex++) {
            if (!outputMView.select.includes(outputMView.key[pkeyIndex][partitionIndex])) {
              outputMView.select.push(outputMView.key[pkeyIndex][partitionIndex]);
            }
          }
        } else if (!outputMView.select.includes(outputMView.key[pkeyIndex])) {
          outputMView.select.push(outputMView.key[pkeyIndex]);
        }
      }
 
      // check if select has * and then add all fields to select
      if (outputMView.select[0] === '*') {
        outputMView.select = Object.keys(outputSchema.fields);
      }
 
      outputMView.select.sort(arraySort);
 
      if (!outputMView.where_clause) {
        outputMView.where_clause = parser.get_mview_where_clause(outputSchema, outputMView).trim();
      }
      if (_.isPlainObject(outputMView.filters)) {
        delete outputMView.filters;
      }
    });
  },
 
  normalize_indexes(outputSchema) {
    if (!outputSchema.indexes) {
      outputSchema.indexes = [];
    }
    for (let i = 0; i < outputSchema.indexes.length; i++) {
      const indexNameList = outputSchema.indexes[i].replace(/["\s]/g, '').split(/[()]/g);
      if (indexNameList.length > 1) {
        indexNameList[0] = indexNameList[0].toLowerCase();
        if (indexNameList[0] === 'values') outputSchema.indexes[i] = indexNameList[1];
        else outputSchema.indexes[i] = util.format('%s(%s)', indexNameList[0], indexNameList[1]);
      } else {
        outputSchema.indexes[i] = indexNameList[0];
      }
    }
    outputSchema.indexes.sort(arraySort);
  },
 
  normalize_custom_indexes(outputSchema) {
    Iif (outputSchema.custom_index) {
      outputSchema.custom_indexes = [outputSchema.custom_index];
      delete outputSchema.custom_index;
    }
 
    if (outputSchema.custom_indexes) {
      const customArraySort = (a, b) => {
        Eif (a.on > b.on) return 1;
        if (a.on < b.on) return -1;
 
        if (a.using > b.using) return 1;
        if (a.using < b.using) return -1;
 
        if (a.options > b.options) return 1;
        if (a.options < b.options) return -1;
 
        return 0;
      };
 
      outputSchema.custom_indexes.sort(customArraySort);
    } else {
      outputSchema.custom_indexes = [];
    }
 
    outputSchema.custom_indexes = _.remove(outputSchema.custom_indexes, (cindex) => (cindex.on !== 'solr_query'));
  },
 
  normalize_model_schema(modelSchema) {
    const outputSchema = _.cloneDeep(modelSchema, true);
    const normalizableSchemaProperties = [
      'fields', 'key', 'clustering_order', 'materialized_views', 'indexes', 'custom_index', 'custom_indexes',
    ];
 
    Object.keys(outputSchema).forEach((schemaProperty) => {
      if (!normalizableSchemaProperties.includes(schemaProperty)) {
        delete outputSchema[schemaProperty];
      }
    });
 
    this.normalize_fields(modelSchema, outputSchema);
    this.normalize_primary_key(outputSchema);
    this.normalize_materialized_views(outputSchema);
    this.normalize_indexes(outputSchema);
    this.normalize_custom_indexes(outputSchema);
 
    return outputSchema;
  },
 
  remove_dependent_views_from_normalized_schema(normalizedDBSchema, dbSchema, fieldName) {
    const dependentViews = [];
    Object.keys(normalizedDBSchema.materialized_views).forEach((dbViewName) => {
      if (normalizedDBSchema.materialized_views[dbViewName].select.includes(fieldName)) {
        dependentViews.push(dbViewName);
      } else if (normalizedDBSchema.materialized_views[dbViewName].select[0] === '*') {
        dependentViews.push(dbViewName);
      } else if (normalizedDBSchema.materialized_views[dbViewName].key.includes(fieldName)) {
        dependentViews.push(dbViewName);
      } else if (_.isArray(normalizedDBSchema.materialized_views[dbViewName].key[0])
                  && normalizedDBSchema.materialized_views[dbViewName].key[0].includes(fieldName)) {
        dependentViews.push(dbViewName);
      }
    });
    dependentViews.forEach((viewName) => {
      normalizedDBSchema.materialized_views[viewName] = {};
    });
  },
};
 
module.exports = normalizer;