All files / src/builders janusgraph.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 2211x 1x   1x 2x 2x     1x                                                                     2x 2x     2x 2x 2x         2x 2x 2x           2x 2x 2x         2x         2x                                             1x 1x         1x       1x 6x 4x 4x 5x   4x 4x 1x   4x 2x 1x 1x 3x   1x 1x     1x 1x 1x 1x 1x 1x   1x     1x   1x 6x 4x 2x 1x 1x 1x       1x 1x 6x 4x 2x 1x 1x 1x     1x   1x 6x 4x 2x 1x 1x 1x     1x 1x         1x       1x 1x 1x           1x       1x 2x       1x 4x       1x 1x 1x         1x 1x 1x               1x  
const _ = require('lodash');
const debug = require('debug')('express-cassandra');
 
const JanusGraphBuilder = function f(client, config) {
  this._client = client;
  this._config = config;
};
 
JanusGraphBuilder.prototype = {
  create_graph(graphName, callback) {
    debug('creating janus graph: %s', graphName);
    const script = `
      Map<String, Object> map = new HashMap<String, Object>();
      map.put("storage.backend", storageBackend);
      map.put("storage.hostname", storageHostname);
      map.put("storage.port", storagePort);
      map.put("index.search.backend", indexBackend);
      map.put("index.search.hostname", indexHostname);
      map.put("index.search.port", indexPort);
      map.put("graph.graphname", graphName);
      ConfiguredGraphFactory.createConfiguration(new MapConfiguration(map));
      ConfiguredGraphFactory.open(graphName).vertices().size();
    `;
    const bindings = {
      storageBackend: this._config.storage.backend,
      storageHostname: this._config.storage.hostname,
      storagePort: this._config.storage.port,
      indexBackend: this._config.index.search.backend,
      indexHostname: this._config.index.search.hostname,
      indexPort: this._config.index.search.port,
      graphName,
    };
    this._client.execute(script, bindings, (err, results) => {
      if (err) {
        callback(err);
        return;
      }
 
      callback(null, results);
    });
  },
 
  check_graph_exist(graphName, callback) {
    debug('check for janus graph: %s', graphName);
    const script = `
      ConfiguredGraphFactory.getGraphNames();
    `;
    const bindings = {};
    this._client.execute(script, bindings, (err, results) => {
      Iif (err) {
        callback(err);
        return;
      }
 
      Eif (_.isArray(results) && results.includes(graphName)) {
        callback(null, true);
        return;
      }
      callback(null, false);
    });
  },
 
  assert_graph(graphName, callback) {
    this.check_graph_exist(graphName, (err, exist) => {
      Iif (err) {
        callback(err);
        return;
      }
 
      Iif (!exist) {
        this.create_graph(graphName, callback);
        return;
      }
 
      callback();
    });
  },
 
  drop_graph(graphName, callback) {
    debug('removing janus graph: %s', graphName);
    const script = `
      ConfiguredGraphFactory.drop(graphName);
    `;
    const bindings = {
      graphName,
    };
    this._client.execute(script, bindings, (err, results) => {
      if (err) {
        callback(err);
        return;
      }
 
      callback(null, results);
    });
  },
 
  put_indexes(graphName, mappingName, indexes, callback) {
    debug('syncing janus graph indexes for: %s', mappingName);
    let script = `
      graph = ConfiguredGraphFactory.open(graphName);
      graph.tx().commit();
      mgmt = graph.openManagement();
    `;
    const bindings = {
      graphName,
    };
    // create indexes if not exist
    Object.keys(indexes).forEach((index) => {
      if (indexes[index].type === 'Composite') {
        script += `if (!mgmt.containsGraphIndex('${index}')) mgmt.buildIndex('${index}', Vertex.class)`;
        indexes[index].keys.forEach((key) => {
          script += `.addKey(mgmt.getPropertyKey('${key}'))`;
        });
        script += `.indexOnly(mgmt.getVertexLabel('${mappingName}'))`;
        if (indexes[index].unique) {
          script += '.unique()';
        }
        script += '.buildCompositeIndex();';
      } else if (indexes[index].type === 'Mixed') {
        script += `if (!mgmt.containsGraphIndex('${index}')) mgmt.buildIndex('${index}', Vertex.class)`;
        indexes[index].keys.forEach((key) => {
          script += `.addKey(mgmt.getPropertyKey('${key}'))`;
        });
        script += `.indexOnly(mgmt.getVertexLabel('${mappingName}'))`;
        Iif (indexes[index].unique) {
          script += '.unique()';
        }
        script += '.buildMixedIndex("search");';
      } else Eif (indexes[index].type === 'VertexCentric') {
        script += `relationLabel = mgmt.getEdgeLabel('${indexes[index].label}');`;
        script += `if (!mgmt.containsRelationIndex(relationLabel, '${index}')) mgmt.buildEdgeIndex(relationLabel, '${index}', Direction.${indexes[index].direction}, Order.${indexes[index].order}`;
        indexes[index].keys.forEach((key) => {
          script += `, mgmt.getPropertyKey('${key}')`;
        });
        script += ');';
      }
    });
    script += 'mgmt.commit();';
    // await index for registered or enabled status
    Object.keys(indexes).forEach((index) => {
      if (indexes[index].type === 'Composite') {
        script += `mgmt.awaitGraphIndexStatus(graph, '${index}').status(SchemaStatus.REGISTERED, SchemaStatus.ENABLED).call();`;
      } else if (indexes[index].type === 'Mixed') {
        script += `mgmt.awaitGraphIndexStatus(graph, '${index}').status(SchemaStatus.REGISTERED, SchemaStatus.ENABLED).call();`;
      } else Eif (indexes[index].type === 'VertexCentric') {
        script += `mgmt.awaitRelationIndexStatus(graph, '${index}', '${indexes[index].label}').status(SchemaStatus.REGISTERED, SchemaStatus.ENABLED).call();`;
      }
    });
    // enable index if in registered state
    script += 'mgmt = graph.openManagement();';
    Object.keys(indexes).forEach((index) => {
      if (indexes[index].type === 'Composite') {
        script += `if (mgmt.getGraphIndex('${index}').getIndexStatus(mgmt.getPropertyKey('${indexes[index].keys[0]}')).equals(SchemaStatus.REGISTERED)) mgmt.updateIndex(mgmt.getGraphIndex('${index}'), SchemaAction.ENABLE_INDEX);`;
      } else if (indexes[index].type === 'Mixed') {
        script += `if (mgmt.getGraphIndex('${index}').getIndexStatus(mgmt.getPropertyKey('${indexes[index].keys[0]}')).equals(SchemaStatus.REGISTERED)) mgmt.updateIndex(mgmt.getGraphIndex('${index}'), SchemaAction.ENABLE_INDEX);`;
      } else Eif (indexes[index].type === 'VertexCentric') {
        script += `if (mgmt.getRelationIndex(mgmt.getEdgeLabel('${indexes[index].label}'), '${index}').getIndexStatus().equals(SchemaStatus.REGISTERED)) mgmt.updateIndex(mgmt.getRelationIndex(mgmt.getEdgeLabel('${indexes[index].label}'), '${index}'), SchemaAction.ENABLE_INDEX);`;
      }
    });
    script += 'mgmt.commit();';
    // await index for enabled status
    Object.keys(indexes).forEach((index) => {
      if (indexes[index].type === 'Composite') {
        script += `mgmt.awaitGraphIndexStatus(graph, '${index}').status(SchemaStatus.ENABLED).call();`;
      } else if (indexes[index].type === 'Mixed') {
        script += `mgmt.awaitGraphIndexStatus(graph, '${index}').status(SchemaStatus.ENABLED).call();`;
      } else Eif (indexes[index].type === 'VertexCentric') {
        script += `mgmt.awaitRelationIndexStatus(graph, '${index}', '${indexes[index].label}').status(SchemaStatus.ENABLED).call();`;
      }
    });
    this._client.execute(script, bindings, (err, results) => {
      Iif (err) {
        callback(err);
        return;
      }
 
      callback(null, results);
    });
  },
 
  put_mapping(graphName, mappingName, mappingBody, callback) {
    debug('syncing janus graph mapping: %s', mappingName);
    let script = `
      graph = ConfiguredGraphFactory.open(graphName);
      graph.tx().commit();
      mgmt = graph.openManagement();
      if (!mgmt.containsVertexLabel(mappingName)) mgmt.makeVertexLabel(mappingName).make();
    `;
    const bindings = {
      graphName,
      mappingName,
    };
    Object.keys(mappingBody.relations).forEach((relation) => {
      script += `
        if (!mgmt.containsEdgeLabel('${relation}')) mgmt.makeEdgeLabel('${relation}').multiplicity(${mappingBody.relations[relation]}).make();
      `;
    });
    Object.keys(mappingBody.properties).forEach((property) => {
      script += `
        if (!mgmt.containsPropertyKey('${property}')) mgmt.makePropertyKey('${property}').dataType(${mappingBody.properties[property].type}.class).cardinality(Cardinality.${mappingBody.properties[property].cardinality}).make();
      `;
    });
    script += 'mgmt.commit();';
    this._client.execute(script, bindings, (err, results) => {
      Iif (err) {
        callback(err);
        return;
      }
 
      Eif (Object.keys(mappingBody.indexes).length > 0) {
        this.put_indexes(graphName, mappingName, mappingBody.indexes, callback);
        return;
      }
 
      callback(null, results);
    });
  },
};
 
module.exports = JanusGraphBuilder;