zhangmeng
2024-03-26 883d8cfee0096974433fe4cbeac1a7a4ee86d9a3
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//ref https://github.com/DeanThompson/syncmap/blob/master/syncmap.go
 
package shardmap
 
import (
    "basic.com/pubsub/protomsg.git"
    "sync"
    //"encoding/json"
    //"fmt"
    "time"
)
 
// var commonmux sync.Mutex
const (
    BKDR_SEED = 131 // 31 131 1313 13131 131313 etc...
)
 
type shardItem struct {
    sync.RWMutex
    data map[string]interface{}
}
 
type ShardMap struct {
    shardCnt uint8
    shards   []*shardItem
    lasttime time.Time
}
 
var Count = make(chan int)
 
type wfOp func(a []byte, b string) float32
 
/**
* @param uint8, shardCnt must be pow of two
 */
func New(shardCnt uint8) *ShardMap {
 
    s := &ShardMap{
        shardCnt: shardCnt,
        shards:   make([]*shardItem, shardCnt),
        lasttime: time.Now(),
    }
 
    for i, _ := range s.shards {
        s.shards[i] = &shardItem{
            data: make(map[string]interface{}),
        }
    }
 
    return s
}
 
func (s *ShardMap) Get(key string) (interface{}, bool) {
    si := s.locate(key)
 
    si.RLock()
    value, ok := si.data[key]
    si.RUnlock()
    return value, ok
}
 
func (s *ShardMap) Gettime() time.Time {
    tm := s.lasttime
    return tm
}
 
func (s *ShardMap) Settime() {
    s.lasttime = time.Now()
}
 
func (s *ShardMap) Set(key string, value interface{}) {
    si := s.locate(key)
 
    si.Lock()
    si.data[key] = value
    si.Unlock()
}
 
func (s *ShardMap) Del(key string) {
    si := s.locate(key)
 
    si.Lock()
    delete(si.data, key)
    si.Unlock()
}
 
type kvItem struct {
    key   string
    value interface{}
}
 
func (s *ShardMap) Walk(wf wfOp, sourceFea []byte, baseScore float32, isWebComp bool, target string) (targets []*protomsg.SdkCompareEach) {
    var wg sync.WaitGroup
    var lock sync.Mutex
    for _, si := range s.shards {
        var tempsi shardItem = *si
 
        if len(tempsi.data) == 0 {
            continue
        }
 
        wg.Add(1)
 
        go func(st *shardItem, fw wfOp, sf []byte, baseSec float32, isWeb bool) {
            defer wg.Done()
            for _, feature := range st.data {
                if eif, ok := feature.(*protomsg.Esinfo); ok {
                    if !isWeb { //不会比对抓拍库,只比对有效的人
                        if eif.Enable == 1 {
                            score := float32(0)
                            if target == "car" {
                                if eif.CarNo != "" {
                                    score = strComp(sf, eif.CarNo)
                                } else {
                                    continue
                                }
                            } else {
                                score = fw(sf, eif.FaceFeature)
                            }
                            if score > 0 && score >= baseScore {
                                lock.Lock()
                                targets = append(targets, &protomsg.SdkCompareEach{
                                    Id:           eif.Id,
                                    CompareScore: score,
                                    Tableid:      eif.Tableid,
                                })
                                lock.Unlock()
                            }
                        }
                    } else { //来源是web,会比对抓拍库,不管是否有效都需要比对
                        score := float32(0)
                        if target == "car" {
                            if eif.CarNo != "" {
                                score = strComp(sf, eif.CarNo)
                            } else {
                                continue
                            }
                        } else {
                            score = fw(sf, eif.FaceFeature)
                        }
                        if score > 0 && score >= baseScore {
                            lock.Lock()
                            targets = append(targets, &protomsg.SdkCompareEach{
                                Id:           eif.Id,
                                CompareScore: score,
                                Tableid:      eif.Tableid,
                            })
                            lock.Unlock()
                        }
                    }
                }
            }
 
        }(&tempsi, wf, sourceFea, baseScore, isWebComp)
    }
 
    wg.Wait()
    return targets
 
}
 
// print all
func (s *ShardMap) Printall() (infos []interface{}) {
    var wg sync.WaitGroup
 
    for _, si := range s.shards {
        wg.Add(1)
        go func(s *shardItem) {
            defer wg.Done()
            s.RLock()
            for _, value := range s.data {
                infos = append(infos, value)
            }
 
            s.RUnlock()
        }(si)
    }
    wg.Wait()
    return
}
 
func (s *ShardMap) GetLen() int {
    var slen int
    for i := 0; i < int(s.shardCnt); i++ {
        slen += len(s.shards[i].data)
    }
    return slen
}
 
func (s *ShardMap) locate(key string) *shardItem {
    i := bkdrHash(key) & uint32(s.shardCnt-1)
 
    return s.shards[i]
}
 
// https://www.byvoid.com/blog/string-hash-compare/
func bkdrHash(str string) uint32 {
    var h uint32
 
    for _, c := range str {
        h = h*BKDR_SEED + uint32(c)
    }
 
    return h
}
 
func strComp(source []byte, target string) float32 {
    sourceStr := string(source)
    if sourceStr == target {
        return 100
    } else {
        return 0
    }
}