mongo_session_store 2.0.0 → 3.0.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +43 -0
  5. data/.travis.yml +37 -0
  6. data/CHANGELOG.md +49 -0
  7. data/Dangerfile +1 -0
  8. data/Gemfile +3 -0
  9. data/LICENSE +23 -0
  10. data/README.md +106 -43
  11. data/Rakefile +90 -0
  12. data/gemfiles/rails-4.0-mongo.gemfile +9 -0
  13. data/gemfiles/rails-4.0-mongoid.gemfile +9 -0
  14. data/gemfiles/rails-4.1-mongo.gemfile +9 -0
  15. data/gemfiles/rails-4.1-mongoid.gemfile +9 -0
  16. data/gemfiles/rails-4.2-mongo.gemfile +9 -0
  17. data/gemfiles/rails-4.2-mongoid.gemfile +9 -0
  18. data/lib/mongo_session_store.rb +27 -1
  19. data/lib/mongo_session_store/mongo_store.rb +102 -0
  20. data/lib/mongo_session_store/mongo_store_base.rb +65 -0
  21. data/lib/mongo_session_store/mongoid_store.rb +52 -0
  22. data/lib/mongo_session_store/version.rb +3 -0
  23. data/mongo_session_store.gemspec +25 -0
  24. data/perf/benchmark.rb +101 -0
  25. data/spec/.rubocop.yml +4 -0
  26. data/spec/integrations/devise_spec.rb +109 -0
  27. data/spec/lib/mongo_session_store/mongo_store/session_spec.rb +247 -0
  28. data/spec/lib/mongo_session_store/mongo_store_base_spec.rb +269 -0
  29. data/spec/lib/mongo_session_store/mongo_store_spec.rb +13 -0
  30. data/spec/lib/mongo_session_store/mongoid_store/session_spec.rb +82 -0
  31. data/spec/lib/mongo_session_store/mongoid_store_spec.rb +13 -0
  32. data/spec/lib/mongo_session_store_spec.rb +64 -0
  33. data/spec/rails_helper.rb +19 -0
  34. data/spec/spec_helper.rb +53 -0
  35. data/spec/support/apps/rails_4.0_app/Rakefile +6 -0
  36. data/spec/support/apps/rails_4.0_app/app/controllers/application_controller.rb +5 -0
  37. data/spec/support/apps/rails_4.0_app/app/controllers/home_controller.rb +4 -0
  38. data/spec/support/apps/rails_4.0_app/app/models/user.rb +5 -0
  39. data/spec/support/apps/rails_4.0_app/app/views/home/index.html.erb +10 -0
  40. data/spec/support/apps/rails_4.0_app/app/views/layouts/application.html.erb +17 -0
  41. data/spec/support/apps/rails_4.0_app/bin/bundle +3 -0
  42. data/spec/support/apps/rails_4.0_app/bin/rails +4 -0
  43. data/spec/support/apps/rails_4.0_app/bin/rake +4 -0
  44. data/spec/support/apps/rails_4.0_app/config.ru +4 -0
  45. data/spec/support/apps/rails_4.0_app/config/application.rb +11 -0
  46. data/spec/support/apps/rails_4.0_app/config/boot.rb +4 -0
  47. data/spec/support/apps/rails_4.0_app/config/database.yml +11 -0
  48. data/spec/support/apps/rails_4.0_app/config/environment.rb +5 -0
  49. data/spec/support/apps/rails_4.0_app/config/environments/development.rb +12 -0
  50. data/spec/support/apps/rails_4.0_app/config/environments/test.rb +19 -0
  51. data/spec/support/apps/rails_4.0_app/config/initializers/devise.rb +254 -0
  52. data/spec/support/apps/rails_4.0_app/config/initializers/secret_token.rb +12 -0
  53. data/spec/support/apps/rails_4.0_app/config/initializers/session_store.rb +1 -0
  54. data/spec/support/apps/rails_4.0_app/config/locales/devise.en.yml +59 -0
  55. data/spec/support/apps/rails_4.0_app/config/locales/en.yml +23 -0
  56. data/spec/support/apps/rails_4.0_app/config/mongo.yml +11 -0
  57. data/spec/support/apps/rails_4.0_app/config/mongoid.yml +13 -0
  58. data/spec/support/apps/rails_4.0_app/config/routes.rb +4 -0
  59. data/spec/support/apps/rails_4.0_app/db/migrate/20140301171212_add_devise_users.rb +11 -0
  60. data/spec/support/apps/rails_4.0_app/db/schema.rb +25 -0
  61. data/spec/support/apps/rails_4.0_app/db/seeds.rb +7 -0
  62. data/spec/support/apps/rails_4.0_app/lib/assets/.keep +0 -0
  63. data/spec/support/apps/rails_4.0_app/lib/tasks/.keep +0 -0
  64. data/spec/support/apps/rails_4.0_app/log/.keep +0 -0
  65. data/spec/support/apps/rails_4.0_app/public/404.html +58 -0
  66. data/spec/support/apps/rails_4.0_app/public/422.html +58 -0
  67. data/spec/support/apps/rails_4.0_app/public/500.html +57 -0
  68. data/spec/support/apps/rails_4.0_app/public/favicon.ico +0 -0
  69. data/spec/support/apps/rails_4.0_app/public/robots.txt +5 -0
  70. data/spec/support/apps/rails_4.1_app/Rakefile +3 -0
  71. data/spec/support/apps/rails_4.1_app/app/controllers/application_controller.rb +5 -0
  72. data/spec/support/apps/rails_4.1_app/app/controllers/home_controller.rb +4 -0
  73. data/spec/support/apps/rails_4.1_app/app/models/user.rb +5 -0
  74. data/spec/support/apps/rails_4.1_app/app/views/home/index.html.erb +10 -0
  75. data/spec/support/apps/rails_4.1_app/app/views/layouts/application.html.erb +17 -0
  76. data/spec/support/apps/rails_4.1_app/bin/bundle +3 -0
  77. data/spec/support/apps/rails_4.1_app/bin/rails +4 -0
  78. data/spec/support/apps/rails_4.1_app/bin/rake +4 -0
  79. data/spec/support/apps/rails_4.1_app/config.ru +2 -0
  80. data/spec/support/apps/rails_4.1_app/config/application.rb +11 -0
  81. data/spec/support/apps/rails_4.1_app/config/boot.rb +4 -0
  82. data/spec/support/apps/rails_4.1_app/config/database.yml +11 -0
  83. data/spec/support/apps/rails_4.1_app/config/environment.rb +5 -0
  84. data/spec/support/apps/rails_4.1_app/config/environments/development.rb +12 -0
  85. data/spec/support/apps/rails_4.1_app/config/environments/test.rb +19 -0
  86. data/spec/support/apps/rails_4.1_app/config/initializers/devise.rb +254 -0
  87. data/spec/support/apps/rails_4.1_app/config/initializers/session_store.rb +1 -0
  88. data/spec/support/apps/rails_4.1_app/config/locales/devise.en.yml +59 -0
  89. data/spec/support/apps/rails_4.1_app/config/locales/en.yml +23 -0
  90. data/spec/support/apps/rails_4.1_app/config/mongo.yml +11 -0
  91. data/spec/support/apps/rails_4.1_app/config/mongoid.yml +13 -0
  92. data/spec/support/apps/rails_4.1_app/config/routes.rb +4 -0
  93. data/spec/support/apps/rails_4.1_app/config/secrets.yml +22 -0
  94. data/spec/support/apps/rails_4.1_app/db/migrate/20140301171212_add_devise_users.rb +11 -0
  95. data/spec/support/apps/rails_4.1_app/db/schema.rb +25 -0
  96. data/spec/support/apps/rails_4.1_app/db/seeds.rb +7 -0
  97. data/spec/support/apps/rails_4.1_app/lib/assets/.keep +0 -0
  98. data/spec/support/apps/rails_4.1_app/lib/tasks/.keep +0 -0
  99. data/spec/support/apps/rails_4.1_app/log/.keep +0 -0
  100. data/spec/support/apps/rails_4.1_app/public/404.html +67 -0
  101. data/spec/support/apps/rails_4.1_app/public/422.html +67 -0
  102. data/spec/support/apps/rails_4.1_app/public/500.html +66 -0
  103. data/spec/support/apps/rails_4.1_app/public/favicon.ico +0 -0
  104. data/spec/support/apps/rails_4.1_app/public/robots.txt +5 -0
  105. data/spec/support/apps/rails_4.2_app/Rakefile +2 -0
  106. data/spec/support/apps/rails_4.2_app/app/controllers/application_controller.rb +5 -0
  107. data/spec/support/apps/rails_4.2_app/app/controllers/home_controller.rb +4 -0
  108. data/spec/support/apps/rails_4.2_app/app/models/user.rb +5 -0
  109. data/spec/support/apps/rails_4.2_app/app/views/home/index.html.erb +10 -0
  110. data/spec/support/apps/rails_4.2_app/app/views/layouts/application.html.erb +15 -0
  111. data/spec/support/apps/rails_4.2_app/bin/bundle +3 -0
  112. data/spec/support/apps/rails_4.2_app/bin/rails +8 -0
  113. data/spec/support/apps/rails_4.2_app/bin/rake +4 -0
  114. data/spec/support/apps/rails_4.2_app/config.ru +4 -0
  115. data/spec/support/apps/rails_4.2_app/config/application.rb +11 -0
  116. data/spec/support/apps/rails_4.2_app/config/boot.rb +3 -0
  117. data/spec/support/apps/rails_4.2_app/config/database.yml +11 -0
  118. data/spec/support/apps/rails_4.2_app/config/environment.rb +5 -0
  119. data/spec/support/apps/rails_4.2_app/config/environments/development.rb +12 -0
  120. data/spec/support/apps/rails_4.2_app/config/environments/test.rb +19 -0
  121. data/spec/support/apps/rails_4.2_app/config/initializers/devise.rb +254 -0
  122. data/spec/support/apps/rails_4.2_app/config/initializers/session_store.rb +1 -0
  123. data/spec/support/apps/rails_4.2_app/config/locales/en.yml +23 -0
  124. data/spec/support/apps/rails_4.2_app/config/mongo.yml +11 -0
  125. data/spec/support/apps/rails_4.2_app/config/mongoid.yml +13 -0
  126. data/spec/support/apps/rails_4.2_app/config/routes.rb +4 -0
  127. data/spec/support/apps/rails_4.2_app/config/secrets.yml +22 -0
  128. data/spec/support/apps/rails_4.2_app/db/migrate/20140301171212_add_devise_users.rb +11 -0
  129. data/spec/support/apps/rails_4.2_app/db/schema.rb +25 -0
  130. data/spec/support/apps/rails_4.2_app/db/seeds.rb +7 -0
  131. data/spec/support/helpers/session_id_helper.rb +5 -0
  132. data/spec/support/helpers/test_database_helper.rb +22 -0
  133. metadata +360 -92
  134. data/lib/mongo_session_store/mongo_mapper.rb +0 -60
  135. data/lib/mongo_session_store/mongoid.rb +0 -59
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "mongo"
4
+
5
+ gem "rails", "4.0"
6
+ gem "devise", "~> 3.5"
7
+ gem "sqlite3"
8
+
9
+ gemspec :path => "../"
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "mongoid"
4
+
5
+ gem "rails", "4.0"
6
+ gem "devise", "~> 3.5"
7
+ gem "sqlite3"
8
+
9
+ gemspec :path => "../"
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "mongo"
4
+
5
+ gem "rails", "4.1"
6
+ gem "devise", Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.1") ? "3.5.10" : ">= 4.2"
7
+ gem "sqlite3"
8
+
9
+ gemspec :path => "../"
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "mongoid"
4
+
5
+ gem "rails", "4.1"
6
+ gem "devise", Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.1") ? "3.5.10" : ">= 4.2"
7
+ gem "sqlite3"
8
+
9
+ gemspec :path => "../"
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "mongo"
4
+
5
+ gem "rails", "4.2"
6
+ gem "devise", Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.1") ? "3.5.10" : ">= 4.2"
7
+ gem "sqlite3"
8
+
9
+ gemspec :path => "../"
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "mongoid"
4
+
5
+ gem "rails", "4.2"
6
+ gem "devise", Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.1") ? "3.5.10" : ">= 4.2"
7
+ gem "sqlite3"
8
+
9
+ gemspec :path => "../"
@@ -1 +1,27 @@
1
- $:.unshift File.dirname(__FILE__)
1
+ module MongoSessionStore
2
+ def self.collection_name
3
+ @collection_name
4
+ end
5
+
6
+ def self.collection_name=(name)
7
+ @collection_name = name
8
+
9
+ if defined?(MongoidStore::Session)
10
+ MongoidStore::Session.store_in \
11
+ :collection => MongoSessionStore.collection_name
12
+ end
13
+
14
+ MongoStore::Session.reset_collection if defined?(MongoStore::Session)
15
+
16
+ @collection_name
17
+ end
18
+ end
19
+
20
+ if defined?(Mongoid)
21
+ require "mongo_session_store/mongoid_store"
22
+ elsif defined?(Mongo)
23
+ require "mongo_session_store/mongo_store"
24
+ end
25
+
26
+ # Default collection name for all the stores.
27
+ MongoSessionStore.collection_name = "sessions"
@@ -0,0 +1,102 @@
1
+ require "mongo_session_store/mongo_store_base"
2
+
3
+ module ActionDispatch
4
+ module Session
5
+ class MongoStore < MongoStoreBase
6
+ class Session
7
+ class << self
8
+ attr_writer :database
9
+
10
+ def load(options = {})
11
+ options[:data] = options["data"] if options["data"]
12
+ options[:data] =
13
+ if options[:data]
14
+ unpack(options[:data])
15
+ else
16
+ {}
17
+ end
18
+ new(options)
19
+ end
20
+
21
+ def where(query = {})
22
+ collection.find(query).map { |doc| load(doc) }
23
+ end
24
+
25
+ def database
26
+ return @database if @database
27
+
28
+ raise NoMongoClientError, "MongoStore needs a database, e.g. "\
29
+ "MongoStore::Session.database = Mongo::Client.new("\
30
+ "[\"127.0.0.1:27017\"], database: \"my_app_development\")"
31
+ end
32
+
33
+ def collection
34
+ @collection ||= database[MongoSessionStore.collection_name]
35
+ end
36
+
37
+ def reset_collection
38
+ @collection = nil
39
+ end
40
+
41
+ private
42
+
43
+ def unpack(packed)
44
+ return unless packed
45
+ data = packed.respond_to?(:data) ? packed.data : packed.to_s
46
+ Marshal.load(StringIO.new(data))
47
+ end
48
+ end
49
+
50
+ class NoMongoClientError < StandardError; end
51
+
52
+ attr_accessor :_id, :data, :created_at, :updated_at
53
+
54
+ def initialize(options = {})
55
+ @_id = options[:_id]
56
+ @data = options[:data]
57
+ @created_at = options[:created_at]
58
+ @updated_at = options[:updated_at]
59
+ end
60
+
61
+ def save
62
+ @created_at ||= Time.now.utc
63
+ @updated_at = Time.now.utc
64
+
65
+ scope.replace_one(attributes_for_save, :upsert => true)
66
+ end
67
+
68
+ def destroy
69
+ scope.delete_one
70
+ end
71
+
72
+ private
73
+
74
+ def collection
75
+ self.class.collection
76
+ end
77
+
78
+ def attributes_for_save
79
+ {
80
+ :data => pack(data),
81
+ :created_at => created_at,
82
+ :updated_at => updated_at
83
+ }
84
+ end
85
+
86
+ def scope
87
+ collection.find(:_id => _id)
88
+ end
89
+
90
+ def pack(data)
91
+ BSON::Binary.new(Marshal.dump(data || {}), :generic)
92
+ end
93
+
94
+ def unpack(packed)
95
+ self.class.unpack(packed)
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
101
+
102
+ MongoStore = ActionDispatch::Session::MongoStore
@@ -0,0 +1,65 @@
1
+ require "action_dispatch/middleware/session/abstract_store"
2
+
3
+ module ActionDispatch
4
+ module Session
5
+ class MongoStoreBase < AbstractStore
6
+ SESSION_RECORD_KEY = "rack.session.record".freeze
7
+ ENV_SESSION_OPTIONS_KEY = Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY
8
+
9
+ def self.session_class
10
+ self::Session
11
+ end
12
+
13
+ private
14
+
15
+ def session_class
16
+ self.class.session_class
17
+ end
18
+
19
+ def get_session(env, sid)
20
+ id, record = find_or_initialize_session(sid)
21
+ env[SESSION_RECORD_KEY] = record
22
+ [id, record.data]
23
+ end
24
+
25
+ def set_session(env, sid, session_data, _options = {})
26
+ id, record = get_session_record(env, sid)
27
+ record.data = session_data
28
+ yield if block_given?
29
+ # Rack spec dictates that set_session should return true or false
30
+ # depending on whether or not the session was saved or not.
31
+ # However, ActionPack seems to want a session id instead.
32
+ record.save ? id : false
33
+ end
34
+
35
+ def find_or_initialize_session(id)
36
+ existing_session = (id && session_class.where(:_id => id).first)
37
+ session = existing_session if existing_session
38
+ session ||= session_class.new(:_id => generate_sid)
39
+ [session._id, session]
40
+ end
41
+
42
+ def get_session_record(env, sid)
43
+ if env[ENV_SESSION_OPTIONS_KEY][:id].nil? || !env[SESSION_RECORD_KEY]
44
+ sid, env[SESSION_RECORD_KEY] = find_or_initialize_session(sid)
45
+ end
46
+
47
+ [sid, env[SESSION_RECORD_KEY]]
48
+ end
49
+
50
+ def destroy_session(env, _session_id, options)
51
+ destroy(env)
52
+ generate_sid unless options[:drop]
53
+ end
54
+
55
+ def destroy(env)
56
+ sid = current_session_id(env)
57
+ return unless sid
58
+
59
+ _, record = get_session_record(env, sid)
60
+ record.destroy
61
+ env[SESSION_RECORD_KEY] = nil
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,52 @@
1
+ require "mongo_session_store/mongo_store_base"
2
+
3
+ module ActionDispatch
4
+ module Session
5
+ class MongoidStore < MongoStoreBase
6
+ class Session
7
+ include Mongoid::Document
8
+ include Mongoid::Timestamps
9
+
10
+ attr_writer :data
11
+
12
+ store_in :collection => MongoSessionStore.collection_name
13
+
14
+ field :_data, :type => BSON::Binary, :default => -> { pack({}) }
15
+
16
+ def self.pack(data)
17
+ BSON::Binary.new(Marshal.dump(data), :generic)
18
+ end
19
+
20
+ def data
21
+ @data ||= unpack(_data)
22
+ end
23
+
24
+ def reload
25
+ @data = nil
26
+ super
27
+ end
28
+
29
+ private
30
+
31
+ before_save do
32
+ self._data = pack(data)
33
+ end
34
+
35
+ def pack(data)
36
+ self.class.pack(data)
37
+ end
38
+
39
+ def unpack(packed)
40
+ return unless packed
41
+ if packed.respond_to? :data
42
+ Marshal.load(packed.data)
43
+ else
44
+ Marshal.load(packed.to_s)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ MongoidStore = ActionDispatch::Session::MongoidStore
@@ -0,0 +1,3 @@
1
+ module MongoSessionStore
2
+ VERSION = "3.0.0.beta.1".freeze
3
+ end
@@ -0,0 +1,25 @@
1
+ require File.expand_path("../lib/mongo_session_store/version", __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "mongo_session_store"
5
+ s.version = MongoSessionStore::VERSION
6
+
7
+ s.authors = ["Tom de Bruijn", "Brian Hempel", "Nicolas M\303\251rouze", "Tony Pitale", "Chris Brickley"]
8
+ s.email = ["tom@tomdebruijn.com"]
9
+ s.files = `git ls-files`.split("\n")
10
+ s.test_files = `git ls-files -- {spec,perf}/*`.split("\n")
11
+ s.homepage = "http://github.com/mongoid/mongo_session_store"
12
+ s.license = "MIT"
13
+ s.require_paths = ["lib"]
14
+ s.summary = "Rails session stores for Mongoid, or any other ODM. Rails 4 compatible."
15
+ s.required_ruby_version = ">= 1.9"
16
+
17
+ s.add_dependency "actionpack", "~> 4.0"
18
+ s.add_dependency "mongo", "~> 2.0"
19
+
20
+ s.add_development_dependency "rspec-rails", "~> 3"
21
+ s.add_development_dependency "pry", "~> 0.10"
22
+ s.add_development_dependency "rake", "~> 11"
23
+ s.add_development_dependency "rubocop", "0.45.0"
24
+ s.add_development_dependency "mongoid-danger", "~> 0.1.0"
25
+ end
@@ -0,0 +1,101 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+ require "benchmark"
4
+ require "action_dispatch"
5
+ require "mongoid"
6
+ require "mongo_session_store"
7
+ require "mongo_session_store/mongoid_store"
8
+ require "mongo_session_store/mongo_store"
9
+ require File.expand_path("../../lib/mongo_session_store", __FILE__)
10
+
11
+ Mongo::Logger.logger.level = Logger::FATAL
12
+ Mongoid.logger.level = Logger::FATAL
13
+
14
+ RUNS = 2000
15
+
16
+ def benchmark(test_name)
17
+ time = Benchmark.realtime do
18
+ RUNS.times do
19
+ yield
20
+ end
21
+ end
22
+ puts "#{(time / RUNS * 100_000).round / 100.0}ms per #{test_name}"
23
+ end
24
+
25
+ def benchmark_store(store)
26
+ collection = store.class.session_class.collection
27
+ collection.drop
28
+
29
+ large_session = {
30
+ :something => "not nothing",
31
+ :left => "not right",
32
+ :welcome => "not despised",
33
+ :visits => [
34
+ "http://www.google.com",
35
+ "localhost:3000",
36
+ "http://localhost:3000/increment_session",
37
+ "http://www.iso.org/iso/country_codes/iso_3166_code_lists/iso-3166-1_decoding_table.htm",
38
+ "http://www.geonames.org/search.html?q=2303+australia&country="
39
+ ],
40
+ :one_k_of_data => "a" * 1024,
41
+ :another_k => "b" * 1024,
42
+ :more_data => [5] * 500,
43
+ :too_much_data_for_a_cookie => "c" * 8000,
44
+ :a_bunch_of_floats_in_embedded_docs => [:float_a => 3.141, :float_b => -1.1] * 100
45
+ }
46
+
47
+ ids = []
48
+
49
+ env = {
50
+ "rack.session" => large_session,
51
+ "rack.session.options" => { :id => store.send(:generate_sid) }
52
+ }
53
+ benchmark "session save" do
54
+ ids << store.send(
55
+ :set_session,
56
+ env.merge("rack.session.options" => { :id => nil }), nil, env["rack.session"]
57
+ )
58
+ end
59
+
60
+ ids.shuffle!
61
+
62
+ benchmark "session load" do
63
+ id = ids.pop
64
+ local_env = { "rack.request.cookie_hash" => { "_session_id" => id } }
65
+ _, data = store.send(:get_session, local_env, id)
66
+ if data[:something] != "not nothing" || data[:a_bunch_of_floats_in_embedded_docs][0][:float_a] != 3.141
67
+ puts data
68
+ raise "saved and fetched data do not match!"
69
+ end
70
+ end
71
+
72
+ database = collection.database
73
+ stats = database.command(:collstats => collection.name).documents.first
74
+ puts "Collection: #{collection.namespace}"
75
+ puts " Total Size: #{stats["size"]}"
76
+ puts " Object count: #{stats["count"]}"
77
+ puts " Average object size: #{stats["avgObjSize"]}"
78
+ puts " Index sizes: #{stats["indexSizes"].inspect}"
79
+ end
80
+
81
+ MongoSessionStore.collection_name = "session_collection"
82
+ MongoStore::Session.database =
83
+ Mongo::Client.new(["127.0.0.1:27017"], :database => "mongo_test_database")
84
+ Mongoid.configure do |c|
85
+ c.load_configuration(
86
+ "clients" => {
87
+ "default" => {
88
+ "database" => "mongoid_test_database",
89
+ "hosts" => ["127.0.0.1:27017"]
90
+ }
91
+ }
92
+ )
93
+ end
94
+
95
+ puts "MongoidStore..."
96
+ mongoid_store = MongoidStore.new(nil)
97
+ benchmark_store(mongoid_store)
98
+
99
+ puts "MongoStore..."
100
+ mongo_store = MongoStore.new(nil)
101
+ benchmark_store(mongo_store)