Class: DbmsBuffers::ClockBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/dbms_buffers/clock.rb

Overview

The clock buffer, also known as second-change buffer, arranges entries in a logical clock and a single hand. When the buffer is full and a not-yet-inserted entry needs space, the hand goes clockwise until it finds an entry with clock_value = 0. This entry is evicted, removed from the buffer, and the new values is inserted with clock_value 1. All entries the hand passes where clock_value = 1, this value is decremented, but they are not replaced yet (they have a second chance). When an element is accessed that has clock_value = 0, this value is set to 1 again.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ ClockBuffer

Returns a new instance of ClockBuffer.



18
19
20
21
22
# File 'lib/dbms_buffers/clock.rb', line 18

def initialize(size)
  @size = size
  @buffer = []
  @pointer = 0
end

Instance Attribute Details

#pointerObject (readonly)

Returns the value of attribute pointer.



16
17
18
# File 'lib/dbms_buffers/clock.rb', line 16

def pointer
  @pointer
end

#sizeObject (readonly)

Returns the value of attribute size.



16
17
18
# File 'lib/dbms_buffers/clock.rb', line 16

def size
  @size
end

Instance Method Details

#access(value) ⇒ Object



24
25
26
# File 'lib/dbms_buffers/clock.rb', line 24

def access(value)
  try_touch(value) || try_insert_new(value) || try_replace(value)
end

#clock_value_of(value) ⇒ Object



28
29
30
# File 'lib/dbms_buffers/clock.rb', line 28

def clock_value_of(value)
  @buffer[index(value)].clock_value
end

#contains?(value) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/dbms_buffers/clock.rb', line 36

def contains?(value)
  @buffer.any? { |entry| value == entry.value }
end

#entriesObject



32
33
34
# File 'lib/dbms_buffers/clock.rb', line 32

def entries
  @buffer.clone
end

#usedObject



40
41
42
# File 'lib/dbms_buffers/clock.rb', line 40

def used
  @buffer.size
end