Class: Daybreak::Journal Private

Inherits:
Queue
  • Object
show all
Defined in:
lib/daybreak/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.

Daybreak::Journal 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.



8
9
10
11
12
13
14
15
# File 'lib/daybreak/journal.rb', line 8

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.



6
7
8
# File 'lib/daybreak/journal.rb', line 6

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.



6
7
8
# File 'lib/daybreak/journal.rb', line 6

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



85
86
87
# File 'lib/daybreak/journal.rb', line 85

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



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/daybreak/journal.rb', line 51

def clear
  flush
  with_tmpfile do |path, file|
    file.write(@format.header)
    file.close
    # Clear replaces the database file like a compactification does
    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



23
24
25
26
27
28
# File 'lib/daybreak/journal.rb', line 23

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:

  • (Boolean)


18
19
20
# File 'lib/daybreak/journal.rb', line 18

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/daybreak/journal.rb', line 65

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
      # Database was replaced (cleared or compactified) in the meantime
      if @pos != nil
        # Append changed journal records if the database changed during compactification
        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



31
32
33
34
# File 'lib/daybreak/journal.rb', line 31

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



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/daybreak/journal.rb', line 37

def lock
  # Flush everything to start with a clean state
  # and to protect the @locked variable
  flush

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