File: fifo.lua

package info (click to toggle)
tarantool 1.9.1.26.g63eb81e3c-1.1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 38,724 kB
  • sloc: ansic: 247,425; cpp: 24,952; sh: 17,809; python: 10,699; makefile: 2,682
file content (32 lines) | stat: -rw-r--r-- 917 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
-- {name, top, bottom, fifo...}
fifomax = 5
function find_or_create_fifo(space, name)
    local fifo = space:get{name}
    if fifo == nil then
        fifo = {}
        for i = 1, fifomax do table.insert(fifo, 0) end
        fifo = space:insert{name, 4, 4, unpack(fifo)}
    end
    return fifo
end
function fifo_push(space, name, val)
    local fifo = find_or_create_fifo(space, name)
    local top = fifo[2]
    local bottom = fifo[3]
    if top == fifomax+3 then -- % size
        top = 4
    elseif top ~= bottom then -- was not empty
        top = top + 1
    end
    if bottom == fifomax + 3 then -- % size
        bottom = 4
    elseif bottom == top then
        bottom = bottom + 1
    end
    return space:update({name}, {{'=', 2, top}, {'=', 3, bottom }, {'=', top, val}})
end
function fifo_top(space, name)
    local fifo = find_or_create_fifo(space, name)
    local top = fifo[2]
    return fifo[top]
end