Sha256: 94fe45c67ce53740dd8adb903093f4a419f54939c3c06328ce147bfe7034cd64

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

# coding: utf-8

require 'json'

module COS

  class Checkpoint < Struct::Base

    # 默认线程数 10
    DEFAULT_THREADS = 10

    def initialize(options = {})
      super(options)

      # 分片大小必须>0
      if options[:options] and options[:options][:slice_size] and options[:options][:slice_size] <= 0
        raise ClientError, 'slice_size must > 0'
      end

      @mutex = Mutex.new
      @file_meta   = {}
      @num_threads = options[:threads] || DEFAULT_THREADS
      @all_mutex   = Mutex.new
      @parts       = []
      @todo_mutex  = Mutex.new
      @todo_parts  = []
    end

    private

    # 写入断点续传状态
    def write_checkpoint(states, file)
      sha1 = Util.string_sha1(states.to_json)

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

    # 加载断点续传状态
    def load_checkpoint(file)
      states = {}

      @mutex.synchronize do
        states = JSON.parse(File.read(file), symbolize_names: true)
      end
      sha1 = states.delete(:sha1)

      raise CheckpointBrokenError, 'Missing SHA1 in checkpoint.' unless sha1

      unless sha1 == Util.string_sha1(states.to_json)
        raise CheckpointBrokenError, 'Unmatched checkpoint SHA1'
      end

      states
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cos-0.1.2 lib/cos/checkpoint.rb
cos-0.1.1 lib/cos/checkpoint.rb