Class: Moneta::Adapters::LMDB

Inherits:
Object
  • Object
show all
Includes:
Defaults
Defined in:
lib/moneta/adapters/lmdb.rb

Overview

LMDB backend

Constant Summary collapse

PUT_FLAGS =
[:nooverwrite, :nodupdata, :current, :append, :appenddup]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Defaults

#[], #[]=, #decrement, #features, #fetch, included, #supports?

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options) ⇒ LMDB

Returns a new instance of LMDB.

Parameters:

  • options (Hash)

Options Hash (options):

  • :dir (String)

    Environment directory

  • :backend (::LMDB::Environment)

    Use existing backend instance

  • :db (String or nil)

    Database name



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/moneta/adapters/lmdb.rb', line 20

def initialize(options)
  db = options.delete(:db)
  @backend = options.delete(:backend) ||
    begin
      raise ArgumentError, 'Option :dir is required' unless dir = options.delete(:dir)
      FileUtils.mkpath(dir)
      ::LMDB.new(dir, options)
    end

  @db = @backend.database(db, create: true)
end

Instance Attribute Details

#backendObject (readonly)



12
13
14
# File 'lib/moneta/adapters/lmdb.rb', line 12

def backend
  @backend
end

#dbObject (readonly)



12
13
14
# File 'lib/moneta/adapters/lmdb.rb', line 12

def db
  @db
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

  • options (Hash) (defaults to: {})


59
60
61
62
# File 'lib/moneta/adapters/lmdb.rb', line 59

def clear(options = {})
  @db.clear
  self
end

#closeObject

Explicitly close the store

Returns:

  • nil



87
88
89
90
# File 'lib/moneta/adapters/lmdb.rb', line 87

def close
  @backend.close
  nil
end

#create(key, value, options = {}) ⇒ Boolean

Note:

Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.

Atomically sets a key to value if it’s not set.

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

Returns:

  • (Boolean)

    key was set



75
76
77
78
79
80
81
82
83
84
# File 'lib/moneta/adapters/lmdb.rb', line 75

def create(key, value, options = {})
  @backend.transaction do
    if @db.get(key)
      false
    else
      @db.put(key, value, Utils.only(options, *PUT_FLAGS))
      true
    end
  end
end

#delete(key, options = {}) ⇒ Object

Delete the key from the store and return the current value

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value



49
50
51
52
53
54
55
56
# File 'lib/moneta/adapters/lmdb.rb', line 49

def delete(key, options = {})
  @backend.transaction do
    if value = @db.get(key)
      @db.delete(key)
      value
    end
  end
end

#increment(key, amount = 1, options = {}) ⇒ Object

Note:

Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.

Atomically increment integer value with key

This method also accepts negative amounts.

Parameters:

  • key (Object)
  • amount (Integer) (defaults to: 1)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value from store



65
66
67
68
69
70
71
72
# File 'lib/moneta/adapters/lmdb.rb', line 65

def increment(key, amount = 1, options = {})
  @backend.transaction do
    value = @db.get(key)
    value = Utils.to_int(value) + amount
    @db.put(key, value.to_s, Utils.only(options, *PUT_FLAGS))
    value
  end
end

#key?(key, options = {}) ⇒ Boolean

Exists the value with key

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Boolean)


33
34
35
# File 'lib/moneta/adapters/lmdb.rb', line 33

def key?(key, options = {})
  !@db.get(key).nil?
end

#load(key, options = {}) ⇒ Object

Fetch value with key. Return nil if the key doesn’t exist

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • :sync (Boolean)

    Synchronized load (Cache reloads from adapter, Daybreak syncs with file)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value



38
39
40
# File 'lib/moneta/adapters/lmdb.rb', line 38

def load(key, options = {})
  @db.get(key)
end

#store(key, value, options = {}) ⇒ Object

Store value with key

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • value



43
44
45
46
# File 'lib/moneta/adapters/lmdb.rb', line 43

def store(key, value, options = {})
  @db.put(key, value, Utils.only(options, *PUT_FLAGS))
  value
end