Pārlūkot izejas kodu

初始化 = 创建索引

zhengkun 3 gadi atpakaļ
vecāks
revīzija
eb2a074bfb
2 mainītis faili ar 32 papildinājumiem un 9 dzēšanām
  1. 26 4
      work_repeat/src/main.go
  2. 6 5
      work_repeat/src/mgo.go

+ 26 - 4
work_repeat/src/main.go

@@ -3,14 +3,14 @@ package main
 
 import (
 	"fmt"
+	"gopkg.in/mgo.v2"
 	"log"
 	"os"
 	qu "qfw/util"
 	"regexp"
 	"strings"
-	//"time"
+	"time"
 )
-
 var zhb_key_list = []string{"budget", "buyer", "agency", "s_winner", "bidamount", "projectcode", "contractcode"}
 var packreg *regexp.Regexp
 var Mgo *MongodbSim
@@ -33,9 +33,9 @@ type dataSource struct {
 var addr, dbname, table,  sortType string
 var cycle int64
 var sysconfig    map[string]interface{} 	//配置文件
+
 func initConfig()  {
 	qu.ReadConfig(&sysconfig)
-
 	addr =  sysconfig["mgo_addr"].(string)
 	dbname =  sysconfig["mgo_db"].(string)
 	table =  sysconfig["mgo_table"].(string)
@@ -43,20 +43,41 @@ func initConfig()  {
 	month := qu.IntAllDef(sysconfig["cycle_month"],6)
 	cycle = qu.Int64All(month)
 }
+//创建mgo索引
+func createMgoIndex(){
+	mongoDBDialInfo := &mgo.DialInfo{
+		Addrs:    []string{addr},
+		Timeout:  60 * time.Second,
+		Database: dbname,
+	}
+	session, err := mgo.DialWithInfo(mongoDBDialInfo)
+	if err != nil {
+		log.Fatalf("CreateSession failed:%\n", err)
+	}
+	coll := session.DB(dbname).C(table)
+	err = coll.EnsureIndexKey("publishtime")
+	fmt.Println("创建索引~publishtime",err)
 
+	//查询所有的已存在索引
+	//indexs, err := coll.Indexes()
+	//fmt.Println("indexs--------------:", indexs)
+}
+//初始化
 func init() {
 	initConfig()
+	createMgoIndex()
 	Mgo = &MongodbSim{
 		MongodbAddr: addr,
 		Size:        3,
 		DbName:      dbname,
 	}
 	Mgo.InitPool()
+
 	packreg, _ = regexp.Compile(`([a-zA-Z0-9①②ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩ一二三四五六七八九十][包标段])`)
 	//packreg, _ = regexp.Compile(`([包标段][::]?[a-zA-Z0-9①②ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩ一二三四五六七八九十]|[a-zA-Z0-9①②ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩ一二三四五六七八九十][包标段]){1,}`)
 	//packreg, _ = regexp.MustCompile("([a-zA-Z0-9①②ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩ一二三四五六七八九十](包|标|段)[::]?)")
 }
-
+//主函数
 func main() {
 	if addr == "" || dbname == "" || table == "" ||  sortType == ""  {
 		log.Println("参数配置有误")
@@ -88,6 +109,7 @@ func main() {
 	q:=map[string]interface{}{}
 	sess := Mgo.GetMgoConn()
 	defer Mgo.DestoryMongoConn(sess)
+
 	//it := sess.DB(Mgo.DbName).C(*table).Find(query).Sort(sort).Iter()
 	it := sess.DB(Mgo.DbName).C(table).Find(q).Sort(sort).Iter()
 	//对标题、项目名称等中英文符号、空格等进行处理

+ 6 - 5
work_repeat/src/mgo.go

@@ -148,18 +148,17 @@ func (m *MongodbSim) InitPool() {
 	opts.ApplyURI("mongodb://" + m.MongodbAddr)
 	opts.SetMaxPoolSize(uint64(m.Size))
 	m.pool = make(chan bool, m.Size)
-
-	if m.UserName !="" && m.Password !="" {
+	if m.UserName != "" && m.Password != "" {
 		cre := options.Credential{
-			Username:m.UserName,
-			Password:m.Password,
+			Username: m.UserName,
+			Password: m.Password,
 		}
 		opts.SetAuth(cre)
 	}
 
+	opts.SetMaxConnIdleTime(2 * time.Hour)
 
 
-	opts.SetMaxConnIdleTime(2 * time.Hour)
 	m.Ctx, _ = context.WithTimeout(context.Background(), 99999*time.Hour)
 	m.ShortCtx, _ = context.WithTimeout(context.Background(), 1*time.Minute)
 	client, err := mongo.Connect(m.ShortCtx, opts)
@@ -169,6 +168,8 @@ func (m *MongodbSim) InitPool() {
 		m.C = client
 		log.Println("init success")
 	}
+
+
 }
 
 func (m *MongodbSim) Open() {