forked from aerospike/aerospike-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch_node.go
117 lines (98 loc) · 3.04 KB
/
batch_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2013-2017 Aerospike, Inc.
//
// 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 aerospike
import (
. "github.com/aerospike/aerospike-client-go/types"
)
type batchNode struct {
Node *Node
BatchNamespaces []*batchNamespace
KeyCapacity int
}
func newBatchNodeList(cluster *Cluster, policy *BasePolicy, keys []*Key) ([]*batchNode, error) {
nodes := cluster.GetNodes()
if len(nodes) == 0 {
return nil, NewAerospikeError(SERVER_NOT_AVAILABLE, "command failed because cluster is empty.")
}
nodeCount := len(nodes)
keysPerNode := len(keys)/nodeCount + 10
// Split keys by server node.
batchNodes := make([]*batchNode, 0, nodeCount)
for i, key := range keys {
partition := NewPartitionByKey(key)
// error not required
node, err := cluster.getReadNode(partition, policy.ReplicaPolicy)
if err != nil {
return nil, err
}
batchNode := findBatchNode(batchNodes, node)
if batchNode == nil {
batchNodes = append(batchNodes, newBatchNode(node, keysPerNode, key.Namespace(), i))
} else {
batchNode.AddKey(key.Namespace(), i)
}
}
return batchNodes, nil
}
func newBatchNode(node *Node, keyCapacity int, namespace string, offset int) *batchNode {
return &batchNode{
Node: node,
KeyCapacity: keyCapacity,
BatchNamespaces: []*batchNamespace{newBatchNamespace(&namespace, keyCapacity, offset)},
}
}
func (bn *batchNode) AddKey(namespace string, offset int) {
batchNamespace := bn.findNamespace(&namespace)
if batchNamespace == nil {
bn.BatchNamespaces = append(bn.BatchNamespaces, newBatchNamespace(&namespace, bn.KeyCapacity, offset))
} else {
batchNamespace.add(offset)
}
}
func (bn *batchNode) findNamespace(ns *string) *batchNamespace {
for _, batchNamespace := range bn.BatchNamespaces {
// Note: use both pointer equality and equals.
if batchNamespace.namespace == ns || *batchNamespace.namespace == *ns {
return batchNamespace
}
}
return nil
}
func findBatchNode(nodes []*batchNode, node *Node) *batchNode {
for i := range nodes {
// Note: using pointer equality for performance.
if nodes[i].Node == node {
return nodes[i]
}
}
return nil
}
type batchNamespace struct {
namespace *string
offsets []int
offsetSize int
}
func newBatchNamespace(namespace *string, capacity, offset int) *batchNamespace {
res := &batchNamespace{
namespace: namespace,
offsets: make([]int, 0, capacity),
offsetSize: 1,
}
res.offsets = append(res.offsets, offset)
return res
}
func (bn *batchNamespace) add(offset int) {
bn.offsets = append(bn.offsets, offset)
bn.offsetSize++
}