Class: Tarantool::Model::Testing::MemoryStorage

Inherits:
Object
  • Object
show all
Defined in:
tarantool-model/lib/tarantool/model/testing/memory_storage.rb,
tarantool-model/lib/tarantool/model/testing/memory_storage/keys.rb,
tarantool-model/lib/tarantool/model/testing/memory_storage/scan.rb,
tarantool-model/lib/tarantool/model/testing/memory_storage/order.rb,
tarantool-model/lib/tarantool/model/testing/memory_storage/space.rb,
tarantool-model/lib/tarantool/model/testing/memory_storage/box_mode.rb,
tarantool-model/lib/tarantool/model/testing/memory_storage/sequence.rb,
tarantool-model/lib/tarantool/model/testing/memory_storage/crud_mode.rb,
tarantool-model/lib/tarantool/model/testing/memory_storage/violation.rb,
tarantool-model/lib/tarantool/model/testing/memory_storage/operations.rb,
tarantool-model/lib/tarantool/model/testing/memory_storage/declaration.rb,
tarantool-model/lib/tarantool/model/testing/memory_storage/index_declaration.rb

Overview

In-memory storage for application tests, without a Tarantool server. Spaces are declared in tarantool-schema's ensure_space shape. behaves_like: :box keeps the Storage::Box contract, :crud the Storage::Crud one: what the server would reject is raised with the classes and codes the application sees in production. The README lists what it does not model.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spaces, behaves_like: :box) ⇒ MemoryStorage

Returns a new instance of MemoryStorage.



18
19
20
21
22
23
24
# File 'tarantool-model/lib/tarantool/model/testing/memory_storage.rb', line 18

def initialize(spaces, behaves_like: :box)
  @mode = MODES.fetch(behaves_like) do
    raise ArgumentError, "behaves_like must be one of #{MODES.keys.join(', ')}, got #{behaves_like.inspect}"
  end.new
  Keys.distinct!(spaces.keys.map(&:to_s), 'MemoryStorage')
  @spaces = spaces.to_h { |name, schema| [name.to_s, build_space(name, schema)] }
end

Class Method Details

.from_definition(definition) ⇒ Object



14
15
16
# File 'tarantool-model/lib/tarantool/model/testing/memory_storage.rb', line 14

def self.from_definition(definition, **)
  new(definition.to_h, **)
end

Instance Method Details

#count(space, index:, key:, iterator:) ⇒ Object



54
55
56
57
58
# File 'tarantool-model/lib/tarantool/model/testing/memory_storage.rb', line 54

def count(space, index:, key:, iterator:)
  @mode.count!

  perform(space, :count) { |table| @mode.scan(table, index:, key:, iterator:, after: nil).size }
end

#delete(space, key) ⇒ Object



42
43
44
# File 'tarantool-model/lib/tarantool/model/testing/memory_storage.rb', line 42

def delete(space, key)
  single(space, :delete) { |table| table.delete(key) }
end

#get(space, key) ⇒ Object



30
31
32
# File 'tarantool-model/lib/tarantool/model/testing/memory_storage.rb', line 30

def get(space, key)
  single(space, :get) { |table| @mode.get(table, key) }
end

#insert(space, attributes) ⇒ Object



34
35
36
# File 'tarantool-model/lib/tarantool/model/testing/memory_storage.rb', line 34

def insert(space, attributes)
  single(space, :insert) { |table| table.insert(@mode.to_tuple(table, attributes)) }
end

#select(space, index:, key:, iterator:, limit:, after:) ⇒ Object



46
47
48
49
50
51
52
# File 'tarantool-model/lib/tarantool/model/testing/memory_storage.rb', line 46

def select(space, index:, key:, iterator:, limit:, after:)
  perform(space, :select) do |table|
    tuples = @mode.scan(table, index:, key:, iterator:, after:)

    (limit ? tuples.first(limit) : tuples).map { |tuple| @mode.row(table, tuple, single: false) }
  end
end

#supports_transactions? ⇒ Boolean

Returns:

  • (Boolean)


60
# File 'tarantool-model/lib/tarantool/model/testing/memory_storage.rb', line 60

def supports_transactions? = @mode.supports_transactions?

#transaction ⇒ Object

Writes apply at once; the rows are restored unless the block finishes normally. isolation: and timeout: are accepted and ignored. Sequences are not restored, as in Tarantool.



64
65
66
67
68
69
70
71
72
73
# File 'tarantool-model/lib/tarantool/model/testing/memory_storage.rb', line 64

def transaction(**, &)
  @mode.transaction!
  raise Tarantool::TransactionError, 'nested transactions are not supported' if Transactions.handle(self)

  snapshots = @spaces.transform_values(&:snapshot)
  committed = false
  Transactions.open(self, :memory, &).tap { committed = true }
ensure
  snapshots&.each { |name, rows| @spaces.fetch(name).restore(rows) } unless committed
end

#update(space, key, operations) ⇒ Object



38
39
40
# File 'tarantool-model/lib/tarantool/model/testing/memory_storage.rb', line 38

def update(space, key, operations)
  single(space, :update) { |table| table.update(key, operations) }
end

#verify(space, attributes:, primary_key:) ⇒ Object



26
27
28
# File 'tarantool-model/lib/tarantool/model/testing/memory_storage.rb', line 26

def verify(space, attributes:, primary_key:)
  @mode.verify(attributes:, primary_key:) { perform(space, :get, &:itself) }
end