All files / src/index Index.js

97.5% Statements 39/40
95.45% Branches 21/22
94.44% Functions 17/18
97.3% Lines 36/37
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    10x                           132x         468x             132x         132x             132x           132x           132x           132x             132x                 29x                                       104x 104x 104x                 21x                     106x               104x   92x                 970x 125x 970x               104x 104x 104x 771x                                                               82x         140x 140x   98x 98x   140x                   1x                       1x                       1x                       1x         10x  
"use strict";
 
const Results = require('../Results');
 
/**
 * An index
 */
class Index
{
  /**
   * Construct a new Index
   *
   * @param {object} [config={}] configuration; see properties
   */
  constructor(config = {}, type = undefined)
  {
    for (let property in config)
    {
      /**
       * @private
       */
      this[property] = config[property];
    }
    /**
     * Type of index.
     * These are usually predefined values. Filled in by class.
     * @type {string}
     */
    this.type = this.type || type || 'none';
    /**
     * List of fields it should index.
     * @type {string[]}
     */
    this.fields = this.fields || [];
    /**
     * List of field values by index.
     * These values are used to add/remove documents from index and also used for sorting.
     * @protected
     * @type {string}
     */
    this.values = this.values || [];
    /**
     * List of filters supported.
     * These are usually predefined values. Filled in by class.
     * @type {string}
     */
    this.filters = this.filters || [];
    /**
     * Actual Index.
     * Format depends on type of index. Override createIndex();
     * @type {object}
     */
    this.index = this.index || false;
 
    /**
     * Field values this index can supply for sorting.
     * @type {string}
     */
    this.sorts = this.sorts || [];
 
    /**
     * Information gain.
     * TODO: should determine which field to query first etc.
     * @type {number}
     */
    this.entropy = this.entropy || 0;
  }
 
  /**
   * Dump current index state.
   * @return {object}
   */
  async state()
  {
    return {
      name: this.name,
      type: this.type,
      fields: this.fields,
      values: this.values,
      index: this.index,
      filters: this.filters,
      sorts: this.sorts,
      entropy: this.entropy
    };
  }
 
  /**
   * Adds a set of documents to Index
   *
   * @param {number[]} documentIndices Index of document ids in the list of ids. Indices must store document data by this index.
   * @param {Document[]} documents Actual documents.
   */
  async addDocuments(documentIndices, documents)
  {
    await this.makeSureIndexExists();
    await this.removeExistingValues(documentIndices);
    await this.addDocumentsToIndex(documentIndices, documents);
  }
 
  /**
   * Removes a set of documents from Index
   * @param {number[]} documentIndices Index of document ids in the list of ids. Indices must store document data by this index.
   */
  async removeDocuments(documentIndices)
  {
    await this.removeExistingValues(documentIndices);
  }
 
  /**
   * Filter results based on query
   *
   * @param {QueryFilter} queryFilter query to filter with
   * @param {Results} results results to filter (modify results object)
   */
  filterDocuments(queryFilter, results = new Results(), score)
  {
    return this.filterBasedOnIndex(this.index, queryFilter, results, score);
  }
 
  /**
   * @private
   */
  async makeSureIndexExists()
  {
    if (!this.index)
    {
      this.index = await this.createIndex();
    }
  }
 
  /**
   * @private
   */
  async removeExistingValues(documentIndices)
  {
    const values = documentIndices.map(index => this.values[index]);
    await this.removeFromIndex(this.index, documentIndices, values);
    documentIndices.forEach(index => this.values[index] = null);
  }
 
  /**
   * @private
   */
  async addDocumentsToIndex(documentIndices, documents)
  {
    let documentsValues = documents.map(this.getDocumentValues.bind(this));
    let analysed = await this.analyseValues(documentsValues);
    await this.addToIndex(this.index, documentIndices, analysed);
    documentIndices.forEach((index, i) => this.values[index] = analysed[i]);
  }
 
  /**
   * Extract values out of a document object.
   *
   * It is set up like this so that we can override this method and filter
   * the values we don't like.
   *
   * @example
   * {field: ['a', 'b']} should return ['a', 'b']
   * @protected
   * @param {Document} document
   * @return {object[]}
   */
  getDocumentValues(document)
  {
    throw new Error('Abstract')
  }
 
  /**
   * Processing before adding to index.
   *
   * By default, it turns list of objects into a map of string representation
   * of the objects to their occurance tally.
   *
   * @protected
   * @param {object[][]} values list of values for each documents
   * @return {object[][]}
   */
  analyseValues(values)
  {
    return values.map(this.analyseValue.bind(this));
  }
 
  analyseValue(row)
  {
    let total = {};
    for (let value of row)
    {
      total[value] = total[value] || 0;
      total[value]++;
    }
    return total;
  }
 
  /**
   * Create an empty index.
   * @protected
   * @return {IndexImplementation} default is {}
   */
  createIndex()
  {
    throw new Error('TODO: createIndex is not implemented');
  }
 
  /**
   * Add to index.
   * @abstract
   * @param {IndexImplementation} index
   * @param {number[]} documentIndices
   * @param {string[][]} documentValues
   */
  addToIndex(index, documentIndices, documentValues)
  {
    throw new Error('TODO: addToIndex is not implemented');
  }
 
  /**
   * Remove from index
   * @abstract
   * @param {IndexImplementation} index
   * @param {number[]} documentIndices
   * @param {string[][]} documentValues
   */
  removeFromIndex(index, documentIndices, documentValues)
  {
    throw new Error('TODO: removeFromIndex is not implemented');
  }
 
  /**
   * Filter results based on index
   * @abstract
   * @param {IndexImplementation} index
   * @param {QueryFilter} queryFilter
   * @param {Results} results
   */
  filterBasedOnIndex(index, queryFilter, results)
  {
    throw new Error('TODO: filterBasedOnIndex is not implemented');
  }
 
}
 
module.exports = Index;