Class: Garcon::Stash::Journal Private

Inherits:
Queue show all
Defined in:
lib/garcon/stash/journal.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Stash::Io handles background io, compaction and is the arbiter of multiprocess safety.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Queue

#<<, #first, #flush, #pop

Constructor Details

#initialize(file, format, serializer, &block) ⇒ Journal

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Journal.



29
30
31
32
33
34
35
36
# File 'lib/garcon/stash/journal.rb', line 29

def initialize(file, format, serializer, &block)
  super()
  @file, @format, @serializer, @emit = file, format, serializer, block
  open
  @worker = Thread.new(&method(:worker))
  @worker.priority = -1
  load
end

Instance Attribute Details

#fileObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



27
28
29
# File 'lib/garcon/stash/journal.rb', line 27

def file
  @file
end

#sizeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



27
28
29
# File 'lib/garcon/stash/journal.rb', line 27

def size
  @size
end

Instance Method Details

#bytesizeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return byte size of journal



107
108
109
# File 'lib/garcon/stash/journal.rb', line 107

def bytesize
  @fd.stat.size
end

#clearObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Clear the database log and yield



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/garcon/stash/journal.rb', line 74

def clear
  flush
  with_tmpfile do |path, file|
    file.write(@format.header)
    file.close
    with_flock(File::LOCK_EX) do
      File.rename(path, @file)
    end
  end
  open
end

#closeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Clear the queue and close the file handler



46
47
48
49
50
51
# File 'lib/garcon/stash/journal.rb', line 46

def close
  self << nil
  @worker.join
  @fd.close
  super
end

#closed?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Is the journal closed?

Returns:



40
41
42
# File 'lib/garcon/stash/journal.rb', line 40

def closed?
  @fd.closed?
end

#compactObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Compact the logfile to represent the in-memory state



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/garcon/stash/journal.rb', line 88

def compact
  load
  with_tmpfile do |path, file|
    # Compactified database has the same size -> return
    return self if @pos == file.write(dump(yield, @format.header))
    with_flock(File::LOCK_EX) do
      if @pos != nil
        file.write(read)
        file.close
        File.rename(path, @file)
      end
    end
  end
  open
  replay
end

#loadObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Load new journal entries



55
56
57
58
# File 'lib/garcon/stash/journal.rb', line 55

def load
  flush
  replay
end

#lockObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Lock the logfile across thread and process boundaries



62
63
64
65
66
67
68
69
70
# File 'lib/garcon/stash/journal.rb', line 62

def lock
  flush
  with_flock(File::LOCK_EX) do
    replay
    result = yield
    flush
    result
  end
end