Class: Longleaf::MetadataSerializer

Inherits:
Object
  • Object
show all
Extended by:
Logging
Defined in:
lib/longleaf/services/metadata_serializer.rb

Overview

Service which serializes MetadataRecord objects

Class Method Summary collapse

Methods included from Logging

initialize_logger, initialize_logger, logger, logger

Class Method Details

.metadata_suffix(format: 'yaml') ⇒ String

Returns the suffix used to indicate that a file is a metadata file in the provided encoding.

Parameters:

  • format (String) (defaults to: 'yaml')

    encoding format used for metadata file

Returns:

  • (String)

    the suffix used to indicate that a file is a metadata file in the provided encoding

Raises:

  • (ArgumentError)

    raised if the provided format is not a supported metadata encoding format



76
77
78
79
80
81
82
83
# File 'lib/longleaf/services/metadata_serializer.rb', line 76

def self.(format: 'yaml')
  case format
  when 'yaml'
    '-llmd.yaml'
  else
    raise ArgumentError.new("Invalid serialization format #{format} specified")
  end
end

.set_perms(file_path, stat_info) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/longleaf/services/metadata_serializer.rb', line 157

def self.set_perms(file_path, stat_info)
  if stat_info
    # Set correct permissions on new file
    begin
      File.chown(stat_info.uid, stat_info.gid, file_path)
      # This operation will affect filesystem ACL's
      File.chmod(stat_info.mode, file_path)
    rescue Errno::EPERM, Errno::EACCES
      # Changing file ownership failed, moving on.
      return false
    end
  end
  true
end

.to_hash(metadata) ⇒ Object

Create a hash representation of the given MetadataRecord file

Parameters:

  • metadata (MetadataRecord)

    metadata record to transform into a hash



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/longleaf/services/metadata_serializer.rb', line 46

def self.to_hash()
  props = Hash.new

  data = Hash.new.merge(.properties)
  data[MDF::REGISTERED_TIMESTAMP] = .registered if .registered
  data[MDF::DEREGISTERED_TIMESTAMP] = .deregistered if .deregistered
  data[MDF::CHECKSUMS] = .checksums unless .checksums && .checksums.empty?
  data[MDF::FILE_SIZE] = .file_size unless .file_size.nil?
  data[MDF::LAST_MODIFIED] = .last_modified if .last_modified
  data[MDF::PHYSICAL_PATH] = .physical_path if .physical_path

  props[MDF::DATA] = data

  services = Hash.new
  .list_services.each do |name|
    service = .service(name)
    service[MDF::STALE_REPLICAS] = service.stale_replicas if service.stale_replicas
    service[MDF::SERVICE_TIMESTAMP] = service.timestamp unless service.timestamp.nil?
    service[MDF::RUN_NEEDED] = service.run_needed if service.run_needed
    services[name] = service.properties unless service.properties.empty?
  end

  props[MDF::SERVICES] = services

  props
end

.to_yaml(metadata) ⇒ String

Returns a yaml representation of the provided MetadataRecord.

Parameters:

Returns:

  • (String)

    a yaml representation of the provided MetadataRecord



39
40
41
42
# File 'lib/longleaf/services/metadata_serializer.rb', line 39

def self.to_yaml()
  props = to_hash()
  props.to_yaml
end

.write(metadata:, file_path:, format: 'yaml', digest_algs: []) ⇒ Object

Serialize the contents of the provided metadata record to the specified path

Parameters:

  • metadata (MetadataRecord)

    metadata record to serialize. Required.

  • file_path (String)

    path to write the file to. Required.

  • format (String) (defaults to: 'yaml')

    format to serialize the metadata in. Default is ‘yaml’.

  • digest_algs (Array) (defaults to: [])

    if provided, sidecar digest files for the metadata file will be generated for each algorithm.

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/longleaf/services/metadata_serializer.rb', line 23

def self.write(metadata:, file_path:, format: 'yaml', digest_algs: [])
  raise ArgumentError.new('metadata parameter must be a MetadataRecord') \
      unless .class == MetadataRecord

  case format
  when 'yaml'
    content = to_yaml()
  else
    raise ArgumentError.new("Invalid serialization format #{format} specified")
  end

  atomic_write(file_path, content, digest_algs)
end