|
|
|
@ -1,43 +1,39 @@
|
|
|
|
|
package ic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
"strconv"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 从文件中读取 uid 列表
|
|
|
|
|
func obtainUIDList(filename string) (uids IDSlice) {
|
|
|
|
|
fp, err := os.Open(filename)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
buf, err := ioutil.ReadAll(fp)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
lines := bytes.Split(buf, []byte("\n"))
|
|
|
|
|
for _, line := range lines {
|
|
|
|
|
if len(line) == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
item, err := strconv.ParseUint(string(line), 10, 32)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
uids = append(uids, uint32(item))
|
|
|
|
|
}
|
|
|
|
|
return uids
|
|
|
|
|
}
|
|
|
|
|
//// 从文件中读取 uid 列表
|
|
|
|
|
//func obtainUIDList(filename string) (uids IDSlice) {
|
|
|
|
|
// fp, err := os.Open(filename)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// panic(err)
|
|
|
|
|
// }
|
|
|
|
|
// buf, err := ioutil.ReadAll(fp)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// panic(err)
|
|
|
|
|
// }
|
|
|
|
|
// lines := bytes.Split(buf, []byte("\n"))
|
|
|
|
|
// for _, line := range lines {
|
|
|
|
|
// if len(line) == 0 {
|
|
|
|
|
// continue
|
|
|
|
|
// }
|
|
|
|
|
// item, err := strconv.ParseUint(string(line), 10, 32)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// panic(err)
|
|
|
|
|
// }
|
|
|
|
|
// uids = append(uids, uint32(item))
|
|
|
|
|
// }
|
|
|
|
|
// return uids
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
//var FILENAME = "uid.txt"
|
|
|
|
|
|
|
|
|
|
func marshalWrap(ids IDSlice) (ret []byte) {
|
|
|
|
|
func marshalWrap(ids []uint32) (ret []byte) {
|
|
|
|
|
log.Println("> sort.")
|
|
|
|
|
sortIDSlice(ids)
|
|
|
|
|
Sort(ids)
|
|
|
|
|
log.Println("< sort.")
|
|
|
|
|
|
|
|
|
|
log.Println("> marshal.")
|
|
|
|
@ -55,7 +51,7 @@ func marshalWrap(ids IDSlice) (ret []byte) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func unmarshalWrap(b []byte) (ret IDSlice) {
|
|
|
|
|
func unmarshalWrap(b []byte) (ret []uint32) {
|
|
|
|
|
b = zlibRead(b)
|
|
|
|
|
|
|
|
|
|
//var oc OriginCompressor
|
|
|
|
@ -71,7 +67,7 @@ func TestIC(t *testing.T) {
|
|
|
|
|
log.SetFlags(log.Lmicroseconds)
|
|
|
|
|
|
|
|
|
|
// 单元测试 case
|
|
|
|
|
uidss := []IDSlice{
|
|
|
|
|
uidss := [][]uint32{
|
|
|
|
|
{1, 2, 3, 18, 32, 100},
|
|
|
|
|
{1, 2, 3, 18, 32},
|
|
|
|
|
{1, 2, 3, 18},
|
|
|
|
@ -86,30 +82,39 @@ func TestIC(t *testing.T) {
|
|
|
|
|
{1},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 从文件加载 uid 白名单
|
|
|
|
|
//uids := obtainUIDList(FILENAME)
|
|
|
|
|
//var uidss []IDSlice
|
|
|
|
|
//uidss = append(uidss, uids)
|
|
|
|
|
var compressors []Compressor
|
|
|
|
|
|
|
|
|
|
for _, uids := range uidss {
|
|
|
|
|
log.Println("-----")
|
|
|
|
|
log.Println("in uid len:", len(uids))
|
|
|
|
|
compressors = append(compressors, &OriginCompressor{})
|
|
|
|
|
compressors = append(compressors, &OriginCompressor{ZlibExt: true})
|
|
|
|
|
|
|
|
|
|
b := marshalWrap(uids)
|
|
|
|
|
log.Println("len(b):", len(b))
|
|
|
|
|
compressors = append(compressors, &LFCompressor{FB: 0})
|
|
|
|
|
compressors = append(compressors, &LFCompressor{FB: 0, ZlibExt: true})
|
|
|
|
|
compressors = append(compressors, &LFCompressor{FB: 2})
|
|
|
|
|
compressors = append(compressors, &LFCompressor{FB: 4})
|
|
|
|
|
compressors = append(compressors, &LFCompressor{FB: 4, ZlibExt: true})
|
|
|
|
|
|
|
|
|
|
uids2 := unmarshalWrap(b)
|
|
|
|
|
log.Println("out uid len:", len(uids2))
|
|
|
|
|
for _, c := range compressors {
|
|
|
|
|
for _, uids := range uidss {
|
|
|
|
|
log.Println("-----")
|
|
|
|
|
log.Println("in uid len:", len(uids))
|
|
|
|
|
|
|
|
|
|
// assert check
|
|
|
|
|
if len(uids) != len(uids2) {
|
|
|
|
|
panic(0)
|
|
|
|
|
}
|
|
|
|
|
for i := range uids {
|
|
|
|
|
if uids[i] != uids2[i] {
|
|
|
|
|
Sort(uids)
|
|
|
|
|
b := c.Marshal(uids)
|
|
|
|
|
log.Println("len(b):", len(b))
|
|
|
|
|
|
|
|
|
|
uids2 := c.Unmarshal(b)
|
|
|
|
|
log.Println("out uid len:", len(uids2))
|
|
|
|
|
|
|
|
|
|
// assert check
|
|
|
|
|
if len(uids) != len(uids2) {
|
|
|
|
|
panic(0)
|
|
|
|
|
}
|
|
|
|
|
for i := range uids {
|
|
|
|
|
if uids[i] != uids2[i] {
|
|
|
|
|
panic(0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
log.Println("-----")
|
|
|
|
|
}
|
|
|
|
|
log.Println("-----")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|