/
data.go
83 lines (76 loc) · 2.1 KB
/
data.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
/*
* Copyright (c) 2020. Aberic - All Rights Reserved.
*
* 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 raft4go
import (
"fmt"
"github.com/aberic/gnomon"
"sort"
"sync"
)
// 数据详细对象
type dataInfo struct {
value []byte // 数据值
hash string // value数据的散列值
version int32
lock sync.RWMutex
}
type data struct {
dataMap map[string]*dataInfo // 数据详细对象集合
hash string // 所有数据集hash,用于比较彼此数据
lock sync.RWMutex
hashLock sync.Mutex
}
func (d *data) put(key string, value []byte, version int32) {
if _, ok := d.dataMap[key]; ok {
if d.dataMap[key].version < version {
defer d.dataMap[key].lock.Unlock()
d.dataMap[key].lock.Lock()
d.dataMap[key].value = value
d.dataMap[key].version = version
d.dataMap[key].hash = gnomon.HashMD5Bytes(value)
}
} else {
defer d.lock.Unlock()
d.lock.Lock()
d.dataMap[key] = &dataInfo{
lock: sync.RWMutex{},
value: value,
version: version,
hash: gnomon.HashMD5Bytes(value),
}
}
d.updateHash()
}
func (d *data) get(key string) (dataInfo *dataInfo, err error) {
if _, ok := d.dataMap[key]; ok {
defer d.dataMap[key].lock.RUnlock()
d.dataMap[key].lock.RLock()
return d.dataMap[key], nil
}
return nil, fmt.Errorf("value of key is not exist")
}
func (d *data) updateHash() {
defer d.hashLock.Unlock()
d.hashLock.Lock()
var vs []string
for _, v := range d.dataMap {
vs = append(vs, v.hash)
}
sort.Strings(vs)
var hashStr string
for _, v := range vs {
hashStr = gnomon.StringBuild(hashStr, v)
}
d.hash = gnomon.HashMD5(hashStr)
}