class AliyunSDK::OSS::Multipart::Transaction

A multipart transaction. Provide the basic checkpoint methods.

Public Class Methods

new(opts = {}) click to toggle source
Calls superclass method AliyunSDK::Common::Struct::Base::new
# File lib/aliyun_sdk/oss/multipart.rb, line 20
def initialize(opts = {})
  super(opts)

  @mutex = Mutex.new
end

Private Instance Methods

get_file_md5(file) click to toggle source
# File lib/aliyun_sdk/oss/multipart.rb, line 56
def get_file_md5(file)
  Digest::MD5.file(file).to_s
end
load_checkpoint(file) click to toggle source

Load transaction states from file

# File lib/aliyun_sdk/oss/multipart.rb, line 39
def load_checkpoint(file)
  states = {}

  @mutex.synchronize {
    states = JSON.load(File.read(file))
  }
  states = Util.symbolize(states)
  md5 = states.delete(:md5)

  fail CheckpointBrokenError, "Missing MD5 in checkpoint." unless md5
  unless md5 == Util.get_content_md5(states.to_json)
    fail CheckpointBrokenError, "Unmatched checkpoint MD5."
  end

  states
end
write_checkpoint(states, file) click to toggle source

Persist transaction states to file

# File lib/aliyun_sdk/oss/multipart.rb, line 28
def write_checkpoint(states, file)
  md5= Util.get_content_md5(states.to_json)

  @mutex.synchronize {
    File.open(file, 'w') {
      |f| f.write(states.merge(md5: md5).to_json)
    }
  }
end