wcc 1 年之前
父節點
當前提交
7ed4985262
共有 3 個文件被更改,包括 102 次插入93 次删除
  1. 96 89
      wuhu_data/dfa.go
  2. 1 1
      wuhu_data/init.go
  3. 5 3
      wuhu_data/main.go

+ 96 - 89
wuhu_data/dfa.go

@@ -1,89 +1,96 @@
-package main
-
-// 多叉树
-type (
-	//MultiTreeNode 多叉树节点
-	MultiTreeNode struct {
-		Key      rune
-		End      bool
-		Children map[rune]*MultiTreeNode
-	}
-	//MultiTree 多叉树
-	MultiTree struct {
-		Root *MultiTreeNode
-	}
-	//结果
-	Result struct {
-		Data       string
-		Start, End int
-	}
-)
-
-func NewMultiTree() *MultiTree {
-	return &MultiTree{
-		Root: &MultiTreeNode{
-			Children: make(map[rune]*MultiTreeNode),
-		},
-	}
-}
-
-func (mt *MultiTree) Add(segment string) {
-	path := []rune(segment)
-	node := mt.Root
-	pathLen := len(path)
-	for index, t := range path {
-		end := index == pathLen-1
-		if v, ok := node.Children[t]; ok {
-			node = v
-			if end { //后加入的词(可能是老词的子集),需要修正属性
-				node.End = end
-			}
-		} else {
-			leafNode := &MultiTreeNode{
-				Key:      t,
-				Children: make(map[rune]*MultiTreeNode),
-			}
-			if end { //是结束节点
-				leafNode.End = true
-			}
-			node.Children[t] = leafNode
-			node = leafNode
-		}
-	}
-}
-
-// Match 匹配 / 查找
-func (mt *MultiTree) Match(segment string, maximum bool) []*Result {
-	var wordNode *MultiTreeNode
-	node := mt.Root
-	var ok bool
-	path := []rune(segment)
-	ret := make([]*Result, 0, 0)
-	pos := 0
-	//找第一层
-	for pos < len(path) {
-		if wordNode, ok = node.Children[path[pos]]; !ok {
-			pos += 1
-			continue
-		}
-		skipStep := 1
-		for w := pos + 1; w < len(path); w++ {
-			if wordNode, ok = wordNode.Children[path[w]]; !ok {
-				break
-			}
-			if wordNode.End && !maximum {
-				return []*Result{
-					&Result{
-						Data: string(path[pos : w+1]), Start: pos, End: w + 1},
-				}
-			} else if wordNode.End {
-				ret = append(ret, &Result{
-					Data: string(path[pos : w+1]), Start: pos, End: w + 1})
-				skipStep = w - pos
-			}
-		}
-		pos += skipStep
-
-	}
-	return ret
-}
+package main
+
+import "sync"
+
+// 多叉树
+type (
+	//MultiTreeNode 多叉树节点
+	MultiTreeNode struct {
+		Key      rune
+		End      bool
+		Children map[rune]*MultiTreeNode
+	}
+	//MultiTree 多叉树
+	MultiTree struct {
+		Root *MultiTreeNode
+		Lock sync.Mutex // 添加互斥锁
+	}
+	//结果
+	Result struct {
+		Data       string
+		Start, End int
+	}
+)
+
+func NewMultiTree() *MultiTree {
+	return &MultiTree{
+		Root: &MultiTreeNode{
+			Children: make(map[rune]*MultiTreeNode),
+		},
+	}
+}
+
+func (mt *MultiTree) Add(segment string) {
+	mt.Lock.Lock()
+	defer mt.Lock.Unlock()
+	path := []rune(segment)
+	node := mt.Root
+	pathLen := len(path)
+	for index, t := range path {
+		end := index == pathLen-1
+		if v, ok := node.Children[t]; ok {
+			node = v
+			if end { //后加入的词(可能是老词的子集),需要修正属性
+				node.End = end
+			}
+		} else {
+			leafNode := &MultiTreeNode{
+				Key:      t,
+				Children: make(map[rune]*MultiTreeNode),
+			}
+			if end { //是结束节点
+				leafNode.End = true
+			}
+			node.Children[t] = leafNode
+			node = leafNode
+		}
+	}
+}
+
+// Match 匹配 / 查找
+func (mt *MultiTree) Match(segment string, maximum bool) []*Result {
+	mt.Lock.Lock()
+	defer mt.Lock.Unlock()
+	var wordNode *MultiTreeNode
+	node := mt.Root
+	var ok bool
+	path := []rune(segment)
+	ret := make([]*Result, 0, 0)
+	pos := 0
+	//找第一层
+	for pos < len(path) {
+		if wordNode, ok = node.Children[path[pos]]; !ok {
+			pos += 1
+			continue
+		}
+		skipStep := 1
+		for w := pos + 1; w < len(path); w++ {
+			if wordNode, ok = wordNode.Children[path[w]]; !ok {
+				break
+			}
+			if wordNode.End && !maximum {
+				return []*Result{
+					&Result{
+						Data: string(path[pos : w+1]), Start: pos, End: w + 1},
+				}
+			} else if wordNode.End {
+				ret = append(ret, &Result{
+					Data: string(path[pos : w+1]), Start: pos, End: w + 1})
+				skipStep = w - pos
+			}
+		}
+		pos += skipStep
+
+	}
+	return ret
+}

+ 1 - 1
wuhu_data/init.go

@@ -87,7 +87,7 @@ func loadData() {
 	now := time.Now()
 	now := time.Now()
 	sess := Mgo.GetMgoConn()
 	sess := Mgo.GetMgoConn()
 	defer Mgo.DestoryMongoConn(sess)
 	defer Mgo.DestoryMongoConn(sess)
-	it := sess.DB("mixdata").C("qyxy_wuhu").Find(nil).Iter()
+	it := sess.DB("mixdata").C(GF.Env.QyxyColl).Find(nil).Iter()
 	index := 0
 	index := 0
 	for tmp := make(map[string]interface{}); it.Next(&tmp); index++ {
 	for tmp := make(map[string]interface{}); it.Next(&tmp); index++ {
 		if index%10000 == 0 {
 		if index%10000 == 0 {

+ 5 - 3
wuhu_data/main.go

@@ -69,11 +69,13 @@ func processUdpMsg(act byte, data []byte, ra *net.UDPAddr) {
 			tasktype, _ := mapInfo["stype"].(string)
 			tasktype, _ := mapInfo["stype"].(string)
 			switch tasktype {
 			switch tasktype {
 			case "inc_data": //每天增量bidding数据
 			case "inc_data": //每天增量bidding数据
-				loadIncBidding()
+				go loadIncBidding()
 			case "inc_qyxy": //每周企业数据
 			case "inc_qyxy": //每周企业数据
-				loadIncQyxy()
+				go loadIncQyxy()
 			case "alldata": // 芜湖存量bidding数据
 			case "alldata": // 芜湖存量bidding数据
-				biddingAllData()
+				go biddingAllData()
+			default:
+				log.Info("processUdpMsg", zap.String("tasktype", tasktype))
 			}
 			}
 
 
 		}
 		}