Class: DbmsBuffers::ClockBuffer
- Inherits:
-
Object
- Object
- DbmsBuffers::ClockBuffer
- 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
-
#pointer ⇒ Object
readonly
Returns the value of attribute pointer.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #access(value) ⇒ Object
- #clock_value_of(value) ⇒ Object
- #contains?(value) ⇒ Boolean
- #entries ⇒ Object
-
#initialize(size) ⇒ ClockBuffer
constructor
A new instance of ClockBuffer.
- #used ⇒ Object
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
#pointer ⇒ Object (readonly)
Returns the value of attribute pointer.
16 17 18 |
# File 'lib/dbms_buffers/clock.rb', line 16 def pointer @pointer end |
#size ⇒ Object (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
36 37 38 |
# File 'lib/dbms_buffers/clock.rb', line 36 def contains?(value) @buffer.any? { |entry| value == entry.value } end |
#entries ⇒ Object
32 33 34 |
# File 'lib/dbms_buffers/clock.rb', line 32 def entries @buffer.clone end |
#used ⇒ Object
40 41 42 |
# File 'lib/dbms_buffers/clock.rb', line 40 def used @buffer.size end |