Esempio n. 1
0
func (this *ByteProfile) WriteToFile() error {

	buf := new(bytes.Buffer)

	//file_name := fmt.Sprintf("./index/detail.dat")
	fout, err := os.Create(fmt.Sprintf("./index/%v_pfl.dat", this.Name))
	if err != nil {
		fmt.Printf("Create Error %v\n", err)
		return err
	}
	defer fout.Close()
	var start int64 = 0
	for index, _ := range this.ProfileList {

		err := binary.Write(buf, binary.LittleEndian, this.ProfileList[index].Data)
		if err != nil {
			fmt.Printf("Write Error ..%v\n", err)
		}
		this.ProfileList[index].Start = start
		this.ProfileList[index].DataLen = len(this.ProfileList[index].Data)
		this.ProfileList[index].InMomory = false
		this.ProfileList[index].Data = nil
		start += int64(this.ProfileList[index].DataLen)
	}

	fout.Write(buf.Bytes())
	u.WriteToJson(this, fmt.Sprintf("./index/%v_pfl.json", this.Name))
	return nil

}
Esempio n. 2
0
func (this *Detail) WriteDetailToFile() error {

	buf := new(bytes.Buffer)

	//file_name := fmt.Sprintf("./index/detail.dat")
	fout, err := os.Create(fmt.Sprintf("./index/detail.dat"))
	if err != nil {
		fmt.Printf("Create Error %v\n", err)
		return err
	}
	defer fout.Close()

	for index, _ := range this.DetailList {

		err := binary.Write(buf, binary.LittleEndian, this.DetailList[index].DetailBytes)
		if err != nil {
			fmt.Printf("Write Error ..%v\n", err)
		}
		this.DetailList[index].DetailBytes = nil
	}

	fout.Write(buf.Bytes())
	utils.WriteToJson(this, "./index/detail.idx.json")
	return nil

}
Esempio n. 3
0
func (this *Segment) storeStruct() error {
	metaFileName := fmt.Sprintf("%v.meta", this.SegmentName)
	if err := utils.WriteToJson(this, metaFileName); err != nil {
		return err
	}
	return nil
}
Esempio n. 4
0
func (this *Dispatcher) storeStruct() error {
	metaFileName := fmt.Sprintf("%v/Dispatcher.json", utils.IDX_DETAIL_PATH)
	if err := utils.WriteToJson(this, metaFileName); err != nil {
		this.Logger.Error("[ERROR] storeStruct %v", err)
		return err
	}
	return nil
}
Esempio n. 5
0
func (this *Index) storeStruct() error {
	metaFileName := fmt.Sprintf("%v%v.meta", this.Pathname, this.Name)
	if err := utils.WriteToJson(this, metaFileName); err != nil {
		return err
	}
	startTime := time.Now()
	this.Logger.Debug("[INFO] start muti set %v", startTime)
	this.primary.MutiSet(this.PrimaryKey, this.pkmap)
	endTime := time.Now()
	this.Logger.Debug("[INFO] cost  muti set  %v", endTime.Sub(startTime))
	this.pkmap = nil
	this.pkmap = make(map[string]string)

	return nil

}
Esempio n. 6
0
// CreateTable function description : 创建数据库表
// params :
// return :
func (this *SDatabase) CreateTable(tablename string, fieldinfos []FieldMeta) error {

	if _, ok := this.tables[tablename]; ok {
		this.Logger.Error("[ERROR] Table[%v] Create Error", tablename)
		return fmt.Errorf("[ERROR] Table[%v] Create Error", tablename)

	}

	table := NewSTable(tablename, this.Fullname, fieldinfos, this.Logger)

	if table == nil {
		this.Logger.Error("[ERROR] Create Table[%v] Fail", tablename)
		return fmt.Errorf("[ERROR] Create Table[%v] Fail", tablename)
	}

	this.tables[tablename] = table
	this.TableNames = append(this.TableNames, tablename)

	utils.WriteToJson(this, this.Fullname+"/_dbinfo.meta")

	return nil

}
Esempio n. 7
0
/*****************************************************************************
*  function name : Buiding
*  params :
*  return :
*
*  description : 构建索引文件,并将detail存入redis中,这个函数太啰嗦了。
*
******************************************************************************/
func (this *DBBuilder) Buiding() error {

	var fields string
	for index, v := range this.Fields {
		//构造sql语句
		fields = fields + "," + v.Name
		//构造索引数据指针
		if v.IsIvt {

			if v.FType == "T" {
				this.Fields[index].IvtIdx = utils.NewInvertIdx(utils.TYPE_TEXT, v.Name)
				this.Fields[index].IvtStrDic = utils.NewStringIdxDic(10000)

			}

			if v.FType == "N" {
				this.Fields[index].IvtIdx = utils.NewInvertIdx(utils.TYPE_NUM, v.Name)
				this.Fields[index].IvtNumDic = utils.NewNumberIdxDic(10000)

			}
		}

		if v.IsPlf {
			if v.FType == "T" {
				this.Fields[index].PlfText = indexer.NewTextProfile(v.Name)
			}

			if v.FType == "N" {
				this.Fields[index].PlfNumber = indexer.NewNumberProfile(v.Name)
			}

		}

	}

	sql := fmt.Sprintf(this.sql, fields[1:len(fields)])
	fmt.Printf("SQL :: %v \n", sql)

	rows, err := this.Dbadaptor.QueryFormat(sql)
	if err != nil {
		this.Logger.Error(" %v", err)
		return err
	}
	defer rows.Close()
	var doc_id int64
	doc_id = 1
	segment := utils.NewSegmenter("./data/dictionary.txt")
	builder := &utils.IndexBuilder{segment}

	for rows.Next() {
		//values := make([]interface{},len(this.Fields))
		values := make([]interface{}, len(this.Fields))
		writeCols := make([]string, len(this.Fields))
		for i, _ := range writeCols {
			values[i] = &writeCols[i]
		}

		err := rows.Scan(values...)
		if err != nil {
			this.Logger.Error("SQL ERROR : %v", err)
			return err
		}
		redis_map := make(map[string]string)
		for index, v := range writeCols {
			redis_map[this.Fields[index].Name] = v
			//v,_ := value.(string)
			if this.Fields[index].IsIvt {

				if this.Fields[index].FType == "T" {
					err := builder.BuildTextIndex(doc_id, v, this.Fields[index].IvtIdx, this.Fields[index].IvtStrDic)
					if err != nil {
						this.Logger.Error("ERROR : %v", err)
					}
				}

				if this.Fields[index].FType == "N" {
					v_num, err := strconv.ParseInt(v, 0, 0)
					if err != nil {
						this.Logger.Error("ERROR : %v", err)
					}

					err = builder.BuildNumberIndex(doc_id, v_num, this.Fields[index].IvtIdx, this.Fields[index].IvtNumDic)
					if err != nil {
						this.Logger.Error("ERROR : %v", err)
					}
				}

			}

			if this.Fields[index].IsPlf {

				if this.Fields[index].FType == "T" {

					err := this.Fields[index].PlfText.PutProfile(doc_id, v)
					if err != nil {
						this.Logger.Error("ERROR : %v", err)
					}
				}

				if this.Fields[index].FType == "N" {
					v_num, err := strconv.ParseInt(v, 0, 0)
					if err != nil {
						this.Logger.Error("ERROR : %v", err)
					}
					err = this.Fields[index].PlfNumber.PutProfile(doc_id, v_num)
					if err != nil {
						this.Logger.Error("ERROR : %v", err)
					}
				}

			}

		}

		this.RedisCli.SetFields(doc_id, redis_map)

		fieldlist := make([]string, 0)
		for k, _ := range redis_map {
			fieldlist = append(fieldlist, k)
		}

		doc_id++

		this.Logger.Info("DOC_ID : %v  VALUE : %v", doc_id, writeCols)

	}

	for index, fields := range this.Fields {

		if this.Fields[index].IsIvt {

			utils.WriteToJson(fields.IvtIdx, fmt.Sprintf("./index/%v_idx.json", fields.Name))
			if this.Fields[index].FType == "T" {

				utils.WriteToJson(fields.IvtStrDic, fmt.Sprintf("./index/%v_dic.json", fields.Name))

			}

			if this.Fields[index].FType == "N" {

				utils.WriteToJson(fields.IvtNumDic, fmt.Sprintf("./index/%v_dic.json", fields.Name))
			}

		}

		if this.Fields[index].IsPlf {

			if this.Fields[index].FType == "T" {

				utils.WriteToJson(fields.PlfText, fmt.Sprintf("./index/%v_pfl.json", fields.Name))

			}

			if this.Fields[index].FType == "N" {

				utils.WriteToJson(fields.PlfNumber, fmt.Sprintf("./index/%v_pfl.json", fields.Name))

			}

		}

	}

	return nil

}
Esempio n. 8
0
/*****************************************************************************
*  function name : Buiding
*  params :
*  return :
*
*  description : 构建索引文件,并将detail存入redis中,这个函数太啰嗦了。
*
******************************************************************************/
func (this *DBBuilder) Buiding() error {

	var fields string
	for index, v := range this.Fields {
		//构造sql语句
		fields = fields + "," + v.Name
		//构造索引数据指针
		if v.IsIvt {

			if v.FType == "T" {
				this.Fields[index].IvtIdx = utils.NewInvertIdx(utils.TYPE_TEXT, v.Name)
				this.Fields[index].IvtStrDic = utils.NewStringIdxDic(5001)

			}

			if v.FType == "N" {
				this.Fields[index].IvtIdx = utils.NewInvertIdx(utils.TYPE_NUM, v.Name)
				if v.IsPK {
					this.Fields[index].IvtNumDic = utils.NewNumberIdxDic(1)
				} else {
					this.Fields[index].IvtNumDic = utils.NewNumberIdxDic(4)
				}

			}
		}

		if v.IsPlf {
			if v.FType == "T" {
				this.Fields[index].PlfText = indexer.NewTextProfile(v.Name)
			}

			if v.FType == "N" {
				this.Fields[index].PlfNumber = indexer.NewNumberProfile(v.Name)
			}

		}

	}
	fmt.Printf("%v\n", fields)

	this.DetailIdx = indexer.NewDetail()

	sql := fmt.Sprintf(this.sql, fields[1:len(fields)])
	fmt.Printf("SQL :: %v \n", sql)

	rows, err := this.Dbadaptor.QueryFormat(sql)
	if err != nil {
		this.Logger.Error(" %v", err)
		return err
	}
	defer rows.Close()
	var doc_id int64
	doc_id = 1
	segment := utils.NewSegmenter("./data/dictionary.txt")
	builder := &utils.IndexBuilder{Segmenter: segment, TempIndex: make(map[string][]utils.TmpIdx), TempIndexNum: make(map[string]int64)}

	for rows.Next() {
		//values := make([]interface{},len(this.Fields))
		values := make([]interface{}, len(this.Fields))
		writeCols := make([]string, len(this.Fields))
		for i, _ := range writeCols {
			values[i] = &writeCols[i]
		}

		err := rows.Scan(values...)
		if err != nil {
			this.Logger.Error("SQL ERROR : %v", err)
			return err
		}
		redis_map := make(map[string]string)
		for index, v := range writeCols {
			redis_map[this.Fields[index].Name] = v
			//v,_ := value.(string)
			if this.Fields[index].IsIvt {

				if this.Fields[index].FType == "T" {
					err := builder.BuildTextIndex(doc_id, v, this.Fields[index].IvtIdx, this.Fields[index].IvtStrDic, this.Fields[index].SType, false)
					//err := builder.BuildTextIndexTemp(doc_id, v, this.Fields[index].IvtIdx, this.Fields[index].IvtStrDic,this.Fields[index].SType,this.Fields[index].Name)
					if err != nil {
						this.Logger.Error("ERROR : %v", err)
					}
				}

				if this.Fields[index].FType == "N" {
					v_num, err := strconv.ParseInt(v, 0, 0)
					if err != nil {
						v_num = 0
						this.Logger.Warn("Warning : name : [%v] , value: [%v] , error : [%v]", this.Fields[index].Name, v, err)
					}

					err = builder.BuildNumberIndex(doc_id, v_num, this.Fields[index].IvtIdx, this.Fields[index].IvtNumDic, false)
					//err = builder.BuildNumberIndexTemp(doc_id, v_num, this.Fields[index].IvtIdx, this.Fields[index].IvtNumDic,this.Fields[index].Name)
					if err != nil {
						this.Logger.Error("ERROR : %v", err)
					}
				}

			}

			if this.Fields[index].IsPlf {

				if this.Fields[index].FType == "T" {
					//添加日期类型的更新,仅精确到天 add by wuyinghao 2015-08-21
					if this.Fields[index].SType == 5 {
						vl := strings.Split(v, " ")
						v = vl[0]
					}
					err := this.Fields[index].PlfText.PutProfile(doc_id, v)
					if err != nil {
						this.Logger.Error("ERROR : %v", err)
					}
				}

				if this.Fields[index].FType == "N" {
					v_num, err := strconv.ParseInt(v, 0, 0)
					if err != nil {
						v_num = 0
						this.Logger.Warn("Warning : name : %v , value: %v , error : %v", this.Fields[index].Name, v, err)
					}
					err = this.Fields[index].PlfNumber.PutProfile(doc_id, v_num)
					if err != nil {
						this.Logger.Error("ERROR : %v", err)
					}
				}

			}

		}
		/////
		//this.RedisCli.SetFields(doc_id, redis_map)
		////
		if this.DetailIdx.PutDocInfo(doc_id, redis_map) != nil {
			this.Logger.Error("PutDocInfo doc_id Error :  %v \n", err)
		}

		fieldlist := make([]string, 0)
		for k, _ := range redis_map {
			fieldlist = append(fieldlist, k)
		}

		doc_id++
		if doc_id%5000 == 0 {
			this.Logger.Info("processing doc_id :  %v \n", doc_id)
		}
		//this.Logger.Info("DOC_ID : %v  VALUE : %v", doc_id, writeCols)

	}

	this.DetailIdx.WriteDetailToFile()

	//写入全部数据
	//builder.WriteAllTempIndexToFile()
	//builder.WriteIndexToFile()

	//writeCount:=0
	//var writeChan chan int
	for index, fields := range this.Fields {

		if this.Fields[index].IsIvt {

			//utils.WriteToJsonWithChan(fields.IvtIdx, fmt.Sprintf("./index/%v_idx.json", fields.Name),writeChan)
			//utils.WriteToJson(fields.IvtIdx, fmt.Sprintf("./index/%v_idx.json", fields.Name))
			utils.WriteToIndexFile(fields.IvtIdx, fmt.Sprintf("./index/%v_idx.idx", fields.Name))
			utils.WriteToJson(fields.IvtIdx, fmt.Sprintf("./index/%v_idx.json", fields.Name))
			//writeCount++
			if this.Fields[index].FType == "T" {

				//utils.WriteToJsonWithChan(fields.IvtStrDic, fmt.Sprintf("./index/%v_dic.json", fields.Name),writeChan)
				utils.WriteToJson(fields.IvtStrDic, fmt.Sprintf("./index/%v_dic.json", fields.Name))
				//writeCount++

			}

			if this.Fields[index].FType == "N" {

				//utils.WriteToJsonWithChan(fields.IvtNumDic, fmt.Sprintf("./index/%v_dic.json", fields.Name),writeChan)
				utils.WriteToJson(fields.IvtNumDic, fmt.Sprintf("./index/%v_dic.json", fields.Name))
				//writeCount++
			}

		}

		if this.Fields[index].IsPlf {

			if this.Fields[index].FType == "T" {

				//utils.WriteToJsonWithChan(fields.PlfText, fmt.Sprintf("./index/%v_pfl.json", fields.Name),writeChan)
				utils.WriteToJson(fields.PlfText, fmt.Sprintf("./index/%v_pfl.json", fields.Name))
				//writeCount++

			}

			if this.Fields[index].FType == "N" {

				//utils.WriteToJsonWithChan(fields.PlfNumber, fmt.Sprintf("./index/%v_pfl.json", fields.Name),writeChan)
				utils.WriteToJson(fields.PlfNumber, fmt.Sprintf("./index/%v_pfl.json", fields.Name))
				//writeCount++

			}

		}

	}
	/*
		fmt.Printf("writeCount : %v \n ",writeCount)
		for {
			select{
				case <-writeChan:
					writeCount--
					fmt.Printf("recive writeCount : %v \n ",writeCount)
					if writeCount == 0 {
						fmt.Printf("Finish building index...\n")
						return nil
					}
			}
		}
	*/
	return nil

}