lib/mongo_auto_increment.rb
# Generate auto increment id
# params:
Generate auto increment id
params.
Generate auto increment id params .
def generate_id(document)
        if MongoAutoIncrement.cache_enabled?
          cache_key = self.maii_cache_key(document)
          if ids = MongoAutoIncrement.cache_store.read(cache_key)
            cached_id = self.shift_id(ids, cache_key)
            return self.generated_token(cached_id) if !cached_id.blank?
          end
        end
        opts = {
            findAndModify: MAII_TABLE_NAME,
            query: { _id: document.collection_name },
            update: { '$inc' => { c: MongoAutoIncrement.seq_cache_size } },
            upsert: true,
            new: true
        }
        o = Mongoid.default_client.database.command(opts, {})
        last_seq = o.documents[0]['value']['c'].to_i
        if MongoAutoIncrement.cache_enabled?
          ids = ((last_seq - MongoAutoIncrement.seq_cache_size) + 1 .. last_seq).to_a
          self.generated_token(self.shift_id(ids, cache_key))
        else
          self.generated_token(last_seq)
        end
      end
def generate_id ( document ) if MongoAutoIncrement . cache_enabled? cache_key = self . maii_cache_key ( document ) if ids = MongoAutoIncrement . cache_store . read ( cache_key ) cached_id = self . shift_id ( ids , cache_key ) return self . generated_token ( cached_id ) if ! cached_id . blank? end end opts = { findAndModify : MAII_TABLE_NAME , query : { _id : document . collection_name } , update : { '$inc' => { c : MongoAutoIncrement . seq_cache_size } } , upsert : true , new : true } o = Mongoid . default_client . database . command ( opts , { } ) last_seq = o . documents [ 0 ] [ 'value' ] [ 'c' ] . to_i if MongoAutoIncrement . cache_enabled? ids = ( ( last_seq - MongoAutoIncrement . seq_cache_size ) + 1 .. last_seq ) . to_a self . generated_token ( self . shift_id ( ids , cache_key ) ) else self . generated_token ( last_seq ) end end
Generate auto increment id
params:
Generate auto increment id params :
document