forked from devver/dm-adapter-simpledb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added legacy object support, versioned records, and unit tests.
* Also began to factor out functionality into separate files
- Loading branch information
Avdi Grimm
committed
Nov 22, 2009
1 parent
c74c534
commit 19be731
Showing
22 changed files
with
1,105 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module SimpleDB | ||
class ChunkedString < String | ||
MAX_CHUNK_SIZE = 1019 | ||
|
||
def self.valid?(values) | ||
values.all?{|v| v =~ /^\d{4}:/} | ||
end | ||
|
||
def initialize(string_or_array) | ||
case string_or_array | ||
when Array then super(chunks_to_string(string_or_array)) | ||
else super(string_or_array) | ||
end | ||
end | ||
|
||
def to_ary | ||
string_to_chunks(self) | ||
end | ||
|
||
alias_method :to_a, :to_ary | ||
|
||
private | ||
|
||
def string_to_chunks(value) | ||
return [value] if value.size <= 1019 | ||
chunks = value.to_s.scan(%r/.{1,1019}/m) # 1024 - '1024:'.size | ||
i = -1 | ||
fmt = '%04d:' | ||
chunks.map!{|chunk| [(fmt % (i += 1)), chunk].join} | ||
raise ArgumentError, 'that is just too big yo!' if chunks.size >= 256 | ||
chunks | ||
end | ||
|
||
def chunks_to_string(value) | ||
begin | ||
chunks = | ||
Array(value).flatten.map do |chunk| | ||
index, text = chunk.split(%r/:/, 2) | ||
[Float(index).to_i, text] | ||
end | ||
chunks.replace chunks.sort_by{|index, text| index} | ||
string_result = chunks.map!{|index, text| text}.join | ||
string_result | ||
rescue ArgumentError, TypeError | ||
#return original value, they could have put strings in the system not | ||
#using the adapter or previous versions that are larger than chunk size, | ||
#but less than 1024 | ||
value | ||
end | ||
end | ||
|
||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
namespace :simpledb do | ||
desc "Migrate records to be compatable with current DM/SimpleDB adapter" | ||
task :migrate, :domain do |t, args| | ||
raise "THIS IS A WORK IN PROGRESS AND WILL DESTROY YOUR DATA" | ||
require 'progressbar' | ||
require 'right_aws' | ||
require 'simpledb/record' | ||
|
||
puts "Initializing connection..." | ||
domain = args.domain | ||
sdb = RightAws::SdbInterface.new | ||
puts "Counting records..." | ||
num_legacy_records = 0 | ||
query = "select count(*) from #{domain} where (simpledb_type is not null) and (__dm_metadata is null)" | ||
next_token = nil | ||
while(results = sdb.select(query, next_token)) do | ||
next_token = results[:next_token] | ||
count = results[:items].first["Domain"]["Count"].first.to_i | ||
num_legacy_records += count | ||
break if next_token.nil? | ||
end | ||
puts "Found #{num_legacy_records} to migrate" | ||
|
||
pbar = ProgressBar.new("migrate", num_legacy_records) | ||
query = "select * from #{domain} where (simpledb_type is not null) and (__dm_metadata is null)" | ||
while(results = sdb.select(query, next_token)) do | ||
next_token = results[:next_token] | ||
items = results[:items] | ||
items.each do |item| | ||
legacy_record = SimpleDB::Record.from_simpledb_hash(item) | ||
new_record = legacy_record.migrate | ||
updates = new_record.writable_attributes | ||
deletes = new_record.deletable_attributes | ||
sdb.put_attributes(domain, new_record.item_name, updates) | ||
sdb.delete_attributes(domain, new_record.item_name, deletes) | ||
pbar.inc | ||
end | ||
break if next_token.nil? | ||
end | ||
pbar.finish | ||
|
||
end | ||
end |
Oops, something went wrong.