Class: Cuboid::Snapshot
Overview
Stores and provides access to the state of the system.
Defined Under Namespace
Classes: Error
Constant Summary collapse
- EXTENSION =
'csf'
Class Attribute Summary collapse
-
.location ⇒ String
Location of the loaded snapshot.
-
.metadata ⇒ Hash
Metadata associated with the loaded snapshot.
Class Method Summary collapse
-
.dump(location) ⇒ String
Location of the snapshot.
-
.load(snapshot) ⇒ Snapshot
‘self`.
-
.read_metadata(snapshot) ⇒ Hash
Metadata associated with the given snapshot.
- .reset ⇒ Object
-
.restored? ⇒ Bool
‘true` if this is a restored snapshot, `false` otherwise.
-
.summary ⇒ Hash
Snapshot summary information.
Class Attribute Details
.location ⇒ String
Returns Location of the loaded snapshot.
38 39 40 |
# File 'lib/cuboid/snapshot.rb', line 38 def location @location end |
Class Method Details
.dump(location) ⇒ String
Returns Location of the snapshot.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/cuboid/snapshot.rb', line 65 def dump( location ) FileUtils.rm_rf( location ) directory = get_temporary_directory FileUtils.rm_rf( directory ) FileUtils.mkdir_p( directory ) begin Data.dump( "#{directory}/data/" ) State.dump( "#{directory}/state/" ) compress directory, location # Append metadata to the end of the file. = Marshal.dump( ) File.open( location, 'ab' ) do |f| f.write [, .size].pack( 'a*N' ) end location ensure FileUtils.rm_rf( directory ) end end |
.load(snapshot) ⇒ Snapshot
Returns ‘self`.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/cuboid/snapshot.rb', line 99 def load( snapshot ) directory = get_temporary_directory @location = snapshot @metadata = ( snapshot ) extract( snapshot, directory ) Data.load( "#{directory}/data/" ) State.load( "#{directory}/state/" ) self ensure # Don't delete the directory immediately because there are disk DBs that # use those files. Kernel.at_exit do FileUtils.rm_rf( directory ) end end |
.read_metadata(snapshot) ⇒ Hash
Returns Metadata associated with the given snapshot.
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/cuboid/snapshot.rb', line 128 def ( snapshot ) File.open( snapshot, 'rb' ) do |f| f.seek -4, IO::SEEK_END = f.read( 4 ).unpack( 'N' ).first f.seek --4, IO::SEEK_END Marshal.load( f.read( ) ) end rescue => e ne = Error::InvalidFile.new( "Invalid snapshot: #{snapshot} (#{e})" ) ne.set_backtrace e.backtrace raise ne end |
.reset ⇒ Object
40 41 42 43 |
# File 'lib/cuboid/snapshot.rb', line 40 def reset @metadata = nil @location = nil end |
.restored? ⇒ Bool
Returns ‘true` if this is a restored snapshot, `false` otherwise.
47 48 49 |
# File 'lib/cuboid/snapshot.rb', line 47 def restored? !!location end |
.summary ⇒ Hash
Returns Snapshot summary information.
53 54 55 56 57 58 |
# File 'lib/cuboid/snapshot.rb', line 53 def summary { data: Data.statistics, state: State.statistics } end |