Class: Hyperion::Riak::Datastore

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperion/riak/datastore.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Datastore

Returns a new instance of Datastore.



10
11
12
13
14
15
# File 'lib/hyperion/riak/datastore.rb', line 10

def initialize(opts={})
  opts ||= {}
  @app = opts[:app] || ''
  @client = ::Riak::Client.new(opts.reject {|k, v| k == :app})
  @buckets = {}
end

Instance Method Details

#count(query) ⇒ Object



50
51
52
53
54
# File 'lib/hyperion/riak/datastore.rb', line 50

def count(query)
  mr = new_mapreduce(query)
  mr.reduce(MapReduceJs.count, :keep => true)
  mr.run.first
end

#delete(query) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/hyperion/riak/datastore.rb', line 42

def delete(query)
  mr = new_mapreduce_with_returned_result(query)
  mr.run.each do |record|
    delete_with_riak_key(query.kind, record['riak_key'])
  end
  nil
end

#delete_by_key(key) ⇒ Object



36
37
38
39
40
# File 'lib/hyperion/riak/datastore.rb', line 36

def delete_by_key(key)
  kind, riak_key = Hyperion::Key.decompose_key(key)
  delete_with_riak_key(kind, riak_key)
  nil
end

#find(query) ⇒ Object



29
30
31
32
33
34
# File 'lib/hyperion/riak/datastore.rb', line 29

def find(query)
  mr = new_mapreduce_with_returned_result(query)
  mr.run.map do |record|
    record_from_db(query.kind, record.delete('riak_key'), record)
  end
end

#find_by_key(key) ⇒ Object



23
24
25
26
27
# File 'lib/hyperion/riak/datastore.rb', line 23

def find_by_key(key)
  kind, riak_key = Hyperion::Key.decompose_key(key)
  robject = bucket(kind).get(riak_key)
  record_from_db(kind, robject.key, robject.data)
end

#pack_key(kind, key) ⇒ Object



56
57
58
59
60
61
# File 'lib/hyperion/riak/datastore.rb', line 56

def pack_key(kind, key)
  if key
    kind, riak_key = Hyperion::Key.decompose_key(key)
    riak_key
  end
end

#save(records) ⇒ Object



17
18
19
20
21
# File 'lib/hyperion/riak/datastore.rb', line 17

def save(records)
  records.map do |record|
    Hyperion.new?(record) ? create(record) : update(record)
  end
end

#unpack_key(kind, riak_key) ⇒ Object



63
64
65
66
67
# File 'lib/hyperion/riak/datastore.rb', line 63

def unpack_key(kind, riak_key)
  if riak_key
    Hyperion::Key.compose_key(kind, riak_key)
  end
end