Class: Longleaf::MetadataSerializer
- Inherits:
-
Object
- Object
- Longleaf::MetadataSerializer
- Extended by:
- Logging
- Defined in:
- lib/longleaf/services/metadata_serializer.rb
Overview
Service which serializes MetadataRecord objects
Class Method Summary collapse
-
.metadata_suffix(format: 'yaml') ⇒ String
The suffix used to indicate that a file is a metadata file in the provided encoding.
-
.to_hash(metadata) ⇒ Object
Create a hash representation of the given MetadataRecord file.
-
.to_yaml(metadata) ⇒ String
A yaml representation of the provided MetadataRecord.
-
.write(metadata:, file_path:, format: 'yaml', digest_algs: []) ⇒ Object
Serialize the contents of the provided metadata record to the specified path.
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.
79 80 81 82 83 84 85 86 |
# File 'lib/longleaf/services/metadata_serializer.rb', line 79 def self.(format: 'yaml') case format when 'yaml' '-llmd.yaml' else raise ArgumentError.new("Invalid serialization format #{format} specified") end end |
.to_hash(metadata) ⇒ Object
Create a hash representation of the given MetadataRecord file
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/longleaf/services/metadata_serializer.rb', line 50 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 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. unless service..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.
43 44 45 46 |
# File 'lib/longleaf/services/metadata_serializer.rb', line 43 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
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/longleaf/services/metadata_serializer.rb', line 22 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 # Fill in parent directories if they do not exist parent_dir = Pathname(file_path).parent parent_dir.mkpath unless parent_dir.exist? File.write(file_path, content) write_digests(file_path, content, digest_algs) end |