Class: Cuboid::Snapshot

Inherits:
Object show all
Defined in:
lib/cuboid/snapshot.rb

Overview

Stores and provides access to the state of the system.

Author:

Defined Under Namespace

Classes: Error

Constant Summary collapse

EXTENSION =
'csf'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.locationString

Returns Location of the loaded snapshot.

Returns:

  • (String)

    Location of the loaded snapshot.



38
39
40
# File 'lib/cuboid/snapshot.rb', line 38

def location
  @location
end

.metadataHash

Returns Metadata associated with the loaded snapshot.

Returns:

  • (Hash)

    Metadata associated with the loaded snapshot.



34
35
36
# File 'lib/cuboid/snapshot.rb', line 34

def 
  @metadata
end

Class Method Details

.dump(location) ⇒ String

Returns Location of the snapshot.

Parameters:

  • location (String)

    Location of the snapshot.

Returns:

  • (String)

    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`.

Parameters:

  • snapshot (String)

    Location of the snapshot to load.

Returns:

Raises:



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.

Parameters:

  • snapshot (String)

    Location of the snapshot.

Returns:

  • (Hash)

    Metadata associated with the given snapshot.

Raises:



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

.resetObject



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.

Returns:

  • (Bool)

    ‘true` if this is a restored snapshot, `false` otherwise.



47
48
49
# File 'lib/cuboid/snapshot.rb', line 47

def restored?
    !!location
end

.summaryHash

Returns Snapshot summary information.

Returns:

  • (Hash)

    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