memdbindex 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env node
  2. // Copyright 2015 The MemDB Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  13. // implied. See the License for the specific language governing
  14. // permissions and limitations under the License. See the AUTHORS file
  15. // for names of contributors.
  16. 'use strict';
  17. var minimist = require('minimist');
  18. var helpContent = '\
  19. MemDB Index Builder\n\n\
  20. Usage: memdbindex [rebuild | drop] [options]\n\
  21. Options:\n\
  22. -c, --conf path Config file path\n\
  23. -t, --coll collection Collection name\n\
  24. -k, --keys key1.key2 Index keys (split with ".")\n';
  25. var getIndexOpts = function(conf, collName, keys){
  26. var indexes = conf.collections && conf.collections[collName] && conf.collections[collName].indexes;
  27. if(!indexes){
  28. return null;
  29. }
  30. for(var i in indexes){
  31. var index = indexes[i];
  32. if(JSON.stringify(index.keys.sort()) === JSON.stringify(keys.sort())){
  33. return index;
  34. }
  35. }
  36. return null;
  37. };
  38. if (require.main === module){
  39. var argv = minimist(process.argv.slice(3));
  40. var cmd = process.argv[2];
  41. if(process.argv.length <= 3 || argv.help || argv.h){
  42. console.log(helpContent);
  43. process.exit(0);
  44. }
  45. var config = require('../app/config');
  46. config.init(argv.conf || argv.c);
  47. var conf = config.shardConfig(config.getShardIds()[0]);
  48. var collName = argv.coll || argv.t;
  49. if(!collName){
  50. throw new Error('collection not specified');
  51. }
  52. var keys = argv.keys || argv.k;
  53. if(!keys){
  54. throw new Error('keys not specified');
  55. }
  56. keys = keys.split('.').sort();
  57. var indexbuilder = require('../app/indexbuilder');
  58. if(cmd === 'rebuild'){
  59. var opts = getIndexOpts(conf, collName, keys);
  60. if(!opts){
  61. throw new Error('specified index is not configured');
  62. }
  63. indexbuilder.rebuild(conf, collName, keys, opts);
  64. }
  65. else if(cmd === 'drop'){
  66. indexbuilder.drop(conf, collName, keys);
  67. }
  68. else{
  69. throw new Error('invalid cmd - ' + cmd);
  70. }
  71. }