-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchaincode_pop.go
More file actions
42 lines (37 loc) · 1.31 KB
/
chaincode_pop.go
File metadata and controls
42 lines (37 loc) · 1.31 KB
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
33
34
35
36
37
38
39
40
41
42
package hlfq
import (
"github.com/pkg/errors"
"github.com/s7techlab/cckit/router"
)
// queuePop read and delete the first queue item (the oldest, FIFO)
func queuePop(c router.Context) (extractedItem interface{}, err error) {
headPresent, _ := hasHead(c) // TODO: handle error
if !headPresent {
return extractedItem, errors.New("Empty queue")
}
headKey, _ := readHeadItemKey(c) // TODO: handle error
resHead, _ := c.State().Get(headKey, &QueueItem{}) // TODO: handle error
headItem := resHead.(QueueItem)
nextKey := EmptyItemPointerKey
// remove Prev link from nextItem if it exists
if headItem.hasNext() { // it's not a tail
// получить следующий элемент
nextKey = headItem.NextKey
// получить из State nextItem
resNext, _ := c.State().Get(nextKey, &QueueItem{}) // TODO: handle error
nextItem := resNext.(QueueItem)
// remove PrevKey from nextItem
nextItem.PrevKey = EmptyItemPointerKey
// save updated nextItem
c.State().Put(nextItem) // TODO: handle error
}
setHeadPointerTo(c, nextKey)
if isKeyEmpty(nextKey) { // reached a TailItem
setTailPointerTo(c, nextKey)
}
// remove extracted item from state
c.State().Delete(headKey) // TODO: handle error
//return item, c.State().Delete(item)
extractedItem = headItem
return extractedItem, nil
}