mongoid_auto_increment_id 0.7.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8e2c38dec21ef0b5b1e74421e2c52c365e361414
4
- data.tar.gz: 63af6a893e7d8e26b96e87f14f21425dc465d7e6
3
+ metadata.gz: a8160d010eab6eba7d3cd0bd95ddbeacfcf5d7c0
4
+ data.tar.gz: cef309121e158817280cadd381a1452b5e750e3a
5
5
  SHA512:
6
- metadata.gz: 93324b2c58cf12bd2cc1875453cb22a7c6d181a4febf24f367f32ff8d7d30b604e0cf5eaa73bff36d972e588cea1e279e49ea7c5be151ad1d85a7b9f5aefcc64
7
- data.tar.gz: 0bede2413f1bb5836e10718d6e4ab902f17f00e531151a198a9f64253a8f7fb83c228b22834dcbb00cbdc6288e2282eceaeda52b5e00d16ac64009b9931c4f7c
6
+ metadata.gz: ff878aec05fdab650f9e6f68fcaef7fc9f20c92c2f2cc2ce17cc48bbdbc05fc071f4a0de7aaaa6ed840c55de291ab5681f50461282920d3f630b3c4b5a8653a0
7
+ data.tar.gz: 5b70429926b8cf38001d5289e86b997d7bf0294cec448121e0e9632c4608068030179db366aae120b40c5469f6aa090eb0b4d9c15d86735d08be71a8909787f7
data/README.md CHANGED
@@ -4,20 +4,20 @@ Idea from MongoDB document: [How to Make an Auto Incrementing Field](http://www.
4
4
 
5
5
  ## Status
6
6
 
7
- - [![Gem Version](https://badge.fury.io/rb/mongoid_auto_increment_id.png)](https://rubygems.org/gems/mongoid_auto_increment_id)
8
- - [![CI Status](https://api.travis-ci.org/huacnlee/mongoid_auto_increment_id.png)](http://travis-ci.org/huacnlee/mongoid_auto_increment_id)
7
+ - [![Gem Version](https://badge.fury.io/rb/mongoid_auto_increment_id.svg)](https://rubygems.org/gems/mongoid_auto_increment_id)
8
+ - [![CI Status](https://api.travis-ci.org/huacnlee/mongoid_auto_increment_id.svg)](http://travis-ci.org/huacnlee/mongoid_auto_increment_id)
9
9
 
10
10
  ## Installation
11
11
 
12
12
  ```ruby
13
13
  # Mongoid 3.0.x
14
14
  gem 'mongoid_auto_increment_id', "0.6.1"
15
-
16
- # Mongoid 3.1.0
15
+ # Mongoid 3.1.x
17
16
  gem 'mongoid_auto_increment_id', "0.6.2"
18
-
19
- # Mongoid 4.0.0+
17
+ # Mongoid 4.x
20
18
  gem 'mongoid_auto_increment_id', "0.7.0"
19
+ # Mongoid 5.x
20
+ gem 'mongoid_auto_increment_id', "0.8.0"
21
21
  ```
22
22
 
23
23
  ## Configure
@@ -27,14 +27,14 @@ If you want use sequence cache to reduce MongoDB write, you can enable cache:
27
27
  config/initializes/mongoid_auto_increment_id.rb
28
28
 
29
29
  ```ruby
30
- Mongoid::AutoIncrementId.cache_store = ActiveSupport::Cache.lookup_store(:memcache_store, "127.0.0.1")
30
+ # Mongoid::AutoIncrementId.cache_store = ActiveSupport::Cache::MemoryStore.new
31
31
  # First call will generate 200 ids and caching in cache_store
32
32
  # Then the next 199 ids will return from cache_store
33
33
  # Until 200 ids used, it will generate next 200 ids again.
34
34
  Mongoid::AutoIncrementId.seq_cache_size = 200
35
35
  ```
36
36
 
37
- > NOTICE! mongoid_auto_increment_id is very fast in default config, you may not need enable that, if you project not need insert huge rows in a moment.
37
+ > NOTE: mongoid_auto_increment_id is very fast in default config, you may don't need enable that, if you project not need insert huge rows in a moment.
38
38
 
39
39
  ## USAGE
40
40
 
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module AutoIncrementId
3
+ VERSION = '0.8.1'
4
+ end
5
+ end
@@ -1,60 +1,65 @@
1
- require "mongoid/auto_increment_id/config"
1
+ require "mongoid"
2
+ require 'active_support/all'
3
+ require 'mongoid/auto_increment_id/config'
4
+ require 'mongoid/auto_increment_id/version'
2
5
 
3
6
  module Mongoid
4
7
  class Identity
5
- # Generate auto increment id
6
- # params:
7
- def self.generate_id(document)
8
- cache_key = self.maii_cache_key(document)
9
- if Mongoid::AutoIncrementId.cache_enabled?
10
- if ids = Mongoid::AutoIncrementId.cache_store.read(cache_key)
11
- cached_id = self.shift_id(ids, cache_key)
12
- return cached_id if !cached_id.blank?
8
+ MAII_TABLE_NAME = 'mongoid.auto_increment_ids'.freeze
9
+
10
+ class << self
11
+
12
+ # Generate auto increment id
13
+ # params:
14
+ def generate_id(document)
15
+ if AutoIncrementId.cache_enabled?
16
+ cache_key = self.maii_cache_key(document)
17
+ if ids = Mongoid::AutoIncrementId.cache_store.read(cache_key)
18
+ cached_id = self.shift_id(ids, cache_key)
19
+ return cached_id if !cached_id.blank?
20
+ end
21
+ end
22
+
23
+ opts = {
24
+ findAndModify: MAII_TABLE_NAME,
25
+ query: { _id: document.collection_name },
26
+ update: { '$inc' => { c: AutoIncrementId.seq_cache_size } },
27
+ upsert: true,
28
+ new: true
29
+ }
30
+ o = Mongoid.default_client.database.command(opts, {})
31
+
32
+ last_seq = o.documents[0]['value']['c'].to_i
33
+
34
+ if AutoIncrementId.cache_enabled?
35
+ ids = ((last_seq - AutoIncrementId.seq_cache_size) + 1 .. last_seq).to_a
36
+ self.shift_id(ids, cache_key)
37
+ else
38
+ last_seq
13
39
  end
14
40
  end
15
-
16
- database_name = Mongoid::Sessions.default.send(:current_database).name
17
41
 
18
- o = nil
19
- Mongoid::Sessions.default.cluster.with_primary do |node|
20
- o = node.command(database_name,
21
- {"findAndModify" => "mongoid.auto_increment_ids",
22
- :query => { :_id => document.collection_name },
23
- :update => { "$inc" => { :c => Mongoid::AutoIncrementId.seq_cache_size }},
24
- :upsert => true,
25
- :new => true }, {})
42
+ def shift_id(ids, cache_key)
43
+ return nil if ids.blank?
44
+ first_id = ids.shift
45
+ AutoIncrementId.cache_store.write(cache_key, ids)
46
+ first_id
26
47
  end
27
-
28
- last_seq = o["value"]["c"].to_i
29
-
30
- if Mongoid::AutoIncrementId.cache_enabled?
31
- ids = ((last_seq - Mongoid::AutoIncrementId.seq_cache_size) + 1 .. last_seq).to_a
32
- self.shift_id(ids, cache_key)
33
- else
34
- last_seq
48
+
49
+ def maii_cache_key(document)
50
+ "maii-seqs-#{document.collection_name}"
35
51
  end
36
52
  end
37
-
38
-
39
- private
40
- def self.shift_id(ids, cache_key)
41
- return nil if ids.blank?
42
- first_id = ids.shift
43
- Mongoid::AutoIncrementId.cache_store.write(cache_key, ids)
44
- first_id
45
- end
46
-
47
- def self.maii_cache_key(document)
48
- "maii-seqs-#{document.collection_name}"
49
- end
50
53
  end
51
54
 
52
55
  module Document
56
+ ID_FIELD = '_id'.freeze
57
+
53
58
  def self.included(base)
54
59
  base.class_eval do
55
60
  # define Integer for id field
56
61
  Mongoid.register_model(self)
57
- field :_id, :type => Integer, :overwrite => true
62
+ field :_id, type: Integer, overwrite: true
58
63
  end
59
64
  end
60
65
 
@@ -64,11 +69,11 @@ module Mongoid
64
69
  nil
65
70
  end
66
71
 
67
- alias_method :super_as_document,:as_document
72
+ alias_method :super_as_document, :as_document
68
73
  def as_document
69
74
  result = super_as_document
70
- if result["_id"].blank?
71
- result["_id"] = Identity.generate_id(self)
75
+ if result[ID_FIELD].blank?
76
+ result[ID_FIELD] = Identity.generate_id(self)
72
77
  end
73
78
  result
74
79
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_auto_increment_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Lee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-10 00:00:00.000000000 Z
11
+ date: 2015-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
@@ -16,20 +16,20 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 3.1.0
19
+ version: 5.0.0
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: 4.1.0
22
+ version: 6.0.0
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: 3.1.0
29
+ version: 5.0.0
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: 4.1.0
32
+ version: 6.0.0
33
33
  description: This gem for change Mongoid id field as Integer like MySQL.
34
34
  email:
35
35
  - huacnlee@gmail.com
@@ -39,9 +39,11 @@ extra_rdoc_files: []
39
39
  files:
40
40
  - README.md
41
41
  - lib/mongoid/auto_increment_id/config.rb
42
+ - lib/mongoid/auto_increment_id/version.rb
42
43
  - lib/mongoid_auto_increment_id.rb
43
44
  homepage: https://github.com/huacnlee/mongoid_auto_increment_id
44
- licenses: []
45
+ licenses:
46
+ - MIT
45
47
  metadata: {}
46
48
  post_install_message:
47
49
  rdoc_options: []
@@ -59,9 +61,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
61
  version: '0'
60
62
  requirements: []
61
63
  rubyforge_project:
62
- rubygems_version: 2.4.5
64
+ rubygems_version: 2.4.8
63
65
  signing_key:
64
66
  specification_version: 4
65
67
  summary: Override id field with MySQL like auto increment for Mongoid
66
68
  test_files: []
67
- has_rdoc: