Skip to content

Commit

Permalink
feat:ap
Browse files Browse the repository at this point in the history
  • Loading branch information
CocaineCong committed Aug 12, 2023
1 parent ecb0443 commit ba6b82d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 22 deletions.
51 changes: 31 additions & 20 deletions pkg/util/codec/byte.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,52 @@ package codec

import (
"bytes"
"encoding/binary"
"encoding/gob"
"encoding/json"
"errors"
"reflect"
"fmt"

"github.com/bytedance/sonic"
"github.com/spf13/cast"

"github.com/CocaineCong/tangseng/app/search_engine/types"
log "github.com/CocaineCong/tangseng/pkg/logger"
)

// BinaryWrite 将所有的类型 转成byte buffer类型,易于存储// TODO change
func BinaryWrite(v any) (buf *bytes.Buffer, err error) {
if v == nil {
err = errors.New("BinaryWrite the value is nil")
// func BinaryWrite(v any) (buf *bytes.Buffer, err error) {
func BinaryWrite(buf *bytes.Buffer, v any) (err error) {
size := binary.Size(v)
// log.Debug("docid size:", size)
fmt.Println("size", size)
if size <= 0 {
log.LogrusObj.Errorf("encodePostings binary.Size err,size: %v", size)
return
}
buf = new(bytes.Buffer)

switch reflect.Indirect(reflect.ValueOf(v)).Kind() { // TODO:反射很影响性能,后续看看怎么优化
case reflect.Int64, reflect.Int32, reflect.Int:
buf.Write([]byte(cast.ToString(v)))
case reflect.String:
buf.Write([]byte(v.(string)))
case reflect.Slice, reflect.Array, reflect.Struct:
value, errx := json.Marshal(v)
if errx != nil {
err = errx
return
}
buf.Write(value)
}
binary.Write(buf, binary.LittleEndian, v)

return
// if v == nil {
// err = errors.New("BinaryWrite the value is nil")
// return
// }
// buf = new(bytes.Buffer)
//
// switch reflect.Indirect(reflect.ValueOf(v)).Kind() { // TODO:反射很影响性能,后续看看怎么优化
// case reflect.Int64, reflect.Int32, reflect.Int:
// buf.Write([]byte(cast.ToString(v)))
// case reflect.String:
// buf.Write([]byte(v.(string)))
// case reflect.Slice, reflect.Array, reflect.Struct:
// value, errx := json.Marshal(v)
// if errx != nil {
// err = errx
// return
// }
// buf.Write(value)
// }

// return
}

// GobWrite 将所有的类型 转成 bytes.Buffer 类型,易于存储// TODO change
Expand Down
24 changes: 22 additions & 2 deletions pkg/util/codec/byte_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,35 @@ import (
"fmt"
"os"
"testing"

"github.com/CocaineCong/tangseng/app/search_engine/types"
"github.com/CocaineCong/tangseng/config"
log "github.com/CocaineCong/tangseng/pkg/logger"
)

func TestMain(m *testing.M) {
re := config.ConfigReader{FileName: "../../../config/config.yaml"}
config.InitConfigForTest(&re)
log.InitLog()
fmt.Println("Write tests on values: ", config.Conf)
m.Run()
}

func TestIntToBytes(t *testing.T) {
docCount := 5
buf, err := BinaryWrite(docCount)
p := new(types.PostingsList)
buf := bytes.NewBuffer([]byte{})
p.DocId = 100
err := BinaryWrite(buf, p.DocId)
if err != nil {
fmt.Println(err)
}
fmt.Println(buf)
var c int64
err = binary.Read(buf, binary.LittleEndian, &c)
if err != nil {
fmt.Println(err)
}
fmt.Println(c)
}

func TestBinarySize(t *testing.T) {
Expand Down

0 comments on commit ba6b82d

Please sign in to comment.