indexbuilder.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2015 The MemDB Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. // implied. See the License for the specific language governing
  13. // permissions and limitations under the License. See the AUTHORS file
  14. // for names of contributors.
  15. 'use strict';
  16. var P = require('bluebird');
  17. var util = require('util');
  18. var path = require('path');
  19. var should = require('should');
  20. var env = require('../env');
  21. var memdb = require('../../lib');
  22. var logger = require('memdb-logger').getLogger('test', __filename);
  23. describe('indexbuilder test', function(){
  24. beforeEach(env.flushdb);
  25. it('rebuild', function(cb){
  26. var shardId = 's1';
  27. var autoconn = null;
  28. return P.try(function(){
  29. return env.startCluster(shardId);
  30. })
  31. .then(function(){
  32. return memdb.autoConnect(env.config)
  33. .then(function(ret){
  34. autoconn = ret;
  35. });
  36. })
  37. .then(function(){
  38. var Player = autoconn.collection('player');
  39. return autoconn.transaction(function(){
  40. return P.try(function(){
  41. return Player.insert({_id : 'p1', areaId : 'a1'});
  42. })
  43. .then(function(){
  44. return Player.insert({_id : 'p2', areaId : 'a2'});
  45. });
  46. }, shardId);
  47. })
  48. .then(function(){
  49. return autoconn.close();
  50. })
  51. .then(function(){
  52. return env.stopCluster();
  53. })
  54. .then(function(){
  55. var script = path.join(__dirname, '../../bin/memdbindex');
  56. var args = ['--conf=' + env.configPath, '--coll=player', '--keys=areaId'];
  57. return P.try(function(){
  58. // drop index
  59. return env.runScript(script, ['drop'].concat(args));
  60. })
  61. .then(function(){
  62. // rebuild index
  63. return env.runScript(script, ['rebuild'].concat(args));
  64. });
  65. })
  66. .then(function(){
  67. return env.startCluster(shardId);
  68. })
  69. .then(function(){
  70. return memdb.autoConnect(env.config)
  71. .then(function(ret){
  72. autoconn = ret;
  73. });
  74. })
  75. .then(function(){
  76. var Player = autoconn.collection('player');
  77. return autoconn.transaction(function(){
  78. return Player.find({areaId : 'a1'})
  79. .then(function(ret){
  80. ret.length.should.eql(1);
  81. ret[0]._id.should.eql('p1');
  82. });
  83. }, shardId);
  84. })
  85. .then(function(){
  86. return autoconn.close();
  87. })
  88. .finally(function(){
  89. return env.stopCluster();
  90. })
  91. .nodeify(cb);
  92. });
  93. });