indexing.js 3.0 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. // Add the following index config in memdb.conf.js and restart memdbcluster
  17. //
  18. // collections : {
  19. // player : {
  20. // indexes : [
  21. // {
  22. // keys : ['areaId'],
  23. // valueIgnore : {
  24. // areaId : ['', -1],
  25. // },
  26. // },
  27. // {
  28. // keys : ['deviceType', 'deviceId'],
  29. // unique : true,
  30. // },
  31. // ]
  32. // }
  33. // },
  34. // npm install memdb-client
  35. // run with node >= 0.12 with --harmony option
  36. // We assume you have started shard 's1' on localhost:31017
  37. var memdb = require('memdb-client');
  38. var P = memdb.Promise;
  39. var should = require('should');
  40. var main = P.coroutine(function*(){
  41. // Connect to memdb
  42. var autoconn = yield memdb.autoConnect({
  43. shards : {s1 : {host : '127.0.0.1', port : 31017}}
  44. });
  45. var Player = autoconn.collection('player');
  46. // make transaction in shard s1
  47. yield autoconn.transaction(P.coroutine(function*(){
  48. // Insert players
  49. var players = [{_id : 'p1', name : 'rain', areaId : 1},
  50. {_id : 'p2', name : 'snow', areaId : 2}];
  51. yield Player.insert(players);
  52. // Find all players in area1
  53. var docs = yield Player.find({areaId : 1});
  54. docs.length.should.eql(1);
  55. docs[0].areaId.should.eql(1);
  56. // Find doc of id p1, return one doc
  57. var doc = yield Player.find('p1');
  58. doc._id.should.eql('p1');
  59. // DO NOT do this! Error will be thrown since name is not indexed.
  60. // yield Player.find({name : 'rain'});
  61. // Move all players in area1 into area2
  62. yield Player.update({areaId : 1}, {$set : {areaId : 2}});
  63. // Remove all players in area2
  64. yield Player.remove({areaId : 2});
  65. }), 's1');
  66. // make transaction in shard s1
  67. yield autoconn.transaction(P.coroutine(function*(){
  68. // Insert a player
  69. yield Player.insert({_id : 'p1', deviceType : 1, deviceId : 'id1'});
  70. // Find with compound key
  71. var docs = yield Player.find({deviceType : 1, deviceId : 'id1'});
  72. docs.length.should.eql(1);
  73. docs[0]._id.should.eql('p1');
  74. // Will throw duplicate key error
  75. // yield Player.insert({deviceType : 1, deviceId : 'id1'});
  76. // Remove player
  77. yield Player.remove('p1');
  78. }), 's1');
  79. });
  80. if (require.main === module) {
  81. main().finally(process.exit);
  82. }