-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradix_time.go
103 lines (79 loc) · 2.84 KB
/
radix_time.go
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (C) 2024 Thierry Fournier <tfournier@arpalert.org>
package radix
import "encoding/binary"
import "time"
const time_length = 64
func time_to_key(value time.Time)([]byte) {
var bytes [8]byte
binary.BigEndian.PutUint64(bytes[:], uint64(value.UnixMicro()))
return bytes[:]
}
// TimeGet gets a time.Time prefix and return exact match of the prefix. Exact match
// is a node wich match the prefix bit and the length. Note the tree precision is microsecond
func (r *Radix)TimeGet(value time.Time)(*Node) {
var key []byte
/* Get the network width. width of 0 id prohibited */
key = time_to_key(value)
/* Perform lookup */
return r.Get(&key, time_length)
}
// In the case of any entry match, TimeLookupAfterEq return the time
// after or equal closest value of the time key. If the tree is
// empty or after value not exists, return nil
func (r *Radix)TimeLookupAfterEq(value time.Time)(*Node) {
var key []byte
/* Get the network width. width of 0 id prohibited */
key = time_to_key(value)
/* Perform lookup */
return r.LookupGe(&key, length)
}
// In the case of any entry match, TimeLookupBeforeEq return the time
// before or equal closest value of the time key. If the tree is
// empty or before value not exists, return nil
func (r *Radix)TimeLookupBeforeEq(value time.Time)(*Node) {
var key []byte
/* Get the network width. width of 0 id prohibited */
key = time_to_key(value)
/* Perform lookup */
return r.LookupGe(&key, length)
}
// TimeInsert time.Time prefix in the tree. The tree accept only unique value, if
// the prefix already exists in the tree, return existing leaf,
// otherwise return nil. Note the tree precision is microsecond
func (r *Radix)TimeInsert(value time.Time, data interface{})(*Node, bool) {
var key []byte
/* Get the network width. width of 0 id prohibited */
key = time_to_key(value)
/* Perform insert */
return r.Insert(&key, time_length, data)
}
// TimeDelete lookup time.Time and remove it. does nothing
// if the network not exists. Note the tree precision is microsecond
func (r *Radix)TimeDelete(value time.Time)() {
var node *Node
var key []byte
/* Get the network width. width of 0 id prohibited */
key = time_to_key(value)
/* Perform lookup */
node = r.Get(&key, time_length)
if node == nil {
return
}
/* Delete entry */
r.Delete(node)
}
// TimeGetValue convert node key/length prefix to time.Time data. Note the
// tree precision is microsecond
func (n *Node)TimeGetValue()(time.Time) {
if len(n.node.Bytes) != 8 {
return time.Time{}
}
return time.UnixMicro(int64(binary.BigEndian.Uint64([]byte(n.node.Bytes))))
}
// UInt64NewIter return struct Iter for browsing all nodes there children
// match the key/length prefix. Note the tree precision is microsecond
func (r *Radix)TimeNewIter(value time.Time)(*Iter) {
var key []byte
key = time_to_key(value)
return r.NewIter(&key, time_length)
}