From e73efb4afc3bc91ddc49ac63f0f3db463d1f8cc1 Mon Sep 17 00:00:00 2001
From: 554325746@qq.com <554325746@qq.com>
Date: 星期五, 12 七月 2019 17:50:28 +0800
Subject: [PATCH] modify cam to Cam

---
 dbpersoninfo.go |  105 +++++++++++++++++++++++++++++++---------------------
 1 files changed, 63 insertions(+), 42 deletions(-)

diff --git a/dbpersoninfo.go b/dbpersoninfo.go
index b42ab0d..5832070 100644
--- a/dbpersoninfo.go
+++ b/dbpersoninfo.go
@@ -7,9 +7,10 @@
     "sync"
     "encoding/base64"
 
-	"basic.com/pubsub/cache.git/esutil"
+    "encoding/json"
+
+    "basic.com/pubsub/cache.git/esutil"
     "basic.com/pubsub/cache.git/shardmap"
-    "basic.com/pubsub/protomsg.git"
 
     "basic.com/valib/gosdk.git"
 )
@@ -17,87 +18,107 @@
 var querynum = flag.Int("querynum", 10, "the query number from database")
 var threadnum = flag.Int("threadnum",32, "the number of thread to deal data.")
 
-var Cmap *shardmap.ShardMap
+type CmapItem struct {
+    sync.Mutex
+    Cam map[string]*shardmap.ShardMap
+}
+
+var Cmap *CmapItem
+
 func Init(){
     flag.Parse()
     gosdk.InitFaceExtractor(16, 0)
 
-    Cmap = shardmap.New(uint8(*threadnum))
+    Cmap = &CmapItem{
+        Cam: make(map[string]*shardmap.ShardMap),
+    }
 
     temptime := time.Now()
     var wg sync.WaitGroup
+
     for i:=0; i<*threadnum; i++ {
         j := i*(*querynum)
         wg.Add(1)
         go func(qs int){
             defer wg.Done()
-
-            escache, err := esutil.DbPersoninfos(qs, *querynum)
+            escache, err := esutil.DbPersoninfos(qs, *querynum) 
             if err != nil {
                 fmt.Println(err)
                 return
             }
 
-        	for _, value := range escache {
-                 Cmap.Set(value.FaceFeature,value) 
+            Cmap.Lock()           
+
+        	for _, value := range escache{
+                if _, ok :=Cmap.Cam[value.Tableid]; !ok {
+                   Cmap.Cam[value.Tableid]=shardmap.New(uint8(*threadnum))        
+                }
+                
+                Cmap.Cam[value.Tableid].Set(value.Id,value.FaceFeature) 
         	}
+
+            Cmap.Unlock()
+
         }(j)
     }
     wg.Wait()
-    fmt.Println("get number of es: ", Cmap.GetLen())
     fmt.Println("time of get data from es.", time.Since(temptime))
     fmt.Println()
 }
 
-//func main(){
-//    Init()
-//    Getdbpersonmsg("")
-//}
+func Getdbpersonmsg(tableid string, teststring []byte, IsCompare bool) ([]byte) {
 
-func Getdbpersonmsg(teststring string, IsCompare bool) ([]byte) {
-
-    var binfos protomsg.Binfos
+    totalmap := make(map[string]float32)
 
     if !IsCompare {
         return nil
     }
 
-    if teststring == "" {
-    testcache, _:= esutil.DbPersoninfos(0,1)
-    teststring = testcache[0].FaceFeature
-    }
-
-    fmt.Println()
-    fmt.Println("======鍗庝附鐨勫垎鍓茬嚎============")
-    fmt.Println()
-
-    firsttime := time.Now()
-    Rscore := Cmap.Walk(Printest, teststring)
-    
-    binfos.Infos = append(binfos.Infos, Rscore...)
-    binfosbytes, err := binfos.Marshal()
-    if err != nil  {
-        fmt.Println(err)
+    if teststring == nil {
         return nil 
     }
+
+    if tableid == "" {
+        for id, val := range Cmap.Cam{
+            fmt.Println("the id is: ", id)
+            tmpmap := val.Walk(Printest, teststring)
+            for key, sec := range tmpmap {
+                totalmap[key] = sec
+            }
+        }
+    }else{
+       for id, value := range Cmap.Cam{
+            if id == tableid{
+                fmt.Println("the id is: ", id)
+                tmpmap :=value.Walk(Printest, teststring)
+                for key, sec := range tmpmap {
+                    totalmap[key]= sec
+                }
+                break
+            }
+       }
+    }
+
+
+    firsttime := time.Now()
     fmt.Println(time.Since(firsttime))
-    return binfosbytes
+
+    buf, err := json.Marshal(totalmap)
+    if err != nil {
+        fmt.Println("map to json error!", err)
+         return nil  
+    }
+    return  buf 
 }
 
-func Printest(ci, co string ) (float32){
+func Printest(ci []byte, co string ) (float32){
 
-    //ci_d, err := base64.StdEncoding.DecodeString(ci)
-    //if err != nil {
-    //    fmt.Println("ci_d : error : ", err)
-    //    return -1
-    //}
-
-    co_d, err := base64.StdEncoding.DecodeString(co)
+   co_d, err := base64.StdEncoding.DecodeString(co)
     if err != nil {
         fmt.Println("co_d : error : ", err)
         return -1
     }
 
-    sec := gosdk.FaceCompare([]byte(ci), co_d)
+    sec := gosdk.FaceCompare(ci, co_d)
     return sec
 }

--
Gitblit v1.8.0