Advertisement
Guest User

Untitled

a guest
Dec 5th, 2011
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. #!/usr/bin/ruby
  2.  
  3. require 'rubygems'
  4. # Memcache client
  5. require 'dalli'
  6. # To divide an array
  7. require 'enumerator'
  8.  
  9. s3cmdpath = "/usr/bin/s3cmd"
  10. bucketpath = "s3://bucketname/
  11. localpath = "/media/ephemeral0/targeting/"
  12.  
  13. # This generates 256 values..from 00-ff. Each value is now considered a "HexPrefix"
  14. HexPrefixes = Array.new 256 do |i| sprintf("%02x", i) end
  15.  
  16. # We're now going to subdivide the work into 8 queues, since this program is meant to run on an 8 core instance.
  17. Queue1, Queue2, Queue3, Queue4, Queue5, Queue6, Queue7, Queue8 = HexPrefixes.enum_slice((HexPrefixes.size / 8.0).round).to_a
  18.  
  19. # This function synchronizes an array of directories, creates a hash based on MD5 filenames & data and pushes the data into Memcache.
  20. class SyncHashPush
  21. def initialize(hexprefixes)
  22. for i in hexprefixes
  23. # Generate a command that will call s3cmd and sync a "HexPrefix" directory. The result is a string, newline delimited, that represents all files that have changed.
  24. commandstring = s3cmdpath + " sync " + bucketpath + i + " " + localpath + " | grep \"targeting.prd1\" | awk -F\"/\" '{print $7}' | awk -F\".txt\" '{print $1}'"
  25. # Run the command
  26. s3cmd = `#{commandstring}`
  27. # This shows what is happening above.
  28. #s3cmd = `s3cmd sync s3://bucketname/00 /media/ephemeral0/targeting/ | grep "targeting.prd1" | awk -F"/" '{print $7}' | awk -F".txt" '{print $1}'`
  29.  
  30. # Sample data for testing purposes. Faster than actually running s3cmd.
  31. #s3cmd = "fefe7e94b341f15186d92d46897e43a1\nfefe85da54d0044e554027a46ebe428b\nfefe8a29ecf31fe26768a7a51f4bc00f\nfefe9137d095f6dc3ae105ceb9aa9c2a\nfefe93418a8fe6f1f0e7688e6bf2f47e"
  32.  
  33. # Convert the string into an array.
  34. s3cmdarray = s3cmd.split(/\n/)
  35.  
  36. # Create a Hash that includes the "MD5 filename" and the file data.
  37. urlTransformHash = Hash.new
  38. for md5string in s3cmdarray
  39. prefix1 = md5string[0..1]
  40. prefix2 = md5string[2..3]
  41.  
  42. filepath=localpath + prefix1 + "/" + prefix2 + "/" + md5string + ".txt"
  43. filedata = File.read(filepath)
  44.  
  45. urlTransformHash[md5string] = filedata
  46. end
  47.  
  48. # Push the contents of the hash into memcache.
  49. dalliclient = Dalli::Client.new('localhost:11211')
  50. urlTransformHash.each do|md5string,filedata|
  51. # Display the hash. Testing purposes.
  52. # puts "#{md5string}: #{filedata}"
  53.  
  54. # Push the string into Memcache.
  55. dalliclient.set(md5string, filedata)
  56. end
  57. end
  58. end
  59. end
  60.  
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement