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



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

def location
  @location
end

.metadataHash



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

def 
  @metadata
end

Class Method Details

.dump(location) ⇒ String



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

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.

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



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

def restored?
    !!location
end

.summaryHash



53
54
55
56
57
58
# File 'lib/cuboid/snapshot.rb', line 53

def summary
    {
        data:  Data.statistics,
        state: State.statistics
    }
end