-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.go
88 lines (78 loc) · 2.57 KB
/
node.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
// Copyright (C) 2022 PuzzleDB Contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package coordinator
import (
"time"
"github.com/cybergarage/puzzledb-go/puzzledb/cluster"
"github.com/cybergarage/puzzledb-go/puzzledb/coordinator"
"github.com/google/uuid"
)
// NodeObject represents a store node state object.
type NodeObject struct {
ID string
Cluster string
Host string
Clock uint64
Time time.Time
Status string
}
// NewNodeWith returns a new node with the specified node object.
func NewNodeWith(obj *NodeObject) (cluster.Node, error) {
uuid, err := uuid.Parse(obj.ID)
if err != nil {
return nil, err
}
node := cluster.NewNode()
node.SetCluster(obj.Cluster)
node.SetID(uuid)
node.SetHost(obj.Host)
node.SetClock(obj.Clock)
node.SetTimestamp(obj.Time)
node.SetStatus(cluster.NewNodeStatusWith(obj.Status))
return node, nil
}
// NewNodeScanKey returns a new scan node key to get all node states.
func NewNodeScanKey() coordinator.Key {
return coordinator.NewKeyWith(coordinator.StateObjectKeyHeader[:], byte(NodeState))
}
// NewClusterScanKeyWith returns a new scan node key to get all node states with the specified cluster.
func NewClusterScanKeyWith(cluster string) coordinator.Key {
return coordinator.NewKeyWith(coordinator.StateObjectKeyHeader[:], byte(NodeState), cluster)
}
// NewNodeKeyWith returns a new node key with the specified node.
func NewNodeKeyWith(node cluster.Node) coordinator.Key {
return coordinator.NewKeyWith(coordinator.StateObjectKeyHeader[:], byte(NodeState), node.Cluster(), node.ID().String())
}
// NewNodeObject returns a new node object.
func NewNodeObject() *NodeObject {
return &NodeObject{
Cluster: "",
ID: "",
Host: "",
Clock: 0,
Time: time.Now(),
Status: "",
}
}
// NewNodeObjectWith returns a new node object with the specified node.
func NewNodeObjectWith(node cluster.Node) *NodeObject {
return &NodeObject{
Cluster: node.Cluster(),
ID: node.ID().String(),
Host: node.Host(),
Clock: uint64(node.Clock()),
Time: node.Timestamp(),
Status: node.Status().String(),
}
}