Skip to content

Commit

Permalink
add get_metadata_items
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzz committed Jan 23, 2014
1 parent 982bc9a commit 4056d6e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/moe/dyna.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def get_item(read_tables, key)
item
end

def implode(item)
item.each_key { |key| item[key] = item[key][:s] }.to_h
end

def put_item(write_tables, item)
write_tables.each do |table_name|
dynamodb.put_item table_name: table_name, item: explode(item)
Expand Down
56 changes: 56 additions & 0 deletions lib/moe/models/sequence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,43 @@ def add(item={})
end
end

def get_metadata_items(owner_id)
results = query owner_id, read_tables, true
end

def query(owner_id, read_tables, join=false)
results = []

read_tables.each do |table_name|

items = {
table_name: table_name,
key_conditions: {
"hash" => {
attribute_value_list: [
{ s: owner_id }
],
comparison_operator: "EQ"
},
}
}

items[:key_conditions]["range"] = {
attribute_value_list: [
{ s: "0" }
],
comparison_operator: "BEGINS_WITH"
} if join

results << { table_name => dyna.dynamodb.query(items).items }
end

parse_query_results(results)
end

def save(item={})
@uuid ||= SecureRandom.uuid

metadata_item = {
"count" => (items.size + flushed_count).to_s,
"saved_at" => Time.now.to_s
Expand Down Expand Up @@ -78,6 +114,26 @@ def keyify
item.update key count
end
end

def parse_query_results(results)
parsed_results = {}

results.each do |table|
table.each do |table_name,item|
parsed_results[table_name] = []
parsed_item = {}

item.each do |attribute|
attribute.each do |name,value|
parsed_item[name] = value.s
end
parsed_results[table_name] << parsed_item
end
end
end

parsed_results
end
end
end
end
10 changes: 9 additions & 1 deletion spec/lib/dyna_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
end

describe "#explode" do
it "given a plain hash it returns a DynamoDB-flavored hash" do
it "returns a DynamoDB-flavored hash given a plain hash" do
expect(
dyna.explode({ "foo" => "bar" })
).to eq({"foo" => { s: "bar" } })
Expand All @@ -68,6 +68,14 @@
end
end

describe "#implode" do
it "returns a plain hash given a DynamoDB-flavored hash" do
expect(
dyna.implode({"foo" => { s: "bar" } })
).to eq({ "foo" => "bar" })
end
end

describe "#find" do
it "finds a table" do
dyna.create "Testy#{count}"
Expand Down
17 changes: 17 additions & 0 deletions spec/lib/models/sequence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@
end
end

describe "#get_metadata_items" do
it "gets all metadata records for a given owner" do
seq.save({ "foo" => "bar" })

expect(
seq.get_metadata_items("owner").first.last.first["foo"]
).to eq("bar")
end
end

describe "#query" do
it "retrieves items" do
seq.save
seq.query("owner", seq.read_tables, true)
end
end

describe "#save" do
it "persists a metadata item" do
seq.add
Expand Down

0 comments on commit 4056d6e

Please sign in to comment.