Class: Cuboid::Support::Database::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/cuboid/support/database/base.rb

Overview

This class is abstract.

Base class for Database data structures

Provides helper methods for data structures to be implemented related to objecting dumping, loading, unique filename generation, etc.

Direct Known Subclasses

CategorizedQueue, Hash, Queue

Constant Summary collapse

DISK_SPACE_FILE =
'Database_disk_space'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.

Parameters:

  • options (Hash) (defaults to: {})

    Any object that responds to ‘dump’ and ‘load’.



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cuboid/support/database/base.rb', line 77

def initialize( options = {} )
    @options = options.dup

    @options[:dumper] ||= Marshal
    @options[:loader] ||= @options[:dumper]

    @filename_counter = 0

    at_exit do
        clear
    end
end

Class Method Details

.decrement_disk_space(int) ⇒ Object



27
28
29
# File 'lib/cuboid/support/database/base.rb', line 27

def decrement_disk_space( int )
    set_disk_space @@disk_space - int
end

.disk_directoryObject



35
36
37
# File 'lib/cuboid/support/database/base.rb', line 35

def disk_directory
    Options.paths.tmpdir
end

.disk_spaceObject



31
32
33
# File 'lib/cuboid/support/database/base.rb', line 31

def disk_space
    @@disk_space
end

.disk_space_fileObject



39
40
41
# File 'lib/cuboid/support/database/base.rb', line 39

def disk_space_file
    disk_space_file_for Process.pid
end

.disk_space_file_for(pid) ⇒ Object



49
50
51
# File 'lib/cuboid/support/database/base.rb', line 49

def disk_space_file_for( pid )
    "#{Options.paths.tmp_dir_for( pid )}/#{DISK_SPACE_FILE}"
end

.disk_space_for(pid) ⇒ Object



43
44
45
46
47
# File 'lib/cuboid/support/database/base.rb', line 43

def disk_space_for( pid )
    return 0 if !Dir.exists?( Options.paths.tmp_dir_for( pid ) )

    IO.read( disk_space_file_for( pid ) ).to_i
end

.increment_disk_space(int) ⇒ Object



23
24
25
# File 'lib/cuboid/support/database/base.rb', line 23

def increment_disk_space( int )
    set_disk_space @@disk_space + int
end

.resetObject



18
19
20
21
# File 'lib/cuboid/support/database/base.rb', line 18

def reset
    @@disk_space = 0
    set_disk_space @@disk_space
end

Instance Method Details

#serialize(obj) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/cuboid/support/database/base.rb', line 90

def serialize( obj )
    compress(
        @options[:dumper].respond_to?( :dump ) ?
            @options[:dumper].dump( obj ) :
            @options[:dumper].call( obj )
    )
end

#unserialize(data) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/cuboid/support/database/base.rb', line 98

def unserialize( data )
    data = decompress( data )

    @options[:loader].respond_to?( :load ) ?
        @options[:loader].load( data ) :
        @options[:loader].call( data )
end