mongoid_auto_increment_id 0.6.5 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -1
- data/lib/mongoid/auto_increment_id/config.rb +28 -0
- data/lib/mongoid_auto_increment_id.rb +34 -3
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8e2c38dec21ef0b5b1e74421e2c52c365e361414
|
4
|
+
data.tar.gz: 63af6a893e7d8e26b96e87f14f21425dc465d7e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 93324b2c58cf12bd2cc1875453cb22a7c6d181a4febf24f367f32ff8d7d30b604e0cf5eaa73bff36d972e588cea1e279e49ea7c5be151ad1d85a7b9f5aefcc64
|
7
|
+
data.tar.gz: 0bede2413f1bb5836e10718d6e4ab902f17f00e531151a198a9f64253a8f7fb83c228b22834dcbb00cbdc6288e2282eceaeda52b5e00d16ac64009b9931c4f7c
|
data/README.md
CHANGED
@@ -17,9 +17,25 @@ gem 'mongoid_auto_increment_id', "0.6.1"
|
|
17
17
|
gem 'mongoid_auto_increment_id', "0.6.2"
|
18
18
|
|
19
19
|
# Mongoid 4.0.0+
|
20
|
-
gem 'mongoid_auto_increment_id', "0.
|
20
|
+
gem 'mongoid_auto_increment_id', "0.7.0"
|
21
21
|
```
|
22
22
|
|
23
|
+
## Configure
|
24
|
+
|
25
|
+
If you want use sequence cache to reduce MongoDB write, you can enable cache:
|
26
|
+
|
27
|
+
config/initializes/mongoid_auto_increment_id.rb
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
Mongoid::AutoIncrementId.cache_store = ActiveSupport::Cache.lookup_store(:memcache_store, "127.0.0.1")
|
31
|
+
# First call will generate 200 ids and caching in cache_store
|
32
|
+
# Then the next 199 ids will return from cache_store
|
33
|
+
# Until 200 ids used, it will generate next 200 ids again.
|
34
|
+
Mongoid::AutoIncrementId.seq_cache_size = 200
|
35
|
+
```
|
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.
|
38
|
+
|
23
39
|
## USAGE
|
24
40
|
|
25
41
|
```ruby
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module AutoIncrementId
|
3
|
+
@seq_cache_size = 1
|
4
|
+
@cache_store = ActiveSupport::Cache::MemoryStore.new
|
5
|
+
|
6
|
+
class << self
|
7
|
+
# How many ids generate in once call, and it will cache ids in Memroy, to reduce MongoDB write
|
8
|
+
# default: 1
|
9
|
+
#
|
10
|
+
# [Call first] -> [occupancy N and Write MongoDB] -> [Save N ids in Memory variable `aii_cache`]
|
11
|
+
# [Next call] -> [Shift aii_cache and return]
|
12
|
+
# .....
|
13
|
+
# [N+1 call] -> [occupancy N and Write MongoDB] -> [Save N ids in Memory variable `aii_cache`]
|
14
|
+
attr_accessor :seq_cache_size
|
15
|
+
|
16
|
+
# ActiveSupport::Cache::Store
|
17
|
+
# default: ActiveSupport::Cache.lookup_store(:memory_store)
|
18
|
+
#
|
19
|
+
# Mongoid::AutoIncrementId.cache_store = ActiveSupport::Cache.lookup_store(:memcache_store, "127.0.0.1")
|
20
|
+
# For cache ids
|
21
|
+
attr_accessor :cache_store
|
22
|
+
|
23
|
+
def cache_enabled?
|
24
|
+
self.seq_cache_size > 1
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -1,7 +1,18 @@
|
|
1
|
+
require "mongoid/auto_increment_id/config"
|
2
|
+
|
1
3
|
module Mongoid
|
2
4
|
class Identity
|
3
5
|
# Generate auto increment id
|
6
|
+
# params:
|
4
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?
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
5
16
|
database_name = Mongoid::Sessions.default.send(:current_database).name
|
6
17
|
|
7
18
|
o = nil
|
@@ -9,12 +20,32 @@ module Mongoid
|
|
9
20
|
o = node.command(database_name,
|
10
21
|
{"findAndModify" => "mongoid.auto_increment_ids",
|
11
22
|
:query => { :_id => document.collection_name },
|
12
|
-
:update => { "$inc" => { :c =>
|
23
|
+
:update => { "$inc" => { :c => Mongoid::AutoIncrementId.seq_cache_size }},
|
13
24
|
:upsert => true,
|
14
25
|
:new => true }, {})
|
15
26
|
end
|
16
|
-
|
17
|
-
o["value"]["c"].to_i
|
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
|
35
|
+
end
|
36
|
+
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}"
|
18
49
|
end
|
19
50
|
end
|
20
51
|
|
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.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Lee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-03-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
@@ -38,6 +38,7 @@ extensions: []
|
|
38
38
|
extra_rdoc_files: []
|
39
39
|
files:
|
40
40
|
- README.md
|
41
|
+
- lib/mongoid/auto_increment_id/config.rb
|
41
42
|
- lib/mongoid_auto_increment_id.rb
|
42
43
|
homepage: https://github.com/huacnlee/mongoid_auto_increment_id
|
43
44
|
licenses: []
|
@@ -58,8 +59,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
59
|
version: '0'
|
59
60
|
requirements: []
|
60
61
|
rubyforge_project:
|
61
|
-
rubygems_version: 2.
|
62
|
+
rubygems_version: 2.4.5
|
62
63
|
signing_key:
|
63
64
|
specification_version: 4
|
64
65
|
summary: Override id field with MySQL like auto increment for Mongoid
|
65
66
|
test_files: []
|
67
|
+
has_rdoc:
|