示例#1
0
func NewFTtlQ(basePath, qname string) *FileTtlQ {
	c := make(chan *qr.Qr, 1000000)
	timerCache := utee.NewTimerCache(60, func(k, v interface{}) {
		c <- v.(*qr.Qr)
	})
	d := fmt.Sprintf("%s/%s/%s", basePath, qname, "ldb")
	log.Println("start @dbpath:", d)
	err := os.MkdirAll(d, 0777)
	utee.Chk(err)
	db, err := leveldb.OpenFile(d, nil)
	utee.Chk(err)
	q := &FileTtlQ{
		Ldb:        db,
		timerCache: timerCache,
		basePath:   basePath,
		qname:      qname,
		shut_q:     c,
	}

	closeQ := func(fq *qr.Qr) {
		defer func() {
			if err := recover(); err != nil {
				log.Println(err, " (recover) @fq:", fq)
			}
		}()
		fq.Close()
	}
	clean := func() {
		for fq := range c {
			closeQ(fq)
		}
	}
	go clean()
	return q
}
示例#2
0
func main() {
	b, err := deflate([]byte("hello world"))
	utee.Chk(err)
	log.Println(string(b))
	rb, err := inflate(b)
	utee.Chk(err)
	log.Println(string(rb))
}
示例#3
0
func incHash(pool *redis.Pool, id string, total, read, sent int) {
	c := pool.Get()
	defer c.Close()
	_, err := c.Do("HINCRBY", id, "total", total)
	utee.Chk(err)
	_, err = c.Do("HINCRBY", id, "read", read)
	utee.Chk(err)
	_, err = c.Do("HINCRBY", id, "sent", sent)
	utee.Chk(err)
}
示例#4
0
func saveHash(pool *redis.Pool, id string) {
	c := pool.Get()
	defer c.Close()

	v, err := redis.Int(c.Do("EXISTS", id))
	utee.Chk(err)
	if v > 0 {
		log.Println("already exist")
		return
	}
	_, err = c.Do("HMSET", id, "total", 0, "read", 0, "send", 0)
	utee.Chk(err)
	c.Do("TTL", id, 60*60*2)
}
示例#5
0
func main() {
	excelFileName := "/home/figo/develop/env/GOPATH/src/github.com/figoxu/goPraticse/3rd/excel/readExcel/test.xlsx"
	xlFile, err := xlsx.OpenFile(excelFileName)
	utee.Chk(err)
	for _, sheet := range xlFile.Sheets {
		for _, row := range sheet.Rows {
			for _, cell := range row.Cells {
				v, err := cell.String()
				utee.Chk(err)
				fmt.Printf("%s\n", v)
			}
		}
	}
}
示例#6
0
func main() {
	m := make(map[string]string)
	m["hello"] = "echo hello"
	m["world"] = "echo world"
	m["go"] = "echo go"
	m["is"] = "echo is"
	m["cool"] = "echo cool"

	for k, v := range m {
		log.Printf("k=%v, v=%v\n", k, v)
	}
	log.Println("---------------")
	for k := range m {
		log.Println(k)
	}

	bm = make(map[string]interface{})
	bm["a"] = Info{
		Made:  "test",
		Daily: 100,
	}
	b, e := json.Marshal(bm)
	utee.Chk(e)
	log.Println(string(b))
}
示例#7
0
func main() {
	log.Println("boot grid mix json by interface")

	items := []interface{}{}
	for i := 0; i < 3; i++ {
		items = append(items, DevItem{
			Daytime: fmt.Sprint("2016-04-", i),
			Alive:   1310 + i,
			Inc:     300 + i,
			Total:   63000 + i,
		})
	}

	for i := 0; i < 3; i++ {
		items = append(items, SampleItem{
			Key:   fmt.Sprint("result", i),
			Value: fmt.Sprint("value", i),
		})
	}

	dataTable := BootGrid{
		Current:  3,
		RowCount: 10,
		Total:    1000,
		Rows:     items,
	}
	v, e := json.Marshal(dataTable)
	utee.Chk(e)
	log.Println("@v:", string(v))
}
示例#8
0
func getHash(pool *redis.Pool, id string) map[string]int {
	c := pool.Get()
	defer c.Close()
	m, err := redis.IntMap(c.Do("HGETALL", id))
	utee.Chk(err)
	return m
}
示例#9
0
//调用系统指令的方法,参数s 就是调用的shell命令
func system(s string) string {
	cmd := exec.Command("/bin/sh", "-c", s) //调用Command函数
	var out bytes.Buffer                    //缓冲字节
	cmd.Stdout = &out                       //标准输出
	err := cmd.Run()                        //运行指令 ,做判断
	utee.Chk(err)
	return out.String() //输出执行结果
}
示例#10
0
func main() {
	log.Println(system("who "))
	log.Println(system("ps -ef "))
	b, err := sh.Command("netstat", "-antpl").Output()
	log.Println(string(b))
	err = sh.Command("ping", "127.0.0.1").Run()
	utee.Chk(err)
}
示例#11
0
func getCountValFromDb() int {
	pwd := "pwd"
	host := "127.0.0.1"
	url := fmt.Sprint("root:", pwd, "@tcp(", host, ":3306)/test?charset=utf8")

	db, err := sql.Open("mysql", url)
	utee.Chk(err)
	defer db.Close()
	rows, err := db.Query("select count(*) from mtest")
	utee.Chk(err)
	if rows.Next() {
		var id int
		err = rows.Scan(&id)
		utee.Chk(err)
		return id
	}
	return 0
}
示例#12
0
func getVal() int {
	mc := memcache.New("192.168.56.101:11211")
	k := "Test"
	it, err := mc.Get(k)
	log.Println("@it:", it, "    @err:", err)
	if err == memcache.ErrCacheMiss {
		v := getCountValFromDb()
		mc.Set(&memcache.Item{Key: k, Value: []byte(strconv.Itoa(v))})
		log.Println("get from db")
		return v
	} else {
		utee.Chk(err)
	}
	log.Println("get from cache")
	v, err := strconv.Atoi(string(it.Value))
	utee.Chk(err)
	return v
}
示例#13
0
func insertBTest() {
	n := fmt.Sprint("figo", uuid.NewUUID().String())
	s := &Student{
		Id:         bson.NewObjectId(),
		Name:       n,
		VisitTimes: 10,
	}
	err := ds.DB(DB).C(C2).Insert(s)
	utee.Chk(err)
}
示例#14
0
func (p *FileTtlQ) getQ(uid interface{}) *qr.Qr {
	qn := p.parseQName(uid)
	v := p.timerCache.Get(uid)
	if v == nil {
		d := fmt.Sprintf("%s/%s/q/%s", p.basePath, p.qname, getOkDir(qn))
		err := os.MkdirAll(d, 0777)
		utee.Chk(err)
		q, err := qr.New(
			d,
			qn,
			qr.OptionBuffer(1000),
		)
		utee.Chk(err)
		p.timerCache.Put(uid, q)
		return q
	}
	q := v.(*qr.Qr)
	return q
}
示例#15
0
func CompressZip() {
	const dir = "/home/figo/develop/env/GOPATH/src/github.com/figoxu/goPraticse/3rd/zip/"
	//获取源文件列表
	f, err := ioutil.ReadDir(dir)
	if err != nil {
		fmt.Println(err)
	}
	fzip, _ := os.Create("3rd.zip")
	w := zip.NewWriter(fzip)
	defer w.Close()
	for _, file := range f {
		fw, _ := w.Create(file.Name())
		content, err := ioutil.ReadFile(dir + file.Name())
		utee.Chk(err)
		n, err := fw.Write(content)
		utee.Chk(err)
		log.Println(n, " bytes write")
	}
}
示例#16
0
func Handler(w http.ResponseWriter, r *http.Request) {
	dumx := Person{
		Name:    "zoro",
		Age:     27,
		Emails:  []string{"*****@*****.**", "*****@*****.**"},
		Company: "Omron",
		Role:    "SE"}

	chxd := Person{Name: "chxd", Age: 27, Emails: []string{"*****@*****.**", "*****@*****.**"}}

	onlineUser := OnlineUser{User: []*Person{&dumx, &chxd}}

	//t := template.New("Person template")
	//t, err := t.Parse(templ)
	t, err := template.ParseFiles("/home/figo/develop/env/GOPATH/src/github.com/figoxu/goPraticse/template/tmpl.html")
	utee.Chk(err)

	err = t.Execute(w, onlineUser)
	utee.Chk(err)
}
示例#17
0
func DeCompressZip() {
	const File = "3rd.zip"
	const dir = "tmp/"
	os.Mkdir(dir, 0777) //创建一个目录

	cf, err := zip.OpenReader(File) //读取zip文件
	utee.Chk(err)
	defer cf.Close()
	for _, file := range cf.File {
		rc, err := file.Open()
		utee.Chk(err)
		f, err := os.Create(dir + file.Name)
		utee.Chk(err)
		defer f.Close()
		n, err := io.Copy(f, rc)
		utee.Chk(err)
		log.Println(n, " bytes read")
	}

}
示例#18
0
func main() {
	fmt.Println("hello")
	mg := connect("192.168.56.101/figoTest")
	cp := mg.Copy()
	defer cp.Close()
	ds = mg
	err := cp.DB(DB).C(C2).EnsureIndex(mgo.Index{Key: []string{"name"}, Unique: true})
	utee.Chk(err)
	A := func() {
		insertATest()
	}
	B := func() {
		insertBTest()
	}
	log.Println("insert with    index 10000 times cost:", execute(B, 10000))
	log.Println("insert without index 10000 times cost:", execute(A, 10000))
}
示例#19
0
//ttl unit is second
func (p *FileTtlQ) Enq(uid interface{}, data []byte, ttl ...uint32) error {
	q := p.getQ(uid)
	k := string(uuid.NewUUID().String()) //16 byte
	q.Enqueue(k)
	t := int64(-1) //never ood (out of day)
	if len(ttl) > 0 {
		t = utee.TickSec() + int64(ttl[0])
	}
	qv := QValue{
		Data: data,
		Dod:  t,
	}
	b, err := json.Marshal(qv)
	utee.Chk(err)
	p.Ldb.Put([]byte(k), b, nil)
	return nil
}
示例#20
0
func main() {
	err := os.MkdirAll("./gldb", 0777)
	utee.Chk(err)
	db, err := leveldb.OpenFile("./gldb", nil)
	utee.Chk(err)
	defer db.Close()

	err = db.Put([]byte("key"), []byte("value"), nil)
	utee.Chk(err)
	data, err := db.Get([]byte("key"), nil)
	utee.Chk(err)
	log.Println("@data:", string(data))
	iter := db.NewIterator(nil, nil)
	i := 0
	for iter.Next() {
		key := iter.Key()
		value := iter.Value()
		log.Println("@key:", string(key), "@value:", string(value))
		i++
	}
	log.Println("read @i:", i)
	iter.Release()
	err = iter.Error()
	err = db.Delete([]byte("key"), nil)
	utee.Chk(err)

	log.Println("热身完毕")

	st := utee.Tick()
	for i := 0; i < 10*10000; i++ {
		b, _ := json.Marshal(Test{Name: "figo", Tp: "android", Count: 1024})
		db.Put([]byte(fmt.Sprint("test", i)), b, nil)
	}
	writeCost := utee.Tick() - st
	st = utee.Tick()
	iter = db.NewIterator(nil, nil)
	for iter.Next() {
		key := iter.Key()
		value := iter.Value()
		log.Println("@key:", string(key), "@value:", string(value))
		err = db.Delete([]byte("key"), nil)
		utee.Chk(err)
	}
	log.Println("100,0000  read cost ", (utee.Tick() - st), "m second")
	log.Println("100,0000  write cost ", writeCost, "m second")

	iter.Release()
	if err := iter.Error(); err != nil {
		log.Println("iter @err:", err)
	}
	log.Println("finish")
}
示例#21
0
func main() {
	fmt.Println("test")

	pool, err := tunny.CreatePool(10, func(input interface{}) interface{} {
		v := input.(string)
		time.Sleep(time.Second * time.Duration(5))
		log.Println("execute @v:", v)
		return nil
	}).Open()
	utee.Chk(err)

	messages := make(chan string, 1000000)
	for i := 0; i < 1000000; i++ {
		messages <- fmt.Sprint("test", i)
	}

	for line := range messages {
		go pool.SendWorkTimed(5.1*1000, line)
	}

}
示例#22
0
func main() {
	fmt.Println("hello")
	mg := connect("192.168.56.101/figoTest")
	cp := mg.Copy()
	defer cp.Close()

	s := &Student{
		Id:         bson.NewObjectId(),
		ds:         cp,
		name:       "figo",
		visitTimes: 10,
	}
	err := cp.DB(DB).C(C).Insert(s)
	utee.Chk(err)
	A := func() {
		s.incTest()
	}
	B := func() {
		s.setTest()
	}
	log.Println("inc 10000 times cost:", execute(A, 10000))
	log.Println("set 10000 times cost:", execute(B, 10000))
}
示例#23
0
func connect(db_connection string) *mgo.Session {
	session, err := mgo.Dial(db_connection)
	utee.Chk(err)
	return session
}
示例#24
0
func main() {

	data := map[string]interface{}{}
	dec := json.NewDecoder(strings.NewReader(iMsgPack))
	dec.Decode(&data)
	jq := jsonq.NewQuery(data)
	obj, err := jq.Object()
	utee.Chk(err)
	log.Println("format1:", obj, " @len:", len(obj))

	data = map[string]interface{}{}
	dec = json.NewDecoder(strings.NewReader(aMsgPack))
	dec.Decode(&data)
	jq = jsonq.NewQuery(data)
	obj, err = jq.Object()
	utee.Chk(err)
	log.Println("format2:", obj, " @len:", len(obj))

	data = map[string]interface{}{}
	dec = json.NewDecoder(strings.NewReader("asdfasdf{figo hello"))
	dec.Decode(&data)
	jq = jsonq.NewQuery(data)
	obj, err = jq.Object()
	utee.Chk(err)
	log.Println("format3:", obj, " @len:", len(obj))

	data = map[string]interface{}{}
	dec = json.NewDecoder(strings.NewReader(jsonstring))
	dec.Decode(&data)
	jq = jsonq.NewQuery(data)

	// data["foo"] -> 1
	i, err := jq.Int("foo")
	utee.Chk(err)
	log.Println("data['foo'] -> 1 : ", i)
	i, err = jq.Int("subobj", "subarray", "1")
	utee.Chk(err)
	log.Println("data['subobj']['subarray'][1] -> 2: ", i)
	s, err := jq.String("subobj", "subsubobj", "array", "0")
	utee.Chk(err)
	log.Println("data['subobj']['subarray']['array'][0] -> 'hello' ", s)

	// data["subobj"] -> map[string]interface{}{"subobj": ...}
	obj, err = jq.Object("subobj")
	utee.Chk(err)
	log.Println("jq.Object('subobj') : ", obj)

	type item map[string]interface{}

	log.Println("====================>")
	jsa := wrapAsArray(msgstring)
	log.Println("@jsa:   ", jsa)
	data2 := map[string]interface{}{}
	dec = json.NewDecoder(strings.NewReader(jsa))
	dec.Decode(&data2)
	jq = jsonq.NewQuery(data2)

	action, err := jq.Int("items", "0", "action", "data", "action")
	utee.Chk(err)
	log.Println("@action:", action)
	msgSign, err := jq.String("items", "0", "action", "data", "msgSign")
	utee.Chk(err)
	log.Println("@msgSign:", msgSign)

	arr, err := jq.Array("items")
	utee.Chk(err)
	log.Println("len of arr is :", len(arr))

	jsb := wrapAsArray(tokenMsg)
	data3 := map[string]interface{}{}

	dec = json.NewDecoder(strings.NewReader(jsb))
	dec.Decode(&data3)
	jq = jsonq.NewQuery(data3)

	aName, err := jq.String("items", "0", "action", "name")
	utee.Chk(err)
	log.Println("@aName:", aName)

	certType, err := jq.Int("items", "0", "action", "data", "certType")
	utee.Chk(err)

	log.Println("@cerType:", certType)
	token, err := jq.String("items", "0", "action", "data", "deviceToken", "dt")
	utee.Chk(err)
	log.Println("@token:", token)

	tid, err := jq.String("items", "0", "device", "deviceId", "tid")
	utee.Chk(err)
	log.Println("@tid:", tid)
	jsb = wrapAsArray(androidTokenMsg)
	dec = json.NewDecoder(strings.NewReader(jsb))
	dec.Decode(&data3)
	jq = jsonq.NewQuery(data3)

	arr2, err := jq.ArrayOfObjects("items", "0", "appContext", "push")
	utee.Chk(err)
	for _, v := range arr2 {
		log.Println(" @v:", v)
		for kk, vv := range v {
			log.Println("\t\t @k:", kk, " @vv:", vv)
		}
	}

	d2 := map[string]interface{}{}
	dec = json.NewDecoder(strings.NewReader(samplePack))
	dec.Decode(&d2)
	jq = jsonq.NewQuery(d2)
	s, err = jq.String("appContext", "push")
	utee.Chk(err)
	log.Println("@s:", s)

	dec = json.NewDecoder(strings.NewReader(wrapAsArray(s)))
	dec.Decode(&d2)
	jq = jsonq.NewQuery(d2)
	dataMap, err := jq.Object("items", "0")
	utee.Chk(err)
	log.Println("@channel:", dataMap["channel"])
	arr, err = jq.Array("items")
	utee.Chk(err)
	for i = 0; i < len(arr); i++ {
		log.Println(arr[i])
	}
	for i, v := range arr {
		log.Println("@i:", i, " @v:", v)
		log.Println(v.(map[string]interface{})["channel"])
	}

}