indexstorage.js 24.8 KB
Newer Older
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
 * JIO extension for resource indexing.
 * Copyright (C) 2013  Nexedi SA
 *
 *   This library is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 18 19
 */

/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true, regexp: true */
20
/*global window, exports, require, define, jIO, RSVP, complex_queries */
21

22 23 24 25 26
/**
 * JIO Index Storage.
 * Manages indexes for specified storages.
 * Description:
 * {
27 28 29
 *   "type": "index",
 *   "indices": [{
 *     "id": "index_title_subject.json", // doc id where to store indices
30 31 32 33 34 35 36 37 38
 *     "index": ["title", "subject"], // metadata to index
 *     "attachment": "youhou", // default "body"
 *     "metadata": { // default {}
 *       "type": "Dataset",
 *       "format": "application/json",
 *       "date": "yyyy-mm-ddTHH:MM:SS+HH:MM",
 *       "title": "My index database",
 *       "creator": "Me"
 *     },
39 40 41 42 43 44 45 46 47 48 49 50 51 52
 *     "sub_storage": <sub storage where to store index>
 *                    (default equal to parent sub_storage field)
 *   }, {
 *     "id": "index_year.json",
 *     "index": "year"
 *     ...
 *   }],
 *   "sub_storage": <sub storage description>
 * }
 *
 * Sent document metadata will be:
 * index_titre_subject.json
 * {
 *   "_id": "index_title_subject.json",
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
 *   "type": "Dataset",
 *   "format": "application/json",
 *   "date": "yyyy-mm-ddTHH:MM:SS+HH:MM",
 *   "title": "My index database",
 *   "creator": "Me",
 *   "_attachments": {
 *     "youhou": {
 *       "length": Num,
 *       "digest": "XXX",
 *       "content_type": "application/json"
 *     }
 *   }
 * }
 * Attachment "youhou"
 * {
68 69 70 71 72 73 74 75 76 77 78 79 80
 *   "indexing": ["title", "subject"],
 *   "free": [0],
 *   "location": {
 *     "foo": 1,
 *     "bar": 2,
 *     ...
 *   },
 *   "database": [
 *     {},
 *     {"_id": "foo", "title": "...", "subject": ...},
 *     {"_id": "bar", "title": "...", "subject": ...},
 *     ...
 *   ]
81
 * }
82 83
 *
 * index_year.json
84
 * {
85
 *   "_id": "index_year.json",
86 87 88 89 90 91
 *   "_attachments": {
 *     "body": {..}
 *   }
 * }
 * Attachment "body"
 * {
92 93 94 95 96 97
 *   "indexing": ["year"],
 *   "free": [1],
 *   "location": {
 *     "foo": 0,
 *     "bar": 2,
 *     ...
98
 *   },
99 100 101 102 103 104
 *   "database": [
 *     {"_id": "foo", "year": "..."},
 *     {},
 *     {"_id": "bar", "year": "..."},
 *     ...
 *   ]
105
 * }
106 107 108 109 110 111 112 113
 *
 * A put document will be indexed to the free location if exist, else it will be
 * indexed at the end of the database. The document id will be indexed, also, in
 * 'location' to quickly replace metadata.
 *
 * Only one or two loops are executed:
 * - one to filter retrieved document list (no query -> no loop)
 * - one to format the result to a JIO response
114
 */
115 116 117 118 119 120
// define([module_name], [dependencies], module);
(function (dependencies, module) {
  "use strict";
  if (typeof define === 'function' && define.amd) {
    return define(dependencies, module);
  }
121 122 123 124 125 126 127
  if (typeof exports === 'object') {
    return module(
      exports,
      require('jio'),
      require('rsvp'),
      require('complex_queries')
    );
128
  }
129 130 131 132 133 134 135 136 137
  window.index_storage = {};
  module(window.index_storage, jIO, RSVP, complex_queries);
}([
  'exports',
  'jio',
  'rsvp',
  'complex_queries'
], function (exports, jIO, RSVP, complex_queries) {
  "use strict";
138

139
  /**
140 141 142 143
   * A JSON Index manipulator
   *
   * @class JSONIndex
   * @constructor
144
   */
145 146 147 148 149 150 151 152 153 154 155 156
  function JSONIndex(spec) {
    var that = this;
    spec = spec || {};

    /**
     * The document id
     *
     * @property _id
     * @type String
     */
    that._id = spec._id;

157 158 159 160 161 162 163 164
    /**
     * The attachment id
     *
     * @property _attachment
     * @type String
     */
    that._attachment = spec._attachment;

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
    /**
     * The array with metadata key to index
     *
     * @property _indexing
     * @type Array
     */
    that._indexing = spec.indexing || [];

    /**
     * The array of free location index
     *
     * @property _free
     * @type Array
     * @default []
     */
    that._free = spec.free || [];

    /**
     * The dictionnary document id -> database index
     *
     * @property _location
     * @type Object
     * @default {}
     */
    that._location = spec.location || {};

    /**
     * The database array containing document metadata
     *
     * @property _database
     * @type Array
     * @default []
     */
    that._database = spec.database || [];

    /**
     * Adds a metadata object in the database, replace if already exist
     *
     * @method put
     * @param  {Object} meta The metadata to add
     * @return {Boolean} true if added, false otherwise
     */
    that.put = function (meta) {
208
      var k, needed_meta = {}, ok = false;
209
      if (typeof meta._id !== "string" || meta._id === "") {
210
        throw new TypeError("Corrupted Metadata");
211
      }
212 213
      for (k in meta) {
        if (meta.hasOwnProperty(k)) {
214
          if (k[0] === "_") {
215 216 217
            if (k === "_id") {
              needed_meta[k] = meta[k];
            }
218 219 220
          } else if (that._indexing_object[k]) {
            needed_meta[k] = meta[k];
            ok = true;
221 222 223
          }
        }
      }
224 225 226 227 228 229 230 231 232 233
      if (ok) {
        if (typeof that._location[meta._id] === "number") {
          that._database[that._location[meta._id]] = needed_meta;
        } else if (that._free.length > 0) {
          k = that._free.shift();
          that._database[k] = needed_meta;
          that._location[meta._id] = k;
        } else {
          that._database.push(needed_meta);
          that._location[meta._id] = that._database.length - 1;
Sven Franck's avatar
Sven Franck committed
234
        }
235
        return true;
Sven Franck's avatar
Sven Franck committed
236
      }
237 238 239 240 241
      if (typeof that._location[meta._id] === "number") {
        that.remove(meta);
      }
      return false;
    };
Sven Franck's avatar
Sven Franck committed
242

243 244 245 246 247 248 249 250 251
    /**
     * Removes a metadata object from the database if exist
     *
     * @method remove
     * @param  {Object} meta The metadata to remove
     */
    that.remove = function (meta) {
      if (typeof meta._id !== "string") {
        throw new TypeError("Corrupted Metadata");
252
      }
253
      if (typeof that._location[meta._id] !== "number") {
254 255
        // throw new ReferenceError("Not Found");
        return;
256
      }
257 258 259 260
      that._database[that._location[meta._id]] = null;
      that._free.push(that._location[meta._id]);
      delete that._location[meta._id];
    };
261

262
    /**
263
     * Checks if the index database document is correct
264 265 266 267 268 269 270
     *
     * @method check
     */
    that.check = function () {
      var id, database_meta;
      if (typeof that._id !== "string" ||
          that._id === "" ||
271 272 273 274 275 276 277
          typeof that._attachment !== "string" ||
          that._attachment === "" ||
          !Array.isArray(that._free) ||
          !Array.isArray(that._indexing) ||
          typeof that._location !== 'object' ||
          Array.isArray(that._location) ||
          !Array.isArray(that._database) ||
278 279 280 281 282 283
          that._indexing.length === 0) {
        throw new TypeError("Corrupted Index");
      }
      for (id in that._location) {
        if (that._location.hasOwnProperty(id)) {
          database_meta = that._database[that._location[id]];
284 285
          if (typeof database_meta !== 'object' ||
              Object.getPrototypeOf(database_meta || []) !== Object.prototype ||
286 287
              database_meta._id !== id) {
            throw new TypeError("Corrupted Index");
288 289 290
          }
        }
      }
291
    };
292

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    that.equals = function (json_index) {
      function equalsDirection(a, b) {
        var k;
        for (k in a._location) {
          if (a._location.hasOwnProperty(k)) {
            if (b._location[k] === undefined ||
                JSON.stringify(b._database[b._location[k]]) !==
                JSON.stringify(a._database[a._location[k]])) {
              return false;
            }
          }
        }
        return true;
      }
      if (!equalsDirection(that, json_index)) {
        return false;
      }
      if (!equalsDirection(json_index, that)) {
        return false;
      }
      return true;
    };

    that.checkDocument = function (doc) {
      var i, key, db_doc;
318 319 320 321 322
      if (typeof that._location[doc._id] !== "number") {
        throw new TypeError("Different Index");
      }
      db_doc = that._database(that._location[doc._id])._id;
      if (db_doc !== doc._id) {
323 324 325 326 327 328 329 330 331 332
        throw new TypeError("Different Index");
      }
      for (i = 0; i < that._indexing.length; i += 1) {
        key = that._indexing[i];
        if (doc[key] !== db_doc[key]) {
          throw new TypeError("Different Index");
        }
      }
    };

333 334 335 336 337 338 339 340 341
    /**
     * Recreates database indices and remove free space
     *
     * @method repair
     */
    that.repair = function () {
      var i = 0, meta;
      that._free = [];
      that._location = {};
342
      if (!Array.isArray(that._database)) {
343
        that._database = [];
344
      }
345 346
      while (i < that._database.length) {
        meta = that._database[i];
347 348
        if (typeof meta === 'object' &&
            Object.getPrototypeOf(meta || []) === Object.prototype &&
349 350 351 352
            typeof meta._id === "string" && meta._id !== "" &&
            !that._location[meta._id]) {
          that._location[meta._id] = i;
          i += 1;
353
        } else {
354
          that._database.splice(i, 1);
355 356
        }
      }
357 358 359 360 361
    };

    /**
     * Returns the serialized version of this object (not cloned)
     *
362
     * @method toJSON
363 364
     * @return {Object} The serialized version
     */
365
    that.toJSON = function () {
366 367 368 369 370 371 372 373 374 375 376 377 378 379
      return {
        "indexing": that._indexing,
        "free": that._free,
        "location": that._location,
        "database": that._database
      };
    };

    that.check();
    that._indexing_object = {};
    that._indexing.forEach(function (meta_key) {
      that._indexing_object[meta_key] = true;
    });
  }
380 381

  /**
382 383 384 385 386
   * Return the similarity percentage (1 >= p >= 0) between two index lists.
   *
   * @param  {Array} list_a An index list
   * @param  {Array} list_b Another index list
   * @return {Number} The similarity percentage
387
   */
388 389 390 391 392 393 394 395 396 397 398 399 400
  function similarityPercentage(list_a, list_b) {
    var ai, bi, count = 0;
    for (ai = 0; ai < list_a.length; ai += 1) {
      for (bi = 0; bi < list_b.length; bi += 1) {
        if (list_a[ai] === list_b[bi]) {
          count += 1;
          break;
        }
      }
    }
    return count / (list_a.length > list_b.length ?
                    list_a.length : list_b.length);
  }
401

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
  /**
   * The JIO index storage constructor
   *
   * @class IndexStorage
   * @constructor
   */
  function IndexStorage(spec) {
    var i;
    if (!Array.isArray(spec.indices)) {
      throw new TypeError("IndexStorage 'indices' must be an array of " +
                          "objects.");
    }
    this._indices = spec.indices;
    if (typeof spec.sub_storage !== 'object' ||
        Object.getPrototypeOf(spec.sub_storage || []) !== Object.prototype) {
      throw new TypeError("IndexStorage 'sub_storage' must be a storage " +
                          "description.");
    }
    // check indices IDs
    for (i = 0; i < this._indices.length; i += 1) {
      if (typeof this._indices[i].id !== "string" ||
          this._indices[i].id === "") {
        throw new TypeError("IndexStorage " +
                            "'indices[x].id' must be a non empty string");
      }
      if (!Array.isArray(this._indices[i].index)) {
        throw new TypeError("IndexStorage " +
                            "'indices[x].index' must be a string array");
      }
    }
    this._sub_storage = spec.sub_storage;
  }
434

435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
  /**
   * Select the good index to use according to a select list.
   *
   * @method selectIndex
   * @param  {Array} select_list An array of strings
   * @return {Number} The index index
   */
  IndexStorage.prototype.selectIndex = function (select_list) {
    var i, tmp, selector = {"index": 0, "similarity": 0};
    for (i = 0; i < this._indices.length; i += 1) {
      tmp = similarityPercentage(select_list, this._indices[i].index);
      if (tmp > selector.similarity) {
        selector.index = i;
        selector.similarity = tmp;
      }
    }
    return selector.index;
  };
453

454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
  IndexStorage.prototype.getIndexDatabase = function (command, index) {
    index = this._indices[index];
    function makeNewIndex() {
      return new JSONIndex({
        "_id": index.id,
        "_attachment": index.attachment || "body",
        "indexing": index.index
      });
    }
    return command.storage(
      index.sub_storage || this._sub_storage
    ).getAttachment({
      "_id": index.id,
      "_attachment": index.attachment || "body"
    }).then(function (response) {
      return jIO.util.readBlobAsText(response.data);
    }).then(function (e) {
      try {
        e = JSON.parse(e.target.result);
        e._id = index.id;
        e._attachment = index.attachment || "body";
      } catch (e1) {
        return makeNewIndex();
      }
      return new JSONIndex(e);
    }, function (err) {
      if (err.status === 404) {
        return makeNewIndex();
        // go back to fulfillment channel
      }
      throw err;
      // propagate err
    });
  };
488

489 490 491 492 493 494 495
  IndexStorage.prototype.getIndexDatabases = function (command) {
    var i, promises = [];
    for (i = 0; i < this._indices.length; i += 1) {
      promises[promises.length] = this.getIndexDatabase(command, i);
    }
    return RSVP.all(promises);
  };
496

497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
  IndexStorage.prototype.storeIndexDatabase = function (command, database,
                                                        index) {
    var that = this;
    index = this._indices[index];
    function putAttachment() {
      return command.storage(
        index.sub_storage || that._sub_storage
      ).putAttachment({
        "_id": index.id,
        "_attachment": index.attachment || "body",
        "_data": JSON.stringify(database),
        "_content_type": "application/json"
      });
    }
    function createDatabaseAndPutAttachmentIfPossible(err) {
      if (err.status === 404) {
        return command.storage(
          index.sub_storage || that._sub_storage
        ).post({
          "_id": index.id
          // XXX add metadata to document if necessary
        }).then(putAttachment, null, function () {
          throw null; // stop post progress propagation
        });
521
      }
522 523 524 525 526
      throw err;
    }
    return putAttachment().
      then(null, createDatabaseAndPutAttachmentIfPossible);
  };
527

528 529 530 531 532 533
  IndexStorage.prototype.storeIndexDatabases = function (command, databases) {
    var i, promises = [];
    for (i = 0; i < this._indices.length; i += 1) {
      if (databases[i] !== undefined) {
        promises[promises.length] =
          this.storeIndexDatabase(command, databases[i], i);
534
      }
535 536 537
    }
    return RSVP.all(promises);
  };
538

539

540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
  /**
   * Generic method for 'post', 'put', 'get' and 'remove'. It delegates the
   * command to the sub storage and update the databases.
   *
   * @method genericCommand
   * @param  {String} method The method to use
   * @param  {Object} command The JIO command
   * @param  {Object} metadata The metadata to post
   * @param  {Object} option The command option
   */
  IndexStorage.prototype.genericCommand = function (method, command,
                                                    metadata, option) {
    var that = this, generic_response;
    function updateAndStoreIndexDatabases(responses) {
      var i, database_list = responses[0];
      generic_response = responses[1];
      if (method === 'get') {
        jIO.util.dictUpdate(metadata, generic_response.data);
Sven Franck's avatar
Sven Franck committed
558
      }
559 560 561 562
      metadata._id = generic_response.id;
      if (method === 'remove') {
        for (i = 0; i < database_list.length; i += 1) {
          database_list[i].remove(metadata);
563
        }
564 565 566
      } else {
        for (i = 0; i < database_list.length; i += 1) {
          database_list[i].put(metadata);
567
        }
Sven Franck's avatar
Sven Franck committed
568
      }
569 570
      return that.storeIndexDatabases(command, database_list);
    }
571

572 573 574 575 576 577 578
    function allProgress(progress) {
      if (progress.index === 1) {
        progress.value.percentage *= 0.7; // 0 to 70%
        command.notify(progress.value);
      }
      throw null; // stop propagation
    }
Sven Franck's avatar
Sven Franck committed
579

580 581 582
    function success() {
      command.success(generic_response);
    }
583

584 585 586 587
    function storeProgress(progress) {
      progress.percentage = (0.3 * progress.percentage) + 70; // 70 to 100%
      command.notify(progress);
    }
588

589 590 591 592 593 594
    RSVP.all([
      this.getIndexDatabases(command),
      command.storage(this._sub_storage)[method](metadata, option)
    ]).then(updateAndStoreIndexDatabases, null, allProgress).
      then(success, command.error, storeProgress);
  };
595

596 597 598 599 600 601 602 603 604 605 606
  /**
   * Post the document metadata and update the index
   *
   * @method post
   * @param  {Object} command The JIO command
   * @param  {Object} metadata The metadata to post
   * @param  {Object} option The command option
   */
  IndexStorage.prototype.post = function (command, metadata, option) {
    this.genericCommand('post', command, metadata, option);
  };
607

608 609 610 611 612 613 614 615 616 617 618
  /**
   * Update the document metadata and update the index
   *
   * @method put
   * @param  {Object} command The JIO command
   * @param  {Object} metadata The metadata to put
   * @param  {Object} option The command option
   */
  IndexStorage.prototype.put = function (command, metadata, option) {
    this.genericCommand('put', command, metadata, option);
  };
619

620 621 622 623 624 625 626 627 628 629 630 631
  /**
   * Add an attachment to a document (no index modification)
   *
   * @method putAttachment
   * @param  {Object} command The JIO command
   * @param  {Object} param The command parameters
   * @param  {Object} option The command option
   */
  IndexStorage.prototype.putAttachment = function (command, param, option) {
    command.storage(this._sub_storage).putAttachment(param, option).
      then(command.success, command.error, command.notify);
  };
632

633 634 635 636 637 638 639 640 641 642 643
  /**
   * Get the document metadata
   *
   * @method get
   * @param  {Object} command The JIO command
   * @param  {Object} param The command parameters
   * @param  {Object} option The command option
   */
  IndexStorage.prototype.get = function (command, param, option) {
    this.genericCommand('get', command, param, option);
  };
644

645 646 647 648 649 650 651 652 653 654 655 656
  /**
   * Get the attachment.
   *
   * @method getAttachment
   * @param  {Object} command The JIO command
   * @param  {Object} param The command parameters
   * @param  {Object} option The command option
   */
  IndexStorage.prototype.getAttachment = function (command, param, option) {
    command.storage(this._sub_storage).getAttachment(param, option).
      then(command.success, command.error, command.notify);
  };
657

658 659 660 661 662 663 664 665 666 667 668
  /**
   * Remove document - removing documents updates index!.
   *
   * @method remove
   * @param  {Object} command The JIO command
   * @param  {Object} param The command parameters
   * @param  {Object} option The command option
   */
  IndexStorage.prototype.remove = function (command, param, option) {
    this.genericCommand('remove', command, param, option);
  };
669

670 671 672 673 674 675 676 677 678 679 680 681
  /**
   * Remove attachment
   *
   * @method removeAttachment
   * @param  {Object} command The JIO command
   * @param  {Object} param The command parameters
   * @param  {Object} option The command option
   */
  IndexStorage.prototype.removeAttachment = function (command, param, option) {
    command.storage(this._sub_storage).removeAttachment(param, option).
      then(command.success, command.error, command.notify);
  };
682

683 684
  /**
   * Gets a document list from the substorage
685
   *
686
   * @method allDocs
687 688 689 690 691
   * @param  {Object} command The JIO command
   * @param  {Object} param The command parameters
   * @param  {Object} option The command option
   * @param  {Boolean} [option.include_docs=false] Also retrieve the actual
   *                   document content.
692 693 694 695
   */
  IndexStorage.prototype.allDocs = function (command, param, option) { // XXX
    /*jslint unparam: true */
    var index = this.selectIndex(option.select_list || []), delete_id;
696

697 698
    // Include docs option is ignored, if you want to get all the document,
    // don't use index storage!
699

700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
    option.select_list = (
      Array.isArray(option.select_list) ? option.select_list : []
    );
    if (option.select_list.indexOf("_id") === -1) {
      option.select_list.push("_id");
      delete_id = true;
    }
    this.getIndexDatabase(command, index).then(function (db) {
      var i, id;
      db = db._database;
      complex_queries.QueryFactory.create(option.query || '').
        exec(db, option);
      for (i = 0; i < db.length; i += 1) {
        id = db[i]._id;
        if (delete_id) {
          delete db[i]._id;
716
        }
717 718 719 720
        db[i] = {
          "id": id,
          "value": db[i]
        };
721
      }
722 723 724 725 726 727 728 729
      command.success(200, {"data": {"total_rows": db.length, "rows": db}});
    }, function (err) {
      if (err.status === 404) {
        return command.success(200, {"data": {"total_rows": 0, "rows": []}});
      }
      command.error(err);
    });
  };
730

731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
  // IndexStorage.prototype.check = function (command, param, option) { // XXX
  //   this.repair(command, true, param, option);
  // };

  // IndexStorage.prototype.repairIndexDatabase = function (
  //   command,
  //   index,
  //   just_check,
  //   param,
  //   option
  // ) { // XXX
  //   var i, that = this;
  //   command.storage(this._sub_storage).allDocs({'include_docs': true}).then(
  //     function (response) {
  //       var db_list = [], db = new JSONIndex({
  //         "_id": param._id,
  //         "_attachment": that._indices[index].attachment || "body",
  //         "indexing": that._indices[index].index
  //       });
  //       for (i = 0; i < response.rows.length; i += 1) {
  //         db.put(response.rows[i].doc);
  //       }
  //       db_list[index] = db;
  //       if (just_check) {
  //       this.getIndexDatabase(command, option, index, function (current_db) {
  //           if (db.equals(current_db)) {
  //             return command.success({"ok": true, "id": param._id});
  //           }
  //           return command.error(
  //             "conflict",
  //             "corrupted",
  //             "Database is not up to date"
  //           );
  //         });
  //       } else {
  //         that.storeIndexDatabaseList(command, db_list, {}, function () {
  //           command.success({"ok": true, "id": param._id});
  //         });
  //       }
  //     },
  //     function (err) {
  //       err.message = "Unable to repair the index database";
  //       command.error(err);
  //     }
  //   );
  // };

  // IndexStorage.prototype.repairDocument = function (
  //   command,
  //   just_check,
  //   param,
  //   option
  // ) { // XXX
  //   var i, that = this;
  //   command.storage(this._sub_storage).get(param, {}).then(
  //     function (response) {
  //       response._id = param._id;
  //       that.getIndexDatabaseList(command, option, function (database_list) {
  //         if (just_check) {
  //           for (i = 0; i < database_list.length; i += 1) {
  //             try {
  //               database_list[i].checkDocument(response);
  //             } catch (e) {
  //               return command.error(
  //                 "conflict",
  //                 e.message,
  //                 "Corrupt index database"
  //               );
  //             }
  //           }
  //           command.success({"_id": param._id, "ok": true});
  //         } else {
  //           for (i = 0; i < database_list.length; i += 1) {
  //             database_list[i].put(response);
  //           }
  //           that.storeIndexDatabaseList(
  //             command,
  //             database_list,
  //             option,
  //             function () {
  //               command.success({"ok": true, "id": param._id});
  //             }
  //           );
  //         }
  //       });
  //     },
  //     function (err) {
  //       err.message = "Unable to repair document";
  //       return command.error(err);
  //     }
  //   );
  // };

  // IndexStorage.prototype.repair = function (command, just_check, param,
  //                                           option) { // XXX
  //   var database_index = -1, i, that = this;
  //   for (i = 0; i < this._indices.length; i += 1) {
  //     if (this._indices[i].id === param._id) {
  //       database_index = i;
  //       break;
  //     }
  //   }
  //   command.storage(this._sub_storage).repair(param, option).then(
  //     function () {
  //       if (database_index !== -1) {
  //         that.repairIndexDatabase(
  //           command,
  //           database_index,
  //           just_check,
  //           param,
  //           option
  //         );
  //       } else {
  //         that.repairDocument(command, just_check, param, option);
  //       }
  //     },
  //     function (err) {
  //       err.message = "Could not repair sub storage";
  //       command.error(err);
  //     }
  //   );
  // };

  jIO.addStorage("indexed", IndexStorage);

  exports.createDescription = function () {
    // XXX
    return;
  };
860

861
}));