mirror of https://github.com/q191201771/naza
new pkg ic
parent
bf8c48549e
commit
f6a3d64d3f
@ -0,0 +1,115 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
//var FILENAME = "uid.txt"
|
||||||
|
|
||||||
|
func marshalWrap(ids IDSlice) (ret []byte) {
|
||||||
|
log.Println("> sort.")
|
||||||
|
sortIDSlice(ids)
|
||||||
|
log.Println("< sort.")
|
||||||
|
|
||||||
|
log.Println("> marshal.")
|
||||||
|
//var oc OriginCompressor
|
||||||
|
//ret = oc.Marshal(ids)
|
||||||
|
|
||||||
|
var lfc LFCompressor
|
||||||
|
lfc.FB = 4
|
||||||
|
ret = lfc.Marshal(ids)
|
||||||
|
log.Println("< marshal.")
|
||||||
|
|
||||||
|
log.Println("> zlib. len:", len(ret))
|
||||||
|
ret = zlibWrite(ret)
|
||||||
|
log.Println("< zlib. len:", len(ret))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func unmarshalWrap(b []byte) (ret IDSlice) {
|
||||||
|
b = zlibRead(b)
|
||||||
|
|
||||||
|
//var oc OriginCompressor
|
||||||
|
//ret = oc.Unmarshal(b)
|
||||||
|
|
||||||
|
var lfc LFCompressor
|
||||||
|
lfc.FB = 4
|
||||||
|
ret = lfc.Unmarshal(b)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIC(t *testing.T) {
|
||||||
|
log.SetFlags(log.Lmicroseconds)
|
||||||
|
|
||||||
|
// 单元测试 case
|
||||||
|
uidss := []IDSlice{
|
||||||
|
{1, 2, 3, 18, 32, 100},
|
||||||
|
{1, 2, 3, 18, 32},
|
||||||
|
{1, 2, 3, 18},
|
||||||
|
{1, 2, 3, 17},
|
||||||
|
{1, 2, 3, 16},
|
||||||
|
{1, 2, 3, 15, 16, 17, 18},
|
||||||
|
{1, 2, 3, 15, 16, 17},
|
||||||
|
{1, 2, 3, 15, 16},
|
||||||
|
{1, 2, 3, 15},
|
||||||
|
{1, 2, 3},
|
||||||
|
{1, 2},
|
||||||
|
{1},
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从文件加载 uid 白名单
|
||||||
|
//uids := obtainUIDList(FILENAME)
|
||||||
|
//var uidss []IDSlice
|
||||||
|
//uidss = append(uidss, uids)
|
||||||
|
|
||||||
|
for _, uids := range uidss {
|
||||||
|
log.Println("-----")
|
||||||
|
log.Println("in uid len:", len(uids))
|
||||||
|
|
||||||
|
b := marshalWrap(uids)
|
||||||
|
log.Println("len(b):", len(b))
|
||||||
|
|
||||||
|
uids2 := unmarshalWrap(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("-----")
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package ic
|
||||||
|
|
||||||
|
import "encoding/binary"
|
||||||
|
|
||||||
|
type OriginCompressor struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (oc *OriginCompressor) Marshal(ids IDSlice) (ret []byte) {
|
||||||
|
ret = make([]byte, len(ids)*4)
|
||||||
|
for i, id := range ids {
|
||||||
|
binary.LittleEndian.PutUint32(ret[i*4:], id)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (oc *OriginCompressor) Unmarshal(b []byte) (ids IDSlice) {
|
||||||
|
n := len(b) / 4
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
id := binary.LittleEndian.Uint32(b[i*4:])
|
||||||
|
ids = append(ids, id)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
@ -0,0 +1,58 @@
|
|||||||
|
package ic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"compress/zlib"
|
||||||
|
"io/ioutil"
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
type IDSlice []uint32
|
||||||
|
|
||||||
|
func (a IDSlice) Len() int { return len(a) }
|
||||||
|
func (a IDSlice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||||
|
func (a IDSlice) Less(i, j int) bool { return a[i] < a[j] }
|
||||||
|
|
||||||
|
func resetBuf(b []byte) []byte {
|
||||||
|
for i := 0; i < len(b); i++ {
|
||||||
|
b[i] = 0
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func sortIDSlice(ids IDSlice) {
|
||||||
|
sort.Sort(ids)
|
||||||
|
}
|
||||||
|
|
||||||
|
func zlibWrite(in []byte) []byte {
|
||||||
|
var b bytes.Buffer
|
||||||
|
w := zlib.NewWriter(&b)
|
||||||
|
_, _ = w.Write(in)
|
||||||
|
_ = w.Close()
|
||||||
|
return b.Bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
func zlibRead(in []byte) (ret []byte) {
|
||||||
|
b := bytes.NewReader(in)
|
||||||
|
r, _ := zlib.NewReader(b)
|
||||||
|
ret, _ = ioutil.ReadAll(r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//func isBufEmpty(b []byte) bool {
|
||||||
|
// for i := 0; i < len(b); i++ {
|
||||||
|
// if b[i] != 0 {
|
||||||
|
// return false
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// return true
|
||||||
|
//}
|
||||||
|
//
|
||||||
|
//func dumpIDSlice(ids IDSlice, filename string) {
|
||||||
|
// fp, _ := os.Create(filename)
|
||||||
|
// for _, id := range ids {
|
||||||
|
// _, _ = fp.WriteString(fmt.Sprintf("%d", id))
|
||||||
|
// _, _ = fp.WriteString("\n")
|
||||||
|
// }
|
||||||
|
// _ = fp.Close()
|
||||||
|
//}
|
Loading…
Reference in New Issue