Method: ContentData::ContentData#validate

Defined in:
lib/content_data/content_data.rb

#validate(params = nil) ⇒ Boolean

Validates index against file system that all instances hold a correct data regarding files that they represents.

There are two levels of validation, controlled by instance_check_level system parameter:

  • shallow - quick, tests instance for file existence and attributes.

  • deep - can take more time, in addition to shallow recalculates hash sum.

Parameters:

  • params (Hash) (defaults to: nil)

    hash of parameters of validation, can be used to return additional data.

    Supported key/value combinations:

    • key is :failed value is ContentData used to return failed instances

Returns:

  • (Boolean)

    true when index is correct, false otherwise

Raises:

  • (ArgumentError)

    when instance_check_level is incorrect



539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# File 'lib/content_data/content_data.rb', line 539

def validate(params = nil)
  # used to answer whether specific param was set
  param_exists = Proc.new do |param|
    !(params.nil? || params[param].nil?)
  end

  # used to process method parameters centrally
  process_params = Proc.new do |values|
    if param_exists.call(:failed)
      info = values[:details]
      unless info.nil?
        checksum = info[0]
        content_mtime = info[1]
        size = info[2]
        inst_mtime = info[3]
        server = info[4]
        file_path = info[5]
        params[:failed].add_instance(checksum, size, server, file_path, inst_mtime)
      end
    end
  end

  is_valid = true
  contents_enum = @contents_info.each_key
  loop {
    checksum = contents_enum.next rescue break
    instances = @contents_info[checksum]
    content_size = instances[0]
    content_mtime = instances[2]
    instances_enum = instances[1].each_key
    loop {
      unique_path = instances_enum.next rescue break
      instance_mtime = instances[1][unique_path][0]
      instance_info = [checksum, content_mtime, content_size, instance_mtime]
      instance_info.concat(unique_path)
      unless check_instance(instance_info)
        is_valid = false

        unless params.nil? || params.empty?
          process_params.call({:details => instance_info})
        end
      end
    }
  }
  is_valid
end