-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch_friendly_parse.inc.php
59 lines (49 loc) · 1.8 KB
/
batch_friendly_parse.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/*
I used this parser with the SDBSelectIterator to clone data between SimpleDB domains for testing.
NOTE: There is a known bug if your data exceeds the size limit before the row limit. (E.g., if 25 rows can ever weigh more than 1 Mb, you will get a fatal error from batch_put_attributes)
$rows_cloned = 0;
$batches_cloned = 0;
$old_domain_data = new SDBSelectIterator("SELECT * FROM `$old_domain`", "batch_friendly_sdb_parse");
if (!$old_domain_data->isOK()){
error_out_and_die("Problem getting old domain data: " . $old_domain_data->error_message());
}
$rows = array();
foreach($old_domain_data as $row){
$rows_cloned += 1;
$rows = array_merge($rows, $row);
if(count($rows) >= 25 or !$old_domain_data->next_valid()){
$overwrite_stats = $sdb->batch_put_attributes($new_domain, $rows);
if(!$overwrite_stats->isOK()){
die("Failed to batch put: " . ((String)$overwrite_stats->body->Errors[0]->Error->Message) );
}
$batches_cloned += 1;
$rows = array();
}
}
echo "Success! Cloned $rows_cloned rows in $batches_cloned batches.<br>";
*/
function batch_friendly_sdb_parse($item){
$return = array();
// Loop through the item's attributes
foreach ($item->Attribute as $attribute){
$column_name = (string) $attribute->Name;
if($return[$column_name] and !is_array($return[$column_name])){
//2nd attribute with same col name, make it an array
$return[$column_name] = array(
$return[$column_name],
(string) $attribute->Value
);
}elseif($return[$column_name] and is_array($return[$column_name])){
//Nth entry into existing array
$return[$column_name][] = (string) $attribute->Value;
}else{
//1st entry
$return[$column_name] = (string) $attribute->Value;
}
}
return array(
(string) $item->Name => $return
);//Calling code needs to array_merge these results together
}
?>