-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueueitem.go
More file actions
83 lines (69 loc) · 2.19 KB
/
queueitem.go
File metadata and controls
83 lines (69 loc) · 2.19 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package hlfq
import (
"fmt"
"reflect"
"time"
"github.com/oklog/ulid/v2"
)
const queuePointerTypeName = "queuePointer"
// QueuePointer holds a key pointing to another state
type QueuePointer struct {
PointerName string
PointerKey []string
}
// NewQueuePointer creates new QueuePointer (for Head or Tail)
func NewQueuePointer(name string) *QueuePointer {
return &QueuePointer{PointerName: name}
}
// NewQueueHeadPointer creates a QueuePointer for HEAD
func NewQueueHeadPointer() *QueuePointer {
return &QueuePointer{PointerName: "HeadPointer"}
}
// NewQueueTailPointer creates a QueuePointer for TAIL
func NewQueueTailPointer() *QueuePointer {
return &QueuePointer{PointerName: "TailPointer"}
}
// Key for QueuePointer entry in chaincode state
func (qp QueuePointer) Key() ([]string, error) {
s := []string{queuePointerTypeName}
s = append(s, qp.PointerName)
return s, nil
}
// QueueItemSpec chaincode method argument
type QueueItemSpec struct {
From string `json:"From"`
To string `json:"To"`
Amount int `json:"Amount"`
ExtraData []byte `json:"ExtraData"`
}
// QueueItem struct for chaincode state
type QueueItem struct {
// Queue sevice data
ID ulid.ULID `json:"ID"`
PrevKey []string `json:"PrevKey"`
NextKey []string `json:"NextKey"`
CreatedTime time.Time `json:"CreatedTime"` // set by chaincode method
// Item Spec
From string `json:"From"`
To string `json:"To"`
Amount int `json:"Amount"`
ExtraData []byte `json:"ExtraData"`
}
// Key for QueueItem entry in chaincode state
func (qi QueueItem) Key() ([]string, error) {
return []string{queueItemKeyPrefix, qi.ID.String()}, nil
}
func (qi QueueItem) String() string {
return fmt.Sprintf("QueueItem{ ID: %s, PrevKey: %v, NextKey: %v, From: %s, To: %s, Amount: %d, ExtraData: %v }",
qi.ID.String(), qi.PrevKey, qi.NextKey, qi.From, qi.To, qi.Amount, qi.ExtraData)
}
func (qi QueueItem) hasNext() bool {
// fmt.Println("== hasNext ==")
// fmt.Printf("qi=%+v\n", qi)
return !reflect.DeepEqual(qi.NextKey, EmptyItemPointerKey)
}
func (qi QueueItem) hasPrev() bool {
// fmt.Println("== hasPrev ==")
// fmt.Printf("qi=%+v\n", qi)
return !reflect.DeepEqual(qi.PrevKey, EmptyItemPointerKey)
}