mirror of
https://gitee.com/johng/gf
synced 2026-06-26 17:35:40 +08:00
Merge branch 'gogf:master' into master
This commit is contained in:
@ -4,7 +4,7 @@
|
||||
[database.logger]
|
||||
Level = "all"
|
||||
Stdout = true
|
||||
CtxKeys = ["Trace-Id"]
|
||||
CtxKeys = ["RequestId"]
|
||||
[database.default]
|
||||
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
|
||||
debug = true
|
||||
|
||||
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/os/gctx"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@ -24,13 +25,16 @@ func init() {
|
||||
}
|
||||
|
||||
func main() {
|
||||
wg := sync.WaitGroup{}
|
||||
var (
|
||||
wg = sync.WaitGroup{}
|
||||
ctx = gctx.New()
|
||||
)
|
||||
for i := 0; i < 100000; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
time.Sleep(10 * time.Second)
|
||||
db.Table("user").Where("id=1").All()
|
||||
db.Ctx(ctx).Model("user").Where("id=1").All()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
@ -3,26 +3,22 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/os/gctx"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
var (
|
||||
db = g.DB()
|
||||
ctx = gctx.New()
|
||||
)
|
||||
// 开启调试模式,以便于记录所有执行的SQL
|
||||
db.SetDebug(true)
|
||||
|
||||
r, e := db.GetAll("SELECT * from `user` where id in(?)", g.Slice{})
|
||||
r, e := db.Ctx(ctx).GetAll("SELECT * from `user` where id in(?)", g.Slice{})
|
||||
if e != nil {
|
||||
fmt.Println(e)
|
||||
}
|
||||
if r != nil {
|
||||
fmt.Println(r)
|
||||
}
|
||||
return
|
||||
//r, e := db.Table("user").Where("id in(?)", g.Slice{}).All()
|
||||
//if e != nil {
|
||||
// fmt.Println(e)
|
||||
//}
|
||||
//if r != nil {
|
||||
// fmt.Println(r.List())
|
||||
//}
|
||||
}
|
||||
|
||||
@ -2,12 +2,18 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/os/gctx"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
var (
|
||||
db = g.DB()
|
||||
ctx = gctx.New()
|
||||
)
|
||||
|
||||
db.Table("user").Where("nickname like ? and passport like ?", g.Slice{"T3", "t3"}).OrderBy("id asc").All()
|
||||
db.Ctx(ctx).Model("user").
|
||||
Where("nickname like ? and passport like ?", g.Slice{"T3", "t3"}).
|
||||
OrderAsc("id").All()
|
||||
|
||||
conditions := g.Map{
|
||||
"nickname like ?": "%T%",
|
||||
@ -16,8 +22,8 @@ func main() {
|
||||
"create_time > ?": 0,
|
||||
"id in(?)": g.Slice{1, 2, 3},
|
||||
}
|
||||
db.Table("user").Where(conditions).OrderBy("id asc").All()
|
||||
db.Ctx(ctx).Model("user").Where(conditions).OrderAsc("id").All()
|
||||
|
||||
var params []interface{}
|
||||
db.Table("user").Where("1=1", params).OrderBy("id asc").All()
|
||||
db.Ctx(ctx).Model("user").Where("1=1", params).OrderAsc("id").All()
|
||||
}
|
||||
|
||||
@ -3,10 +3,14 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/os/gctx"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
var (
|
||||
db = g.DB()
|
||||
ctx = gctx.New()
|
||||
)
|
||||
db.SetDebug(true)
|
||||
list := make(g.List, 0)
|
||||
for i := 0; i < 100; i++ {
|
||||
@ -14,7 +18,7 @@ func main() {
|
||||
"name": fmt.Sprintf(`name_%d`, i),
|
||||
})
|
||||
}
|
||||
r, e := db.Table("user").Data(list).Batch(2).Insert()
|
||||
r, e := db.Ctx(ctx).Model("user").Data(list).Batch(2).Insert()
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/os/gctx"
|
||||
|
||||
"github.com/gogf/gf/crypto/gaes"
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
@ -19,6 +20,9 @@ func main() {
|
||||
Role: "master",
|
||||
Charset: "utf8",
|
||||
})
|
||||
var (
|
||||
ctx = gctx.New()
|
||||
)
|
||||
db, err := gdb.New()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -33,7 +37,7 @@ func main() {
|
||||
}
|
||||
|
||||
// 写入
|
||||
r, err := db.Table("user").Data(g.Map{
|
||||
r, err := db.Ctx(ctx).Model("user").Data(g.Map{
|
||||
"uid": 1,
|
||||
"name": encryptedName,
|
||||
}).Save()
|
||||
@ -43,9 +47,9 @@ func main() {
|
||||
fmt.Println(r.RowsAffected())
|
||||
|
||||
// 查询
|
||||
one, err := db.Table("user").Where("name=?", encryptedName).One()
|
||||
one, err := db.Ctx(ctx).Model("user").Where("name=?", encryptedName).One()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(one.ToMap())
|
||||
fmt.Println(one.Map())
|
||||
}
|
||||
|
||||
@ -2,19 +2,23 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/os/gctx"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
var (
|
||||
db = g.DB()
|
||||
ctx = gctx.New()
|
||||
)
|
||||
db.SetDebug(true)
|
||||
|
||||
r, e := db.Table("test").All()
|
||||
r, e := db.Ctx(ctx).Model("test").All()
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
if r != nil {
|
||||
fmt.Println(r.ToList())
|
||||
fmt.Println(r.List())
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/os/gctx"
|
||||
"github.com/gogf/gf/util/gutil"
|
||||
"time"
|
||||
)
|
||||
@ -17,6 +18,9 @@ func main() {
|
||||
Role: "master",
|
||||
Charset: "utf8",
|
||||
})
|
||||
var (
|
||||
ctx = gctx.New()
|
||||
)
|
||||
db, err := gdb.New()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -27,7 +31,7 @@ func main() {
|
||||
|
||||
// 执行2次查询并将查询结果缓存3秒,并可执行缓存名称(可选)
|
||||
for i := 0; i < 3; i++ {
|
||||
r, _ := db.Table("user").Cache(3000*time.Second).Where("id=?", 1).One()
|
||||
r, _ := db.Ctx(ctx).Model("user").Cache(3000*time.Second).Where("id=?", 1).One()
|
||||
gutil.Dump(r.Map())
|
||||
}
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@ import (
|
||||
|
||||
func main() {
|
||||
// error!
|
||||
r, err := g.DB().Table("user").Where(g.Map{
|
||||
r, err := g.DB().Model("user").Where(g.Map{
|
||||
"or": g.Map{
|
||||
"nickname": "jim",
|
||||
"create_time > ": "2019-10-01",
|
||||
|
||||
@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
if r, err := g.DB().Table("user").Where("uid=?", 1).One(); err == nil {
|
||||
if r, err := g.DB().Model("user").Where("uid=?", 1).One(); err == nil {
|
||||
fmt.Println(r["uid"].Int())
|
||||
fmt.Println(r["name"].String())
|
||||
} else {
|
||||
|
||||
@ -8,7 +8,7 @@ import (
|
||||
|
||||
func main() {
|
||||
g.Config().SetFileName("config2.toml")
|
||||
if r, err := g.DB().Table("user").Where("uid=?", 1).One(); err == nil {
|
||||
if r, err := g.DB().Model("user").Where("uid=?", 1).One(); err == nil {
|
||||
fmt.Println(r["uid"].Int())
|
||||
fmt.Println(r["name"].String())
|
||||
} else {
|
||||
|
||||
@ -8,14 +8,14 @@ import (
|
||||
|
||||
func main() {
|
||||
g.Config().SetFileName("config3.toml")
|
||||
if r, err := g.DB().Table("user").Where("uid=?", 1).One(); err == nil {
|
||||
if r, err := g.DB().Model("user").Where("uid=?", 1).One(); err == nil {
|
||||
fmt.Println(r["uid"].Int())
|
||||
fmt.Println(r["name"].String())
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
if r, err := g.DB("user").Table("user").Where("uid=?", 1).One(); err == nil {
|
||||
if r, err := g.DB("user").Model("user").Where("uid=?", 1).One(); err == nil {
|
||||
fmt.Println(r["uid"].Int())
|
||||
fmt.Println(r["name"].String())
|
||||
} else {
|
||||
|
||||
@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx := context.WithValue(context.Background(), "Trace-Id", "123456789")
|
||||
ctx := context.WithValue(context.Background(), "RequestId", "123456789")
|
||||
_, err := g.DB().Ctx(ctx).Query("SELECT 1")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
||||
@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx := context.WithValue(context.Background(), "Trace-Id", "123456789")
|
||||
ctx := context.WithValue(context.Background(), "RequestId", "123456789")
|
||||
_, err := g.DB().Model("user").Ctx(ctx).All()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
||||
@ -2,23 +2,20 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/os/gctx"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.Database()
|
||||
var (
|
||||
db = g.DB()
|
||||
ctx = gctx.New()
|
||||
)
|
||||
db.SetDebug(true)
|
||||
|
||||
//r, err := db.Table("user").Data("create_time", gtime.Now().String()).Insert()
|
||||
//if err == nil {
|
||||
// fmt.Println(r.LastInsertId())
|
||||
//} else {
|
||||
// panic(err)
|
||||
//}
|
||||
|
||||
r, err := db.Table("user").Data(g.Map{
|
||||
r, err := db.Ctx(ctx).Model("user").Data(g.Map{
|
||||
"name": "john",
|
||||
"create_time": gtime.Now().String(),
|
||||
}).Insert()
|
||||
|
||||
@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/os/gctx"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
)
|
||||
|
||||
@ -17,6 +18,9 @@ func main() {
|
||||
Role: "master",
|
||||
Charset: "utf8",
|
||||
})
|
||||
var (
|
||||
ctx = gctx.New()
|
||||
)
|
||||
db, err := gdb.New()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@ -27,11 +31,11 @@ func main() {
|
||||
|
||||
// 执行3条SQL查询
|
||||
for i := 1; i <= 3; i++ {
|
||||
db.Table("user").Where("uid=?", i).One()
|
||||
db.Ctx(ctx).Model("user").Where("uid=?", i).One()
|
||||
}
|
||||
// 构造一条错误查询
|
||||
db.Table("user").Where("no_such_field=?", "just_test").One()
|
||||
db.Model("user").Where("no_such_field=?", "just_test").One()
|
||||
|
||||
db.Table("user").Data(g.Map{"name": "smith"}).Where("uid=?", 1).Save()
|
||||
db.Ctx(ctx).Model("user").Data(g.Map{"name": "smith"}).Where("uid=?", 1).Save()
|
||||
|
||||
}
|
||||
|
||||
@ -2,17 +2,21 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/os/gctx"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
var (
|
||||
db = g.DB()
|
||||
ctx = gctx.New()
|
||||
)
|
||||
|
||||
// 执行3条SQL查询
|
||||
for i := 1; i <= 3; i++ {
|
||||
db.Table("user").Where("id=?", i).One()
|
||||
db.Ctx(ctx).Model("user").Where("id=?", i).One()
|
||||
}
|
||||
// 构造一条错误查询
|
||||
db.Table("user").Where("no_such_field=?", "just_test").One()
|
||||
db.Ctx(ctx).Model("user").Where("no_such_field=?", "just_test").One()
|
||||
|
||||
db.Table("user").Data(g.Map{"name": "smith"}).Where("uid=?", 1).Save()
|
||||
db.Ctx(ctx).Model("user").Data(g.Map{"name": "smith"}).Where("uid=?", 1).Save()
|
||||
}
|
||||
|
||||
@ -18,7 +18,7 @@ func main() {
|
||||
|
||||
db.SetDebug(true)
|
||||
|
||||
r, e := db.Table("user").Data(g.Map{
|
||||
r, e := db.Model("user").Data(g.Map{
|
||||
"create_at": "now()",
|
||||
}).Unscoped().Insert()
|
||||
if e != nil {
|
||||
|
||||
@ -8,7 +8,7 @@ import (
|
||||
|
||||
var (
|
||||
tableName = "orders"
|
||||
dao = g.DB().Table(tableName).Safe()
|
||||
dao = g.DB().Model(tableName).Safe()
|
||||
)
|
||||
|
||||
type OrderServiceEntity struct {
|
||||
|
||||
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/os/gctx"
|
||||
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/encoding/gparser"
|
||||
@ -19,19 +20,22 @@ func main() {
|
||||
Role: "master",
|
||||
Charset: "utf8",
|
||||
})
|
||||
db := g.DB()
|
||||
one, err := db.Table("user").Where("id=?", 1).One()
|
||||
var (
|
||||
db = g.DB()
|
||||
ctx = gctx.New()
|
||||
)
|
||||
one, err := db.Ctx(ctx).Model("user").Where("id=?", 1).One()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// 使用内置方法转换为json/xml
|
||||
fmt.Println(one.ToJson())
|
||||
fmt.Println(one.ToXml())
|
||||
fmt.Println(one.Json())
|
||||
fmt.Println(one.Xml())
|
||||
|
||||
// 自定义方法方法转换为json/xml
|
||||
jsonContent, _ := gparser.VarToJson(one.ToMap())
|
||||
jsonContent, _ := gparser.VarToJson(one.Map())
|
||||
fmt.Println(string(jsonContent))
|
||||
xmlContent, _ := gparser.VarToXml(one.ToMap())
|
||||
xmlContent, _ := gparser.VarToXml(one.Map())
|
||||
fmt.Println(string(xmlContent))
|
||||
}
|
||||
|
||||
@ -1,20 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/os/gctx"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
var (
|
||||
db = g.DB()
|
||||
ctx = gctx.New()
|
||||
)
|
||||
|
||||
// 开启调试模式,以便于记录所有执行的SQL
|
||||
db.SetDebug(true)
|
||||
|
||||
for {
|
||||
for i := 0; i < 10; i++ {
|
||||
go db.Table("user").All()
|
||||
go db.Ctx(ctx).Model("user").All()
|
||||
}
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
}
|
||||
|
||||
@ -3,14 +3,18 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/os/gctx"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
var (
|
||||
db = g.DB()
|
||||
ctx = gctx.New()
|
||||
)
|
||||
db.SetDebug(true)
|
||||
for {
|
||||
r, err := db.Table("user").All()
|
||||
r, err := db.Ctx(ctx).Model("user").All()
|
||||
fmt.Println(err)
|
||||
fmt.Println(r)
|
||||
time.Sleep(time.Second * 10)
|
||||
|
||||
@ -17,7 +17,7 @@ func main() {
|
||||
}
|
||||
user := (*User)(nil)
|
||||
fmt.Println(user)
|
||||
err := db.Table("test").Where("id=1").Struct(&user)
|
||||
err := db.Model("test").Where("id=1").Scan(&user)
|
||||
fmt.Println(err)
|
||||
fmt.Println(user)
|
||||
}
|
||||
|
||||
@ -2,13 +2,17 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/os/gctx"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
var (
|
||||
db = g.DB()
|
||||
ctx = gctx.New()
|
||||
)
|
||||
db.SetDebug(true)
|
||||
|
||||
tables, err := db.Tables()
|
||||
tables, err := db.Tables(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@ -2,20 +2,24 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/os/gctx"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db := g.DB()
|
||||
var (
|
||||
db = g.DB()
|
||||
ctx = gctx.New()
|
||||
)
|
||||
db.SetDebug(true)
|
||||
|
||||
tables, e := db.Tables()
|
||||
tables, e := db.Tables(ctx)
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
if tables != nil {
|
||||
g.Dump(tables)
|
||||
for _, table := range tables {
|
||||
fields, err := db.TableFields(table)
|
||||
fields, err := db.TableFields(ctx, table)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@ -1,26 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/os/gctx"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var (
|
||||
err error
|
||||
db = g.DB()
|
||||
ctx = gctx.New()
|
||||
table = "user"
|
||||
)
|
||||
if err = db.Transaction(func(tx *gdb.TX) error {
|
||||
if err = db.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
|
||||
// Nested transaction 1.
|
||||
if err = tx.Transaction(func(tx *gdb.TX) error {
|
||||
if err = tx.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
|
||||
_, err = tx.Model(table).Data(g.Map{"id": 1, "name": "john"}).Insert()
|
||||
return err
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
// Nested transaction 2, panic.
|
||||
if err = tx.Transaction(func(tx *gdb.TX) error {
|
||||
if err = tx.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
|
||||
_, err = tx.Model(table).Data(g.Map{"id": 2, "name": "smith"}).Insert()
|
||||
// Create a panic that can make this transaction rollback automatically.
|
||||
panic("error")
|
||||
|
||||
@ -12,7 +12,7 @@ import (
|
||||
func main() {
|
||||
db := g.DB()
|
||||
table := "medicine_clinics_upload_yinchuan"
|
||||
list, err := db.Table(table).All()
|
||||
list, err := db.Model(table).All()
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@ -9,7 +9,7 @@ import (
|
||||
func main() {
|
||||
db := g.DB()
|
||||
db.SetDebug(true)
|
||||
result, err := db.Table("pw_passageway m,pw_template t").Data("t.status", 99).Where("m.templateId=t.id AND m.status = 0").Update()
|
||||
result, err := db.Model("pw_passageway m,pw_template t").Data("t.status", 99).Where("m.templateId=t.id AND m.status = 0").Update()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
one, err := g.DB().Table("carlist c").
|
||||
one, err := g.Model("carlist c").
|
||||
LeftJoin("cardetail d", "c.postid=d.carid").
|
||||
Where("c.postid", "142039140032006").
|
||||
Fields("c.*,d.*").One()
|
||||
|
||||
@ -1,9 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/database/gdb"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/os/gctx"
|
||||
"github.com/gogf/gf/util/gmeta"
|
||||
)
|
||||
|
||||
@ -30,13 +32,13 @@ func main() {
|
||||
}
|
||||
|
||||
db := g.DB()
|
||||
db.Transaction(func(tx *gdb.TX) error {
|
||||
err := db.Transaction(gctx.New(), func(ctx context.Context, tx *gdb.TX) error {
|
||||
for i := 1; i <= 5; i++ {
|
||||
// User.
|
||||
user := User{
|
||||
Name: fmt.Sprintf(`name_%d`, i),
|
||||
}
|
||||
lastInsertId, err := db.Model(user).Data(user).OmitEmpty().InsertAndGetId()
|
||||
lastInsertId, err := db.Ctx(ctx).Model(user).Data(user).OmitEmpty().InsertAndGetId()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -45,7 +47,7 @@ func main() {
|
||||
Uid: int(lastInsertId),
|
||||
Address: fmt.Sprintf(`address_%d`, lastInsertId),
|
||||
}
|
||||
_, err = db.Model(userDetail).Data(userDetail).OmitEmpty().Insert()
|
||||
_, err = db.Ctx(ctx).Model(userDetail).Data(userDetail).OmitEmpty().Insert()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -55,7 +57,7 @@ func main() {
|
||||
Uid: int(lastInsertId),
|
||||
Score: j,
|
||||
}
|
||||
_, err = db.Model(userScore).Data(userScore).OmitEmpty().Insert()
|
||||
_, err = db.Ctx(ctx).Model(userScore).Data(userScore).OmitEmpty().Insert()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -63,4 +65,5 @@ func main() {
|
||||
}
|
||||
return nil
|
||||
})
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
@ -9,7 +9,7 @@ func test1() {
|
||||
db := g.DB()
|
||||
db.SetDebug(true)
|
||||
time.Sleep(1 * time.Minute)
|
||||
r, e := db.Table("test").Where("id", 10000).Count()
|
||||
r, e := db.Model("test").Where("id", 10000).Count()
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
@ -19,7 +19,7 @@ func test1() {
|
||||
func test2() {
|
||||
db := g.DB()
|
||||
db.SetDebug(true)
|
||||
dao := db.Table("test").Safe()
|
||||
dao := db.Model("test").Safe()
|
||||
time.Sleep(1 * time.Minute)
|
||||
r, e := dao.Where("id", 10000).Count()
|
||||
if e != nil {
|
||||
|
||||
@ -8,8 +8,8 @@ package garray
|
||||
|
||||
import "strings"
|
||||
|
||||
// apiInterfaces is used for type assert api for Interfaces.
|
||||
type apiInterfaces interface {
|
||||
// iInterfaces is used for type assert api for Interfaces.
|
||||
type iInterfaces interface {
|
||||
Interfaces() []interface{}
|
||||
}
|
||||
|
||||
|
||||
@ -709,7 +709,7 @@ func TestArray_UnmarshalValue(t *testing.T) {
|
||||
"name": "john",
|
||||
"array": []byte(`[1,2,3]`),
|
||||
}, &v)
|
||||
t.Assert(err, nil)
|
||||
t.AssertNil(err)
|
||||
t.Assert(v.Name, "john")
|
||||
t.Assert(v.Array.Slice(), g.Slice{1, 2, 3})
|
||||
})
|
||||
|
||||
@ -36,8 +36,8 @@ func New(safe ...bool) *List {
|
||||
}
|
||||
}
|
||||
|
||||
// NewFrom creates and returns a list from a copy of given slice <array>.
|
||||
// The parameter <safe> is used to specify whether using list in concurrent-safety,
|
||||
// NewFrom creates and returns a list from a copy of given slice `array`.
|
||||
// The parameter `safe` is used to specify whether using list in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewFrom(array []interface{}, safe ...bool) *List {
|
||||
l := list.New()
|
||||
@ -50,7 +50,7 @@ func NewFrom(array []interface{}, safe ...bool) *List {
|
||||
}
|
||||
}
|
||||
|
||||
// PushFront inserts a new element <e> with value <v> at the front of list <l> and returns <e>.
|
||||
// PushFront inserts a new element <e> with value <v> at the front of list <l> and returns `e`.
|
||||
func (l *List) PushFront(v interface{}) (e *Element) {
|
||||
l.mu.Lock()
|
||||
if l.list == nil {
|
||||
@ -61,7 +61,7 @@ func (l *List) PushFront(v interface{}) (e *Element) {
|
||||
return
|
||||
}
|
||||
|
||||
// PushBack inserts a new element <e> with value <v> at the back of list <l> and returns <e>.
|
||||
// PushBack inserts a new element <e> with value <v> at the back of list <l> and returns `e`.
|
||||
func (l *List) PushBack(v interface{}) (e *Element) {
|
||||
l.mu.Lock()
|
||||
if l.list == nil {
|
||||
@ -72,7 +72,7 @@ func (l *List) PushBack(v interface{}) (e *Element) {
|
||||
return
|
||||
}
|
||||
|
||||
// PushFronts inserts multiple new elements with values <values> at the front of list <l>.
|
||||
// PushFronts inserts multiple new elements with values <values> at the front of list `l`.
|
||||
func (l *List) PushFronts(values []interface{}) {
|
||||
l.mu.Lock()
|
||||
if l.list == nil {
|
||||
@ -84,7 +84,7 @@ func (l *List) PushFronts(values []interface{}) {
|
||||
l.mu.Unlock()
|
||||
}
|
||||
|
||||
// PushBacks inserts multiple new elements with values <values> at the back of list <l>.
|
||||
// PushBacks inserts multiple new elements with values <values> at the back of list `l`.
|
||||
func (l *List) PushBacks(values []interface{}) {
|
||||
l.mu.Lock()
|
||||
if l.list == nil {
|
||||
@ -96,7 +96,7 @@ func (l *List) PushBacks(values []interface{}) {
|
||||
l.mu.Unlock()
|
||||
}
|
||||
|
||||
// PopBack removes the element from back of <l> and returns the value of the element.
|
||||
// PopBack removes the element from back of `l` and returns the value of the element.
|
||||
func (l *List) PopBack() (value interface{}) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
@ -110,7 +110,7 @@ func (l *List) PopBack() (value interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
// PopFront removes the element from front of <l> and returns the value of the element.
|
||||
// PopFront removes the element from front of `l` and returns the value of the element.
|
||||
func (l *List) PopFront() (value interface{}) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
@ -124,7 +124,7 @@ func (l *List) PopFront() (value interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
// PopBacks removes <max> elements from back of <l>
|
||||
// PopBacks removes <max> elements from back of `l`
|
||||
// and returns values of the removed elements as slice.
|
||||
func (l *List) PopBacks(max int) (values []interface{}) {
|
||||
l.mu.Lock()
|
||||
@ -146,7 +146,7 @@ func (l *List) PopBacks(max int) (values []interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
// PopFronts removes <max> elements from front of <l>
|
||||
// PopFronts removes <max> elements from front of `l`
|
||||
// and returns values of the removed elements as slice.
|
||||
func (l *List) PopFronts(max int) (values []interface{}) {
|
||||
l.mu.Lock()
|
||||
@ -168,19 +168,19 @@ func (l *List) PopFronts(max int) (values []interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
// PopBackAll removes all elements from back of <l>
|
||||
// PopBackAll removes all elements from back of `l`
|
||||
// and returns values of the removed elements as slice.
|
||||
func (l *List) PopBackAll() []interface{} {
|
||||
return l.PopBacks(-1)
|
||||
}
|
||||
|
||||
// PopFrontAll removes all elements from front of <l>
|
||||
// PopFrontAll removes all elements from front of `l`
|
||||
// and returns values of the removed elements as slice.
|
||||
func (l *List) PopFrontAll() []interface{} {
|
||||
return l.PopFronts(-1)
|
||||
}
|
||||
|
||||
// FrontAll copies and returns values of all elements from front of <l> as slice.
|
||||
// FrontAll copies and returns values of all elements from front of `l` as slice.
|
||||
func (l *List) FrontAll() (values []interface{}) {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
@ -197,7 +197,7 @@ func (l *List) FrontAll() (values []interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
// BackAll copies and returns values of all elements from back of <l> as slice.
|
||||
// BackAll copies and returns values of all elements from back of `l` as slice.
|
||||
func (l *List) BackAll() (values []interface{}) {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
@ -214,7 +214,7 @@ func (l *List) BackAll() (values []interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
// FrontValue returns value of the first element of <l> or nil if the list is empty.
|
||||
// FrontValue returns value of the first element of `l` or nil if the list is empty.
|
||||
func (l *List) FrontValue() (value interface{}) {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
@ -227,7 +227,7 @@ func (l *List) FrontValue() (value interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
// BackValue returns value of the last element of <l> or nil if the list is empty.
|
||||
// BackValue returns value of the last element of `l` or nil if the list is empty.
|
||||
func (l *List) BackValue() (value interface{}) {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
@ -240,7 +240,7 @@ func (l *List) BackValue() (value interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
// Front returns the first element of list <l> or nil if the list is empty.
|
||||
// Front returns the first element of list `l` or nil if the list is empty.
|
||||
func (l *List) Front() (e *Element) {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
@ -251,7 +251,7 @@ func (l *List) Front() (e *Element) {
|
||||
return
|
||||
}
|
||||
|
||||
// Back returns the last element of list <l> or nil if the list is empty.
|
||||
// Back returns the last element of list `l` or nil if the list is empty.
|
||||
func (l *List) Back() (e *Element) {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
@ -262,7 +262,7 @@ func (l *List) Back() (e *Element) {
|
||||
return
|
||||
}
|
||||
|
||||
// Len returns the number of elements of list <l>.
|
||||
// Len returns the number of elements of list `l`.
|
||||
// The complexity is O(1).
|
||||
func (l *List) Len() (length int) {
|
||||
l.mu.RLock()
|
||||
@ -279,9 +279,9 @@ func (l *List) Size() int {
|
||||
return l.Len()
|
||||
}
|
||||
|
||||
// MoveBefore moves element <e> to its new position before <p>.
|
||||
// If <e> or <p> is not an element of <l>, or <e> == <p>, the list is not modified.
|
||||
// The element and <p> must not be nil.
|
||||
// MoveBefore moves element <e> to its new position before `p`.
|
||||
// If <e> or <p> is not an element of <l>, or <e> == `p`, the list is not modified.
|
||||
// The element and `p` must not be nil.
|
||||
func (l *List) MoveBefore(e, p *Element) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
@ -291,9 +291,9 @@ func (l *List) MoveBefore(e, p *Element) {
|
||||
l.list.MoveBefore(e, p)
|
||||
}
|
||||
|
||||
// MoveAfter moves element <e> to its new position after <p>.
|
||||
// If <e> or <p> is not an element of <l>, or <e> == <p>, the list is not modified.
|
||||
// The element and <p> must not be nil.
|
||||
// MoveAfter moves element <e> to its new position after `p`.
|
||||
// If <e> or <p> is not an element of <l>, or <e> == `p`, the list is not modified.
|
||||
// The element and `p` must not be nil.
|
||||
func (l *List) MoveAfter(e, p *Element) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
@ -303,8 +303,8 @@ func (l *List) MoveAfter(e, p *Element) {
|
||||
l.list.MoveAfter(e, p)
|
||||
}
|
||||
|
||||
// MoveToFront moves element <e> to the front of list <l>.
|
||||
// If <e> is not an element of <l>, the list is not modified.
|
||||
// MoveToFront moves element <e> to the front of list `l`.
|
||||
// If <e> is not an element of `l`, the list is not modified.
|
||||
// The element must not be nil.
|
||||
func (l *List) MoveToFront(e *Element) {
|
||||
l.mu.Lock()
|
||||
@ -315,8 +315,8 @@ func (l *List) MoveToFront(e *Element) {
|
||||
l.list.MoveToFront(e)
|
||||
}
|
||||
|
||||
// MoveToBack moves element <e> to the back of list <l>.
|
||||
// If <e> is not an element of <l>, the list is not modified.
|
||||
// MoveToBack moves element <e> to the back of list `l`.
|
||||
// If <e> is not an element of `l`, the list is not modified.
|
||||
// The element must not be nil.
|
||||
func (l *List) MoveToBack(e *Element) {
|
||||
l.mu.Lock()
|
||||
@ -327,8 +327,8 @@ func (l *List) MoveToBack(e *Element) {
|
||||
l.list.MoveToBack(e)
|
||||
}
|
||||
|
||||
// PushBackList inserts a copy of an other list at the back of list <l>.
|
||||
// The lists <l> and <other> may be the same, but they must not be nil.
|
||||
// PushBackList inserts a copy of an other list at the back of list `l`.
|
||||
// The lists <l> and `other` may be the same, but they must not be nil.
|
||||
func (l *List) PushBackList(other *List) {
|
||||
if l != other {
|
||||
other.mu.RLock()
|
||||
@ -342,8 +342,8 @@ func (l *List) PushBackList(other *List) {
|
||||
l.list.PushBackList(other.list)
|
||||
}
|
||||
|
||||
// PushFrontList inserts a copy of an other list at the front of list <l>.
|
||||
// The lists <l> and <other> may be the same, but they must not be nil.
|
||||
// PushFrontList inserts a copy of an other list at the front of list `l`.
|
||||
// The lists <l> and `other` may be the same, but they must not be nil.
|
||||
func (l *List) PushFrontList(other *List) {
|
||||
if l != other {
|
||||
other.mu.RLock()
|
||||
@ -357,9 +357,9 @@ func (l *List) PushFrontList(other *List) {
|
||||
l.list.PushFrontList(other.list)
|
||||
}
|
||||
|
||||
// InsertAfter inserts a new element <e> with value <v> immediately after <p> and returns <e>.
|
||||
// If <p> is not an element of <l>, the list is not modified.
|
||||
// The <p> must not be nil.
|
||||
// InsertAfter inserts a new element <e> with value <v> immediately after <p> and returns `e`.
|
||||
// If <p> is not an element of `l`, the list is not modified.
|
||||
// The `p` must not be nil.
|
||||
func (l *List) InsertAfter(p *Element, v interface{}) (e *Element) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
@ -370,9 +370,9 @@ func (l *List) InsertAfter(p *Element, v interface{}) (e *Element) {
|
||||
return
|
||||
}
|
||||
|
||||
// InsertBefore inserts a new element <e> with value <v> immediately before <p> and returns <e>.
|
||||
// If <p> is not an element of <l>, the list is not modified.
|
||||
// The <p> must not be nil.
|
||||
// InsertBefore inserts a new element <e> with value <v> immediately before <p> and returns `e`.
|
||||
// If <p> is not an element of `l`, the list is not modified.
|
||||
// The `p` must not be nil.
|
||||
func (l *List) InsertBefore(p *Element, v interface{}) (e *Element) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
@ -383,7 +383,7 @@ func (l *List) InsertBefore(p *Element, v interface{}) (e *Element) {
|
||||
return
|
||||
}
|
||||
|
||||
// Remove removes <e> from <l> if <e> is an element of list <l>.
|
||||
// Remove removes <e> from <l> if <e> is an element of list `l`.
|
||||
// It returns the element value e.Value.
|
||||
// The element must not be nil.
|
||||
func (l *List) Remove(e *Element) (value interface{}) {
|
||||
@ -396,7 +396,7 @@ func (l *List) Remove(e *Element) (value interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
// Removes removes multiple elements <es> from <l> if <es> are elements of list <l>.
|
||||
// Removes removes multiple elements <es> from <l> if <es> are elements of list `l`.
|
||||
func (l *List) Removes(es []*Element) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
@ -409,7 +409,7 @@ func (l *List) Removes(es []*Element) {
|
||||
return
|
||||
}
|
||||
|
||||
// RemoveAll removes all elements from list <l>.
|
||||
// RemoveAll removes all elements from list `l`.
|
||||
func (l *List) RemoveAll() {
|
||||
l.mu.Lock()
|
||||
l.list = list.New()
|
||||
@ -421,7 +421,7 @@ func (l *List) Clear() {
|
||||
l.RemoveAll()
|
||||
}
|
||||
|
||||
// RLockFunc locks reading with given callback function <f> within RWMutex.RLock.
|
||||
// RLockFunc locks reading with given callback function `f` within RWMutex.RLock.
|
||||
func (l *List) RLockFunc(f func(list *list.List)) {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
@ -430,7 +430,7 @@ func (l *List) RLockFunc(f func(list *list.List)) {
|
||||
}
|
||||
}
|
||||
|
||||
// LockFunc locks writing with given callback function <f> within RWMutex.Lock.
|
||||
// LockFunc locks writing with given callback function `f` within RWMutex.Lock.
|
||||
func (l *List) LockFunc(f func(list *list.List)) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
@ -445,8 +445,8 @@ func (l *List) Iterator(f func(e *Element) bool) {
|
||||
l.IteratorAsc(f)
|
||||
}
|
||||
|
||||
// IteratorAsc iterates the list readonly in ascending order with given callback function <f>.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// IteratorAsc iterates the list readonly in ascending order with given callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (l *List) IteratorAsc(f func(e *Element) bool) {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
@ -463,8 +463,8 @@ func (l *List) IteratorAsc(f func(e *Element) bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// IteratorDesc iterates the list readonly in descending order with given callback function <f>.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// IteratorDesc iterates the list readonly in descending order with given callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (l *List) IteratorDesc(f func(e *Element) bool) {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
@ -481,7 +481,7 @@ func (l *List) IteratorDesc(f func(e *Element) bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// Join joins list elements with a string <glue>.
|
||||
// Join joins list elements with a string `glue`.
|
||||
func (l *List) Join(glue string) string {
|
||||
l.mu.RLock()
|
||||
defer l.mu.RUnlock()
|
||||
|
||||
@ -13,32 +13,32 @@ type (
|
||||
)
|
||||
|
||||
// New creates and returns an empty hash map.
|
||||
// The parameter <safe> is used to specify whether using map in concurrent-safety,
|
||||
// The parameter `safe` is used to specify whether using map in concurrent-safety,
|
||||
// which is false in default.
|
||||
func New(safe ...bool) *Map {
|
||||
return NewAnyAnyMap(safe...)
|
||||
}
|
||||
|
||||
// NewFrom creates and returns a hash map from given map <data>.
|
||||
// Note that, the param <data> map will be set as the underlying data map(no deep copy),
|
||||
// NewFrom creates and returns a hash map from given map `data`.
|
||||
// Note that, the param `data` map will be set as the underlying data map(no deep copy),
|
||||
// there might be some concurrent-safe issues when changing the map outside.
|
||||
// The parameter <safe> is used to specify whether using tree in concurrent-safety,
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewFrom(data map[interface{}]interface{}, safe ...bool) *Map {
|
||||
return NewAnyAnyMapFrom(data, safe...)
|
||||
}
|
||||
|
||||
// NewHashMap creates and returns an empty hash map.
|
||||
// The parameter <safe> is used to specify whether using map in concurrent-safety,
|
||||
// The parameter `safe` is used to specify whether using map in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewHashMap(safe ...bool) *Map {
|
||||
return NewAnyAnyMap(safe...)
|
||||
}
|
||||
|
||||
// NewHashMapFrom creates and returns a hash map from given map <data>.
|
||||
// Note that, the param <data> map will be set as the underlying data map(no deep copy),
|
||||
// NewHashMapFrom creates and returns a hash map from given map `data`.
|
||||
// Note that, the param `data` map will be set as the underlying data map(no deep copy),
|
||||
// there might be some concurrent-safe issues when changing the map outside.
|
||||
// The parameter <safe> is used to specify whether using tree in concurrent-safety,
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewHashMapFrom(data map[interface{}]interface{}, safe ...bool) *Map {
|
||||
return NewAnyAnyMapFrom(data, safe...)
|
||||
|
||||
@ -23,7 +23,7 @@ type AnyAnyMap struct {
|
||||
}
|
||||
|
||||
// NewAnyAnyMap creates and returns an empty hash map.
|
||||
// The parameter <safe> is used to specify whether using map in concurrent-safety,
|
||||
// The parameter `safe` is used to specify whether using map in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewAnyAnyMap(safe ...bool) *AnyAnyMap {
|
||||
return &AnyAnyMap{
|
||||
@ -32,8 +32,8 @@ func NewAnyAnyMap(safe ...bool) *AnyAnyMap {
|
||||
}
|
||||
}
|
||||
|
||||
// NewAnyAnyMapFrom creates and returns a hash map from given map <data>.
|
||||
// Note that, the param <data> map will be set as the underlying data map(no deep copy),
|
||||
// NewAnyAnyMapFrom creates and returns a hash map from given map `data`.
|
||||
// Note that, the param `data` map will be set as the underlying data map(no deep copy),
|
||||
// there might be some concurrent-safe issues when changing the map outside.
|
||||
func NewAnyAnyMapFrom(data map[interface{}]interface{}, safe ...bool) *AnyAnyMap {
|
||||
return &AnyAnyMap{
|
||||
@ -42,8 +42,8 @@ func NewAnyAnyMapFrom(data map[interface{}]interface{}, safe ...bool) *AnyAnyMap
|
||||
}
|
||||
}
|
||||
|
||||
// Iterator iterates the hash map readonly with custom callback function <f>.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// Iterator iterates the hash map readonly with custom callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (m *AnyAnyMap) Iterator(f func(k interface{}, v interface{}) bool) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
@ -143,8 +143,8 @@ func (m *AnyAnyMap) Sets(data map[interface{}]interface{}) {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// Search searches the map with given <key>.
|
||||
// Second return parameter <found> is true if key was found, otherwise false.
|
||||
// Search searches the map with given `key`.
|
||||
// Second return parameter `found` is true if key was found, otherwise false.
|
||||
func (m *AnyAnyMap) Search(key interface{}) (value interface{}, found bool) {
|
||||
m.mu.RLock()
|
||||
if m.data != nil {
|
||||
@ -154,7 +154,7 @@ func (m *AnyAnyMap) Search(key interface{}) (value interface{}, found bool) {
|
||||
return
|
||||
}
|
||||
|
||||
// Get returns the value by given <key>.
|
||||
// Get returns the value by given `key`.
|
||||
func (m *AnyAnyMap) Get(key interface{}) (value interface{}) {
|
||||
m.mu.RLock()
|
||||
if m.data != nil {
|
||||
@ -175,7 +175,7 @@ func (m *AnyAnyMap) Pop() (key, value interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
// Pops retrieves and deletes <size> items from the map.
|
||||
// Pops retrieves and deletes `size` items from the map.
|
||||
// It returns all items if size == -1.
|
||||
func (m *AnyAnyMap) Pops(size int) map[interface{}]interface{} {
|
||||
m.mu.Lock()
|
||||
@ -202,14 +202,14 @@ func (m *AnyAnyMap) Pops(size int) map[interface{}]interface{} {
|
||||
}
|
||||
|
||||
// doSetWithLockCheck checks whether value of the key exists with mutex.Lock,
|
||||
// if not exists, set value to the map with given <key>,
|
||||
// if not exists, set value to the map with given `key`,
|
||||
// or else just return the existing value.
|
||||
//
|
||||
// When setting value, if <value> is type of <func() interface {}>,
|
||||
// When setting value, if `value` is type of <func() interface {}>,
|
||||
// it will be executed with mutex.Lock of the hash map,
|
||||
// and its return value will be set to the map with <key>.
|
||||
// and its return value will be set to the map with `key`.
|
||||
//
|
||||
// It returns value with given <key>.
|
||||
// It returns value with given `key`.
|
||||
func (m *AnyAnyMap) doSetWithLockCheck(key interface{}, value interface{}) interface{} {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -229,7 +229,7 @@ func (m *AnyAnyMap) doSetWithLockCheck(key interface{}, value interface{}) inter
|
||||
}
|
||||
|
||||
// GetOrSet returns the value by key,
|
||||
// or sets value with given <value> if it does not exist and then returns this value.
|
||||
// or sets value with given `value` if it does not exist and then returns this value.
|
||||
func (m *AnyAnyMap) GetOrSet(key interface{}, value interface{}) interface{} {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
return m.doSetWithLockCheck(key, value)
|
||||
@ -239,7 +239,7 @@ func (m *AnyAnyMap) GetOrSet(key interface{}, value interface{}) interface{} {
|
||||
}
|
||||
|
||||
// GetOrSetFunc returns the value by key,
|
||||
// or sets value with returned value of callback function <f> if it does not exist
|
||||
// or sets value with returned value of callback function `f` if it does not exist
|
||||
// and then returns this value.
|
||||
func (m *AnyAnyMap) GetOrSetFunc(key interface{}, f func() interface{}) interface{} {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
@ -250,10 +250,10 @@ func (m *AnyAnyMap) GetOrSetFunc(key interface{}, f func() interface{}) interfac
|
||||
}
|
||||
|
||||
// GetOrSetFuncLock returns the value by key,
|
||||
// or sets value with returned value of callback function <f> if it does not exist
|
||||
// or sets value with returned value of callback function `f` if it does not exist
|
||||
// and then returns this value.
|
||||
//
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function <f>
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`
|
||||
// with mutex.Lock of the hash map.
|
||||
func (m *AnyAnyMap) GetOrSetFuncLock(key interface{}, f func() interface{}) interface{} {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
@ -263,7 +263,7 @@ func (m *AnyAnyMap) GetOrSetFuncLock(key interface{}, f func() interface{}) inte
|
||||
}
|
||||
}
|
||||
|
||||
// GetVar returns a Var with the value by given <key>.
|
||||
// GetVar returns a Var with the value by given `key`.
|
||||
// The returned Var is un-concurrent safe.
|
||||
func (m *AnyAnyMap) GetVar(key interface{}) *gvar.Var {
|
||||
return gvar.New(m.Get(key))
|
||||
@ -287,8 +287,8 @@ func (m *AnyAnyMap) GetVarOrSetFuncLock(key interface{}, f func() interface{}) *
|
||||
return gvar.New(m.GetOrSetFuncLock(key, f))
|
||||
}
|
||||
|
||||
// SetIfNotExist sets <value> to the map if the <key> does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExist sets <value> to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
func (m *AnyAnyMap) SetIfNotExist(key interface{}, value interface{}) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, value)
|
||||
@ -297,8 +297,8 @@ func (m *AnyAnyMap) SetIfNotExist(key interface{}, value interface{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SetIfNotExistFunc sets value with return value of callback function <f>, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
func (m *AnyAnyMap) SetIfNotExistFunc(key interface{}, f func() interface{}) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, f())
|
||||
@ -307,11 +307,11 @@ func (m *AnyAnyMap) SetIfNotExistFunc(key interface{}, f func() interface{}) boo
|
||||
return false
|
||||
}
|
||||
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function <f>, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function <f> with mutex.Lock of the hash map.
|
||||
// it executes function `f` with mutex.Lock of the hash map.
|
||||
func (m *AnyAnyMap) SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, f)
|
||||
@ -320,7 +320,7 @@ func (m *AnyAnyMap) SetIfNotExistFuncLock(key interface{}, f func() interface{})
|
||||
return false
|
||||
}
|
||||
|
||||
// Remove deletes value from map by given <key>, and return this deleted value.
|
||||
// Remove deletes value from map by given `key`, and return this deleted value.
|
||||
func (m *AnyAnyMap) Remove(key interface{}) (value interface{}) {
|
||||
m.mu.Lock()
|
||||
if m.data != nil {
|
||||
@ -375,7 +375,7 @@ func (m *AnyAnyMap) Values() []interface{} {
|
||||
}
|
||||
|
||||
// Contains checks whether a key exists.
|
||||
// It returns true if the <key> exists, or else false.
|
||||
// It returns true if the `key` exists, or else false.
|
||||
func (m *AnyAnyMap) Contains(key interface{}) bool {
|
||||
var ok bool
|
||||
m.mu.RLock()
|
||||
@ -407,21 +407,21 @@ func (m *AnyAnyMap) Clear() {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// Replace the data of the map with given <data>.
|
||||
// Replace the data of the map with given `data`.
|
||||
func (m *AnyAnyMap) Replace(data map[interface{}]interface{}) {
|
||||
m.mu.Lock()
|
||||
m.data = data
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// LockFunc locks writing with given callback function <f> within RWMutex.Lock.
|
||||
// LockFunc locks writing with given callback function `f` within RWMutex.Lock.
|
||||
func (m *AnyAnyMap) LockFunc(f func(m map[interface{}]interface{})) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
f(m.data)
|
||||
}
|
||||
|
||||
// RLockFunc locks reading with given callback function <f> within RWMutex.RLock.
|
||||
// RLockFunc locks reading with given callback function `f` within RWMutex.RLock.
|
||||
func (m *AnyAnyMap) RLockFunc(f func(m map[interface{}]interface{})) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
@ -440,7 +440,7 @@ func (m *AnyAnyMap) Flip() {
|
||||
}
|
||||
|
||||
// Merge merges two hash maps.
|
||||
// The <other> map will be merged into the map <m>.
|
||||
// The <other> map will be merged into the map `m`.
|
||||
func (m *AnyAnyMap) Merge(other *AnyAnyMap) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -460,7 +460,7 @@ func (m *AnyAnyMap) Merge(other *AnyAnyMap) {
|
||||
// String returns the map as a string.
|
||||
func (m *AnyAnyMap) String() string {
|
||||
b, _ := m.MarshalJSON()
|
||||
return gconv.UnsafeBytesToStr(b)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
|
||||
@ -23,7 +23,7 @@ type IntAnyMap struct {
|
||||
}
|
||||
|
||||
// NewIntAnyMap returns an empty IntAnyMap object.
|
||||
// The parameter <safe> is used to specify whether using map in concurrent-safety,
|
||||
// The parameter `safe` is used to specify whether using map in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewIntAnyMap(safe ...bool) *IntAnyMap {
|
||||
return &IntAnyMap{
|
||||
@ -32,8 +32,8 @@ func NewIntAnyMap(safe ...bool) *IntAnyMap {
|
||||
}
|
||||
}
|
||||
|
||||
// NewIntAnyMapFrom creates and returns a hash map from given map <data>.
|
||||
// Note that, the param <data> map will be set as the underlying data map(no deep copy),
|
||||
// NewIntAnyMapFrom creates and returns a hash map from given map `data`.
|
||||
// Note that, the param `data` map will be set as the underlying data map(no deep copy),
|
||||
// there might be some concurrent-safe issues when changing the map outside.
|
||||
func NewIntAnyMapFrom(data map[int]interface{}, safe ...bool) *IntAnyMap {
|
||||
return &IntAnyMap{
|
||||
@ -42,8 +42,8 @@ func NewIntAnyMapFrom(data map[int]interface{}, safe ...bool) *IntAnyMap {
|
||||
}
|
||||
}
|
||||
|
||||
// Iterator iterates the hash map readonly with custom callback function <f>.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// Iterator iterates the hash map readonly with custom callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (m *IntAnyMap) Iterator(f func(k int, v interface{}) bool) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
@ -143,8 +143,8 @@ func (m *IntAnyMap) Sets(data map[int]interface{}) {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// Search searches the map with given <key>.
|
||||
// Second return parameter <found> is true if key was found, otherwise false.
|
||||
// Search searches the map with given `key`.
|
||||
// Second return parameter `found` is true if key was found, otherwise false.
|
||||
func (m *IntAnyMap) Search(key int) (value interface{}, found bool) {
|
||||
m.mu.RLock()
|
||||
if m.data != nil {
|
||||
@ -154,7 +154,7 @@ func (m *IntAnyMap) Search(key int) (value interface{}, found bool) {
|
||||
return
|
||||
}
|
||||
|
||||
// Get returns the value by given <key>.
|
||||
// Get returns the value by given `key`.
|
||||
func (m *IntAnyMap) Get(key int) (value interface{}) {
|
||||
m.mu.RLock()
|
||||
if m.data != nil {
|
||||
@ -175,7 +175,7 @@ func (m *IntAnyMap) Pop() (key int, value interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
// Pops retrieves and deletes <size> items from the map.
|
||||
// Pops retrieves and deletes `size` items from the map.
|
||||
// It returns all items if size == -1.
|
||||
func (m *IntAnyMap) Pops(size int) map[int]interface{} {
|
||||
m.mu.Lock()
|
||||
@ -202,14 +202,14 @@ func (m *IntAnyMap) Pops(size int) map[int]interface{} {
|
||||
}
|
||||
|
||||
// doSetWithLockCheck checks whether value of the key exists with mutex.Lock,
|
||||
// if not exists, set value to the map with given <key>,
|
||||
// if not exists, set value to the map with given `key`,
|
||||
// or else just return the existing value.
|
||||
//
|
||||
// When setting value, if <value> is type of <func() interface {}>,
|
||||
// When setting value, if `value` is type of <func() interface {}>,
|
||||
// it will be executed with mutex.Lock of the hash map,
|
||||
// and its return value will be set to the map with <key>.
|
||||
// and its return value will be set to the map with `key`.
|
||||
//
|
||||
// It returns value with given <key>.
|
||||
// It returns value with given `key`.
|
||||
func (m *IntAnyMap) doSetWithLockCheck(key int, value interface{}) interface{} {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -229,7 +229,7 @@ func (m *IntAnyMap) doSetWithLockCheck(key int, value interface{}) interface{} {
|
||||
}
|
||||
|
||||
// GetOrSet returns the value by key,
|
||||
// or sets value with given <value> if it does not exist and then returns this value.
|
||||
// or sets value with given `value` if it does not exist and then returns this value.
|
||||
func (m *IntAnyMap) GetOrSet(key int, value interface{}) interface{} {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
return m.doSetWithLockCheck(key, value)
|
||||
@ -239,7 +239,7 @@ func (m *IntAnyMap) GetOrSet(key int, value interface{}) interface{} {
|
||||
}
|
||||
|
||||
// GetOrSetFunc returns the value by key,
|
||||
// or sets value with returned value of callback function <f> if it does not exist and returns this value.
|
||||
// or sets value with returned value of callback function `f` if it does not exist and returns this value.
|
||||
func (m *IntAnyMap) GetOrSetFunc(key int, f func() interface{}) interface{} {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
return m.doSetWithLockCheck(key, f())
|
||||
@ -249,9 +249,9 @@ func (m *IntAnyMap) GetOrSetFunc(key int, f func() interface{}) interface{} {
|
||||
}
|
||||
|
||||
// GetOrSetFuncLock returns the value by key,
|
||||
// or sets value with returned value of callback function <f> if it does not exist and returns this value.
|
||||
// or sets value with returned value of callback function `f` if it does not exist and returns this value.
|
||||
//
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function <f>
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`
|
||||
// with mutex.Lock of the hash map.
|
||||
func (m *IntAnyMap) GetOrSetFuncLock(key int, f func() interface{}) interface{} {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
@ -261,7 +261,7 @@ func (m *IntAnyMap) GetOrSetFuncLock(key int, f func() interface{}) interface{}
|
||||
}
|
||||
}
|
||||
|
||||
// GetVar returns a Var with the value by given <key>.
|
||||
// GetVar returns a Var with the value by given `key`.
|
||||
// The returned Var is un-concurrent safe.
|
||||
func (m *IntAnyMap) GetVar(key int) *gvar.Var {
|
||||
return gvar.New(m.Get(key))
|
||||
@ -285,8 +285,8 @@ func (m *IntAnyMap) GetVarOrSetFuncLock(key int, f func() interface{}) *gvar.Var
|
||||
return gvar.New(m.GetOrSetFuncLock(key, f))
|
||||
}
|
||||
|
||||
// SetIfNotExist sets <value> to the map if the <key> does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExist sets <value> to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
func (m *IntAnyMap) SetIfNotExist(key int, value interface{}) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, value)
|
||||
@ -295,8 +295,8 @@ func (m *IntAnyMap) SetIfNotExist(key int, value interface{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SetIfNotExistFunc sets value with return value of callback function <f>, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
func (m *IntAnyMap) SetIfNotExistFunc(key int, f func() interface{}) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, f())
|
||||
@ -305,11 +305,11 @@ func (m *IntAnyMap) SetIfNotExistFunc(key int, f func() interface{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function <f>, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function <f> with mutex.Lock of the hash map.
|
||||
// it executes function `f` with mutex.Lock of the hash map.
|
||||
func (m *IntAnyMap) SetIfNotExistFuncLock(key int, f func() interface{}) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, f)
|
||||
@ -329,7 +329,7 @@ func (m *IntAnyMap) Removes(keys []int) {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// Remove deletes value from map by given <key>, and return this deleted value.
|
||||
// Remove deletes value from map by given `key`, and return this deleted value.
|
||||
func (m *IntAnyMap) Remove(key int) (value interface{}) {
|
||||
m.mu.Lock()
|
||||
if m.data != nil {
|
||||
@ -373,7 +373,7 @@ func (m *IntAnyMap) Values() []interface{} {
|
||||
}
|
||||
|
||||
// Contains checks whether a key exists.
|
||||
// It returns true if the <key> exists, or else false.
|
||||
// It returns true if the `key` exists, or else false.
|
||||
func (m *IntAnyMap) Contains(key int) bool {
|
||||
var ok bool
|
||||
m.mu.RLock()
|
||||
@ -405,21 +405,21 @@ func (m *IntAnyMap) Clear() {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// Replace the data of the map with given <data>.
|
||||
// Replace the data of the map with given `data`.
|
||||
func (m *IntAnyMap) Replace(data map[int]interface{}) {
|
||||
m.mu.Lock()
|
||||
m.data = data
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// LockFunc locks writing with given callback function <f> within RWMutex.Lock.
|
||||
// LockFunc locks writing with given callback function `f` within RWMutex.Lock.
|
||||
func (m *IntAnyMap) LockFunc(f func(m map[int]interface{})) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
f(m.data)
|
||||
}
|
||||
|
||||
// RLockFunc locks reading with given callback function <f> within RWMutex.RLock.
|
||||
// RLockFunc locks reading with given callback function `f` within RWMutex.RLock.
|
||||
func (m *IntAnyMap) RLockFunc(f func(m map[int]interface{})) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
@ -438,7 +438,7 @@ func (m *IntAnyMap) Flip() {
|
||||
}
|
||||
|
||||
// Merge merges two hash maps.
|
||||
// The <other> map will be merged into the map <m>.
|
||||
// The <other> map will be merged into the map `m`.
|
||||
func (m *IntAnyMap) Merge(other *IntAnyMap) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -458,7 +458,7 @@ func (m *IntAnyMap) Merge(other *IntAnyMap) {
|
||||
// String returns the map as a string.
|
||||
func (m *IntAnyMap) String() string {
|
||||
b, _ := m.MarshalJSON()
|
||||
return gconv.UnsafeBytesToStr(b)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
|
||||
@ -21,7 +21,7 @@ type IntIntMap struct {
|
||||
}
|
||||
|
||||
// NewIntIntMap returns an empty IntIntMap object.
|
||||
// The parameter <safe> is used to specify whether using map in concurrent-safety,
|
||||
// The parameter `safe` is used to specify whether using map in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewIntIntMap(safe ...bool) *IntIntMap {
|
||||
return &IntIntMap{
|
||||
@ -30,8 +30,8 @@ func NewIntIntMap(safe ...bool) *IntIntMap {
|
||||
}
|
||||
}
|
||||
|
||||
// NewIntIntMapFrom creates and returns a hash map from given map <data>.
|
||||
// Note that, the param <data> map will be set as the underlying data map(no deep copy),
|
||||
// NewIntIntMapFrom creates and returns a hash map from given map `data`.
|
||||
// Note that, the param `data` map will be set as the underlying data map(no deep copy),
|
||||
// there might be some concurrent-safe issues when changing the map outside.
|
||||
func NewIntIntMapFrom(data map[int]int, safe ...bool) *IntIntMap {
|
||||
return &IntIntMap{
|
||||
@ -40,8 +40,8 @@ func NewIntIntMapFrom(data map[int]int, safe ...bool) *IntIntMap {
|
||||
}
|
||||
}
|
||||
|
||||
// Iterator iterates the hash map readonly with custom callback function <f>.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// Iterator iterates the hash map readonly with custom callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (m *IntIntMap) Iterator(f func(k int, v int) bool) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
@ -130,8 +130,8 @@ func (m *IntIntMap) Sets(data map[int]int) {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// Search searches the map with given <key>.
|
||||
// Second return parameter <found> is true if key was found, otherwise false.
|
||||
// Search searches the map with given `key`.
|
||||
// Second return parameter `found` is true if key was found, otherwise false.
|
||||
func (m *IntIntMap) Search(key int) (value int, found bool) {
|
||||
m.mu.RLock()
|
||||
if m.data != nil {
|
||||
@ -141,7 +141,7 @@ func (m *IntIntMap) Search(key int) (value int, found bool) {
|
||||
return
|
||||
}
|
||||
|
||||
// Get returns the value by given <key>.
|
||||
// Get returns the value by given `key`.
|
||||
func (m *IntIntMap) Get(key int) (value int) {
|
||||
m.mu.RLock()
|
||||
if m.data != nil {
|
||||
@ -162,7 +162,7 @@ func (m *IntIntMap) Pop() (key, value int) {
|
||||
return
|
||||
}
|
||||
|
||||
// Pops retrieves and deletes <size> items from the map.
|
||||
// Pops retrieves and deletes `size` items from the map.
|
||||
// It returns all items if size == -1.
|
||||
func (m *IntIntMap) Pops(size int) map[int]int {
|
||||
m.mu.Lock()
|
||||
@ -189,10 +189,10 @@ func (m *IntIntMap) Pops(size int) map[int]int {
|
||||
}
|
||||
|
||||
// doSetWithLockCheck checks whether value of the key exists with mutex.Lock,
|
||||
// if not exists, set value to the map with given <key>,
|
||||
// if not exists, set value to the map with given `key`,
|
||||
// or else just return the existing value.
|
||||
//
|
||||
// It returns value with given <key>.
|
||||
// It returns value with given `key`.
|
||||
func (m *IntIntMap) doSetWithLockCheck(key int, value int) int {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -207,7 +207,7 @@ func (m *IntIntMap) doSetWithLockCheck(key int, value int) int {
|
||||
}
|
||||
|
||||
// GetOrSet returns the value by key,
|
||||
// or sets value with given <value> if it does not exist and then returns this value.
|
||||
// or sets value with given `value` if it does not exist and then returns this value.
|
||||
func (m *IntIntMap) GetOrSet(key int, value int) int {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
return m.doSetWithLockCheck(key, value)
|
||||
@ -217,7 +217,7 @@ func (m *IntIntMap) GetOrSet(key int, value int) int {
|
||||
}
|
||||
|
||||
// GetOrSetFunc returns the value by key,
|
||||
// or sets value with returned value of callback function <f> if it does not exist and returns this value.
|
||||
// or sets value with returned value of callback function `f` if it does not exist and returns this value.
|
||||
func (m *IntIntMap) GetOrSetFunc(key int, f func() int) int {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
return m.doSetWithLockCheck(key, f())
|
||||
@ -227,9 +227,9 @@ func (m *IntIntMap) GetOrSetFunc(key int, f func() int) int {
|
||||
}
|
||||
|
||||
// GetOrSetFuncLock returns the value by key,
|
||||
// or sets value with returned value of callback function <f> if it does not exist and returns this value.
|
||||
// or sets value with returned value of callback function `f` if it does not exist and returns this value.
|
||||
//
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function <f>
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`
|
||||
// with mutex.Lock of the hash map.
|
||||
func (m *IntIntMap) GetOrSetFuncLock(key int, f func() int) int {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
@ -249,8 +249,8 @@ func (m *IntIntMap) GetOrSetFuncLock(key int, f func() int) int {
|
||||
}
|
||||
}
|
||||
|
||||
// SetIfNotExist sets <value> to the map if the <key> does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExist sets <value> to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
func (m *IntIntMap) SetIfNotExist(key int, value int) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, value)
|
||||
@ -259,8 +259,8 @@ func (m *IntIntMap) SetIfNotExist(key int, value int) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SetIfNotExistFunc sets value with return value of callback function <f>, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
func (m *IntIntMap) SetIfNotExistFunc(key int, f func() int) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, f())
|
||||
@ -269,11 +269,11 @@ func (m *IntIntMap) SetIfNotExistFunc(key int, f func() int) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function <f>, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function <f> with mutex.Lock of the hash map.
|
||||
// it executes function `f` with mutex.Lock of the hash map.
|
||||
func (m *IntIntMap) SetIfNotExistFuncLock(key int, f func() int) bool {
|
||||
if !m.Contains(key) {
|
||||
m.mu.Lock()
|
||||
@ -300,7 +300,7 @@ func (m *IntIntMap) Removes(keys []int) {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// Remove deletes value from map by given <key>, and return this deleted value.
|
||||
// Remove deletes value from map by given `key`, and return this deleted value.
|
||||
func (m *IntIntMap) Remove(key int) (value int) {
|
||||
m.mu.Lock()
|
||||
if m.data != nil {
|
||||
@ -344,7 +344,7 @@ func (m *IntIntMap) Values() []int {
|
||||
}
|
||||
|
||||
// Contains checks whether a key exists.
|
||||
// It returns true if the <key> exists, or else false.
|
||||
// It returns true if the `key` exists, or else false.
|
||||
func (m *IntIntMap) Contains(key int) bool {
|
||||
var ok bool
|
||||
m.mu.RLock()
|
||||
@ -376,21 +376,21 @@ func (m *IntIntMap) Clear() {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// Replace the data of the map with given <data>.
|
||||
// Replace the data of the map with given `data`.
|
||||
func (m *IntIntMap) Replace(data map[int]int) {
|
||||
m.mu.Lock()
|
||||
m.data = data
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// LockFunc locks writing with given callback function <f> within RWMutex.Lock.
|
||||
// LockFunc locks writing with given callback function `f` within RWMutex.Lock.
|
||||
func (m *IntIntMap) LockFunc(f func(m map[int]int)) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
f(m.data)
|
||||
}
|
||||
|
||||
// RLockFunc locks reading with given callback function <f> within RWMutex.RLock.
|
||||
// RLockFunc locks reading with given callback function `f` within RWMutex.RLock.
|
||||
func (m *IntIntMap) RLockFunc(f func(m map[int]int)) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
@ -409,7 +409,7 @@ func (m *IntIntMap) Flip() {
|
||||
}
|
||||
|
||||
// Merge merges two hash maps.
|
||||
// The <other> map will be merged into the map <m>.
|
||||
// The <other> map will be merged into the map `m`.
|
||||
func (m *IntIntMap) Merge(other *IntIntMap) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -429,7 +429,7 @@ func (m *IntIntMap) Merge(other *IntIntMap) {
|
||||
// String returns the map as a string.
|
||||
func (m *IntIntMap) String() string {
|
||||
b, _ := m.MarshalJSON()
|
||||
return gconv.UnsafeBytesToStr(b)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
|
||||
@ -21,7 +21,7 @@ type IntStrMap struct {
|
||||
}
|
||||
|
||||
// NewIntStrMap returns an empty IntStrMap object.
|
||||
// The parameter <safe> is used to specify whether using map in concurrent-safety,
|
||||
// The parameter `safe` is used to specify whether using map in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewIntStrMap(safe ...bool) *IntStrMap {
|
||||
return &IntStrMap{
|
||||
@ -30,8 +30,8 @@ func NewIntStrMap(safe ...bool) *IntStrMap {
|
||||
}
|
||||
}
|
||||
|
||||
// NewIntStrMapFrom creates and returns a hash map from given map <data>.
|
||||
// Note that, the param <data> map will be set as the underlying data map(no deep copy),
|
||||
// NewIntStrMapFrom creates and returns a hash map from given map `data`.
|
||||
// Note that, the param `data` map will be set as the underlying data map(no deep copy),
|
||||
// there might be some concurrent-safe issues when changing the map outside.
|
||||
func NewIntStrMapFrom(data map[int]string, safe ...bool) *IntStrMap {
|
||||
return &IntStrMap{
|
||||
@ -40,8 +40,8 @@ func NewIntStrMapFrom(data map[int]string, safe ...bool) *IntStrMap {
|
||||
}
|
||||
}
|
||||
|
||||
// Iterator iterates the hash map readonly with custom callback function <f>.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// Iterator iterates the hash map readonly with custom callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (m *IntStrMap) Iterator(f func(k int, v string) bool) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
@ -130,8 +130,8 @@ func (m *IntStrMap) Sets(data map[int]string) {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// Search searches the map with given <key>.
|
||||
// Second return parameter <found> is true if key was found, otherwise false.
|
||||
// Search searches the map with given `key`.
|
||||
// Second return parameter `found` is true if key was found, otherwise false.
|
||||
func (m *IntStrMap) Search(key int) (value string, found bool) {
|
||||
m.mu.RLock()
|
||||
if m.data != nil {
|
||||
@ -141,7 +141,7 @@ func (m *IntStrMap) Search(key int) (value string, found bool) {
|
||||
return
|
||||
}
|
||||
|
||||
// Get returns the value by given <key>.
|
||||
// Get returns the value by given `key`.
|
||||
func (m *IntStrMap) Get(key int) (value string) {
|
||||
m.mu.RLock()
|
||||
if m.data != nil {
|
||||
@ -162,7 +162,7 @@ func (m *IntStrMap) Pop() (key int, value string) {
|
||||
return
|
||||
}
|
||||
|
||||
// Pops retrieves and deletes <size> items from the map.
|
||||
// Pops retrieves and deletes `size` items from the map.
|
||||
// It returns all items if size == -1.
|
||||
func (m *IntStrMap) Pops(size int) map[int]string {
|
||||
m.mu.Lock()
|
||||
@ -189,10 +189,10 @@ func (m *IntStrMap) Pops(size int) map[int]string {
|
||||
}
|
||||
|
||||
// doSetWithLockCheck checks whether value of the key exists with mutex.Lock,
|
||||
// if not exists, set value to the map with given <key>,
|
||||
// if not exists, set value to the map with given `key`,
|
||||
// or else just return the existing value.
|
||||
//
|
||||
// It returns value with given <key>.
|
||||
// It returns value with given `key`.
|
||||
func (m *IntStrMap) doSetWithLockCheck(key int, value string) string {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -207,7 +207,7 @@ func (m *IntStrMap) doSetWithLockCheck(key int, value string) string {
|
||||
}
|
||||
|
||||
// GetOrSet returns the value by key,
|
||||
// or sets value with given <value> if it does not exist and then returns this value.
|
||||
// or sets value with given `value` if it does not exist and then returns this value.
|
||||
func (m *IntStrMap) GetOrSet(key int, value string) string {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
return m.doSetWithLockCheck(key, value)
|
||||
@ -217,7 +217,7 @@ func (m *IntStrMap) GetOrSet(key int, value string) string {
|
||||
}
|
||||
|
||||
// GetOrSetFunc returns the value by key,
|
||||
// or sets value with returned value of callback function <f> if it does not exist and returns this value.
|
||||
// or sets value with returned value of callback function `f` if it does not exist and returns this value.
|
||||
func (m *IntStrMap) GetOrSetFunc(key int, f func() string) string {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
return m.doSetWithLockCheck(key, f())
|
||||
@ -227,9 +227,9 @@ func (m *IntStrMap) GetOrSetFunc(key int, f func() string) string {
|
||||
}
|
||||
|
||||
// GetOrSetFuncLock returns the value by key,
|
||||
// or sets value with returned value of callback function <f> if it does not exist and returns this value.
|
||||
// or sets value with returned value of callback function `f` if it does not exist and returns this value.
|
||||
//
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function <f>
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`
|
||||
// with mutex.Lock of the hash map.
|
||||
func (m *IntStrMap) GetOrSetFuncLock(key int, f func() string) string {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
@ -249,8 +249,8 @@ func (m *IntStrMap) GetOrSetFuncLock(key int, f func() string) string {
|
||||
}
|
||||
}
|
||||
|
||||
// SetIfNotExist sets <value> to the map if the <key> does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExist sets <value> to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
func (m *IntStrMap) SetIfNotExist(key int, value string) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, value)
|
||||
@ -259,8 +259,8 @@ func (m *IntStrMap) SetIfNotExist(key int, value string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SetIfNotExistFunc sets value with return value of callback function <f>, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
func (m *IntStrMap) SetIfNotExistFunc(key int, f func() string) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, f())
|
||||
@ -269,11 +269,11 @@ func (m *IntStrMap) SetIfNotExistFunc(key int, f func() string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function <f>, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function <f> with mutex.Lock of the hash map.
|
||||
// it executes function `f` with mutex.Lock of the hash map.
|
||||
func (m *IntStrMap) SetIfNotExistFuncLock(key int, f func() string) bool {
|
||||
if !m.Contains(key) {
|
||||
m.mu.Lock()
|
||||
@ -300,7 +300,7 @@ func (m *IntStrMap) Removes(keys []int) {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// Remove deletes value from map by given <key>, and return this deleted value.
|
||||
// Remove deletes value from map by given `key`, and return this deleted value.
|
||||
func (m *IntStrMap) Remove(key int) (value string) {
|
||||
m.mu.Lock()
|
||||
if m.data != nil {
|
||||
@ -344,7 +344,7 @@ func (m *IntStrMap) Values() []string {
|
||||
}
|
||||
|
||||
// Contains checks whether a key exists.
|
||||
// It returns true if the <key> exists, or else false.
|
||||
// It returns true if the `key` exists, or else false.
|
||||
func (m *IntStrMap) Contains(key int) bool {
|
||||
var ok bool
|
||||
m.mu.RLock()
|
||||
@ -376,21 +376,21 @@ func (m *IntStrMap) Clear() {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// Replace the data of the map with given <data>.
|
||||
// Replace the data of the map with given `data`.
|
||||
func (m *IntStrMap) Replace(data map[int]string) {
|
||||
m.mu.Lock()
|
||||
m.data = data
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// LockFunc locks writing with given callback function <f> within RWMutex.Lock.
|
||||
// LockFunc locks writing with given callback function `f` within RWMutex.Lock.
|
||||
func (m *IntStrMap) LockFunc(f func(m map[int]string)) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
f(m.data)
|
||||
}
|
||||
|
||||
// RLockFunc locks reading with given callback function <f> within RWMutex.RLock.
|
||||
// RLockFunc locks reading with given callback function `f` within RWMutex.RLock.
|
||||
func (m *IntStrMap) RLockFunc(f func(m map[int]string)) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
@ -409,7 +409,7 @@ func (m *IntStrMap) Flip() {
|
||||
}
|
||||
|
||||
// Merge merges two hash maps.
|
||||
// The <other> map will be merged into the map <m>.
|
||||
// The <other> map will be merged into the map `m`.
|
||||
func (m *IntStrMap) Merge(other *IntStrMap) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -429,7 +429,7 @@ func (m *IntStrMap) Merge(other *IntStrMap) {
|
||||
// String returns the map as a string.
|
||||
func (m *IntStrMap) String() string {
|
||||
b, _ := m.MarshalJSON()
|
||||
return gconv.UnsafeBytesToStr(b)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
|
||||
@ -23,7 +23,7 @@ type StrAnyMap struct {
|
||||
}
|
||||
|
||||
// NewStrAnyMap returns an empty StrAnyMap object.
|
||||
// The parameter <safe> is used to specify whether using map in concurrent-safety,
|
||||
// The parameter `safe` is used to specify whether using map in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewStrAnyMap(safe ...bool) *StrAnyMap {
|
||||
return &StrAnyMap{
|
||||
@ -32,8 +32,8 @@ func NewStrAnyMap(safe ...bool) *StrAnyMap {
|
||||
}
|
||||
}
|
||||
|
||||
// NewStrAnyMapFrom creates and returns a hash map from given map <data>.
|
||||
// Note that, the param <data> map will be set as the underlying data map(no deep copy),
|
||||
// NewStrAnyMapFrom creates and returns a hash map from given map `data`.
|
||||
// Note that, the param `data` map will be set as the underlying data map(no deep copy),
|
||||
// there might be some concurrent-safe issues when changing the map outside.
|
||||
func NewStrAnyMapFrom(data map[string]interface{}, safe ...bool) *StrAnyMap {
|
||||
return &StrAnyMap{
|
||||
@ -42,8 +42,8 @@ func NewStrAnyMapFrom(data map[string]interface{}, safe ...bool) *StrAnyMap {
|
||||
}
|
||||
}
|
||||
|
||||
// Iterator iterates the hash map readonly with custom callback function <f>.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// Iterator iterates the hash map readonly with custom callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (m *StrAnyMap) Iterator(f func(k string, v interface{}) bool) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
@ -137,8 +137,8 @@ func (m *StrAnyMap) Sets(data map[string]interface{}) {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// Search searches the map with given <key>.
|
||||
// Second return parameter <found> is true if key was found, otherwise false.
|
||||
// Search searches the map with given `key`.
|
||||
// Second return parameter `found` is true if key was found, otherwise false.
|
||||
func (m *StrAnyMap) Search(key string) (value interface{}, found bool) {
|
||||
m.mu.RLock()
|
||||
if m.data != nil {
|
||||
@ -148,7 +148,7 @@ func (m *StrAnyMap) Search(key string) (value interface{}, found bool) {
|
||||
return
|
||||
}
|
||||
|
||||
// Get returns the value by given <key>.
|
||||
// Get returns the value by given `key`.
|
||||
func (m *StrAnyMap) Get(key string) (value interface{}) {
|
||||
m.mu.RLock()
|
||||
if m.data != nil {
|
||||
@ -169,7 +169,7 @@ func (m *StrAnyMap) Pop() (key string, value interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
// Pops retrieves and deletes <size> items from the map.
|
||||
// Pops retrieves and deletes `size` items from the map.
|
||||
// It returns all items if size == -1.
|
||||
func (m *StrAnyMap) Pops(size int) map[string]interface{} {
|
||||
m.mu.Lock()
|
||||
@ -196,14 +196,14 @@ func (m *StrAnyMap) Pops(size int) map[string]interface{} {
|
||||
}
|
||||
|
||||
// doSetWithLockCheck checks whether value of the key exists with mutex.Lock,
|
||||
// if not exists, set value to the map with given <key>,
|
||||
// if not exists, set value to the map with given `key`,
|
||||
// or else just return the existing value.
|
||||
//
|
||||
// When setting value, if <value> is type of <func() interface {}>,
|
||||
// When setting value, if `value` is type of <func() interface {}>,
|
||||
// it will be executed with mutex.Lock of the hash map,
|
||||
// and its return value will be set to the map with <key>.
|
||||
// and its return value will be set to the map with `key`.
|
||||
//
|
||||
// It returns value with given <key>.
|
||||
// It returns value with given `key`.
|
||||
func (m *StrAnyMap) doSetWithLockCheck(key string, value interface{}) interface{} {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -223,7 +223,7 @@ func (m *StrAnyMap) doSetWithLockCheck(key string, value interface{}) interface{
|
||||
}
|
||||
|
||||
// GetOrSet returns the value by key,
|
||||
// or sets value with given <value> if it does not exist and then returns this value.
|
||||
// or sets value with given `value` if it does not exist and then returns this value.
|
||||
func (m *StrAnyMap) GetOrSet(key string, value interface{}) interface{} {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
return m.doSetWithLockCheck(key, value)
|
||||
@ -233,7 +233,7 @@ func (m *StrAnyMap) GetOrSet(key string, value interface{}) interface{} {
|
||||
}
|
||||
|
||||
// GetOrSetFunc returns the value by key,
|
||||
// or sets value with returned value of callback function <f> if it does not exist
|
||||
// or sets value with returned value of callback function `f` if it does not exist
|
||||
// and then returns this value.
|
||||
func (m *StrAnyMap) GetOrSetFunc(key string, f func() interface{}) interface{} {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
@ -244,10 +244,10 @@ func (m *StrAnyMap) GetOrSetFunc(key string, f func() interface{}) interface{} {
|
||||
}
|
||||
|
||||
// GetOrSetFuncLock returns the value by key,
|
||||
// or sets value with returned value of callback function <f> if it does not exist
|
||||
// or sets value with returned value of callback function `f` if it does not exist
|
||||
// and then returns this value.
|
||||
//
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function <f>
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`
|
||||
// with mutex.Lock of the hash map.
|
||||
func (m *StrAnyMap) GetOrSetFuncLock(key string, f func() interface{}) interface{} {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
@ -257,7 +257,7 @@ func (m *StrAnyMap) GetOrSetFuncLock(key string, f func() interface{}) interface
|
||||
}
|
||||
}
|
||||
|
||||
// GetVar returns a Var with the value by given <key>.
|
||||
// GetVar returns a Var with the value by given `key`.
|
||||
// The returned Var is un-concurrent safe.
|
||||
func (m *StrAnyMap) GetVar(key string) *gvar.Var {
|
||||
return gvar.New(m.Get(key))
|
||||
@ -281,8 +281,8 @@ func (m *StrAnyMap) GetVarOrSetFuncLock(key string, f func() interface{}) *gvar.
|
||||
return gvar.New(m.GetOrSetFuncLock(key, f))
|
||||
}
|
||||
|
||||
// SetIfNotExist sets <value> to the map if the <key> does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExist sets <value> to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
func (m *StrAnyMap) SetIfNotExist(key string, value interface{}) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, value)
|
||||
@ -291,8 +291,8 @@ func (m *StrAnyMap) SetIfNotExist(key string, value interface{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SetIfNotExistFunc sets value with return value of callback function <f>, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
func (m *StrAnyMap) SetIfNotExistFunc(key string, f func() interface{}) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, f())
|
||||
@ -301,11 +301,11 @@ func (m *StrAnyMap) SetIfNotExistFunc(key string, f func() interface{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function <f>, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function <f> with mutex.Lock of the hash map.
|
||||
// it executes function `f` with mutex.Lock of the hash map.
|
||||
func (m *StrAnyMap) SetIfNotExistFuncLock(key string, f func() interface{}) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, f)
|
||||
@ -325,7 +325,7 @@ func (m *StrAnyMap) Removes(keys []string) {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// Remove deletes value from map by given <key>, and return this deleted value.
|
||||
// Remove deletes value from map by given `key`, and return this deleted value.
|
||||
func (m *StrAnyMap) Remove(key string) (value interface{}) {
|
||||
m.mu.Lock()
|
||||
if m.data != nil {
|
||||
@ -369,7 +369,7 @@ func (m *StrAnyMap) Values() []interface{} {
|
||||
}
|
||||
|
||||
// Contains checks whether a key exists.
|
||||
// It returns true if the <key> exists, or else false.
|
||||
// It returns true if the `key` exists, or else false.
|
||||
func (m *StrAnyMap) Contains(key string) bool {
|
||||
var ok bool
|
||||
m.mu.RLock()
|
||||
@ -401,21 +401,21 @@ func (m *StrAnyMap) Clear() {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// Replace the data of the map with given <data>.
|
||||
// Replace the data of the map with given `data`.
|
||||
func (m *StrAnyMap) Replace(data map[string]interface{}) {
|
||||
m.mu.Lock()
|
||||
m.data = data
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// LockFunc locks writing with given callback function <f> within RWMutex.Lock.
|
||||
// LockFunc locks writing with given callback function `f` within RWMutex.Lock.
|
||||
func (m *StrAnyMap) LockFunc(f func(m map[string]interface{})) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
f(m.data)
|
||||
}
|
||||
|
||||
// RLockFunc locks reading with given callback function <f> within RWMutex.RLock.
|
||||
// RLockFunc locks reading with given callback function `f` within RWMutex.RLock.
|
||||
func (m *StrAnyMap) RLockFunc(f func(m map[string]interface{})) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
@ -434,7 +434,7 @@ func (m *StrAnyMap) Flip() {
|
||||
}
|
||||
|
||||
// Merge merges two hash maps.
|
||||
// The <other> map will be merged into the map <m>.
|
||||
// The <other> map will be merged into the map `m`.
|
||||
func (m *StrAnyMap) Merge(other *StrAnyMap) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -454,7 +454,7 @@ func (m *StrAnyMap) Merge(other *StrAnyMap) {
|
||||
// String returns the map as a string.
|
||||
func (m *StrAnyMap) String() string {
|
||||
b, _ := m.MarshalJSON()
|
||||
return gconv.UnsafeBytesToStr(b)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
|
||||
@ -21,7 +21,7 @@ type StrIntMap struct {
|
||||
}
|
||||
|
||||
// NewStrIntMap returns an empty StrIntMap object.
|
||||
// The parameter <safe> is used to specify whether using map in concurrent-safety,
|
||||
// The parameter `safe` is used to specify whether using map in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewStrIntMap(safe ...bool) *StrIntMap {
|
||||
return &StrIntMap{
|
||||
@ -30,8 +30,8 @@ func NewStrIntMap(safe ...bool) *StrIntMap {
|
||||
}
|
||||
}
|
||||
|
||||
// NewStrIntMapFrom creates and returns a hash map from given map <data>.
|
||||
// Note that, the param <data> map will be set as the underlying data map(no deep copy),
|
||||
// NewStrIntMapFrom creates and returns a hash map from given map `data`.
|
||||
// Note that, the param `data` map will be set as the underlying data map(no deep copy),
|
||||
// there might be some concurrent-safe issues when changing the map outside.
|
||||
func NewStrIntMapFrom(data map[string]int, safe ...bool) *StrIntMap {
|
||||
return &StrIntMap{
|
||||
@ -40,8 +40,8 @@ func NewStrIntMapFrom(data map[string]int, safe ...bool) *StrIntMap {
|
||||
}
|
||||
}
|
||||
|
||||
// Iterator iterates the hash map readonly with custom callback function <f>.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// Iterator iterates the hash map readonly with custom callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (m *StrIntMap) Iterator(f func(k string, v int) bool) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
@ -130,8 +130,8 @@ func (m *StrIntMap) Sets(data map[string]int) {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// Search searches the map with given <key>.
|
||||
// Second return parameter <found> is true if key was found, otherwise false.
|
||||
// Search searches the map with given `key`.
|
||||
// Second return parameter `found` is true if key was found, otherwise false.
|
||||
func (m *StrIntMap) Search(key string) (value int, found bool) {
|
||||
m.mu.RLock()
|
||||
if m.data != nil {
|
||||
@ -141,7 +141,7 @@ func (m *StrIntMap) Search(key string) (value int, found bool) {
|
||||
return
|
||||
}
|
||||
|
||||
// Get returns the value by given <key>.
|
||||
// Get returns the value by given `key`.
|
||||
func (m *StrIntMap) Get(key string) (value int) {
|
||||
m.mu.RLock()
|
||||
if m.data != nil {
|
||||
@ -162,7 +162,7 @@ func (m *StrIntMap) Pop() (key string, value int) {
|
||||
return
|
||||
}
|
||||
|
||||
// Pops retrieves and deletes <size> items from the map.
|
||||
// Pops retrieves and deletes `size` items from the map.
|
||||
// It returns all items if size == -1.
|
||||
func (m *StrIntMap) Pops(size int) map[string]int {
|
||||
m.mu.Lock()
|
||||
@ -189,10 +189,10 @@ func (m *StrIntMap) Pops(size int) map[string]int {
|
||||
}
|
||||
|
||||
// doSetWithLockCheck checks whether value of the key exists with mutex.Lock,
|
||||
// if not exists, set value to the map with given <key>,
|
||||
// if not exists, set value to the map with given `key`,
|
||||
// or else just return the existing value.
|
||||
//
|
||||
// It returns value with given <key>.
|
||||
// It returns value with given `key`.
|
||||
func (m *StrIntMap) doSetWithLockCheck(key string, value int) int {
|
||||
m.mu.Lock()
|
||||
if m.data == nil {
|
||||
@ -208,7 +208,7 @@ func (m *StrIntMap) doSetWithLockCheck(key string, value int) int {
|
||||
}
|
||||
|
||||
// GetOrSet returns the value by key,
|
||||
// or sets value with given <value> if it does not exist and then returns this value.
|
||||
// or sets value with given `value` if it does not exist and then returns this value.
|
||||
func (m *StrIntMap) GetOrSet(key string, value int) int {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
return m.doSetWithLockCheck(key, value)
|
||||
@ -218,7 +218,7 @@ func (m *StrIntMap) GetOrSet(key string, value int) int {
|
||||
}
|
||||
|
||||
// GetOrSetFunc returns the value by key,
|
||||
// or sets value with returned value of callback function <f> if it does not exist
|
||||
// or sets value with returned value of callback function `f` if it does not exist
|
||||
// and then returns this value.
|
||||
func (m *StrIntMap) GetOrSetFunc(key string, f func() int) int {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
@ -229,10 +229,10 @@ func (m *StrIntMap) GetOrSetFunc(key string, f func() int) int {
|
||||
}
|
||||
|
||||
// GetOrSetFuncLock returns the value by key,
|
||||
// or sets value with returned value of callback function <f> if it does not exist
|
||||
// or sets value with returned value of callback function `f` if it does not exist
|
||||
// and then returns this value.
|
||||
//
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function <f>
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`
|
||||
// with mutex.Lock of the hash map.
|
||||
func (m *StrIntMap) GetOrSetFuncLock(key string, f func() int) int {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
@ -252,8 +252,8 @@ func (m *StrIntMap) GetOrSetFuncLock(key string, f func() int) int {
|
||||
}
|
||||
}
|
||||
|
||||
// SetIfNotExist sets <value> to the map if the <key> does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExist sets <value> to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
func (m *StrIntMap) SetIfNotExist(key string, value int) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, value)
|
||||
@ -262,8 +262,8 @@ func (m *StrIntMap) SetIfNotExist(key string, value int) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SetIfNotExistFunc sets value with return value of callback function <f>, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
func (m *StrIntMap) SetIfNotExistFunc(key string, f func() int) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, f())
|
||||
@ -272,11 +272,11 @@ func (m *StrIntMap) SetIfNotExistFunc(key string, f func() int) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function <f>, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function <f> with mutex.Lock of the hash map.
|
||||
// it executes function `f` with mutex.Lock of the hash map.
|
||||
func (m *StrIntMap) SetIfNotExistFuncLock(key string, f func() int) bool {
|
||||
if !m.Contains(key) {
|
||||
m.mu.Lock()
|
||||
@ -303,7 +303,7 @@ func (m *StrIntMap) Removes(keys []string) {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// Remove deletes value from map by given <key>, and return this deleted value.
|
||||
// Remove deletes value from map by given `key`, and return this deleted value.
|
||||
func (m *StrIntMap) Remove(key string) (value int) {
|
||||
m.mu.Lock()
|
||||
if m.data != nil {
|
||||
@ -347,7 +347,7 @@ func (m *StrIntMap) Values() []int {
|
||||
}
|
||||
|
||||
// Contains checks whether a key exists.
|
||||
// It returns true if the <key> exists, or else false.
|
||||
// It returns true if the `key` exists, or else false.
|
||||
func (m *StrIntMap) Contains(key string) bool {
|
||||
var ok bool
|
||||
m.mu.RLock()
|
||||
@ -379,21 +379,21 @@ func (m *StrIntMap) Clear() {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// Replace the data of the map with given <data>.
|
||||
// Replace the data of the map with given `data`.
|
||||
func (m *StrIntMap) Replace(data map[string]int) {
|
||||
m.mu.Lock()
|
||||
m.data = data
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// LockFunc locks writing with given callback function <f> within RWMutex.Lock.
|
||||
// LockFunc locks writing with given callback function `f` within RWMutex.Lock.
|
||||
func (m *StrIntMap) LockFunc(f func(m map[string]int)) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
f(m.data)
|
||||
}
|
||||
|
||||
// RLockFunc locks reading with given callback function <f> within RWMutex.RLock.
|
||||
// RLockFunc locks reading with given callback function `f` within RWMutex.RLock.
|
||||
func (m *StrIntMap) RLockFunc(f func(m map[string]int)) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
@ -412,7 +412,7 @@ func (m *StrIntMap) Flip() {
|
||||
}
|
||||
|
||||
// Merge merges two hash maps.
|
||||
// The <other> map will be merged into the map <m>.
|
||||
// The <other> map will be merged into the map `m`.
|
||||
func (m *StrIntMap) Merge(other *StrIntMap) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -432,7 +432,7 @@ func (m *StrIntMap) Merge(other *StrIntMap) {
|
||||
// String returns the map as a string.
|
||||
func (m *StrIntMap) String() string {
|
||||
b, _ := m.MarshalJSON()
|
||||
return gconv.UnsafeBytesToStr(b)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
|
||||
@ -22,7 +22,7 @@ type StrStrMap struct {
|
||||
}
|
||||
|
||||
// NewStrStrMap returns an empty StrStrMap object.
|
||||
// The parameter <safe> is used to specify whether using map in concurrent-safety,
|
||||
// The parameter `safe` is used to specify whether using map in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewStrStrMap(safe ...bool) *StrStrMap {
|
||||
return &StrStrMap{
|
||||
@ -31,8 +31,8 @@ func NewStrStrMap(safe ...bool) *StrStrMap {
|
||||
}
|
||||
}
|
||||
|
||||
// NewStrStrMapFrom creates and returns a hash map from given map <data>.
|
||||
// Note that, the param <data> map will be set as the underlying data map(no deep copy),
|
||||
// NewStrStrMapFrom creates and returns a hash map from given map `data`.
|
||||
// Note that, the param `data` map will be set as the underlying data map(no deep copy),
|
||||
// there might be some concurrent-safe issues when changing the map outside.
|
||||
func NewStrStrMapFrom(data map[string]string, safe ...bool) *StrStrMap {
|
||||
return &StrStrMap{
|
||||
@ -41,8 +41,8 @@ func NewStrStrMapFrom(data map[string]string, safe ...bool) *StrStrMap {
|
||||
}
|
||||
}
|
||||
|
||||
// Iterator iterates the hash map readonly with custom callback function <f>.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// Iterator iterates the hash map readonly with custom callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (m *StrStrMap) Iterator(f func(k string, v string) bool) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
@ -131,8 +131,8 @@ func (m *StrStrMap) Sets(data map[string]string) {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// Search searches the map with given <key>.
|
||||
// Second return parameter <found> is true if key was found, otherwise false.
|
||||
// Search searches the map with given `key`.
|
||||
// Second return parameter `found` is true if key was found, otherwise false.
|
||||
func (m *StrStrMap) Search(key string) (value string, found bool) {
|
||||
m.mu.RLock()
|
||||
if m.data != nil {
|
||||
@ -142,7 +142,7 @@ func (m *StrStrMap) Search(key string) (value string, found bool) {
|
||||
return
|
||||
}
|
||||
|
||||
// Get returns the value by given <key>.
|
||||
// Get returns the value by given `key`.
|
||||
func (m *StrStrMap) Get(key string) (value string) {
|
||||
m.mu.RLock()
|
||||
if m.data != nil {
|
||||
@ -163,7 +163,7 @@ func (m *StrStrMap) Pop() (key, value string) {
|
||||
return
|
||||
}
|
||||
|
||||
// Pops retrieves and deletes <size> items from the map.
|
||||
// Pops retrieves and deletes `size` items from the map.
|
||||
// It returns all items if size == -1.
|
||||
func (m *StrStrMap) Pops(size int) map[string]string {
|
||||
m.mu.Lock()
|
||||
@ -190,10 +190,10 @@ func (m *StrStrMap) Pops(size int) map[string]string {
|
||||
}
|
||||
|
||||
// doSetWithLockCheck checks whether value of the key exists with mutex.Lock,
|
||||
// if not exists, set value to the map with given <key>,
|
||||
// if not exists, set value to the map with given `key`,
|
||||
// or else just return the existing value.
|
||||
//
|
||||
// It returns value with given <key>.
|
||||
// It returns value with given `key`.
|
||||
func (m *StrStrMap) doSetWithLockCheck(key string, value string) string {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -208,7 +208,7 @@ func (m *StrStrMap) doSetWithLockCheck(key string, value string) string {
|
||||
}
|
||||
|
||||
// GetOrSet returns the value by key,
|
||||
// or sets value with given <value> if it does not exist and then returns this value.
|
||||
// or sets value with given `value` if it does not exist and then returns this value.
|
||||
func (m *StrStrMap) GetOrSet(key string, value string) string {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
return m.doSetWithLockCheck(key, value)
|
||||
@ -218,7 +218,7 @@ func (m *StrStrMap) GetOrSet(key string, value string) string {
|
||||
}
|
||||
|
||||
// GetOrSetFunc returns the value by key,
|
||||
// or sets value with returned value of callback function <f> if it does not exist
|
||||
// or sets value with returned value of callback function `f` if it does not exist
|
||||
// and then returns this value.
|
||||
func (m *StrStrMap) GetOrSetFunc(key string, f func() string) string {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
@ -229,10 +229,10 @@ func (m *StrStrMap) GetOrSetFunc(key string, f func() string) string {
|
||||
}
|
||||
|
||||
// GetOrSetFuncLock returns the value by key,
|
||||
// or sets value with returned value of callback function <f> if it does not exist
|
||||
// or sets value with returned value of callback function `f` if it does not exist
|
||||
// and then returns this value.
|
||||
//
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function <f>
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`
|
||||
// with mutex.Lock of the hash map.
|
||||
func (m *StrStrMap) GetOrSetFuncLock(key string, f func() string) string {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
@ -252,8 +252,8 @@ func (m *StrStrMap) GetOrSetFuncLock(key string, f func() string) string {
|
||||
}
|
||||
}
|
||||
|
||||
// SetIfNotExist sets <value> to the map if the <key> does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExist sets <value> to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
func (m *StrStrMap) SetIfNotExist(key string, value string) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, value)
|
||||
@ -262,8 +262,8 @@ func (m *StrStrMap) SetIfNotExist(key string, value string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SetIfNotExistFunc sets value with return value of callback function <f>, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
func (m *StrStrMap) SetIfNotExistFunc(key string, f func() string) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, f())
|
||||
@ -272,11 +272,11 @@ func (m *StrStrMap) SetIfNotExistFunc(key string, f func() string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function <f>, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function <f> with mutex.Lock of the hash map.
|
||||
// it executes function `f` with mutex.Lock of the hash map.
|
||||
func (m *StrStrMap) SetIfNotExistFuncLock(key string, f func() string) bool {
|
||||
if !m.Contains(key) {
|
||||
m.mu.Lock()
|
||||
@ -303,7 +303,7 @@ func (m *StrStrMap) Removes(keys []string) {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// Remove deletes value from map by given <key>, and return this deleted value.
|
||||
// Remove deletes value from map by given `key`, and return this deleted value.
|
||||
func (m *StrStrMap) Remove(key string) (value string) {
|
||||
m.mu.Lock()
|
||||
if m.data != nil {
|
||||
@ -347,7 +347,7 @@ func (m *StrStrMap) Values() []string {
|
||||
}
|
||||
|
||||
// Contains checks whether a key exists.
|
||||
// It returns true if the <key> exists, or else false.
|
||||
// It returns true if the `key` exists, or else false.
|
||||
func (m *StrStrMap) Contains(key string) bool {
|
||||
var ok bool
|
||||
m.mu.RLock()
|
||||
@ -379,21 +379,21 @@ func (m *StrStrMap) Clear() {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// Replace the data of the map with given <data>.
|
||||
// Replace the data of the map with given `data`.
|
||||
func (m *StrStrMap) Replace(data map[string]string) {
|
||||
m.mu.Lock()
|
||||
m.data = data
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// LockFunc locks writing with given callback function <f> within RWMutex.Lock.
|
||||
// LockFunc locks writing with given callback function `f` within RWMutex.Lock.
|
||||
func (m *StrStrMap) LockFunc(f func(m map[string]string)) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
f(m.data)
|
||||
}
|
||||
|
||||
// RLockFunc locks reading with given callback function <f> within RWMutex.RLock.
|
||||
// RLockFunc locks reading with given callback function `f` within RWMutex.RLock.
|
||||
func (m *StrStrMap) RLockFunc(f func(m map[string]string)) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
@ -412,7 +412,7 @@ func (m *StrStrMap) Flip() {
|
||||
}
|
||||
|
||||
// Merge merges two hash maps.
|
||||
// The <other> map will be merged into the map <m>.
|
||||
// The <other> map will be merged into the map `m`.
|
||||
func (m *StrStrMap) Merge(other *StrStrMap) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -432,7 +432,7 @@ func (m *StrStrMap) Merge(other *StrStrMap) {
|
||||
// String returns the map as a string.
|
||||
func (m *StrStrMap) String() string {
|
||||
b, _ := m.MarshalJSON()
|
||||
return gconv.UnsafeBytesToStr(b)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
|
||||
@ -31,7 +31,7 @@ type gListMapNode struct {
|
||||
|
||||
// NewListMap returns an empty link map.
|
||||
// ListMap is backed by a hash table to store values and doubly-linked list to store ordering.
|
||||
// The parameter <safe> is used to specify whether using map in concurrent-safety,
|
||||
// The parameter `safe` is used to specify whether using map in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewListMap(safe ...bool) *ListMap {
|
||||
return &ListMap{
|
||||
@ -41,8 +41,8 @@ func NewListMap(safe ...bool) *ListMap {
|
||||
}
|
||||
}
|
||||
|
||||
// NewListMapFrom returns a link map from given map <data>.
|
||||
// Note that, the param <data> map will be set as the underlying data map(no deep copy),
|
||||
// NewListMapFrom returns a link map from given map `data`.
|
||||
// Note that, the param `data` map will be set as the underlying data map(no deep copy),
|
||||
// there might be some concurrent-safe issues when changing the map outside.
|
||||
func NewListMapFrom(data map[interface{}]interface{}, safe ...bool) *ListMap {
|
||||
m := NewListMap(safe...)
|
||||
@ -55,8 +55,8 @@ func (m *ListMap) Iterator(f func(key, value interface{}) bool) {
|
||||
m.IteratorAsc(f)
|
||||
}
|
||||
|
||||
// IteratorAsc iterates the map readonly in ascending order with given callback function <f>.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// IteratorAsc iterates the map readonly in ascending order with given callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (m *ListMap) IteratorAsc(f func(key interface{}, value interface{}) bool) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
@ -69,8 +69,8 @@ func (m *ListMap) IteratorAsc(f func(key interface{}, value interface{}) bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// IteratorDesc iterates the map readonly in descending order with given callback function <f>.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// IteratorDesc iterates the map readonly in descending order with given callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (m *ListMap) IteratorDesc(f func(key interface{}, value interface{}) bool) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
@ -96,7 +96,7 @@ func (m *ListMap) Clear() {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// Replace the data of the map with given <data>.
|
||||
// Replace the data of the map with given `data`.
|
||||
func (m *ListMap) Replace(data map[interface{}]interface{}) {
|
||||
m.mu.Lock()
|
||||
m.data = make(map[interface{}]*glist.Element)
|
||||
@ -202,8 +202,8 @@ func (m *ListMap) Sets(data map[interface{}]interface{}) {
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// Search searches the map with given <key>.
|
||||
// Second return parameter <found> is true if key was found, otherwise false.
|
||||
// Search searches the map with given `key`.
|
||||
// Second return parameter `found` is true if key was found, otherwise false.
|
||||
func (m *ListMap) Search(key interface{}) (value interface{}, found bool) {
|
||||
m.mu.RLock()
|
||||
if m.data != nil {
|
||||
@ -216,7 +216,7 @@ func (m *ListMap) Search(key interface{}) (value interface{}, found bool) {
|
||||
return
|
||||
}
|
||||
|
||||
// Get returns the value by given <key>.
|
||||
// Get returns the value by given `key`.
|
||||
func (m *ListMap) Get(key interface{}) (value interface{}) {
|
||||
m.mu.RLock()
|
||||
if m.data != nil {
|
||||
@ -241,7 +241,7 @@ func (m *ListMap) Pop() (key, value interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
// Pops retrieves and deletes <size> items from the map.
|
||||
// Pops retrieves and deletes `size` items from the map.
|
||||
// It returns all items if size == -1.
|
||||
func (m *ListMap) Pops(size int) map[interface{}]interface{} {
|
||||
m.mu.Lock()
|
||||
@ -268,14 +268,14 @@ func (m *ListMap) Pops(size int) map[interface{}]interface{} {
|
||||
}
|
||||
|
||||
// doSetWithLockCheck checks whether value of the key exists with mutex.Lock,
|
||||
// if not exists, set value to the map with given <key>,
|
||||
// if not exists, set value to the map with given `key`,
|
||||
// or else just return the existing value.
|
||||
//
|
||||
// When setting value, if <value> is type of <func() interface {}>,
|
||||
// When setting value, if `value` is type of <func() interface {}>,
|
||||
// it will be executed with mutex.Lock of the map,
|
||||
// and its return value will be set to the map with <key>.
|
||||
// and its return value will be set to the map with `key`.
|
||||
//
|
||||
// It returns value with given <key>.
|
||||
// It returns value with given `key`.
|
||||
func (m *ListMap) doSetWithLockCheck(key interface{}, value interface{}) interface{} {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -296,7 +296,7 @@ func (m *ListMap) doSetWithLockCheck(key interface{}, value interface{}) interfa
|
||||
}
|
||||
|
||||
// GetOrSet returns the value by key,
|
||||
// or sets value with given <value> if it does not exist and then returns this value.
|
||||
// or sets value with given `value` if it does not exist and then returns this value.
|
||||
func (m *ListMap) GetOrSet(key interface{}, value interface{}) interface{} {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
return m.doSetWithLockCheck(key, value)
|
||||
@ -306,7 +306,7 @@ func (m *ListMap) GetOrSet(key interface{}, value interface{}) interface{} {
|
||||
}
|
||||
|
||||
// GetOrSetFunc returns the value by key,
|
||||
// or sets value with returned value of callback function <f> if it does not exist
|
||||
// or sets value with returned value of callback function `f` if it does not exist
|
||||
// and then returns this value.
|
||||
func (m *ListMap) GetOrSetFunc(key interface{}, f func() interface{}) interface{} {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
@ -317,10 +317,10 @@ func (m *ListMap) GetOrSetFunc(key interface{}, f func() interface{}) interface{
|
||||
}
|
||||
|
||||
// GetOrSetFuncLock returns the value by key,
|
||||
// or sets value with returned value of callback function <f> if it does not exist
|
||||
// or sets value with returned value of callback function `f` if it does not exist
|
||||
// and then returns this value.
|
||||
//
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function <f>
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`
|
||||
// with mutex.Lock of the map.
|
||||
func (m *ListMap) GetOrSetFuncLock(key interface{}, f func() interface{}) interface{} {
|
||||
if v, ok := m.Search(key); !ok {
|
||||
@ -330,7 +330,7 @@ func (m *ListMap) GetOrSetFuncLock(key interface{}, f func() interface{}) interf
|
||||
}
|
||||
}
|
||||
|
||||
// GetVar returns a Var with the value by given <key>.
|
||||
// GetVar returns a Var with the value by given `key`.
|
||||
// The returned Var is un-concurrent safe.
|
||||
func (m *ListMap) GetVar(key interface{}) *gvar.Var {
|
||||
return gvar.New(m.Get(key))
|
||||
@ -354,8 +354,8 @@ func (m *ListMap) GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gv
|
||||
return gvar.New(m.GetOrSetFuncLock(key, f))
|
||||
}
|
||||
|
||||
// SetIfNotExist sets <value> to the map if the <key> does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExist sets <value> to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
func (m *ListMap) SetIfNotExist(key interface{}, value interface{}) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, value)
|
||||
@ -364,8 +364,8 @@ func (m *ListMap) SetIfNotExist(key interface{}, value interface{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SetIfNotExistFunc sets value with return value of callback function <f>, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
func (m *ListMap) SetIfNotExistFunc(key interface{}, f func() interface{}) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, f())
|
||||
@ -374,11 +374,11 @@ func (m *ListMap) SetIfNotExistFunc(key interface{}, f func() interface{}) bool
|
||||
return false
|
||||
}
|
||||
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function <f>, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function <f> with mutex.Lock of the map.
|
||||
// it executes function `f` with mutex.Lock of the map.
|
||||
func (m *ListMap) SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool {
|
||||
if !m.Contains(key) {
|
||||
m.doSetWithLockCheck(key, f)
|
||||
@ -387,7 +387,7 @@ func (m *ListMap) SetIfNotExistFuncLock(key interface{}, f func() interface{}) b
|
||||
return false
|
||||
}
|
||||
|
||||
// Remove deletes value from map by given <key>, and return this deleted value.
|
||||
// Remove deletes value from map by given `key`, and return this deleted value.
|
||||
func (m *ListMap) Remove(key interface{}) (value interface{}) {
|
||||
m.mu.Lock()
|
||||
if m.data != nil {
|
||||
@ -452,7 +452,7 @@ func (m *ListMap) Values() []interface{} {
|
||||
}
|
||||
|
||||
// Contains checks whether a key exists.
|
||||
// It returns true if the <key> exists, or else false.
|
||||
// It returns true if the `key` exists, or else false.
|
||||
func (m *ListMap) Contains(key interface{}) (ok bool) {
|
||||
m.mu.RLock()
|
||||
if m.data != nil {
|
||||
@ -486,7 +486,7 @@ func (m *ListMap) Flip() {
|
||||
}
|
||||
|
||||
// Merge merges two link maps.
|
||||
// The <other> map will be merged into the map <m>.
|
||||
// The <other> map will be merged into the map `m`.
|
||||
func (m *ListMap) Merge(other *ListMap) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
@ -513,7 +513,7 @@ func (m *ListMap) Merge(other *ListMap) {
|
||||
// String returns the map as a string.
|
||||
func (m *ListMap) String() string {
|
||||
b, _ := m.MarshalJSON()
|
||||
return gconv.UnsafeBytesToStr(b)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
|
||||
@ -14,16 +14,16 @@ import (
|
||||
type TreeMap = gtree.RedBlackTree
|
||||
|
||||
// NewTreeMap instantiates a tree map with the custom comparator.
|
||||
// The parameter <safe> is used to specify whether using tree in concurrent-safety,
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewTreeMap(comparator func(v1, v2 interface{}) int, safe ...bool) *TreeMap {
|
||||
return gtree.NewRedBlackTree(comparator, safe...)
|
||||
}
|
||||
|
||||
// NewTreeMapFrom instantiates a tree map with the custom comparator and <data> map.
|
||||
// Note that, the param <data> map will be set as the underlying data map(no deep copy),
|
||||
// NewTreeMapFrom instantiates a tree map with the custom comparator and `data` map.
|
||||
// Note that, the param `data` map will be set as the underlying data map(no deep copy),
|
||||
// there might be some concurrent-safe issues when changing the map outside.
|
||||
// The parameter <safe> is used to specify whether using tree in concurrent-safety,
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewTreeMapFrom(comparator func(v1, v2 interface{}) int, data map[interface{}]interface{}, safe ...bool) *TreeMap {
|
||||
return gtree.NewRedBlackTreeFrom(comparator, data, safe...)
|
||||
|
||||
@ -126,7 +126,7 @@ func (p *Pool) Size() int {
|
||||
return p.list.Len()
|
||||
}
|
||||
|
||||
// Close closes the pool. If <p> has ExpireFunc,
|
||||
// Close closes the pool. If `p` has ExpireFunc,
|
||||
// then it automatically closes all items using this function before it's closed.
|
||||
// Commonly you do not need call this function manually.
|
||||
func (p *Pool) Close() {
|
||||
|
||||
@ -40,8 +40,8 @@ const (
|
||||
)
|
||||
|
||||
// New returns an empty queue object.
|
||||
// Optional parameter <limit> is used to limit the size of the queue, which is unlimited in default.
|
||||
// When <limit> is given, the queue will be static and high performance which is comparable with stdlib channel.
|
||||
// Optional parameter `limit` is used to limit the size of the queue, which is unlimited in default.
|
||||
// When `limit` is given, the queue will be static and high performance which is comparable with stdlib channel.
|
||||
func New(limit ...int) *Queue {
|
||||
q := &Queue{
|
||||
closed: gtype.NewBool(),
|
||||
@ -87,12 +87,12 @@ func (q *Queue) asyncLoopFromListToChannel() {
|
||||
<-q.events
|
||||
}
|
||||
}
|
||||
// It should be here to close q.C if <q> is unlimited size.
|
||||
// It should be here to close q.C if `q` is unlimited size.
|
||||
// It's the sender's responsibility to close channel when it should be closed.
|
||||
close(q.C)
|
||||
}
|
||||
|
||||
// Push pushes the data <v> into the queue.
|
||||
// Push pushes the data `v` into the queue.
|
||||
// Note that it would panics if Push is called after the queue is closed.
|
||||
func (q *Queue) Push(v interface{}) {
|
||||
if q.limit > 0 {
|
||||
|
||||
@ -23,8 +23,13 @@ type Ring struct {
|
||||
dirty *gtype.Bool // Dirty, which means the len and cap should be recalculated. It's marked dirty when the size of ring changes.
|
||||
}
|
||||
|
||||
// New creates and returns a Ring structure of <cap> elements.
|
||||
// The optional parameter <safe> specifies whether using this structure in concurrent safety,
|
||||
// internalRingItem stores the ring element value.
|
||||
type internalRingItem struct {
|
||||
Value interface{}
|
||||
}
|
||||
|
||||
// New creates and returns a Ring structure of `cap` elements.
|
||||
// The optional parameter `safe` specifies whether using this structure in concurrent safety,
|
||||
// which is false in default.
|
||||
func New(cap int, safe ...bool) *Ring {
|
||||
return &Ring{
|
||||
@ -38,10 +43,13 @@ func New(cap int, safe ...bool) *Ring {
|
||||
|
||||
// Val returns the item's value of current position.
|
||||
func (r *Ring) Val() interface{} {
|
||||
var value interface{}
|
||||
r.mu.RLock()
|
||||
v := r.ring.Value
|
||||
if r.ring.Value != nil {
|
||||
value = r.ring.Value.(internalRingItem).Value
|
||||
}
|
||||
r.mu.RUnlock()
|
||||
return v
|
||||
return value
|
||||
}
|
||||
|
||||
// Len returns the size of ring.
|
||||
@ -61,17 +69,21 @@ func (r *Ring) checkAndUpdateLenAndCap() {
|
||||
if !r.dirty.Val() {
|
||||
return
|
||||
}
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
totalLen := 0
|
||||
emptyLen := 0
|
||||
if r.ring != nil {
|
||||
r.mu.RLock()
|
||||
if r.ring.Value == nil {
|
||||
emptyLen++
|
||||
}
|
||||
totalLen++
|
||||
for p := r.ring.Next(); p != r.ring; p = p.Next() {
|
||||
if p.Value == nil {
|
||||
emptyLen++
|
||||
}
|
||||
totalLen++
|
||||
}
|
||||
r.mu.RUnlock()
|
||||
}
|
||||
r.cap.Set(totalLen)
|
||||
r.len.Set(totalLen - emptyLen)
|
||||
@ -84,18 +96,18 @@ func (r *Ring) Set(value interface{}) *Ring {
|
||||
if r.ring.Value == nil {
|
||||
r.len.Add(1)
|
||||
}
|
||||
r.ring.Value = value
|
||||
r.ring.Value = internalRingItem{Value: value}
|
||||
r.mu.Unlock()
|
||||
return r
|
||||
}
|
||||
|
||||
// Put sets <value> to current item of ring and moves position to next item.
|
||||
// Put sets `value` to current item of ring and moves position to next item.
|
||||
func (r *Ring) Put(value interface{}) *Ring {
|
||||
r.mu.Lock()
|
||||
if r.ring.Value == nil {
|
||||
r.len.Add(1)
|
||||
}
|
||||
r.ring.Value = value
|
||||
r.ring.Value = internalRingItem{Value: value}
|
||||
r.ring = r.ring.Next()
|
||||
r.mu.Unlock()
|
||||
return r
|
||||
@ -132,8 +144,8 @@ func (r *Ring) Next() *Ring {
|
||||
//
|
||||
// If r and s point to the same ring, linking
|
||||
// them removes the elements between r and s from the ring.
|
||||
// The removed elements form a subring and the result is a
|
||||
// reference to that subring (if no elements were removed,
|
||||
// The removed elements form a sub-ring and the result is a
|
||||
// reference to that sub-ring (if no elements were removed,
|
||||
// the result is still the original value for r.Next(),
|
||||
// and not nil).
|
||||
//
|
||||
@ -155,7 +167,7 @@ func (r *Ring) Link(s *Ring) *Ring {
|
||||
|
||||
// Unlink removes n % r.Len() elements from the ring r, starting
|
||||
// at r.Next(). If n % r.Len() == 0, r remains unchanged.
|
||||
// The result is the removed subring. r must not be empty.
|
||||
// The result is the removed sub-ring. r must not be empty.
|
||||
//
|
||||
func (r *Ring) Unlink(n int) *Ring {
|
||||
r.mu.Lock()
|
||||
@ -166,56 +178,24 @@ func (r *Ring) Unlink(n int) *Ring {
|
||||
}
|
||||
|
||||
// RLockIteratorNext iterates and locks reading forward
|
||||
// with given callback function <f> within RWMutex.RLock.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// with given callback function `f` within RWMutex.RLock.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (r *Ring) RLockIteratorNext(f func(value interface{}) bool) {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
if !f(r.ring.Value) {
|
||||
if r.ring.Value != nil && !f(r.ring.Value.(internalRingItem).Value) {
|
||||
return
|
||||
}
|
||||
for p := r.ring.Next(); p != r.ring; p = p.Next() {
|
||||
if !f(p.Value) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RLockIteratorPrev iterates and locks reading backward
|
||||
// with given callback function <f> within RWMutex.RLock.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
func (r *Ring) RLockIteratorPrev(f func(value interface{}) bool) {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
if !f(r.ring.Value) {
|
||||
return
|
||||
}
|
||||
for p := r.ring.Prev(); p != r.ring; p = p.Prev() {
|
||||
if !f(p.Value) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// LockIteratorNext iterates and locks writing forward
|
||||
// with given callback function <f> within RWMutex.RLock.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
func (r *Ring) LockIteratorNext(f func(item *ring.Ring) bool) {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
if !f(r.ring) {
|
||||
return
|
||||
}
|
||||
for p := r.ring.Next(); p != r.ring; p = p.Next() {
|
||||
if !f(p) {
|
||||
if p.Value == nil || !f(p.Value.(internalRingItem).Value) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// LockIteratorPrev iterates and locks writing backward
|
||||
// with given callback function <f> within RWMutex.RLock.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// with given callback function `f` within RWMutex.RLock.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (r *Ring) LockIteratorPrev(f func(item *ring.Ring) bool) {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
@ -234,12 +214,13 @@ func (r *Ring) SliceNext() []interface{} {
|
||||
s := make([]interface{}, 0)
|
||||
r.mu.RLock()
|
||||
if r.ring.Value != nil {
|
||||
s = append(s, r.ring.Value)
|
||||
s = append(s, r.ring.Value.(internalRingItem).Value)
|
||||
}
|
||||
for p := r.ring.Next(); p != r.ring; p = p.Next() {
|
||||
if p.Value != nil {
|
||||
s = append(s, p.Value)
|
||||
if p.Value == nil {
|
||||
break
|
||||
}
|
||||
s = append(s, p.Value.(internalRingItem).Value)
|
||||
}
|
||||
r.mu.RUnlock()
|
||||
return s
|
||||
@ -250,12 +231,13 @@ func (r *Ring) SlicePrev() []interface{} {
|
||||
s := make([]interface{}, 0)
|
||||
r.mu.RLock()
|
||||
if r.ring.Value != nil {
|
||||
s = append(s, r.ring.Value)
|
||||
s = append(s, r.ring.Value.(internalRingItem).Value)
|
||||
}
|
||||
for p := r.ring.Prev(); p != r.ring; p = p.Prev() {
|
||||
if p.Value != nil {
|
||||
s = append(s, p.Value)
|
||||
if p.Value == nil {
|
||||
break
|
||||
}
|
||||
s = append(s, p.Value.(internalRingItem).Value)
|
||||
}
|
||||
r.mu.RUnlock()
|
||||
return s
|
||||
|
||||
@ -7,7 +7,6 @@
|
||||
package gring_test
|
||||
|
||||
import (
|
||||
"container/ring"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/container/gring"
|
||||
@ -44,6 +43,11 @@ func TestRing_Val(t *testing.T) {
|
||||
})
|
||||
}
|
||||
func TestRing_CapLen(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r := gring.New(10)
|
||||
t.Assert(r.Cap(), 10)
|
||||
t.Assert(r.Len(), 0)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r := gring.New(10)
|
||||
r.Put("goframe")
|
||||
@ -81,22 +85,22 @@ func TestRing_Link(t *testing.T) {
|
||||
|
||||
rs := r.Link(s)
|
||||
t.Assert(rs.Move(2).Val(), "b")
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestRing_Unlink(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
r := gring.New(5)
|
||||
for i := 0; i < 5; i++ {
|
||||
r.Put(i + 1)
|
||||
for i := 1; i <= 5; i++ {
|
||||
r.Put(i)
|
||||
}
|
||||
t.Assert(r.Val(), 1)
|
||||
// 1 2 3 4
|
||||
// 删除当前位置往后的2个数据,返回被删除的数据
|
||||
// 重新计算s len
|
||||
s := r.Unlink(2) // 2 3
|
||||
t.Assert(s.Val(), 2)
|
||||
t.Assert(s.Len(), 1)
|
||||
t.Assert(s.Len(), 2)
|
||||
})
|
||||
}
|
||||
|
||||
@ -120,10 +124,10 @@ func TestRing_Slice(t *testing.T) {
|
||||
r.Set(nil)
|
||||
array2 := r.SliceNext() //[4 5 1 2]
|
||||
//返回当前位置往后不为空的元素数组,长度为4
|
||||
t.Assert(array2, g.Slice{4, 5, 1, 2})
|
||||
t.Assert(array2, g.Slice{nil, 4, 5, 1, 2})
|
||||
|
||||
array3 := r.SlicePrev() //[2 1 5 4]
|
||||
t.Assert(array3, g.Slice{2, 1, 5, 4})
|
||||
t.Assert(array3, g.Slice{nil, 2, 1, 5, 4})
|
||||
|
||||
s := gring.New(ringLen)
|
||||
for i := 0; i < ringLen; i++ {
|
||||
@ -131,106 +135,5 @@ func TestRing_Slice(t *testing.T) {
|
||||
}
|
||||
array4 := s.SlicePrev() // []
|
||||
t.Assert(array4, g.Slice{1, 5, 4, 3, 2})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestRing_RLockIterator(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
ringLen := 5
|
||||
r := gring.New(ringLen)
|
||||
|
||||
//ring不存在有值元素
|
||||
r.RLockIteratorNext(func(v interface{}) bool {
|
||||
t.Assert(v, nil)
|
||||
return false
|
||||
})
|
||||
r.RLockIteratorNext(func(v interface{}) bool {
|
||||
t.Assert(v, nil)
|
||||
return true
|
||||
})
|
||||
|
||||
r.RLockIteratorPrev(func(v interface{}) bool {
|
||||
t.Assert(v, nil)
|
||||
return true
|
||||
})
|
||||
|
||||
for i := 0; i < ringLen; i++ {
|
||||
r.Put(i + 1)
|
||||
}
|
||||
|
||||
//回调函数返回true,RLockIteratorNext遍历5次,期望值分别是1、2、3、4、5
|
||||
i := 0
|
||||
r.RLockIteratorNext(func(v interface{}) bool {
|
||||
t.Assert(v, i+1)
|
||||
i++
|
||||
return true
|
||||
})
|
||||
|
||||
//RLockIteratorPrev遍历1次返回 false,退出遍历
|
||||
r.RLockIteratorPrev(func(v interface{}) bool {
|
||||
t.Assert(v, 1)
|
||||
return false
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestRing_LockIterator(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
ringLen := 5
|
||||
r := gring.New(ringLen)
|
||||
|
||||
//不存在有值元素
|
||||
r.LockIteratorNext(func(item *ring.Ring) bool {
|
||||
t.Assert(item.Value, nil)
|
||||
return false
|
||||
})
|
||||
r.LockIteratorNext(func(item *ring.Ring) bool {
|
||||
t.Assert(item.Value, nil)
|
||||
return false
|
||||
})
|
||||
r.LockIteratorNext(func(item *ring.Ring) bool {
|
||||
t.Assert(item.Value, nil)
|
||||
return true
|
||||
})
|
||||
|
||||
r.LockIteratorPrev(func(item *ring.Ring) bool {
|
||||
t.Assert(item.Value, nil)
|
||||
return false
|
||||
})
|
||||
r.LockIteratorPrev(func(item *ring.Ring) bool {
|
||||
t.Assert(item.Value, nil)
|
||||
return true
|
||||
})
|
||||
|
||||
//ring初始化元素值
|
||||
for i := 0; i < ringLen; i++ {
|
||||
r.Put(i + 1)
|
||||
}
|
||||
|
||||
//往后遍历组成数据 [1,2,3,4,5]
|
||||
array1 := g.Slice{1, 2, 3, 4, 5}
|
||||
ii := 0
|
||||
r.LockIteratorNext(func(item *ring.Ring) bool {
|
||||
//校验每一次遍历取值是否是期望值
|
||||
t.Assert(item.Value, array1[ii])
|
||||
ii++
|
||||
return true
|
||||
})
|
||||
|
||||
//往后取3个元素组成数组
|
||||
//获得 [1,5,4]
|
||||
i := 0
|
||||
a := g.Slice{1, 5, 4}
|
||||
r.LockIteratorPrev(func(item *ring.Ring) bool {
|
||||
if i > 2 {
|
||||
return false
|
||||
}
|
||||
t.Assert(item.Value, a[i])
|
||||
i++
|
||||
return true
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
@ -21,7 +21,7 @@ type Set struct {
|
||||
}
|
||||
|
||||
// New create and returns a new set, which contains un-repeated items.
|
||||
// The parameter <safe> is used to specify whether using set in concurrent-safety,
|
||||
// The parameter `safe` is used to specify whether using set in concurrent-safety,
|
||||
// which is false in default.
|
||||
func New(safe ...bool) *Set {
|
||||
return NewSet(safe...)
|
||||
@ -35,8 +35,8 @@ func NewSet(safe ...bool) *Set {
|
||||
}
|
||||
}
|
||||
|
||||
// NewFrom returns a new set from <items>.
|
||||
// Parameter <items> can be either a variable of any type, or a slice.
|
||||
// NewFrom returns a new set from `items`.
|
||||
// Parameter `items` can be either a variable of any type, or a slice.
|
||||
func NewFrom(items interface{}, safe ...bool) *Set {
|
||||
m := make(map[interface{}]struct{})
|
||||
for _, v := range gconv.Interfaces(items) {
|
||||
@ -48,8 +48,8 @@ func NewFrom(items interface{}, safe ...bool) *Set {
|
||||
}
|
||||
}
|
||||
|
||||
// Iterator iterates the set readonly with given callback function <f>,
|
||||
// if <f> returns true then continue iterating; or false to stop.
|
||||
// Iterator iterates the set readonly with given callback function `f`,
|
||||
// if `f` returns true then continue iterating; or false to stop.
|
||||
func (set *Set) Iterator(f func(v interface{}) bool) {
|
||||
set.mu.RLock()
|
||||
defer set.mu.RUnlock()
|
||||
@ -76,7 +76,7 @@ func (set *Set) Add(items ...interface{}) {
|
||||
// it adds the item to set and returns true if it does not exists in the set,
|
||||
// or else it does nothing and returns false.
|
||||
//
|
||||
// Note that, if <item> is nil, it does nothing and returns false.
|
||||
// Note that, if `item` is nil, it does nothing and returns false.
|
||||
func (set *Set) AddIfNotExist(item interface{}) bool {
|
||||
if item == nil {
|
||||
return false
|
||||
@ -97,9 +97,9 @@ func (set *Set) AddIfNotExist(item interface{}) bool {
|
||||
|
||||
// AddIfNotExistFunc checks whether item exists in the set,
|
||||
// it adds the item to set and returns true if it does not exists in the set and
|
||||
// function <f> returns true, or else it does nothing and returns false.
|
||||
// function `f` returns true, or else it does nothing and returns false.
|
||||
//
|
||||
// Note that, if <item> is nil, it does nothing and returns false. The function <f>
|
||||
// Note that, if <item> is nil, it does nothing and returns false. The function `f`
|
||||
// is executed without writing lock.
|
||||
func (set *Set) AddIfNotExistFunc(item interface{}, f func() bool) bool {
|
||||
if item == nil {
|
||||
@ -123,9 +123,9 @@ func (set *Set) AddIfNotExistFunc(item interface{}, f func() bool) bool {
|
||||
|
||||
// AddIfNotExistFunc checks whether item exists in the set,
|
||||
// it adds the item to set and returns true if it does not exists in the set and
|
||||
// function <f> returns true, or else it does nothing and returns false.
|
||||
// function `f` returns true, or else it does nothing and returns false.
|
||||
//
|
||||
// Note that, if <item> is nil, it does nothing and returns false. The function <f>
|
||||
// Note that, if <item> is nil, it does nothing and returns false. The function `f`
|
||||
// is executed within writing lock.
|
||||
func (set *Set) AddIfNotExistFuncLock(item interface{}, f func() bool) bool {
|
||||
if item == nil {
|
||||
@ -147,7 +147,7 @@ func (set *Set) AddIfNotExistFuncLock(item interface{}, f func() bool) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Contains checks whether the set contains <item>.
|
||||
// Contains checks whether the set contains `item`.
|
||||
func (set *Set) Contains(item interface{}) bool {
|
||||
var ok bool
|
||||
set.mu.RLock()
|
||||
@ -158,7 +158,7 @@ func (set *Set) Contains(item interface{}) bool {
|
||||
return ok
|
||||
}
|
||||
|
||||
// Remove deletes <item> from set.
|
||||
// Remove deletes `item` from set.
|
||||
func (set *Set) Remove(item interface{}) {
|
||||
set.mu.Lock()
|
||||
if set.data != nil {
|
||||
@ -197,7 +197,7 @@ func (set *Set) Slice() []interface{} {
|
||||
return ret
|
||||
}
|
||||
|
||||
// Join joins items with a string <glue>.
|
||||
// Join joins items with a string `glue`.
|
||||
func (set *Set) Join(glue string) string {
|
||||
set.mu.RLock()
|
||||
defer set.mu.RUnlock()
|
||||
@ -246,14 +246,14 @@ func (set *Set) String() string {
|
||||
return buffer.String()
|
||||
}
|
||||
|
||||
// LockFunc locks writing with callback function <f>.
|
||||
// LockFunc locks writing with callback function `f`.
|
||||
func (set *Set) LockFunc(f func(m map[interface{}]struct{})) {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
f(set.data)
|
||||
}
|
||||
|
||||
// RLockFunc locks reading with callback function <f>.
|
||||
// RLockFunc locks reading with callback function `f`.
|
||||
func (set *Set) RLockFunc(f func(m map[interface{}]struct{})) {
|
||||
set.mu.RLock()
|
||||
defer set.mu.RUnlock()
|
||||
@ -280,7 +280,7 @@ func (set *Set) Equal(other *Set) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// IsSubsetOf checks whether the current set is a sub-set of <other>.
|
||||
// IsSubsetOf checks whether the current set is a sub-set of `other`.
|
||||
func (set *Set) IsSubsetOf(other *Set) bool {
|
||||
if set == other {
|
||||
return true
|
||||
@ -297,8 +297,8 @@ func (set *Set) IsSubsetOf(other *Set) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Union returns a new set which is the union of <set> and <others>.
|
||||
// Which means, all the items in <newSet> are in <set> or in <others>.
|
||||
// Union returns a new set which is the union of <set> and `others`.
|
||||
// Which means, all the items in <newSet> are in <set> or in `others`.
|
||||
func (set *Set) Union(others ...*Set) (newSet *Set) {
|
||||
newSet = NewSet()
|
||||
set.mu.RLock()
|
||||
@ -323,8 +323,8 @@ func (set *Set) Union(others ...*Set) (newSet *Set) {
|
||||
return
|
||||
}
|
||||
|
||||
// Diff returns a new set which is the difference set from <set> to <others>.
|
||||
// Which means, all the items in <newSet> are in <set> but not in <others>.
|
||||
// Diff returns a new set which is the difference set from <set> to `others`.
|
||||
// Which means, all the items in <newSet> are in <set> but not in `others`.
|
||||
func (set *Set) Diff(others ...*Set) (newSet *Set) {
|
||||
newSet = NewSet()
|
||||
set.mu.RLock()
|
||||
@ -344,8 +344,8 @@ func (set *Set) Diff(others ...*Set) (newSet *Set) {
|
||||
return
|
||||
}
|
||||
|
||||
// Intersect returns a new set which is the intersection from <set> to <others>.
|
||||
// Which means, all the items in <newSet> are in <set> and also in <others>.
|
||||
// Intersect returns a new set which is the intersection from <set> to `others`.
|
||||
// Which means, all the items in <newSet> are in <set> and also in `others`.
|
||||
func (set *Set) Intersect(others ...*Set) (newSet *Set) {
|
||||
newSet = NewSet()
|
||||
set.mu.RLock()
|
||||
@ -366,11 +366,11 @@ func (set *Set) Intersect(others ...*Set) (newSet *Set) {
|
||||
return
|
||||
}
|
||||
|
||||
// Complement returns a new set which is the complement from <set> to <full>.
|
||||
// Which means, all the items in <newSet> are in <full> and not in <set>.
|
||||
// Complement returns a new set which is the complement from <set> to `full`.
|
||||
// Which means, all the items in <newSet> are in <full> and not in `set`.
|
||||
//
|
||||
// It returns the difference between <full> and <set>
|
||||
// if the given set <full> is not the full set of <set>.
|
||||
// It returns the difference between <full> and `set`
|
||||
// if the given set <full> is not the full set of `set`.
|
||||
func (set *Set) Complement(full *Set) (newSet *Set) {
|
||||
newSet = NewSet()
|
||||
set.mu.RLock()
|
||||
@ -387,7 +387,7 @@ func (set *Set) Complement(full *Set) (newSet *Set) {
|
||||
return
|
||||
}
|
||||
|
||||
// Merge adds items from <others> sets into <set>.
|
||||
// Merge adds items from <others> sets into `set`.
|
||||
func (set *Set) Merge(others ...*Set) *Set {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
@ -428,7 +428,7 @@ func (set *Set) Pop() interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Pops randomly pops <size> items from set.
|
||||
// Pops randomly pops `size` items from set.
|
||||
// It returns all items if size == -1.
|
||||
func (set *Set) Pops(size int) []interface{} {
|
||||
set.mu.Lock()
|
||||
@ -452,7 +452,7 @@ func (set *Set) Pops(size int) []interface{} {
|
||||
return array
|
||||
}
|
||||
|
||||
// Walk applies a user supplied function <f> to every item of set.
|
||||
// Walk applies a user supplied function `f` to every item of set.
|
||||
func (set *Set) Walk(f func(item interface{}) interface{}) *Set {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
|
||||
@ -20,7 +20,7 @@ type IntSet struct {
|
||||
}
|
||||
|
||||
// New create and returns a new set, which contains un-repeated items.
|
||||
// The parameter <safe> is used to specify whether using set in concurrent-safety,
|
||||
// The parameter `safe` is used to specify whether using set in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewIntSet(safe ...bool) *IntSet {
|
||||
return &IntSet{
|
||||
@ -29,7 +29,7 @@ func NewIntSet(safe ...bool) *IntSet {
|
||||
}
|
||||
}
|
||||
|
||||
// NewIntSetFrom returns a new set from <items>.
|
||||
// NewIntSetFrom returns a new set from `items`.
|
||||
func NewIntSetFrom(items []int, safe ...bool) *IntSet {
|
||||
m := make(map[int]struct{})
|
||||
for _, v := range items {
|
||||
@ -41,8 +41,8 @@ func NewIntSetFrom(items []int, safe ...bool) *IntSet {
|
||||
}
|
||||
}
|
||||
|
||||
// Iterator iterates the set readonly with given callback function <f>,
|
||||
// if <f> returns true then continue iterating; or false to stop.
|
||||
// Iterator iterates the set readonly with given callback function `f`,
|
||||
// if `f` returns true then continue iterating; or false to stop.
|
||||
func (set *IntSet) Iterator(f func(v int) bool) {
|
||||
set.mu.RLock()
|
||||
defer set.mu.RUnlock()
|
||||
@ -69,7 +69,7 @@ func (set *IntSet) Add(item ...int) {
|
||||
// it adds the item to set and returns true if it does not exists in the set,
|
||||
// or else it does nothing and returns false.
|
||||
//
|
||||
// Note that, if <item> is nil, it does nothing and returns false.
|
||||
// Note that, if `item` is nil, it does nothing and returns false.
|
||||
func (set *IntSet) AddIfNotExist(item int) bool {
|
||||
if !set.Contains(item) {
|
||||
set.mu.Lock()
|
||||
@ -87,9 +87,9 @@ func (set *IntSet) AddIfNotExist(item int) bool {
|
||||
|
||||
// AddIfNotExistFunc checks whether item exists in the set,
|
||||
// it adds the item to set and returns true if it does not exists in the set and
|
||||
// function <f> returns true, or else it does nothing and returns false.
|
||||
// function `f` returns true, or else it does nothing and returns false.
|
||||
//
|
||||
// Note that, the function <f> is executed without writing lock.
|
||||
// Note that, the function `f` is executed without writing lock.
|
||||
func (set *IntSet) AddIfNotExistFunc(item int, f func() bool) bool {
|
||||
if !set.Contains(item) {
|
||||
if f() {
|
||||
@ -109,9 +109,9 @@ func (set *IntSet) AddIfNotExistFunc(item int, f func() bool) bool {
|
||||
|
||||
// AddIfNotExistFunc checks whether item exists in the set,
|
||||
// it adds the item to set and returns true if it does not exists in the set and
|
||||
// function <f> returns true, or else it does nothing and returns false.
|
||||
// function `f` returns true, or else it does nothing and returns false.
|
||||
//
|
||||
// Note that, the function <f> is executed without writing lock.
|
||||
// Note that, the function `f` is executed without writing lock.
|
||||
func (set *IntSet) AddIfNotExistFuncLock(item int, f func() bool) bool {
|
||||
if !set.Contains(item) {
|
||||
set.mu.Lock()
|
||||
@ -129,7 +129,7 @@ func (set *IntSet) AddIfNotExistFuncLock(item int, f func() bool) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Contains checks whether the set contains <item>.
|
||||
// Contains checks whether the set contains `item`.
|
||||
func (set *IntSet) Contains(item int) bool {
|
||||
var ok bool
|
||||
set.mu.RLock()
|
||||
@ -140,7 +140,7 @@ func (set *IntSet) Contains(item int) bool {
|
||||
return ok
|
||||
}
|
||||
|
||||
// Remove deletes <item> from set.
|
||||
// Remove deletes `item` from set.
|
||||
func (set *IntSet) Remove(item int) {
|
||||
set.mu.Lock()
|
||||
if set.data != nil {
|
||||
@ -179,7 +179,7 @@ func (set *IntSet) Slice() []int {
|
||||
return ret
|
||||
}
|
||||
|
||||
// Join joins items with a string <glue>.
|
||||
// Join joins items with a string `glue`.
|
||||
func (set *IntSet) Join(glue string) string {
|
||||
set.mu.RLock()
|
||||
defer set.mu.RUnlock()
|
||||
@ -206,14 +206,14 @@ func (set *IntSet) String() string {
|
||||
return "[" + set.Join(",") + "]"
|
||||
}
|
||||
|
||||
// LockFunc locks writing with callback function <f>.
|
||||
// LockFunc locks writing with callback function `f`.
|
||||
func (set *IntSet) LockFunc(f func(m map[int]struct{})) {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
f(set.data)
|
||||
}
|
||||
|
||||
// RLockFunc locks reading with callback function <f>.
|
||||
// RLockFunc locks reading with callback function `f`.
|
||||
func (set *IntSet) RLockFunc(f func(m map[int]struct{})) {
|
||||
set.mu.RLock()
|
||||
defer set.mu.RUnlock()
|
||||
@ -240,7 +240,7 @@ func (set *IntSet) Equal(other *IntSet) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// IsSubsetOf checks whether the current set is a sub-set of <other>.
|
||||
// IsSubsetOf checks whether the current set is a sub-set of `other`.
|
||||
func (set *IntSet) IsSubsetOf(other *IntSet) bool {
|
||||
if set == other {
|
||||
return true
|
||||
@ -257,8 +257,8 @@ func (set *IntSet) IsSubsetOf(other *IntSet) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Union returns a new set which is the union of <set> and <other>.
|
||||
// Which means, all the items in <newSet> are in <set> or in <other>.
|
||||
// Union returns a new set which is the union of <set> and `other`.
|
||||
// Which means, all the items in <newSet> are in <set> or in `other`.
|
||||
func (set *IntSet) Union(others ...*IntSet) (newSet *IntSet) {
|
||||
newSet = NewIntSet()
|
||||
set.mu.RLock()
|
||||
@ -283,8 +283,8 @@ func (set *IntSet) Union(others ...*IntSet) (newSet *IntSet) {
|
||||
return
|
||||
}
|
||||
|
||||
// Diff returns a new set which is the difference set from <set> to <other>.
|
||||
// Which means, all the items in <newSet> are in <set> but not in <other>.
|
||||
// Diff returns a new set which is the difference set from <set> to `other`.
|
||||
// Which means, all the items in <newSet> are in <set> but not in `other`.
|
||||
func (set *IntSet) Diff(others ...*IntSet) (newSet *IntSet) {
|
||||
newSet = NewIntSet()
|
||||
set.mu.RLock()
|
||||
@ -304,8 +304,8 @@ func (set *IntSet) Diff(others ...*IntSet) (newSet *IntSet) {
|
||||
return
|
||||
}
|
||||
|
||||
// Intersect returns a new set which is the intersection from <set> to <other>.
|
||||
// Which means, all the items in <newSet> are in <set> and also in <other>.
|
||||
// Intersect returns a new set which is the intersection from <set> to `other`.
|
||||
// Which means, all the items in <newSet> are in <set> and also in `other`.
|
||||
func (set *IntSet) Intersect(others ...*IntSet) (newSet *IntSet) {
|
||||
newSet = NewIntSet()
|
||||
set.mu.RLock()
|
||||
@ -326,11 +326,11 @@ func (set *IntSet) Intersect(others ...*IntSet) (newSet *IntSet) {
|
||||
return
|
||||
}
|
||||
|
||||
// Complement returns a new set which is the complement from <set> to <full>.
|
||||
// Which means, all the items in <newSet> are in <full> and not in <set>.
|
||||
// Complement returns a new set which is the complement from <set> to `full`.
|
||||
// Which means, all the items in <newSet> are in <full> and not in `set`.
|
||||
//
|
||||
// It returns the difference between <full> and <set>
|
||||
// if the given set <full> is not the full set of <set>.
|
||||
// It returns the difference between <full> and `set`
|
||||
// if the given set <full> is not the full set of `set`.
|
||||
func (set *IntSet) Complement(full *IntSet) (newSet *IntSet) {
|
||||
newSet = NewIntSet()
|
||||
set.mu.RLock()
|
||||
@ -347,7 +347,7 @@ func (set *IntSet) Complement(full *IntSet) (newSet *IntSet) {
|
||||
return
|
||||
}
|
||||
|
||||
// Merge adds items from <others> sets into <set>.
|
||||
// Merge adds items from <others> sets into `set`.
|
||||
func (set *IntSet) Merge(others ...*IntSet) *IntSet {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
@ -388,7 +388,7 @@ func (set *IntSet) Pop() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
// Pops randomly pops <size> items from set.
|
||||
// Pops randomly pops `size` items from set.
|
||||
// It returns all items if size == -1.
|
||||
func (set *IntSet) Pops(size int) []int {
|
||||
set.mu.Lock()
|
||||
@ -412,7 +412,7 @@ func (set *IntSet) Pops(size int) []int {
|
||||
return array
|
||||
}
|
||||
|
||||
// Walk applies a user supplied function <f> to every item of set.
|
||||
// Walk applies a user supplied function `f` to every item of set.
|
||||
func (set *IntSet) Walk(f func(item int) int) *IntSet {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
|
||||
@ -22,7 +22,7 @@ type StrSet struct {
|
||||
}
|
||||
|
||||
// NewStrSet create and returns a new set, which contains un-repeated items.
|
||||
// The parameter <safe> is used to specify whether using set in concurrent-safety,
|
||||
// The parameter `safe` is used to specify whether using set in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewStrSet(safe ...bool) *StrSet {
|
||||
return &StrSet{
|
||||
@ -31,7 +31,7 @@ func NewStrSet(safe ...bool) *StrSet {
|
||||
}
|
||||
}
|
||||
|
||||
// NewStrSetFrom returns a new set from <items>.
|
||||
// NewStrSetFrom returns a new set from `items`.
|
||||
func NewStrSetFrom(items []string, safe ...bool) *StrSet {
|
||||
m := make(map[string]struct{})
|
||||
for _, v := range items {
|
||||
@ -43,8 +43,8 @@ func NewStrSetFrom(items []string, safe ...bool) *StrSet {
|
||||
}
|
||||
}
|
||||
|
||||
// Iterator iterates the set readonly with given callback function <f>,
|
||||
// if <f> returns true then continue iterating; or false to stop.
|
||||
// Iterator iterates the set readonly with given callback function `f`,
|
||||
// if `f` returns true then continue iterating; or false to stop.
|
||||
func (set *StrSet) Iterator(f func(v string) bool) {
|
||||
set.mu.RLock()
|
||||
defer set.mu.RUnlock()
|
||||
@ -87,9 +87,9 @@ func (set *StrSet) AddIfNotExist(item string) bool {
|
||||
|
||||
// AddIfNotExistFunc checks whether item exists in the set,
|
||||
// it adds the item to set and returns true if it does not exists in the set and
|
||||
// function <f> returns true, or else it does nothing and returns false.
|
||||
// function `f` returns true, or else it does nothing and returns false.
|
||||
//
|
||||
// Note that, the function <f> is executed without writing lock.
|
||||
// Note that, the function `f` is executed without writing lock.
|
||||
func (set *StrSet) AddIfNotExistFunc(item string, f func() bool) bool {
|
||||
if !set.Contains(item) {
|
||||
if f() {
|
||||
@ -109,9 +109,9 @@ func (set *StrSet) AddIfNotExistFunc(item string, f func() bool) bool {
|
||||
|
||||
// AddIfNotExistFunc checks whether item exists in the set,
|
||||
// it adds the item to set and returns true if it does not exists in the set and
|
||||
// function <f> returns true, or else it does nothing and returns false.
|
||||
// function `f` returns true, or else it does nothing and returns false.
|
||||
//
|
||||
// Note that, the function <f> is executed without writing lock.
|
||||
// Note that, the function `f` is executed without writing lock.
|
||||
func (set *StrSet) AddIfNotExistFuncLock(item string, f func() bool) bool {
|
||||
if !set.Contains(item) {
|
||||
set.mu.Lock()
|
||||
@ -129,7 +129,7 @@ func (set *StrSet) AddIfNotExistFuncLock(item string, f func() bool) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Contains checks whether the set contains <item>.
|
||||
// Contains checks whether the set contains `item`.
|
||||
func (set *StrSet) Contains(item string) bool {
|
||||
var ok bool
|
||||
set.mu.RLock()
|
||||
@ -153,7 +153,7 @@ func (set *StrSet) ContainsI(item string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Remove deletes <item> from set.
|
||||
// Remove deletes `item` from set.
|
||||
func (set *StrSet) Remove(item string) {
|
||||
set.mu.Lock()
|
||||
if set.data != nil {
|
||||
@ -193,7 +193,7 @@ func (set *StrSet) Slice() []string {
|
||||
return ret
|
||||
}
|
||||
|
||||
// Join joins items with a string <glue>.
|
||||
// Join joins items with a string `glue`.
|
||||
func (set *StrSet) Join(glue string) string {
|
||||
set.mu.RLock()
|
||||
defer set.mu.RUnlock()
|
||||
@ -234,14 +234,14 @@ func (set *StrSet) String() string {
|
||||
return buffer.String()
|
||||
}
|
||||
|
||||
// LockFunc locks writing with callback function <f>.
|
||||
// LockFunc locks writing with callback function `f`.
|
||||
func (set *StrSet) LockFunc(f func(m map[string]struct{})) {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
f(set.data)
|
||||
}
|
||||
|
||||
// RLockFunc locks reading with callback function <f>.
|
||||
// RLockFunc locks reading with callback function `f`.
|
||||
func (set *StrSet) RLockFunc(f func(m map[string]struct{})) {
|
||||
set.mu.RLock()
|
||||
defer set.mu.RUnlock()
|
||||
@ -268,7 +268,7 @@ func (set *StrSet) Equal(other *StrSet) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// IsSubsetOf checks whether the current set is a sub-set of <other>.
|
||||
// IsSubsetOf checks whether the current set is a sub-set of `other`.
|
||||
func (set *StrSet) IsSubsetOf(other *StrSet) bool {
|
||||
if set == other {
|
||||
return true
|
||||
@ -285,8 +285,8 @@ func (set *StrSet) IsSubsetOf(other *StrSet) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// Union returns a new set which is the union of <set> and <other>.
|
||||
// Which means, all the items in <newSet> are in <set> or in <other>.
|
||||
// Union returns a new set which is the union of <set> and `other`.
|
||||
// Which means, all the items in <newSet> are in <set> or in `other`.
|
||||
func (set *StrSet) Union(others ...*StrSet) (newSet *StrSet) {
|
||||
newSet = NewStrSet()
|
||||
set.mu.RLock()
|
||||
@ -311,8 +311,8 @@ func (set *StrSet) Union(others ...*StrSet) (newSet *StrSet) {
|
||||
return
|
||||
}
|
||||
|
||||
// Diff returns a new set which is the difference set from <set> to <other>.
|
||||
// Which means, all the items in <newSet> are in <set> but not in <other>.
|
||||
// Diff returns a new set which is the difference set from <set> to `other`.
|
||||
// Which means, all the items in <newSet> are in <set> but not in `other`.
|
||||
func (set *StrSet) Diff(others ...*StrSet) (newSet *StrSet) {
|
||||
newSet = NewStrSet()
|
||||
set.mu.RLock()
|
||||
@ -332,8 +332,8 @@ func (set *StrSet) Diff(others ...*StrSet) (newSet *StrSet) {
|
||||
return
|
||||
}
|
||||
|
||||
// Intersect returns a new set which is the intersection from <set> to <other>.
|
||||
// Which means, all the items in <newSet> are in <set> and also in <other>.
|
||||
// Intersect returns a new set which is the intersection from <set> to `other`.
|
||||
// Which means, all the items in <newSet> are in <set> and also in `other`.
|
||||
func (set *StrSet) Intersect(others ...*StrSet) (newSet *StrSet) {
|
||||
newSet = NewStrSet()
|
||||
set.mu.RLock()
|
||||
@ -354,11 +354,11 @@ func (set *StrSet) Intersect(others ...*StrSet) (newSet *StrSet) {
|
||||
return
|
||||
}
|
||||
|
||||
// Complement returns a new set which is the complement from <set> to <full>.
|
||||
// Which means, all the items in <newSet> are in <full> and not in <set>.
|
||||
// Complement returns a new set which is the complement from <set> to `full`.
|
||||
// Which means, all the items in <newSet> are in <full> and not in `set`.
|
||||
//
|
||||
// It returns the difference between <full> and <set>
|
||||
// if the given set <full> is not the full set of <set>.
|
||||
// It returns the difference between <full> and `set`
|
||||
// if the given set <full> is not the full set of `set`.
|
||||
func (set *StrSet) Complement(full *StrSet) (newSet *StrSet) {
|
||||
newSet = NewStrSet()
|
||||
set.mu.RLock()
|
||||
@ -375,7 +375,7 @@ func (set *StrSet) Complement(full *StrSet) (newSet *StrSet) {
|
||||
return
|
||||
}
|
||||
|
||||
// Merge adds items from <others> sets into <set>.
|
||||
// Merge adds items from <others> sets into `set`.
|
||||
func (set *StrSet) Merge(others ...*StrSet) *StrSet {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
@ -416,7 +416,7 @@ func (set *StrSet) Pop() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Pops randomly pops <size> items from set.
|
||||
// Pops randomly pops `size` items from set.
|
||||
// It returns all items if size == -1.
|
||||
func (set *StrSet) Pops(size int) []string {
|
||||
set.mu.Lock()
|
||||
@ -440,7 +440,7 @@ func (set *StrSet) Pops(size int) []string {
|
||||
return array
|
||||
}
|
||||
|
||||
// Walk applies a user supplied function <f> to every item of set.
|
||||
// Walk applies a user supplied function `f` to every item of set.
|
||||
func (set *StrSet) Walk(f func(item string) string) *StrSet {
|
||||
set.mu.Lock()
|
||||
defer set.mu.Unlock()
|
||||
|
||||
@ -34,7 +34,7 @@ type AVLTreeNode struct {
|
||||
}
|
||||
|
||||
// NewAVLTree instantiates an AVL tree with the custom key comparator.
|
||||
// The parameter <safe> is used to specify whether using tree in concurrent-safety,
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewAVLTree(comparator func(v1, v2 interface{}) int, safe ...bool) *AVLTree {
|
||||
return &AVLTree{
|
||||
@ -44,7 +44,7 @@ func NewAVLTree(comparator func(v1, v2 interface{}) int, safe ...bool) *AVLTree
|
||||
}
|
||||
|
||||
// NewAVLTreeFrom instantiates an AVL tree with the custom key comparator and data map.
|
||||
// The parameter <safe> is used to specify whether using tree in concurrent-safety,
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewAVLTreeFrom(comparator func(v1, v2 interface{}) int, data map[interface{}]interface{}, safe ...bool) *AVLTree {
|
||||
tree := NewAVLTree(comparator, safe...)
|
||||
@ -77,8 +77,8 @@ func (tree *AVLTree) Sets(data map[interface{}]interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// Search searches the tree with given <key>.
|
||||
// Second return parameter <found> is true if key was found, otherwise false.
|
||||
// Search searches the tree with given `key`.
|
||||
// Second return parameter `found` is true if key was found, otherwise false.
|
||||
func (tree *AVLTree) Search(key interface{}) (value interface{}, found bool) {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
@ -88,8 +88,8 @@ func (tree *AVLTree) Search(key interface{}) (value interface{}, found bool) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// doSearch searches the tree with given <key>.
|
||||
// Second return parameter <found> is true if key was found, otherwise false.
|
||||
// doSearch searches the tree with given `key`.
|
||||
// Second return parameter `found` is true if key was found, otherwise false.
|
||||
func (tree *AVLTree) doSearch(key interface{}) (node *AVLTreeNode, found bool) {
|
||||
node = tree.root
|
||||
for node != nil {
|
||||
@ -106,21 +106,21 @@ func (tree *AVLTree) doSearch(key interface{}) (node *AVLTreeNode, found bool) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// Get searches the node in the tree by <key> and returns its value or nil if key is not found in tree.
|
||||
// Get searches the node in the tree by `key` and returns its value or nil if key is not found in tree.
|
||||
func (tree *AVLTree) Get(key interface{}) (value interface{}) {
|
||||
value, _ = tree.Search(key)
|
||||
return
|
||||
}
|
||||
|
||||
// doSetWithLockCheck checks whether value of the key exists with mutex.Lock,
|
||||
// if not exists, set value to the map with given <key>,
|
||||
// if not exists, set value to the map with given `key`,
|
||||
// or else just return the existing value.
|
||||
//
|
||||
// When setting value, if <value> is type of <func() interface {}>,
|
||||
// When setting value, if `value` is type of <func() interface {}>,
|
||||
// it will be executed with mutex.Lock of the hash map,
|
||||
// and its return value will be set to the map with <key>.
|
||||
// and its return value will be set to the map with `key`.
|
||||
//
|
||||
// It returns value with given <key>.
|
||||
// It returns value with given `key`.
|
||||
func (tree *AVLTree) doSetWithLockCheck(key interface{}, value interface{}) interface{} {
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
@ -137,7 +137,7 @@ func (tree *AVLTree) doSetWithLockCheck(key interface{}, value interface{}) inte
|
||||
}
|
||||
|
||||
// GetOrSet returns the value by key,
|
||||
// or sets value with given <value> if it does not exist and then returns this value.
|
||||
// or sets value with given `value` if it does not exist and then returns this value.
|
||||
func (tree *AVLTree) GetOrSet(key interface{}, value interface{}) interface{} {
|
||||
if v, ok := tree.Search(key); !ok {
|
||||
return tree.doSetWithLockCheck(key, value)
|
||||
@ -147,7 +147,7 @@ func (tree *AVLTree) GetOrSet(key interface{}, value interface{}) interface{} {
|
||||
}
|
||||
|
||||
// GetOrSetFunc returns the value by key,
|
||||
// or sets value with returned value of callback function <f> if it does not exist
|
||||
// or sets value with returned value of callback function `f` if it does not exist
|
||||
// and then returns this value.
|
||||
func (tree *AVLTree) GetOrSetFunc(key interface{}, f func() interface{}) interface{} {
|
||||
if v, ok := tree.Search(key); !ok {
|
||||
@ -158,10 +158,10 @@ func (tree *AVLTree) GetOrSetFunc(key interface{}, f func() interface{}) interfa
|
||||
}
|
||||
|
||||
// GetOrSetFuncLock returns the value by key,
|
||||
// or sets value with returned value of callback function <f> if it does not exist
|
||||
// or sets value with returned value of callback function `f` if it does not exist
|
||||
// and then returns this value.
|
||||
//
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function <f>
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`
|
||||
// with mutex.Lock of the hash map.
|
||||
func (tree *AVLTree) GetOrSetFuncLock(key interface{}, f func() interface{}) interface{} {
|
||||
if v, ok := tree.Search(key); !ok {
|
||||
@ -171,7 +171,7 @@ func (tree *AVLTree) GetOrSetFuncLock(key interface{}, f func() interface{}) int
|
||||
}
|
||||
}
|
||||
|
||||
// GetVar returns a gvar.Var with the value by given <key>.
|
||||
// GetVar returns a gvar.Var with the value by given `key`.
|
||||
// The returned gvar.Var is un-concurrent safe.
|
||||
func (tree *AVLTree) GetVar(key interface{}) *gvar.Var {
|
||||
return gvar.New(tree.Get(key))
|
||||
@ -195,8 +195,8 @@ func (tree *AVLTree) GetVarOrSetFuncLock(key interface{}, f func() interface{})
|
||||
return gvar.New(tree.GetOrSetFuncLock(key, f))
|
||||
}
|
||||
|
||||
// SetIfNotExist sets <value> to the map if the <key> does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExist sets <value> to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
func (tree *AVLTree) SetIfNotExist(key interface{}, value interface{}) bool {
|
||||
if !tree.Contains(key) {
|
||||
tree.doSetWithLockCheck(key, value)
|
||||
@ -205,8 +205,8 @@ func (tree *AVLTree) SetIfNotExist(key interface{}, value interface{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SetIfNotExistFunc sets value with return value of callback function <f>, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
func (tree *AVLTree) SetIfNotExistFunc(key interface{}, f func() interface{}) bool {
|
||||
if !tree.Contains(key) {
|
||||
tree.doSetWithLockCheck(key, f())
|
||||
@ -215,11 +215,11 @@ func (tree *AVLTree) SetIfNotExistFunc(key interface{}, f func() interface{}) bo
|
||||
return false
|
||||
}
|
||||
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function <f>, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function <f> with mutex.Lock of the hash map.
|
||||
// it executes function `f` with mutex.Lock of the hash map.
|
||||
func (tree *AVLTree) SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool {
|
||||
if !tree.Contains(key) {
|
||||
tree.doSetWithLockCheck(key, f)
|
||||
@ -228,7 +228,7 @@ func (tree *AVLTree) SetIfNotExistFuncLock(key interface{}, f func() interface{}
|
||||
return false
|
||||
}
|
||||
|
||||
// Contains checks whether <key> exists in the tree.
|
||||
// Contains checks whether `key` exists in the tree.
|
||||
func (tree *AVLTree) Contains(key interface{}) bool {
|
||||
_, ok := tree.Search(key)
|
||||
return ok
|
||||
@ -243,7 +243,7 @@ func (tree *AVLTree) Remove(key interface{}) (value interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
// Removes batch deletes values of the tree by <keys>.
|
||||
// Removes batch deletes values of the tree by `keys`.
|
||||
func (tree *AVLTree) Removes(keys []interface{}) {
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
@ -386,7 +386,7 @@ func (tree *AVLTree) Clear() {
|
||||
tree.size = 0
|
||||
}
|
||||
|
||||
// Replace the data of the tree with given <data>.
|
||||
// Replace the data of the tree with given `data`.
|
||||
func (tree *AVLTree) Replace(data map[interface{}]interface{}) {
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
@ -437,7 +437,7 @@ func (tree *AVLTree) MapStrAny() map[string]interface{} {
|
||||
// Note that you should guarantee the value is the same type as key,
|
||||
// or else the comparator would panic.
|
||||
//
|
||||
// If the type of value is different with key, you pass the new <comparator>.
|
||||
// If the type of value is different with key, you pass the new `comparator`.
|
||||
func (tree *AVLTree) Flip(comparator ...func(v1, v2 interface{}) int) {
|
||||
t := (*AVLTree)(nil)
|
||||
if len(comparator) > 0 {
|
||||
@ -465,18 +465,18 @@ func (tree *AVLTree) IteratorFrom(key interface{}, match bool, f func(key, value
|
||||
tree.IteratorAscFrom(key, match, f)
|
||||
}
|
||||
|
||||
// IteratorAsc iterates the tree readonly in ascending order with given callback function <f>.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// IteratorAsc iterates the tree readonly in ascending order with given callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (tree *AVLTree) IteratorAsc(f func(key, value interface{}) bool) {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
tree.doIteratorAsc(tree.bottom(0), f)
|
||||
}
|
||||
|
||||
// IteratorAscFrom iterates the tree readonly in ascending order with given callback function <f>.
|
||||
// The parameter <key> specifies the start entry for iterating. The <match> specifies whether
|
||||
// starting iterating if the <key> is fully matched, or else using index searching iterating.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// IteratorAscFrom iterates the tree readonly in ascending order with given callback function `f`.
|
||||
// The parameter <key> specifies the start entry for iterating. The `match` specifies whether
|
||||
// starting iterating if the `key` is fully matched, or else using index searching iterating.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (tree *AVLTree) IteratorAscFrom(key interface{}, match bool, f func(key, value interface{}) bool) {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
@ -499,18 +499,18 @@ func (tree *AVLTree) doIteratorAsc(node *AVLTreeNode, f func(key, value interfac
|
||||
}
|
||||
}
|
||||
|
||||
// IteratorDesc iterates the tree readonly in descending order with given callback function <f>.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// IteratorDesc iterates the tree readonly in descending order with given callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (tree *AVLTree) IteratorDesc(f func(key, value interface{}) bool) {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
tree.doIteratorDesc(tree.bottom(1), f)
|
||||
}
|
||||
|
||||
// IteratorDescFrom iterates the tree readonly in descending order with given callback function <f>.
|
||||
// The parameter <key> specifies the start entry for iterating. The <match> specifies whether
|
||||
// starting iterating if the <key> is fully matched, or else using index searching iterating.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// IteratorDescFrom iterates the tree readonly in descending order with given callback function `f`.
|
||||
// The parameter <key> specifies the start entry for iterating. The `match` specifies whether
|
||||
// starting iterating if the `key` is fully matched, or else using index searching iterating.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (tree *AVLTree) IteratorDescFrom(key interface{}, match bool, f func(key, value interface{}) bool) {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
|
||||
@ -40,10 +40,10 @@ type BTreeEntry struct {
|
||||
Value interface{}
|
||||
}
|
||||
|
||||
// NewBTree instantiates a B-tree with <m> (maximum number of children) and a custom key comparator.
|
||||
// The parameter <safe> is used to specify whether using tree in concurrent-safety,
|
||||
// NewBTree instantiates a B-tree with `m` (maximum number of children) and a custom key comparator.
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety,
|
||||
// which is false in default.
|
||||
// Note that the <m> must be greater or equal than 3, or else it panics.
|
||||
// Note that the `m` must be greater or equal than 3, or else it panics.
|
||||
func NewBTree(m int, comparator func(v1, v2 interface{}) int, safe ...bool) *BTree {
|
||||
if m < 3 {
|
||||
panic("Invalid order, should be at least 3")
|
||||
@ -55,8 +55,8 @@ func NewBTree(m int, comparator func(v1, v2 interface{}) int, safe ...bool) *BTr
|
||||
}
|
||||
}
|
||||
|
||||
// NewBTreeFrom instantiates a B-tree with <m> (maximum number of children), a custom key comparator and data map.
|
||||
// The parameter <safe> is used to specify whether using tree in concurrent-safety,
|
||||
// NewBTreeFrom instantiates a B-tree with `m` (maximum number of children), a custom key comparator and data map.
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewBTreeFrom(m int, comparator func(v1, v2 interface{}) int, data map[interface{}]interface{}, safe ...bool) *BTree {
|
||||
tree := NewBTree(m, comparator, safe...)
|
||||
@ -104,21 +104,21 @@ func (tree *BTree) Sets(data map[interface{}]interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// Get searches the node in the tree by <key> and returns its value or nil if key is not found in tree.
|
||||
// Get searches the node in the tree by `key` and returns its value or nil if key is not found in tree.
|
||||
func (tree *BTree) Get(key interface{}) (value interface{}) {
|
||||
value, _ = tree.Search(key)
|
||||
return
|
||||
}
|
||||
|
||||
// doSetWithLockCheck checks whether value of the key exists with mutex.Lock,
|
||||
// if not exists, set value to the map with given <key>,
|
||||
// if not exists, set value to the map with given `key`,
|
||||
// or else just return the existing value.
|
||||
//
|
||||
// When setting value, if <value> is type of <func() interface {}>,
|
||||
// When setting value, if `value` is type of <func() interface {}>,
|
||||
// it will be executed with mutex.Lock of the hash map,
|
||||
// and its return value will be set to the map with <key>.
|
||||
// and its return value will be set to the map with `key`.
|
||||
//
|
||||
// It returns value with given <key>.
|
||||
// It returns value with given `key`.
|
||||
func (tree *BTree) doSetWithLockCheck(key interface{}, value interface{}) interface{} {
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
@ -135,7 +135,7 @@ func (tree *BTree) doSetWithLockCheck(key interface{}, value interface{}) interf
|
||||
}
|
||||
|
||||
// GetOrSet returns the value by key,
|
||||
// or sets value with given <value> if it does not exist and then returns this value.
|
||||
// or sets value with given `value` if it does not exist and then returns this value.
|
||||
func (tree *BTree) GetOrSet(key interface{}, value interface{}) interface{} {
|
||||
if v, ok := tree.Search(key); !ok {
|
||||
return tree.doSetWithLockCheck(key, value)
|
||||
@ -145,7 +145,7 @@ func (tree *BTree) GetOrSet(key interface{}, value interface{}) interface{} {
|
||||
}
|
||||
|
||||
// GetOrSetFunc returns the value by key,
|
||||
// or sets value with returned value of callback function <f> if it does not exist
|
||||
// or sets value with returned value of callback function `f` if it does not exist
|
||||
// and then returns this value.
|
||||
func (tree *BTree) GetOrSetFunc(key interface{}, f func() interface{}) interface{} {
|
||||
if v, ok := tree.Search(key); !ok {
|
||||
@ -156,10 +156,10 @@ func (tree *BTree) GetOrSetFunc(key interface{}, f func() interface{}) interface
|
||||
}
|
||||
|
||||
// GetOrSetFuncLock returns the value by key,
|
||||
// or sets value with returned value of callback function <f> if it does not exist
|
||||
// or sets value with returned value of callback function `f` if it does not exist
|
||||
// and then returns this value.
|
||||
//
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function <f>
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`
|
||||
// with mutex.Lock of the hash map.
|
||||
func (tree *BTree) GetOrSetFuncLock(key interface{}, f func() interface{}) interface{} {
|
||||
if v, ok := tree.Search(key); !ok {
|
||||
@ -169,7 +169,7 @@ func (tree *BTree) GetOrSetFuncLock(key interface{}, f func() interface{}) inter
|
||||
}
|
||||
}
|
||||
|
||||
// GetVar returns a gvar.Var with the value by given <key>.
|
||||
// GetVar returns a gvar.Var with the value by given `key`.
|
||||
// The returned gvar.Var is un-concurrent safe.
|
||||
func (tree *BTree) GetVar(key interface{}) *gvar.Var {
|
||||
return gvar.New(tree.Get(key))
|
||||
@ -193,8 +193,8 @@ func (tree *BTree) GetVarOrSetFuncLock(key interface{}, f func() interface{}) *g
|
||||
return gvar.New(tree.GetOrSetFuncLock(key, f))
|
||||
}
|
||||
|
||||
// SetIfNotExist sets <value> to the map if the <key> does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExist sets <value> to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
func (tree *BTree) SetIfNotExist(key interface{}, value interface{}) bool {
|
||||
if !tree.Contains(key) {
|
||||
tree.doSetWithLockCheck(key, value)
|
||||
@ -203,8 +203,8 @@ func (tree *BTree) SetIfNotExist(key interface{}, value interface{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SetIfNotExistFunc sets value with return value of callback function <f>, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
func (tree *BTree) SetIfNotExistFunc(key interface{}, f func() interface{}) bool {
|
||||
if !tree.Contains(key) {
|
||||
tree.doSetWithLockCheck(key, f())
|
||||
@ -213,11 +213,11 @@ func (tree *BTree) SetIfNotExistFunc(key interface{}, f func() interface{}) bool
|
||||
return false
|
||||
}
|
||||
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function <f>, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function <f> with mutex.Lock of the hash map.
|
||||
// it executes function `f` with mutex.Lock of the hash map.
|
||||
func (tree *BTree) SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool {
|
||||
if !tree.Contains(key) {
|
||||
tree.doSetWithLockCheck(key, f)
|
||||
@ -226,7 +226,7 @@ func (tree *BTree) SetIfNotExistFuncLock(key interface{}, f func() interface{})
|
||||
return false
|
||||
}
|
||||
|
||||
// Contains checks whether <key> exists in the tree.
|
||||
// Contains checks whether `key` exists in the tree.
|
||||
func (tree *BTree) Contains(key interface{}) bool {
|
||||
_, ok := tree.Search(key)
|
||||
return ok
|
||||
@ -244,14 +244,14 @@ func (tree *BTree) doRemove(key interface{}) (value interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
// Remove removes the node from the tree by <key>.
|
||||
// Remove removes the node from the tree by `key`.
|
||||
func (tree *BTree) Remove(key interface{}) (value interface{}) {
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
return tree.doRemove(key)
|
||||
}
|
||||
|
||||
// Removes batch deletes values of the tree by <keys>.
|
||||
// Removes batch deletes values of the tree by `keys`.
|
||||
func (tree *BTree) Removes(keys []interface{}) {
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
@ -324,7 +324,7 @@ func (tree *BTree) Clear() {
|
||||
tree.size = 0
|
||||
}
|
||||
|
||||
// Replace the data of the tree with given <data>.
|
||||
// Replace the data of the tree with given `data`.
|
||||
func (tree *BTree) Replace(data map[interface{}]interface{}) {
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
@ -369,8 +369,8 @@ func (tree *BTree) String() string {
|
||||
return buffer.String()
|
||||
}
|
||||
|
||||
// Search searches the tree with given <key>.
|
||||
// Second return parameter <found> is true if key was found, otherwise false.
|
||||
// Search searches the tree with given `key`.
|
||||
// Second return parameter `found` is true if key was found, otherwise false.
|
||||
func (tree *BTree) Search(key interface{}) (value interface{}, found bool) {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
@ -381,7 +381,7 @@ func (tree *BTree) Search(key interface{}) (value interface{}, found bool) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// Search searches the tree with given <key> without mutex.
|
||||
// Search searches the tree with given `key` without mutex.
|
||||
// It returns the entry if found or otherwise nil.
|
||||
func (tree *BTree) doSearch(key interface{}) *BTreeEntry {
|
||||
node, index, found := tree.searchRecursively(tree.root, key)
|
||||
@ -406,8 +406,8 @@ func (tree *BTree) IteratorFrom(key interface{}, match bool, f func(key, value i
|
||||
tree.IteratorAscFrom(key, match, f)
|
||||
}
|
||||
|
||||
// IteratorAsc iterates the tree readonly in ascending order with given callback function <f>.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// IteratorAsc iterates the tree readonly in ascending order with given callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (tree *BTree) IteratorAsc(f func(key, value interface{}) bool) {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
@ -418,10 +418,10 @@ func (tree *BTree) IteratorAsc(f func(key, value interface{}) bool) {
|
||||
tree.doIteratorAsc(node, node.Entries[0], 0, f)
|
||||
}
|
||||
|
||||
// IteratorAscFrom iterates the tree readonly in ascending order with given callback function <f>.
|
||||
// The parameter <key> specifies the start entry for iterating. The <match> specifies whether
|
||||
// starting iterating if the <key> is fully matched, or else using index searching iterating.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// IteratorAscFrom iterates the tree readonly in ascending order with given callback function `f`.
|
||||
// The parameter <key> specifies the start entry for iterating. The `match` specifies whether
|
||||
// starting iterating if the `key` is fully matched, or else using index searching iterating.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (tree *BTree) IteratorAscFrom(key interface{}, match bool, f func(key, value interface{}) bool) {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
@ -479,8 +479,8 @@ loop:
|
||||
}
|
||||
}
|
||||
|
||||
// IteratorDesc iterates the tree readonly in descending order with given callback function <f>.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// IteratorDesc iterates the tree readonly in descending order with given callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (tree *BTree) IteratorDesc(f func(key, value interface{}) bool) {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
@ -493,10 +493,10 @@ func (tree *BTree) IteratorDesc(f func(key, value interface{}) bool) {
|
||||
tree.doIteratorDesc(node, entry, index, f)
|
||||
}
|
||||
|
||||
// IteratorDescFrom iterates the tree readonly in descending order with given callback function <f>.
|
||||
// The parameter <key> specifies the start entry for iterating. The <match> specifies whether
|
||||
// starting iterating if the <key> is fully matched, or else using index searching iterating.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// IteratorDescFrom iterates the tree readonly in descending order with given callback function `f`.
|
||||
// The parameter <key> specifies the start entry for iterating. The `match` specifies whether
|
||||
// starting iterating if the `key` is fully matched, or else using index searching iterating.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (tree *BTree) IteratorDescFrom(key interface{}, match bool, f func(key, value interface{}) bool) {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
@ -510,8 +510,8 @@ func (tree *BTree) IteratorDescFrom(key interface{}, match bool, f func(key, val
|
||||
}
|
||||
}
|
||||
|
||||
// IteratorDesc iterates the tree readonly in descending order with given callback function <f>.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// IteratorDesc iterates the tree readonly in descending order with given callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (tree *BTree) doIteratorDesc(node *BTreeNode, entry *BTreeEntry, index int, f func(key, value interface{}) bool) {
|
||||
first := true
|
||||
loop:
|
||||
|
||||
@ -41,7 +41,7 @@ type RedBlackTreeNode struct {
|
||||
}
|
||||
|
||||
// NewRedBlackTree instantiates a red-black tree with the custom key comparator.
|
||||
// The parameter <safe> is used to specify whether using tree in concurrent-safety,
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewRedBlackTree(comparator func(v1, v2 interface{}) int, safe ...bool) *RedBlackTree {
|
||||
return &RedBlackTree{
|
||||
@ -50,8 +50,8 @@ func NewRedBlackTree(comparator func(v1, v2 interface{}) int, safe ...bool) *Red
|
||||
}
|
||||
}
|
||||
|
||||
// NewRedBlackTreeFrom instantiates a red-black tree with the custom key comparator and <data> map.
|
||||
// The parameter <safe> is used to specify whether using tree in concurrent-safety,
|
||||
// NewRedBlackTreeFrom instantiates a red-black tree with the custom key comparator and `data` map.
|
||||
// The parameter `safe` is used to specify whether using tree in concurrent-safety,
|
||||
// which is false in default.
|
||||
func NewRedBlackTreeFrom(comparator func(v1, v2 interface{}) int, data map[interface{}]interface{}, safe ...bool) *RedBlackTree {
|
||||
tree := NewRedBlackTree(comparator, safe...)
|
||||
@ -146,21 +146,21 @@ func (tree *RedBlackTree) doSet(key interface{}, value interface{}) {
|
||||
tree.size++
|
||||
}
|
||||
|
||||
// Get searches the node in the tree by <key> and returns its value or nil if key is not found in tree.
|
||||
// Get searches the node in the tree by `key` and returns its value or nil if key is not found in tree.
|
||||
func (tree *RedBlackTree) Get(key interface{}) (value interface{}) {
|
||||
value, _ = tree.Search(key)
|
||||
return
|
||||
}
|
||||
|
||||
// doSetWithLockCheck checks whether value of the key exists with mutex.Lock,
|
||||
// if not exists, set value to the map with given <key>,
|
||||
// if not exists, set value to the map with given `key`,
|
||||
// or else just return the existing value.
|
||||
//
|
||||
// When setting value, if <value> is type of <func() interface {}>,
|
||||
// When setting value, if `value` is type of <func() interface {}>,
|
||||
// it will be executed with mutex.Lock of the hash map,
|
||||
// and its return value will be set to the map with <key>.
|
||||
// and its return value will be set to the map with `key`.
|
||||
//
|
||||
// It returns value with given <key>.
|
||||
// It returns value with given `key`.
|
||||
func (tree *RedBlackTree) doSetWithLockCheck(key interface{}, value interface{}) interface{} {
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
@ -177,7 +177,7 @@ func (tree *RedBlackTree) doSetWithLockCheck(key interface{}, value interface{})
|
||||
}
|
||||
|
||||
// GetOrSet returns the value by key,
|
||||
// or sets value with given <value> if it does not exist and then returns this value.
|
||||
// or sets value with given `value` if it does not exist and then returns this value.
|
||||
func (tree *RedBlackTree) GetOrSet(key interface{}, value interface{}) interface{} {
|
||||
if v, ok := tree.Search(key); !ok {
|
||||
return tree.doSetWithLockCheck(key, value)
|
||||
@ -187,7 +187,7 @@ func (tree *RedBlackTree) GetOrSet(key interface{}, value interface{}) interface
|
||||
}
|
||||
|
||||
// GetOrSetFunc returns the value by key,
|
||||
// or sets value with returned value of callback function <f> if it does not exist
|
||||
// or sets value with returned value of callback function `f` if it does not exist
|
||||
// and then returns this value.
|
||||
func (tree *RedBlackTree) GetOrSetFunc(key interface{}, f func() interface{}) interface{} {
|
||||
if v, ok := tree.Search(key); !ok {
|
||||
@ -198,10 +198,10 @@ func (tree *RedBlackTree) GetOrSetFunc(key interface{}, f func() interface{}) in
|
||||
}
|
||||
|
||||
// GetOrSetFuncLock returns the value by key,
|
||||
// or sets value with returned value of callback function <f> if it does not exist
|
||||
// or sets value with returned value of callback function `f` if it does not exist
|
||||
// and then returns this value.
|
||||
//
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function <f>
|
||||
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`
|
||||
// with mutex.Lock of the hash map.
|
||||
func (tree *RedBlackTree) GetOrSetFuncLock(key interface{}, f func() interface{}) interface{} {
|
||||
if v, ok := tree.Search(key); !ok {
|
||||
@ -211,7 +211,7 @@ func (tree *RedBlackTree) GetOrSetFuncLock(key interface{}, f func() interface{}
|
||||
}
|
||||
}
|
||||
|
||||
// GetVar returns a gvar.Var with the value by given <key>.
|
||||
// GetVar returns a gvar.Var with the value by given `key`.
|
||||
// The returned gvar.Var is un-concurrent safe.
|
||||
func (tree *RedBlackTree) GetVar(key interface{}) *gvar.Var {
|
||||
return gvar.New(tree.Get(key))
|
||||
@ -235,8 +235,8 @@ func (tree *RedBlackTree) GetVarOrSetFuncLock(key interface{}, f func() interfac
|
||||
return gvar.New(tree.GetOrSetFuncLock(key, f))
|
||||
}
|
||||
|
||||
// SetIfNotExist sets <value> to the map if the <key> does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExist sets <value> to the map if the `key` does not exist, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
func (tree *RedBlackTree) SetIfNotExist(key interface{}, value interface{}) bool {
|
||||
if !tree.Contains(key) {
|
||||
tree.doSetWithLockCheck(key, value)
|
||||
@ -245,8 +245,8 @@ func (tree *RedBlackTree) SetIfNotExist(key interface{}, value interface{}) bool
|
||||
return false
|
||||
}
|
||||
|
||||
// SetIfNotExistFunc sets value with return value of callback function <f>, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
func (tree *RedBlackTree) SetIfNotExistFunc(key interface{}, f func() interface{}) bool {
|
||||
if !tree.Contains(key) {
|
||||
tree.doSetWithLockCheck(key, f())
|
||||
@ -255,11 +255,11 @@ func (tree *RedBlackTree) SetIfNotExistFunc(key interface{}, f func() interface{
|
||||
return false
|
||||
}
|
||||
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function <f>, and then returns true.
|
||||
// It returns false if <key> exists, and <value> would be ignored.
|
||||
// SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true.
|
||||
// It returns false if <key> exists, and `value` would be ignored.
|
||||
//
|
||||
// SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that
|
||||
// it executes function <f> with mutex.Lock of the hash map.
|
||||
// it executes function `f` with mutex.Lock of the hash map.
|
||||
func (tree *RedBlackTree) SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool {
|
||||
if !tree.Contains(key) {
|
||||
tree.doSetWithLockCheck(key, f)
|
||||
@ -268,13 +268,13 @@ func (tree *RedBlackTree) SetIfNotExistFuncLock(key interface{}, f func() interf
|
||||
return false
|
||||
}
|
||||
|
||||
// Contains checks whether <key> exists in the tree.
|
||||
// Contains checks whether `key` exists in the tree.
|
||||
func (tree *RedBlackTree) Contains(key interface{}) bool {
|
||||
_, ok := tree.Search(key)
|
||||
return ok
|
||||
}
|
||||
|
||||
// doRemove removes the node from the tree by <key> without mutex.
|
||||
// doRemove removes the node from the tree by `key` without mutex.
|
||||
func (tree *RedBlackTree) doRemove(key interface{}) (value interface{}) {
|
||||
child := (*RedBlackTreeNode)(nil)
|
||||
node, found := tree.doSearch(key)
|
||||
@ -307,14 +307,14 @@ func (tree *RedBlackTree) doRemove(key interface{}) (value interface{}) {
|
||||
return
|
||||
}
|
||||
|
||||
// Remove removes the node from the tree by <key>.
|
||||
// Remove removes the node from the tree by `key`.
|
||||
func (tree *RedBlackTree) Remove(key interface{}) (value interface{}) {
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
return tree.doRemove(key)
|
||||
}
|
||||
|
||||
// Removes batch deletes values of the tree by <keys>.
|
||||
// Removes batch deletes values of the tree by `keys`.
|
||||
func (tree *RedBlackTree) Removes(keys []interface{}) {
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
@ -436,7 +436,7 @@ func (tree *RedBlackTree) rightNode() *RedBlackTreeNode {
|
||||
// Floor Finds floor node of the input key, return the floor node or nil if no floor node is found.
|
||||
// Second return parameter is true if floor was found, otherwise false.
|
||||
//
|
||||
// Floor node is defined as the largest node that its key is smaller than or equal to the given <key>.
|
||||
// Floor node is defined as the largest node that its key is smaller than or equal to the given `key`.
|
||||
// A floor node may not be found, either because the tree is empty, or because
|
||||
// all nodes in the tree are larger than the given node.
|
||||
func (tree *RedBlackTree) Floor(key interface{}) (floor *RedBlackTreeNode, found bool) {
|
||||
@ -464,7 +464,7 @@ func (tree *RedBlackTree) Floor(key interface{}) (floor *RedBlackTreeNode, found
|
||||
// Ceiling finds ceiling node of the input key, return the ceiling node or nil if no ceiling node is found.
|
||||
// Second return parameter is true if ceiling was found, otherwise false.
|
||||
//
|
||||
// Ceiling node is defined as the smallest node that its key is larger than or equal to the given <key>.
|
||||
// Ceiling node is defined as the smallest node that its key is larger than or equal to the given `key`.
|
||||
// A ceiling node may not be found, either because the tree is empty, or because
|
||||
// all nodes in the tree are smaller than the given node.
|
||||
func (tree *RedBlackTree) Ceiling(key interface{}) (ceiling *RedBlackTreeNode, found bool) {
|
||||
@ -499,18 +499,18 @@ func (tree *RedBlackTree) IteratorFrom(key interface{}, match bool, f func(key,
|
||||
tree.IteratorAscFrom(key, match, f)
|
||||
}
|
||||
|
||||
// IteratorAsc iterates the tree readonly in ascending order with given callback function <f>.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// IteratorAsc iterates the tree readonly in ascending order with given callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (tree *RedBlackTree) IteratorAsc(f func(key, value interface{}) bool) {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
tree.doIteratorAsc(tree.leftNode(), f)
|
||||
}
|
||||
|
||||
// IteratorAscFrom iterates the tree readonly in ascending order with given callback function <f>.
|
||||
// The parameter <key> specifies the start entry for iterating. The <match> specifies whether
|
||||
// starting iterating if the <key> is fully matched, or else using index searching iterating.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// IteratorAscFrom iterates the tree readonly in ascending order with given callback function `f`.
|
||||
// The parameter <key> specifies the start entry for iterating. The `match` specifies whether
|
||||
// starting iterating if the `key` is fully matched, or else using index searching iterating.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (tree *RedBlackTree) IteratorAscFrom(key interface{}, match bool, f func(key, value interface{}) bool) {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
@ -550,18 +550,18 @@ loop:
|
||||
}
|
||||
}
|
||||
|
||||
// IteratorDesc iterates the tree readonly in descending order with given callback function <f>.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// IteratorDesc iterates the tree readonly in descending order with given callback function `f`.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (tree *RedBlackTree) IteratorDesc(f func(key, value interface{}) bool) {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
tree.doIteratorDesc(tree.rightNode(), f)
|
||||
}
|
||||
|
||||
// IteratorDescFrom iterates the tree readonly in descending order with given callback function <f>.
|
||||
// The parameter <key> specifies the start entry for iterating. The <match> specifies whether
|
||||
// starting iterating if the <key> is fully matched, or else using index searching iterating.
|
||||
// If <f> returns true, then it continues iterating; or false to stop.
|
||||
// IteratorDescFrom iterates the tree readonly in descending order with given callback function `f`.
|
||||
// The parameter <key> specifies the start entry for iterating. The `match` specifies whether
|
||||
// starting iterating if the `key` is fully matched, or else using index searching iterating.
|
||||
// If `f` returns true, then it continues iterating; or false to stop.
|
||||
func (tree *RedBlackTree) IteratorDescFrom(key interface{}, match bool, f func(key, value interface{}) bool) {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
@ -609,7 +609,7 @@ func (tree *RedBlackTree) Clear() {
|
||||
tree.size = 0
|
||||
}
|
||||
|
||||
// Replace the data of the tree with given <data>.
|
||||
// Replace the data of the tree with given `data`.
|
||||
func (tree *RedBlackTree) Replace(data map[interface{}]interface{}) {
|
||||
tree.mu.Lock()
|
||||
defer tree.mu.Unlock()
|
||||
@ -636,8 +636,8 @@ func (tree *RedBlackTree) Print() {
|
||||
fmt.Println(tree.String())
|
||||
}
|
||||
|
||||
// Search searches the tree with given <key>.
|
||||
// Second return parameter <found> is true if key was found, otherwise false.
|
||||
// Search searches the tree with given `key`.
|
||||
// Second return parameter `found` is true if key was found, otherwise false.
|
||||
func (tree *RedBlackTree) Search(key interface{}) (value interface{}, found bool) {
|
||||
tree.mu.RLock()
|
||||
defer tree.mu.RUnlock()
|
||||
@ -652,7 +652,7 @@ func (tree *RedBlackTree) Search(key interface{}) (value interface{}, found bool
|
||||
// Note that you should guarantee the value is the same type as key,
|
||||
// or else the comparator would panic.
|
||||
//
|
||||
// If the type of value is different with key, you pass the new <comparator>.
|
||||
// If the type of value is different with key, you pass the new `comparator`.
|
||||
func (tree *RedBlackTree) Flip(comparator ...func(v1, v2 interface{}) int) {
|
||||
t := (*RedBlackTree)(nil)
|
||||
if len(comparator) > 0 {
|
||||
@ -698,7 +698,7 @@ func (tree *RedBlackTree) output(node *RedBlackTreeNode, prefix string, isTail b
|
||||
}
|
||||
}
|
||||
|
||||
// doSearch searches the tree with given <key> without mutex.
|
||||
// doSearch searches the tree with given `key` without mutex.
|
||||
// It returns the node if found or otherwise nil.
|
||||
func (tree *RedBlackTree) doSearch(key interface{}) (node *RedBlackTreeNode, found bool) {
|
||||
node = tree.root
|
||||
|
||||
@ -23,7 +23,7 @@ var (
|
||||
)
|
||||
|
||||
// NewBool creates and returns a concurrent-safe object for bool type,
|
||||
// with given initial value <value>.
|
||||
// with given initial value `value`.
|
||||
func NewBool(value ...bool) *Bool {
|
||||
t := &Bool{}
|
||||
if len(value) > 0 {
|
||||
@ -41,7 +41,7 @@ func (v *Bool) Clone() *Bool {
|
||||
return NewBool(v.Val())
|
||||
}
|
||||
|
||||
// Set atomically stores <value> into t.value and returns the previous value of t.value.
|
||||
// Set atomically stores `value` into t.value and returns the previous value of t.value.
|
||||
func (v *Bool) Set(value bool) (old bool) {
|
||||
if value {
|
||||
old = atomic.SwapInt32(&v.value, 1) == 1
|
||||
@ -91,7 +91,7 @@ func (v *Bool) UnmarshalJSON(b []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
// UnmarshalValue is an interface implement which sets any type of value for `v`.
|
||||
func (v *Bool) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.Bool(value))
|
||||
return nil
|
||||
@ -18,7 +18,7 @@ type Byte struct {
|
||||
}
|
||||
|
||||
// NewByte creates and returns a concurrent-safe object for byte type,
|
||||
// with given initial value <value>.
|
||||
// with given initial value `value`.
|
||||
func NewByte(value ...byte) *Byte {
|
||||
if len(value) > 0 {
|
||||
return &Byte{
|
||||
@ -33,7 +33,7 @@ func (v *Byte) Clone() *Byte {
|
||||
return NewByte(v.Val())
|
||||
}
|
||||
|
||||
// Set atomically stores <value> into t.value and returns the previous value of t.value.
|
||||
// Set atomically stores `value` into t.value and returns the previous value of t.value.
|
||||
func (v *Byte) Set(value byte) (old byte) {
|
||||
return byte(atomic.SwapInt32(&v.value, int32(value)))
|
||||
}
|
||||
@ -43,7 +43,7 @@ func (v *Byte) Val() byte {
|
||||
return byte(atomic.LoadInt32(&v.value))
|
||||
}
|
||||
|
||||
// Add atomically adds <delta> to t.value and returns the new value.
|
||||
// Add atomically adds `delta` to t.value and returns the new value.
|
||||
func (v *Byte) Add(delta byte) (new byte) {
|
||||
return byte(atomic.AddInt32(&v.value, int32(delta)))
|
||||
}
|
||||
@ -60,16 +60,16 @@ func (v *Byte) String() string {
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (v *Byte) MarshalJSON() ([]byte, error) {
|
||||
return gconv.UnsafeStrToBytes(strconv.FormatUint(uint64(v.Val()), 10)), nil
|
||||
return []byte(strconv.FormatUint(uint64(v.Val()), 10)), nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
|
||||
func (v *Byte) UnmarshalJSON(b []byte) error {
|
||||
v.Set(gconv.Uint8(gconv.UnsafeBytesToStr(b)))
|
||||
v.Set(gconv.Uint8(string(b)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
// UnmarshalValue is an interface implement which sets any type of value for `v`.
|
||||
func (v *Byte) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.Byte(value))
|
||||
return nil
|
||||
@ -19,7 +19,7 @@ type Bytes struct {
|
||||
}
|
||||
|
||||
// NewBytes creates and returns a concurrent-safe object for []byte type,
|
||||
// with given initial value <value>.
|
||||
// with given initial value `value`.
|
||||
func NewBytes(value ...[]byte) *Bytes {
|
||||
t := &Bytes{}
|
||||
if len(value) > 0 {
|
||||
@ -33,8 +33,8 @@ func (v *Bytes) Clone() *Bytes {
|
||||
return NewBytes(v.Val())
|
||||
}
|
||||
|
||||
// Set atomically stores <value> into t.value and returns the previous value of t.value.
|
||||
// Note: The parameter <value> cannot be nil.
|
||||
// Set atomically stores `value` into t.value and returns the previous value of t.value.
|
||||
// Note: The parameter `value` cannot be nil.
|
||||
func (v *Bytes) Set(value []byte) (old []byte) {
|
||||
old = v.Val()
|
||||
v.value.Store(value)
|
||||
@ -59,7 +59,7 @@ func (v *Bytes) MarshalJSON() ([]byte, error) {
|
||||
val := v.Val()
|
||||
dst := make([]byte, base64.StdEncoding.EncodedLen(len(val)))
|
||||
base64.StdEncoding.Encode(dst, val)
|
||||
return gconv.UnsafeStrToBytes(`"` + gconv.UnsafeBytesToStr(dst) + `"`), nil
|
||||
return []byte(`"` + string(dst) + `"`), nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
|
||||
@ -73,7 +73,7 @@ func (v *Bytes) UnmarshalJSON(b []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
// UnmarshalValue is an interface implement which sets any type of value for `v`.
|
||||
func (v *Bytes) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.Bytes(value))
|
||||
return nil
|
||||
@ -20,7 +20,7 @@ type Float32 struct {
|
||||
}
|
||||
|
||||
// NewFloat32 creates and returns a concurrent-safe object for float32 type,
|
||||
// with given initial value <value>.
|
||||
// with given initial value `value`.
|
||||
func NewFloat32(value ...float32) *Float32 {
|
||||
if len(value) > 0 {
|
||||
return &Float32{
|
||||
@ -35,7 +35,7 @@ func (v *Float32) Clone() *Float32 {
|
||||
return NewFloat32(v.Val())
|
||||
}
|
||||
|
||||
// Set atomically stores <value> into t.value and returns the previous value of t.value.
|
||||
// Set atomically stores `value` into t.value and returns the previous value of t.value.
|
||||
func (v *Float32) Set(value float32) (old float32) {
|
||||
return math.Float32frombits(atomic.SwapUint32(&v.value, math.Float32bits(value)))
|
||||
}
|
||||
@ -45,7 +45,7 @@ func (v *Float32) Val() float32 {
|
||||
return math.Float32frombits(atomic.LoadUint32(&v.value))
|
||||
}
|
||||
|
||||
// Add atomically adds <delta> to t.value and returns the new value.
|
||||
// Add atomically adds `delta` to t.value and returns the new value.
|
||||
func (v *Float32) Add(delta float32) (new float32) {
|
||||
for {
|
||||
old := math.Float32frombits(v.value)
|
||||
@ -73,16 +73,16 @@ func (v *Float32) String() string {
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (v *Float32) MarshalJSON() ([]byte, error) {
|
||||
return gconv.UnsafeStrToBytes(strconv.FormatFloat(float64(v.Val()), 'g', -1, 32)), nil
|
||||
return []byte(strconv.FormatFloat(float64(v.Val()), 'g', -1, 32)), nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
|
||||
func (v *Float32) UnmarshalJSON(b []byte) error {
|
||||
v.Set(gconv.Float32(gconv.UnsafeBytesToStr(b)))
|
||||
v.Set(gconv.Float32(string(b)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
// UnmarshalValue is an interface implement which sets any type of value for `v`.
|
||||
func (v *Float32) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.Float32(value))
|
||||
return nil
|
||||
@ -20,7 +20,7 @@ type Float64 struct {
|
||||
}
|
||||
|
||||
// NewFloat64 creates and returns a concurrent-safe object for float64 type,
|
||||
// with given initial value <value>.
|
||||
// with given initial value `value`.
|
||||
func NewFloat64(value ...float64) *Float64 {
|
||||
if len(value) > 0 {
|
||||
return &Float64{
|
||||
@ -35,7 +35,7 @@ func (v *Float64) Clone() *Float64 {
|
||||
return NewFloat64(v.Val())
|
||||
}
|
||||
|
||||
// Set atomically stores <value> into t.value and returns the previous value of t.value.
|
||||
// Set atomically stores `value` into t.value and returns the previous value of t.value.
|
||||
func (v *Float64) Set(value float64) (old float64) {
|
||||
return math.Float64frombits(atomic.SwapUint64(&v.value, math.Float64bits(value)))
|
||||
}
|
||||
@ -45,7 +45,7 @@ func (v *Float64) Val() float64 {
|
||||
return math.Float64frombits(atomic.LoadUint64(&v.value))
|
||||
}
|
||||
|
||||
// Add atomically adds <delta> to t.value and returns the new value.
|
||||
// Add atomically adds `delta` to t.value and returns the new value.
|
||||
func (v *Float64) Add(delta float64) (new float64) {
|
||||
for {
|
||||
old := math.Float64frombits(v.value)
|
||||
@ -73,16 +73,16 @@ func (v *Float64) String() string {
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (v *Float64) MarshalJSON() ([]byte, error) {
|
||||
return gconv.UnsafeStrToBytes(strconv.FormatFloat(v.Val(), 'g', -1, 64)), nil
|
||||
return []byte(strconv.FormatFloat(v.Val(), 'g', -1, 64)), nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
|
||||
func (v *Float64) UnmarshalJSON(b []byte) error {
|
||||
v.Set(gconv.Float64(gconv.UnsafeBytesToStr(b)))
|
||||
v.Set(gconv.Float64(string(b)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
// UnmarshalValue is an interface implement which sets any type of value for `v`.
|
||||
func (v *Float64) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.Float64(value))
|
||||
return nil
|
||||
@ -18,7 +18,7 @@ type Int struct {
|
||||
}
|
||||
|
||||
// NewInt creates and returns a concurrent-safe object for int type,
|
||||
// with given initial value <value>.
|
||||
// with given initial value `value`.
|
||||
func NewInt(value ...int) *Int {
|
||||
if len(value) > 0 {
|
||||
return &Int{
|
||||
@ -33,7 +33,7 @@ func (v *Int) Clone() *Int {
|
||||
return NewInt(v.Val())
|
||||
}
|
||||
|
||||
// Set atomically stores <value> into t.value and returns the previous value of t.value.
|
||||
// Set atomically stores `value` into t.value and returns the previous value of t.value.
|
||||
func (v *Int) Set(value int) (old int) {
|
||||
return int(atomic.SwapInt64(&v.value, int64(value)))
|
||||
}
|
||||
@ -43,7 +43,7 @@ func (v *Int) Val() int {
|
||||
return int(atomic.LoadInt64(&v.value))
|
||||
}
|
||||
|
||||
// Add atomically adds <delta> to t.value and returns the new value.
|
||||
// Add atomically adds `delta` to t.value and returns the new value.
|
||||
func (v *Int) Add(delta int) (new int) {
|
||||
return int(atomic.AddInt64(&v.value, int64(delta)))
|
||||
}
|
||||
@ -60,16 +60,16 @@ func (v *Int) String() string {
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (v *Int) MarshalJSON() ([]byte, error) {
|
||||
return gconv.UnsafeStrToBytes(strconv.Itoa(v.Val())), nil
|
||||
return []byte(strconv.Itoa(v.Val())), nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
|
||||
func (v *Int) UnmarshalJSON(b []byte) error {
|
||||
v.Set(gconv.Int(gconv.UnsafeBytesToStr(b)))
|
||||
v.Set(gconv.Int(string(b)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
// UnmarshalValue is an interface implement which sets any type of value for `v`.
|
||||
func (v *Int) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.Int(value))
|
||||
return nil
|
||||
@ -18,7 +18,7 @@ type Int32 struct {
|
||||
}
|
||||
|
||||
// NewInt32 creates and returns a concurrent-safe object for int32 type,
|
||||
// with given initial value <value>.
|
||||
// with given initial value `value`.
|
||||
func NewInt32(value ...int32) *Int32 {
|
||||
if len(value) > 0 {
|
||||
return &Int32{
|
||||
@ -33,7 +33,7 @@ func (v *Int32) Clone() *Int32 {
|
||||
return NewInt32(v.Val())
|
||||
}
|
||||
|
||||
// Set atomically stores <value> into t.value and returns the previous value of t.value.
|
||||
// Set atomically stores `value` into t.value and returns the previous value of t.value.
|
||||
func (v *Int32) Set(value int32) (old int32) {
|
||||
return atomic.SwapInt32(&v.value, value)
|
||||
}
|
||||
@ -43,7 +43,7 @@ func (v *Int32) Val() int32 {
|
||||
return atomic.LoadInt32(&v.value)
|
||||
}
|
||||
|
||||
// Add atomically adds <delta> to t.value and returns the new value.
|
||||
// Add atomically adds `delta` to t.value and returns the new value.
|
||||
func (v *Int32) Add(delta int32) (new int32) {
|
||||
return atomic.AddInt32(&v.value, delta)
|
||||
}
|
||||
@ -60,16 +60,16 @@ func (v *Int32) String() string {
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (v *Int32) MarshalJSON() ([]byte, error) {
|
||||
return gconv.UnsafeStrToBytes(strconv.Itoa(int(v.Val()))), nil
|
||||
return []byte(strconv.Itoa(int(v.Val()))), nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
|
||||
func (v *Int32) UnmarshalJSON(b []byte) error {
|
||||
v.Set(gconv.Int32(gconv.UnsafeBytesToStr(b)))
|
||||
v.Set(gconv.Int32(string(b)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
// UnmarshalValue is an interface implement which sets any type of value for `v`.
|
||||
func (v *Int32) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.Int32(value))
|
||||
return nil
|
||||
@ -18,7 +18,7 @@ type Int64 struct {
|
||||
}
|
||||
|
||||
// NewInt64 creates and returns a concurrent-safe object for int64 type,
|
||||
// with given initial value <value>.
|
||||
// with given initial value `value`.
|
||||
func NewInt64(value ...int64) *Int64 {
|
||||
if len(value) > 0 {
|
||||
return &Int64{
|
||||
@ -33,7 +33,7 @@ func (v *Int64) Clone() *Int64 {
|
||||
return NewInt64(v.Val())
|
||||
}
|
||||
|
||||
// Set atomically stores <value> into t.value and returns the previous value of t.value.
|
||||
// Set atomically stores `value` into t.value and returns the previous value of t.value.
|
||||
func (v *Int64) Set(value int64) (old int64) {
|
||||
return atomic.SwapInt64(&v.value, value)
|
||||
}
|
||||
@ -43,7 +43,7 @@ func (v *Int64) Val() int64 {
|
||||
return atomic.LoadInt64(&v.value)
|
||||
}
|
||||
|
||||
// Add atomically adds <delta> to t.value and returns the new value.
|
||||
// Add atomically adds `delta` to t.value and returns the new value.
|
||||
func (v *Int64) Add(delta int64) (new int64) {
|
||||
return atomic.AddInt64(&v.value, delta)
|
||||
}
|
||||
@ -60,16 +60,16 @@ func (v *Int64) String() string {
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (v *Int64) MarshalJSON() ([]byte, error) {
|
||||
return gconv.UnsafeStrToBytes(strconv.FormatInt(v.Val(), 10)), nil
|
||||
return []byte(strconv.FormatInt(v.Val(), 10)), nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
|
||||
func (v *Int64) UnmarshalJSON(b []byte) error {
|
||||
v.Set(gconv.Int64(gconv.UnsafeBytesToStr(b)))
|
||||
v.Set(gconv.Int64(string(b)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
// UnmarshalValue is an interface implement which sets any type of value for `v`.
|
||||
func (v *Int64) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.Int64(value))
|
||||
return nil
|
||||
@ -18,7 +18,7 @@ type Interface struct {
|
||||
}
|
||||
|
||||
// NewInterface creates and returns a concurrent-safe object for interface{} type,
|
||||
// with given initial value <value>.
|
||||
// with given initial value `value`.
|
||||
func NewInterface(value ...interface{}) *Interface {
|
||||
t := &Interface{}
|
||||
if len(value) > 0 && value[0] != nil {
|
||||
@ -32,8 +32,8 @@ func (v *Interface) Clone() *Interface {
|
||||
return NewInterface(v.Val())
|
||||
}
|
||||
|
||||
// Set atomically stores <value> into t.value and returns the previous value of t.value.
|
||||
// Note: The parameter <value> cannot be nil.
|
||||
// Set atomically stores `value` into t.value and returns the previous value of t.value.
|
||||
// Note: The parameter `value` cannot be nil.
|
||||
func (v *Interface) Set(value interface{}) (old interface{}) {
|
||||
old = v.Val()
|
||||
v.value.Store(value)
|
||||
@ -66,7 +66,7 @@ func (v *Interface) UnmarshalJSON(b []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
// UnmarshalValue is an interface implement which sets any type of value for `v`.
|
||||
func (v *Interface) UnmarshalValue(value interface{}) error {
|
||||
v.Set(value)
|
||||
return nil
|
||||
@ -18,7 +18,7 @@ type String struct {
|
||||
}
|
||||
|
||||
// NewString creates and returns a concurrent-safe object for string type,
|
||||
// with given initial value <value>.
|
||||
// with given initial value `value`.
|
||||
func NewString(value ...string) *String {
|
||||
t := &String{}
|
||||
if len(value) > 0 {
|
||||
@ -32,7 +32,7 @@ func (v *String) Clone() *String {
|
||||
return NewString(v.Val())
|
||||
}
|
||||
|
||||
// Set atomically stores <value> into t.value and returns the previous value of t.value.
|
||||
// Set atomically stores `value` into t.value and returns the previous value of t.value.
|
||||
func (v *String) Set(value string) (old string) {
|
||||
old = v.Val()
|
||||
v.value.Store(value)
|
||||
@ -55,16 +55,16 @@ func (v *String) String() string {
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (v *String) MarshalJSON() ([]byte, error) {
|
||||
return gconv.UnsafeStrToBytes(`"` + v.Val() + `"`), nil
|
||||
return []byte(`"` + v.Val() + `"`), nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
|
||||
func (v *String) UnmarshalJSON(b []byte) error {
|
||||
v.Set(gconv.UnsafeBytesToStr(bytes.Trim(b, `"`)))
|
||||
v.Set(string(bytes.Trim(b, `"`)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
// UnmarshalValue is an interface implement which sets any type of value for `v`.
|
||||
func (v *String) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.String(value))
|
||||
return nil
|
||||
@ -18,7 +18,7 @@ type Uint struct {
|
||||
}
|
||||
|
||||
// NewUint creates and returns a concurrent-safe object for uint type,
|
||||
// with given initial value <value>.
|
||||
// with given initial value `value`.
|
||||
func NewUint(value ...uint) *Uint {
|
||||
if len(value) > 0 {
|
||||
return &Uint{
|
||||
@ -33,7 +33,7 @@ func (v *Uint) Clone() *Uint {
|
||||
return NewUint(v.Val())
|
||||
}
|
||||
|
||||
// Set atomically stores <value> into t.value and returns the previous value of t.value.
|
||||
// Set atomically stores `value` into t.value and returns the previous value of t.value.
|
||||
func (v *Uint) Set(value uint) (old uint) {
|
||||
return uint(atomic.SwapUint64(&v.value, uint64(value)))
|
||||
}
|
||||
@ -43,7 +43,7 @@ func (v *Uint) Val() uint {
|
||||
return uint(atomic.LoadUint64(&v.value))
|
||||
}
|
||||
|
||||
// Add atomically adds <delta> to t.value and returns the new value.
|
||||
// Add atomically adds `delta` to t.value and returns the new value.
|
||||
func (v *Uint) Add(delta uint) (new uint) {
|
||||
return uint(atomic.AddUint64(&v.value, uint64(delta)))
|
||||
}
|
||||
@ -60,16 +60,16 @@ func (v *Uint) String() string {
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (v *Uint) MarshalJSON() ([]byte, error) {
|
||||
return gconv.UnsafeStrToBytes(strconv.FormatUint(uint64(v.Val()), 10)), nil
|
||||
return []byte(strconv.FormatUint(uint64(v.Val()), 10)), nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
|
||||
func (v *Uint) UnmarshalJSON(b []byte) error {
|
||||
v.Set(gconv.Uint(gconv.UnsafeBytesToStr(b)))
|
||||
v.Set(gconv.Uint(string(b)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
// UnmarshalValue is an interface implement which sets any type of value for `v`.
|
||||
func (v *Uint) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.Uint(value))
|
||||
return nil
|
||||
@ -18,7 +18,7 @@ type Uint32 struct {
|
||||
}
|
||||
|
||||
// NewUint32 creates and returns a concurrent-safe object for uint32 type,
|
||||
// with given initial value <value>.
|
||||
// with given initial value `value`.
|
||||
func NewUint32(value ...uint32) *Uint32 {
|
||||
if len(value) > 0 {
|
||||
return &Uint32{
|
||||
@ -33,7 +33,7 @@ func (v *Uint32) Clone() *Uint32 {
|
||||
return NewUint32(v.Val())
|
||||
}
|
||||
|
||||
// Set atomically stores <value> into t.value and returns the previous value of t.value.
|
||||
// Set atomically stores `value` into t.value and returns the previous value of t.value.
|
||||
func (v *Uint32) Set(value uint32) (old uint32) {
|
||||
return atomic.SwapUint32(&v.value, value)
|
||||
}
|
||||
@ -43,7 +43,7 @@ func (v *Uint32) Val() uint32 {
|
||||
return atomic.LoadUint32(&v.value)
|
||||
}
|
||||
|
||||
// Add atomically adds <delta> to t.value and returns the new value.
|
||||
// Add atomically adds `delta` to t.value and returns the new value.
|
||||
func (v *Uint32) Add(delta uint32) (new uint32) {
|
||||
return atomic.AddUint32(&v.value, delta)
|
||||
}
|
||||
@ -60,16 +60,16 @@ func (v *Uint32) String() string {
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (v *Uint32) MarshalJSON() ([]byte, error) {
|
||||
return gconv.UnsafeStrToBytes(strconv.FormatUint(uint64(v.Val()), 10)), nil
|
||||
return []byte(strconv.FormatUint(uint64(v.Val()), 10)), nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
|
||||
func (v *Uint32) UnmarshalJSON(b []byte) error {
|
||||
v.Set(gconv.Uint32(gconv.UnsafeBytesToStr(b)))
|
||||
v.Set(gconv.Uint32(string(b)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
// UnmarshalValue is an interface implement which sets any type of value for `v`.
|
||||
func (v *Uint32) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.Uint32(value))
|
||||
return nil
|
||||
@ -18,7 +18,7 @@ type Uint64 struct {
|
||||
}
|
||||
|
||||
// NewUint64 creates and returns a concurrent-safe object for uint64 type,
|
||||
// with given initial value <value>.
|
||||
// with given initial value `value`.
|
||||
func NewUint64(value ...uint64) *Uint64 {
|
||||
if len(value) > 0 {
|
||||
return &Uint64{
|
||||
@ -33,7 +33,7 @@ func (v *Uint64) Clone() *Uint64 {
|
||||
return NewUint64(v.Val())
|
||||
}
|
||||
|
||||
// Set atomically stores <value> into t.value and returns the previous value of t.value.
|
||||
// Set atomically stores `value` into t.value and returns the previous value of t.value.
|
||||
func (v *Uint64) Set(value uint64) (old uint64) {
|
||||
return atomic.SwapUint64(&v.value, value)
|
||||
}
|
||||
@ -43,7 +43,7 @@ func (v *Uint64) Val() uint64 {
|
||||
return atomic.LoadUint64(&v.value)
|
||||
}
|
||||
|
||||
// Add atomically adds <delta> to t.value and returns the new value.
|
||||
// Add atomically adds `delta` to t.value and returns the new value.
|
||||
func (v *Uint64) Add(delta uint64) (new uint64) {
|
||||
return atomic.AddUint64(&v.value, delta)
|
||||
}
|
||||
@ -60,16 +60,16 @@ func (v *Uint64) String() string {
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (v *Uint64) MarshalJSON() ([]byte, error) {
|
||||
return gconv.UnsafeStrToBytes(strconv.FormatUint(v.Val(), 10)), nil
|
||||
return []byte(strconv.FormatUint(v.Val(), 10)), nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
|
||||
func (v *Uint64) UnmarshalJSON(b []byte) error {
|
||||
v.Set(gconv.Uint64(gconv.UnsafeBytesToStr(b)))
|
||||
v.Set(gconv.Uint64(string(b)))
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalValue is an interface implement which sets any type of value for <v>.
|
||||
// UnmarshalValue is an interface implement which sets any type of value for `v`.
|
||||
func (v *Uint64) UnmarshalValue(value interface{}) error {
|
||||
v.Set(gconv.Uint64(value))
|
||||
return nil
|
||||
@ -31,9 +31,9 @@ func Decrypt(cipherText []byte, key []byte, iv ...[]byte) ([]byte, error) {
|
||||
return DecryptCBC(cipherText, key, iv...)
|
||||
}
|
||||
|
||||
// EncryptCBC encrypts <plainText> using CBC mode.
|
||||
// EncryptCBC encrypts `plainText` using CBC mode.
|
||||
// Note that the key must be 16/24/32 bit length.
|
||||
// The parameter <iv> initialization vector is unnecessary.
|
||||
// The parameter `iv` initialization vector is unnecessary.
|
||||
func EncryptCBC(plainText []byte, key []byte, iv ...[]byte) ([]byte, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
@ -54,9 +54,9 @@ func EncryptCBC(plainText []byte, key []byte, iv ...[]byte) ([]byte, error) {
|
||||
return cipherText, nil
|
||||
}
|
||||
|
||||
// DecryptCBC decrypts <cipherText> using CBC mode.
|
||||
// DecryptCBC decrypts `cipherText` using CBC mode.
|
||||
// Note that the key must be 16/24/32 bit length.
|
||||
// The parameter <iv> initialization vector is unnecessary.
|
||||
// The parameter `iv` initialization vector is unnecessary.
|
||||
func DecryptCBC(cipherText []byte, key []byte, iv ...[]byte) ([]byte, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
@ -116,9 +116,9 @@ func PKCS5UnPadding(src []byte, blockSize int) ([]byte, error) {
|
||||
return src[:(length - unpadding)], nil
|
||||
}
|
||||
|
||||
// EncryptCFB encrypts <plainText> using CFB mode.
|
||||
// EncryptCFB encrypts `plainText` using CFB mode.
|
||||
// Note that the key must be 16/24/32 bit length.
|
||||
// The parameter <iv> initialization vector is unnecessary.
|
||||
// The parameter `iv` initialization vector is unnecessary.
|
||||
func EncryptCFB(plainText []byte, key []byte, padding *int, iv ...[]byte) ([]byte, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
@ -138,9 +138,9 @@ func EncryptCFB(plainText []byte, key []byte, padding *int, iv ...[]byte) ([]byt
|
||||
return cipherText, nil
|
||||
}
|
||||
|
||||
// DecryptCFB decrypts <plainText> using CFB mode.
|
||||
// DecryptCFB decrypts `plainText` using CFB mode.
|
||||
// Note that the key must be 16/24/32 bit length.
|
||||
// The parameter <iv> initialization vector is unnecessary.
|
||||
// The parameter `iv` initialization vector is unnecessary.
|
||||
func DecryptCFB(cipherText []byte, key []byte, unPadding int, iv ...[]byte) ([]byte, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
|
||||
@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
// Encrypt encrypts any type of variable using CRC32 algorithms.
|
||||
// It uses gconv package to convert <v> to its bytes type.
|
||||
// It uses gconv package to convert `v` to its bytes type.
|
||||
func Encrypt(v interface{}) uint32 {
|
||||
return crc32.ChecksumIEEE(gconv.Bytes(v))
|
||||
}
|
||||
|
||||
@ -20,7 +20,7 @@ const (
|
||||
PKCS5PADDING
|
||||
)
|
||||
|
||||
// EncryptECB encrypts <plainText> using ECB mode.
|
||||
// EncryptECB encrypts `plainText` using ECB mode.
|
||||
func EncryptECB(plainText []byte, key []byte, padding int) ([]byte, error) {
|
||||
text, err := Padding(plainText, padding)
|
||||
if err != nil {
|
||||
@ -42,7 +42,7 @@ func EncryptECB(plainText []byte, key []byte, padding int) ([]byte, error) {
|
||||
return cipherText, nil
|
||||
}
|
||||
|
||||
// DecryptECB decrypts <cipherText> using ECB mode.
|
||||
// DecryptECB decrypts `cipherText` using ECB mode.
|
||||
func DecryptECB(cipherText []byte, key []byte, padding int) ([]byte, error) {
|
||||
text := make([]byte, len(cipherText))
|
||||
block, err := des.NewCipher(key)
|
||||
@ -63,8 +63,8 @@ func DecryptECB(cipherText []byte, key []byte, padding int) ([]byte, error) {
|
||||
return plainText, nil
|
||||
}
|
||||
|
||||
// EncryptECBTriple encrypts <plainText> using TripleDES and ECB mode.
|
||||
// The length of the <key> should be either 16 or 24 bytes.
|
||||
// EncryptECBTriple encrypts `plainText` using TripleDES and ECB mode.
|
||||
// The length of the `key` should be either 16 or 24 bytes.
|
||||
func EncryptECBTriple(plainText []byte, key []byte, padding int) ([]byte, error) {
|
||||
if len(key) != 16 && len(key) != 24 {
|
||||
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "key length error")
|
||||
@ -97,8 +97,8 @@ func EncryptECBTriple(plainText []byte, key []byte, padding int) ([]byte, error)
|
||||
return cipherText, nil
|
||||
}
|
||||
|
||||
// DecryptECBTriple decrypts <cipherText> using TripleDES and ECB mode.
|
||||
// The length of the <key> should be either 16 or 24 bytes.
|
||||
// DecryptECBTriple decrypts `cipherText` using TripleDES and ECB mode.
|
||||
// The length of the `key` should be either 16 or 24 bytes.
|
||||
func DecryptECBTriple(cipherText []byte, key []byte, padding int) ([]byte, error) {
|
||||
if len(key) != 16 && len(key) != 24 {
|
||||
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "key length error")
|
||||
@ -131,7 +131,7 @@ func DecryptECBTriple(cipherText []byte, key []byte, padding int) ([]byte, error
|
||||
return plainText, nil
|
||||
}
|
||||
|
||||
// EncryptCBC encrypts <plainText> using CBC mode.
|
||||
// EncryptCBC encrypts `plainText` using CBC mode.
|
||||
func EncryptCBC(plainText []byte, key []byte, iv []byte, padding int) ([]byte, error) {
|
||||
block, err := des.NewCipher(key)
|
||||
if err != nil {
|
||||
@ -154,7 +154,7 @@ func EncryptCBC(plainText []byte, key []byte, iv []byte, padding int) ([]byte, e
|
||||
return cipherText, nil
|
||||
}
|
||||
|
||||
// DecryptCBC decrypts <cipherText> using CBC mode.
|
||||
// DecryptCBC decrypts `cipherText` using CBC mode.
|
||||
func DecryptCBC(cipherText []byte, key []byte, iv []byte, padding int) ([]byte, error) {
|
||||
block, err := des.NewCipher(key)
|
||||
if err != nil {
|
||||
@ -177,7 +177,7 @@ func DecryptCBC(cipherText []byte, key []byte, iv []byte, padding int) ([]byte,
|
||||
return plainText, nil
|
||||
}
|
||||
|
||||
// EncryptCBCTriple encrypts <plainText> using TripleDES and CBC mode.
|
||||
// EncryptCBCTriple encrypts `plainText` using TripleDES and CBC mode.
|
||||
func EncryptCBCTriple(plainText []byte, key []byte, iv []byte, padding int) ([]byte, error) {
|
||||
if len(key) != 16 && len(key) != 24 {
|
||||
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "key length invalid")
|
||||
@ -212,7 +212,7 @@ func EncryptCBCTriple(plainText []byte, key []byte, iv []byte, padding int) ([]b
|
||||
return cipherText, nil
|
||||
}
|
||||
|
||||
// DecryptCBCTriple decrypts <cipherText> using TripleDES and CBC mode.
|
||||
// DecryptCBCTriple decrypts `cipherText` using TripleDES and CBC mode.
|
||||
func DecryptCBCTriple(cipherText []byte, key []byte, iv []byte, padding int) ([]byte, error) {
|
||||
if len(key) != 16 && len(key) != 24 {
|
||||
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "key length invalid")
|
||||
|
||||
@ -17,13 +17,13 @@ import (
|
||||
)
|
||||
|
||||
// Encrypt encrypts any type of variable using MD5 algorithms.
|
||||
// It uses gconv package to convert <v> to its bytes type.
|
||||
// It uses gconv package to convert `v` to its bytes type.
|
||||
func Encrypt(data interface{}) (encrypt string, err error) {
|
||||
return EncryptBytes(gconv.Bytes(data))
|
||||
}
|
||||
|
||||
// MustEncrypt encrypts any type of variable using MD5 algorithms.
|
||||
// It uses gconv package to convert <v> to its bytes type.
|
||||
// It uses gconv package to convert `v` to its bytes type.
|
||||
// It panics if any error occurs.
|
||||
func MustEncrypt(data interface{}) string {
|
||||
result, err := Encrypt(data)
|
||||
@ -33,7 +33,7 @@ func MustEncrypt(data interface{}) string {
|
||||
return result
|
||||
}
|
||||
|
||||
// EncryptBytes encrypts <data> using MD5 algorithms.
|
||||
// EncryptBytes encrypts `data` using MD5 algorithms.
|
||||
func EncryptBytes(data []byte) (encrypt string, err error) {
|
||||
h := md5.New()
|
||||
if _, err = h.Write([]byte(data)); err != nil {
|
||||
@ -42,7 +42,7 @@ func EncryptBytes(data []byte) (encrypt string, err error) {
|
||||
return fmt.Sprintf("%x", h.Sum(nil)), nil
|
||||
}
|
||||
|
||||
// MustEncryptBytes encrypts <data> using MD5 algorithms.
|
||||
// MustEncryptBytes encrypts `data` using MD5 algorithms.
|
||||
// It panics if any error occurs.
|
||||
func MustEncryptBytes(data []byte) string {
|
||||
result, err := EncryptBytes(data)
|
||||
@ -52,12 +52,12 @@ func MustEncryptBytes(data []byte) string {
|
||||
return result
|
||||
}
|
||||
|
||||
// EncryptBytes encrypts string <data> using MD5 algorithms.
|
||||
// EncryptBytes encrypts string `data` using MD5 algorithms.
|
||||
func EncryptString(data string) (encrypt string, err error) {
|
||||
return EncryptBytes([]byte(data))
|
||||
}
|
||||
|
||||
// MustEncryptString encrypts string <data> using MD5 algorithms.
|
||||
// MustEncryptString encrypts string `data` using MD5 algorithms.
|
||||
// It panics if any error occurs.
|
||||
func MustEncryptString(data string) string {
|
||||
result, err := EncryptString(data)
|
||||
@ -67,7 +67,7 @@ func MustEncryptString(data string) string {
|
||||
return result
|
||||
}
|
||||
|
||||
// EncryptFile encrypts file content of <path> using MD5 algorithms.
|
||||
// EncryptFile encrypts file content of `path` using MD5 algorithms.
|
||||
func EncryptFile(path string) (encrypt string, err error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
@ -82,7 +82,7 @@ func EncryptFile(path string) (encrypt string, err error) {
|
||||
return fmt.Sprintf("%x", h.Sum(nil)), nil
|
||||
}
|
||||
|
||||
// MustEncryptFile encrypts file content of <path> using MD5 algorithms.
|
||||
// MustEncryptFile encrypts file content of `path` using MD5 algorithms.
|
||||
// It panics if any error occurs.
|
||||
func MustEncryptFile(path string) string {
|
||||
result, err := EncryptFile(path)
|
||||
|
||||
@ -17,13 +17,13 @@ import (
|
||||
)
|
||||
|
||||
// Encrypt encrypts any type of variable using SHA1 algorithms.
|
||||
// It uses gconv package to convert <v> to its bytes type.
|
||||
// It uses package gconv to convert `v` to its bytes type.
|
||||
func Encrypt(v interface{}) string {
|
||||
r := sha1.Sum(gconv.Bytes(v))
|
||||
return hex.EncodeToString(r[:])
|
||||
}
|
||||
|
||||
// EncryptFile encrypts file content of <path> using SHA1 algorithms.
|
||||
// EncryptFile encrypts file content of `path` using SHA1 algorithms.
|
||||
func EncryptFile(path string) (encrypt string, err error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
@ -38,7 +38,7 @@ func EncryptFile(path string) (encrypt string, err error) {
|
||||
return hex.EncodeToString(h.Sum(nil)), nil
|
||||
}
|
||||
|
||||
// MustEncryptFile encrypts file content of <path> using SHA1 algorithms.
|
||||
// MustEncryptFile encrypts file content of `path` using SHA1 algorithms.
|
||||
// It panics if any error occurs.
|
||||
func MustEncryptFile(path string) string {
|
||||
result, err := EncryptFile(path)
|
||||
|
||||
@ -205,20 +205,21 @@ func (c *Core) GetScan(pointer interface{}, sql string, args ...interface{}) err
|
||||
t := reflect.TypeOf(pointer)
|
||||
k := t.Kind()
|
||||
if k != reflect.Ptr {
|
||||
return fmt.Errorf("params should be type of pointer, but got: %v", k)
|
||||
return gerror.NewCodef(gcode.CodeInvalidParameter, "params should be type of pointer, but got: %v", k)
|
||||
}
|
||||
k = t.Elem().Kind()
|
||||
switch k {
|
||||
case reflect.Array, reflect.Slice:
|
||||
return c.db.GetCore().GetStructs(pointer, sql, args...)
|
||||
|
||||
case reflect.Struct:
|
||||
return c.db.GetCore().GetStruct(pointer, sql, args...)
|
||||
}
|
||||
return fmt.Errorf("element type should be type of struct/slice, unsupported: %v", k)
|
||||
return gerror.NewCodef(gcode.CodeInvalidParameter, "element type should be type of struct/slice, unsupported: %v", k)
|
||||
}
|
||||
|
||||
// GetValue queries and returns the field value from database.
|
||||
// The sql should queries only one field from database, or else it returns only one
|
||||
// The sql should query only one field from database, or else it returns only one
|
||||
// field of the result.
|
||||
func (c *Core) GetValue(sql string, args ...interface{}) (Value, error) {
|
||||
one, err := c.db.GetOne(sql, args...)
|
||||
@ -392,7 +393,7 @@ func (c *Core) DoInsert(ctx context.Context, link Link, table string, list List,
|
||||
params []interface{} // Values that will be committed to underlying database driver.
|
||||
onDuplicateStr string // onDuplicateStr is used in "ON DUPLICATE KEY UPDATE" statement.
|
||||
)
|
||||
// Handle the field names and place holders.
|
||||
// Handle the field names and placeholders.
|
||||
for k, _ := range list[0] {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
@ -423,7 +424,7 @@ func (c *Core) DoInsert(ctx context.Context, link Link, table string, list List,
|
||||
}
|
||||
}
|
||||
valueHolder = append(valueHolder, "("+gstr.Join(values, ",")+")")
|
||||
// Batch package checks: It meets the batch number or it is the last element.
|
||||
// Batch package checks: It meets the batch number, or it is the last element.
|
||||
if len(valueHolder) == option.BatchCount || (i == listLength-1 && len(valueHolder) > 0) {
|
||||
r, err := c.db.DoExec(ctx, link, fmt.Sprintf(
|
||||
"%s INTO %s(%s) VALUES%s %s",
|
||||
|
||||
@ -9,7 +9,8 @@ package gdb
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/errors/gcode"
|
||||
"github.com/gogf/gf/errors/gerror"
|
||||
"reflect"
|
||||
|
||||
"github.com/gogf/gf/container/gtype"
|
||||
@ -117,8 +118,12 @@ func (c *Core) Transaction(ctx context.Context, f func(ctx context.Context, tx *
|
||||
tx.ctx = WithTX(tx.ctx, tx)
|
||||
defer func() {
|
||||
if err == nil {
|
||||
if e := recover(); e != nil {
|
||||
err = fmt.Errorf("%v", e)
|
||||
if exception := recover(); exception != nil {
|
||||
if v, ok := exception.(error); ok && gerror.HasStack(v) {
|
||||
err = v
|
||||
} else {
|
||||
err = gerror.NewCodef(gcode.CodeInternalError, "%+v", exception)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
@ -307,8 +312,12 @@ func (tx *TX) Transaction(ctx context.Context, f func(ctx context.Context, tx *T
|
||||
}
|
||||
defer func() {
|
||||
if err == nil {
|
||||
if e := recover(); e != nil {
|
||||
err = fmt.Errorf("%v", e)
|
||||
if exception := recover(); exception != nil {
|
||||
if v, ok := exception.(error); ok && gerror.HasStack(v) {
|
||||
err = v
|
||||
} else {
|
||||
err = gerror.NewCodef(gcode.CodeInternalError, "%+v", exception)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
@ -398,21 +407,21 @@ func (tx *TX) GetScan(pointer interface{}, sql string, args ...interface{}) erro
|
||||
t := reflect.TypeOf(pointer)
|
||||
k := t.Kind()
|
||||
if k != reflect.Ptr {
|
||||
return fmt.Errorf("params should be type of pointer, but got: %v", k)
|
||||
return gerror.NewCodef(gcode.CodeInvalidParameter, "params should be type of pointer, but got: %v", k)
|
||||
}
|
||||
k = t.Elem().Kind()
|
||||
switch k {
|
||||
case reflect.Array, reflect.Slice:
|
||||
return tx.GetStructs(pointer, sql, args...)
|
||||
|
||||
case reflect.Struct:
|
||||
return tx.GetStruct(pointer, sql, args...)
|
||||
default:
|
||||
return fmt.Errorf("element type should be type of struct/slice, unsupported: %v", k)
|
||||
}
|
||||
return gerror.NewCodef(gcode.CodeInvalidParameter, "element type should be type of struct/slice, unsupported: %v", k)
|
||||
}
|
||||
|
||||
// GetValue queries and returns the field value from database.
|
||||
// The sql should queries only one field from database, or else it returns only one
|
||||
// The sql should query only one field from database, or else it returns only one
|
||||
// field of the result.
|
||||
func (tx *TX) GetValue(sql string, args ...interface{}) (Value, error) {
|
||||
one, err := tx.GetOne(sql, args...)
|
||||
|
||||
@ -31,28 +31,28 @@ import (
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
// apiString is the type assert api for String.
|
||||
type apiString interface {
|
||||
// iString is the type assert api for String.
|
||||
type iString interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
// apiIterator is the type assert api for Iterator.
|
||||
type apiIterator interface {
|
||||
// iIterator is the type assert api for Iterator.
|
||||
type iIterator interface {
|
||||
Iterator(f func(key, value interface{}) bool)
|
||||
}
|
||||
|
||||
// apiInterfaces is the type assert api for Interfaces.
|
||||
type apiInterfaces interface {
|
||||
// iInterfaces is the type assert api for Interfaces.
|
||||
type iInterfaces interface {
|
||||
Interfaces() []interface{}
|
||||
}
|
||||
|
||||
// apiMapStrAny is the interface support for converting struct parameter to map.
|
||||
type apiMapStrAny interface {
|
||||
// iMapStrAny is the interface support for converting struct parameter to map.
|
||||
type iMapStrAny interface {
|
||||
MapStrAny() map[string]interface{}
|
||||
}
|
||||
|
||||
// apiTableName is the interface for retrieving table name fro struct.
|
||||
type apiTableName interface {
|
||||
// iTableName is the interface for retrieving table name fro struct.
|
||||
type iTableName interface {
|
||||
TableName() string
|
||||
}
|
||||
|
||||
@ -104,7 +104,7 @@ func (m *Model) guessPrimaryTableName(tableStr string) string {
|
||||
func getTableNameFromOrmTag(object interface{}) string {
|
||||
var tableName string
|
||||
// Use the interface value.
|
||||
if r, ok := object.(apiTableName); ok {
|
||||
if r, ok := object.(iTableName); ok {
|
||||
tableName = r.TableName()
|
||||
}
|
||||
// User meta data tag "orm".
|
||||
@ -222,7 +222,7 @@ func ConvertDataForTableRecord(value interface{}) map[string]interface{} {
|
||||
|
||||
default:
|
||||
// Use string conversion in default.
|
||||
if s, ok := v.(apiString); ok {
|
||||
if s, ok := v.(iString); ok {
|
||||
data[k] = s.String()
|
||||
} else {
|
||||
// Convert the value to JSON.
|
||||
@ -246,7 +246,7 @@ func DataToMapDeep(value interface{}) map[string]interface{} {
|
||||
|
||||
default:
|
||||
// Use string conversion in default.
|
||||
if s, ok := v.(apiString); ok {
|
||||
if s, ok := v.(iString); ok {
|
||||
m[k] = s.String()
|
||||
} else {
|
||||
m[k] = v
|
||||
@ -457,11 +457,11 @@ func formatWhere(db DB, in formatWhereInput) (newWhere string, newArgs []interfa
|
||||
}
|
||||
|
||||
case reflect.Struct:
|
||||
// If `where` struct implements apiIterator interface,
|
||||
// If `where` struct implements iIterator interface,
|
||||
// it then uses its Iterate function to iterate its key-value pairs.
|
||||
// For example, ListMap and TreeMap are ordered map,
|
||||
// which implement apiIterator interface and are index-friendly for where conditions.
|
||||
if iterator, ok := in.Where.(apiIterator); ok {
|
||||
// which implement iIterator interface and are index-friendly for where conditions.
|
||||
if iterator, ok := in.Where.(iIterator); ok {
|
||||
iterator.Iterator(func(key, value interface{}) bool {
|
||||
ketStr := gconv.String(key)
|
||||
if gregex.IsMatchString(regularFieldNameRegPattern, ketStr) {
|
||||
@ -764,7 +764,7 @@ func handleArguments(sql string, args []interface{}) (newSql string, newArgs []i
|
||||
default:
|
||||
// It converts the struct to string in default
|
||||
// if it has implemented the String interface.
|
||||
if v, ok := arg.(apiString); ok {
|
||||
if v, ok := arg.(iString); ok {
|
||||
newArgs = append(newArgs, v.String())
|
||||
continue
|
||||
}
|
||||
|
||||
@ -57,7 +57,7 @@ type Model struct {
|
||||
type ModelHandler func(m *Model) *Model
|
||||
|
||||
// ChunkHandler is a function that is used in function Chunk, which handles given Result and error.
|
||||
// It returns true if it wants continue chunking, or else it returns false to stop chunking.
|
||||
// It returns true if it wants to continue chunking, or else it returns false to stop chunking.
|
||||
type ChunkHandler func(result Result, err error) bool
|
||||
|
||||
// ModelWhereHolder is the holder for where condition preparing.
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
package gdb
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/internal/intlog"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -38,6 +39,10 @@ func (m *Model) Cache(duration time.Duration, name ...string) *Model {
|
||||
// cache feature is enabled.
|
||||
func (m *Model) checkAndRemoveCache() {
|
||||
if m.cacheEnabled && m.cacheDuration < 0 && len(m.cacheName) > 0 {
|
||||
m.db.GetCache().Ctx(m.GetCtx()).Remove(m.cacheName)
|
||||
var ctx = m.GetCtx()
|
||||
_, err := m.db.GetCache().Remove(ctx, m.cacheName)
|
||||
if err != nil {
|
||||
intlog.Error(ctx, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -62,7 +62,7 @@ func (m *Model) WherePri(where interface{}, args ...interface{}) *Model {
|
||||
}
|
||||
|
||||
// Wheref builds condition string using fmt.Sprintf and arguments.
|
||||
// Note that if the number of `args` is more than the place holder in `format`,
|
||||
// Note that if the number of `args` is more than the placeholder in `format`,
|
||||
// the extra `args` will be used as the where condition arguments of the Model.
|
||||
func (m *Model) Wheref(format string, args ...interface{}) *Model {
|
||||
var (
|
||||
|
||||
@ -9,6 +9,8 @@ package gdb
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/gset"
|
||||
"github.com/gogf/gf/errors/gcode"
|
||||
"github.com/gogf/gf/errors/gerror"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"github.com/gogf/gf/util/gutil"
|
||||
@ -247,7 +249,7 @@ func (m *Model) HasField(field string) (bool, error) {
|
||||
return false, err
|
||||
}
|
||||
if len(tableFields) == 0 {
|
||||
return false, fmt.Errorf(`empty table fields for table "%s"`, m.tables)
|
||||
return false, gerror.NewCodef(gcode.CodeNotFound, `empty table fields for table "%s"`, m.tables)
|
||||
}
|
||||
fieldsArray := make([]string, len(tableFields))
|
||||
for k, v := range tableFields {
|
||||
|
||||
@ -88,7 +88,7 @@ func (m *Model) Data(data ...interface{}) *Model {
|
||||
model.data = ConvertDataForTableRecord(data[0])
|
||||
|
||||
case reflect.Struct:
|
||||
if v, ok := data[0].(apiInterfaces); ok {
|
||||
if v, ok := data[0].(iInterfaces); ok {
|
||||
var (
|
||||
array = v.Interfaces()
|
||||
list = make(List, len(array))
|
||||
@ -265,7 +265,7 @@ func (m *Model) doInsertWithOption(insertOption int) (result sql.Result, err err
|
||||
list = List{ConvertDataForTableRecord(value)}
|
||||
|
||||
case reflect.Struct:
|
||||
if v, ok := value.(apiInterfaces); ok {
|
||||
if v, ok := value.(iInterfaces); ok {
|
||||
var (
|
||||
array = v.Interfaces()
|
||||
)
|
||||
|
||||
@ -226,7 +226,11 @@ func (m *Model) doStruct(pointer interface{}, where ...interface{}) error {
|
||||
model := m
|
||||
// Auto selecting fields by struct attributes.
|
||||
if model.fieldsEx == "" && (model.fields == "" || model.fields == "*") {
|
||||
model = m.Fields(pointer)
|
||||
if v, ok := pointer.(reflect.Value); ok {
|
||||
model = m.Fields(v.Interface())
|
||||
} else {
|
||||
model = m.Fields(pointer)
|
||||
}
|
||||
}
|
||||
one, err := model.One(where...)
|
||||
if err != nil {
|
||||
@ -267,11 +271,19 @@ func (m *Model) doStructs(pointer interface{}, where ...interface{}) error {
|
||||
model := m
|
||||
// Auto selecting fields by struct attributes.
|
||||
if model.fieldsEx == "" && (model.fields == "" || model.fields == "*") {
|
||||
model = m.Fields(
|
||||
reflect.New(
|
||||
reflect.ValueOf(pointer).Elem().Type().Elem(),
|
||||
).Interface(),
|
||||
)
|
||||
if v, ok := pointer.(reflect.Value); ok {
|
||||
model = m.Fields(
|
||||
reflect.New(
|
||||
v.Type().Elem(),
|
||||
).Interface(),
|
||||
)
|
||||
} else {
|
||||
model = m.Fields(
|
||||
reflect.New(
|
||||
reflect.ValueOf(pointer).Elem().Type().Elem(),
|
||||
).Interface(),
|
||||
)
|
||||
}
|
||||
}
|
||||
all, err := model.All(where...)
|
||||
if err != nil {
|
||||
@ -323,7 +335,6 @@ func (m *Model) Scan(pointer interface{}, where ...interface{}) error {
|
||||
reflectValue = reflectValue.Elem()
|
||||
reflectKind = reflectValue.Kind()
|
||||
}
|
||||
|
||||
switch reflectKind {
|
||||
case reflect.Slice, reflect.Array:
|
||||
return m.doStructs(pointer, where...)
|
||||
@ -520,15 +531,18 @@ func (m *Model) UnionAll(unions ...*Model) *Model {
|
||||
|
||||
// doGetAllBySql does the select statement on the database.
|
||||
func (m *Model) doGetAllBySql(sql string, args ...interface{}) (result Result, err error) {
|
||||
cacheKey := ""
|
||||
cacheObj := m.db.GetCache().Ctx(m.GetCtx())
|
||||
var (
|
||||
ctx = m.GetCtx()
|
||||
cacheKey = ""
|
||||
cacheObj = m.db.GetCache()
|
||||
)
|
||||
// Retrieve from cache.
|
||||
if m.cacheEnabled && m.tx == nil {
|
||||
cacheKey = m.cacheName
|
||||
if len(cacheKey) == 0 {
|
||||
cacheKey = sql + ", @PARAMS:" + gconv.String(args)
|
||||
}
|
||||
if v, _ := cacheObj.GetVar(cacheKey); !v.IsNil() {
|
||||
if v, _ := cacheObj.Get(ctx, cacheKey); !v.IsNil() {
|
||||
if result, ok := v.Val().(Result); ok {
|
||||
// In-memory cache.
|
||||
return result, nil
|
||||
@ -549,7 +563,7 @@ func (m *Model) doGetAllBySql(sql string, args ...interface{}) (result Result, e
|
||||
// Cache the result.
|
||||
if cacheKey != "" && err == nil {
|
||||
if m.cacheDuration < 0 {
|
||||
if _, err := cacheObj.Remove(cacheKey); err != nil {
|
||||
if _, err := cacheObj.Remove(ctx, cacheKey); err != nil {
|
||||
intlog.Error(m.GetCtx(), err)
|
||||
}
|
||||
} else {
|
||||
@ -557,7 +571,7 @@ func (m *Model) doGetAllBySql(sql string, args ...interface{}) (result Result, e
|
||||
if result == nil {
|
||||
result = Result{}
|
||||
}
|
||||
if err := cacheObj.Set(cacheKey, result, m.cacheDuration); err != nil {
|
||||
if err := cacheObj.Set(ctx, cacheKey, result, m.cacheDuration); err != nil {
|
||||
intlog.Error(m.GetCtx(), err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -155,12 +155,10 @@ func (m *Model) doWithScanStruct(pointer interface{}) error {
|
||||
if parsedTagOutput.Order != "" {
|
||||
model = model.Order(parsedTagOutput.Order)
|
||||
}
|
||||
|
||||
err = model.Fields(fieldKeys).Where(relatedSourceName, relatedTargetValue).Scan(bindToReflectValue)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -14,21 +14,16 @@ import (
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
// Interface converts and returns `r` as type of interface{}.
|
||||
func (r Record) Interface() interface{} {
|
||||
return r
|
||||
}
|
||||
|
||||
// Json converts `r` to JSON format content.
|
||||
func (r Record) Json() string {
|
||||
content, _ := gparser.VarToJson(r.Map())
|
||||
return gconv.UnsafeBytesToStr(content)
|
||||
return string(content)
|
||||
}
|
||||
|
||||
// Xml converts `r` to XML format content.
|
||||
func (r Record) Xml(rootTag ...string) string {
|
||||
content, _ := gparser.VarToXml(r.Map(), rootTag...)
|
||||
return gconv.UnsafeBytesToStr(content)
|
||||
return string(content)
|
||||
}
|
||||
|
||||
// Map converts `r` to map[string]interface{}.
|
||||
|
||||
@ -13,11 +13,6 @@ import (
|
||||
"math"
|
||||
)
|
||||
|
||||
// Interface converts and returns `r` as type of interface{}.
|
||||
func (r Result) Interface() interface{} {
|
||||
return r
|
||||
}
|
||||
|
||||
// IsEmpty checks and returns whether `r` is empty.
|
||||
func (r Result) IsEmpty() bool {
|
||||
return r.Len() == 0
|
||||
@ -33,7 +28,7 @@ func (r Result) Size() int {
|
||||
return r.Len()
|
||||
}
|
||||
|
||||
// Chunk splits an Result into multiple Results,
|
||||
// Chunk splits a Result into multiple Results,
|
||||
// the size of each array is determined by `size`.
|
||||
// The last chunk may contain less than size elements.
|
||||
func (r Result) Chunk(size int) []Result {
|
||||
|
||||
@ -1991,3 +1991,44 @@ PRIMARY KEY (id)
|
||||
t.Assert(user.UserScores[4].Score, 5)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_With_Feature_Issue1401(t *testing.T) {
|
||||
var (
|
||||
table1 = "parcels"
|
||||
table2 = "parcel_items"
|
||||
)
|
||||
array := gstr.SplitAndTrim(gtest.TestDataContent(`issue1401.sql`), ";")
|
||||
for _, v := range array {
|
||||
if _, err := db.Exec(v); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
}
|
||||
defer dropTable(table1)
|
||||
defer dropTable(table2)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type NItem struct {
|
||||
Id int `json:"id"`
|
||||
ParcelId int `json:"parcel_id"`
|
||||
}
|
||||
|
||||
type ParcelItem struct {
|
||||
gmeta.Meta `orm:"table:parcel_items"`
|
||||
NItem
|
||||
}
|
||||
|
||||
type ParcelRsp struct {
|
||||
gmeta.Meta `orm:"table:parcels"`
|
||||
Id int `json:"id"`
|
||||
Items []*ParcelItem `json:"items" orm:"with:parcel_id=Id"`
|
||||
}
|
||||
|
||||
parcelDetail := &ParcelRsp{}
|
||||
err := db.Model(table1).With(parcelDetail.Items).Where("id", 3).Scan(&parcelDetail)
|
||||
t.AssertNil(err)
|
||||
t.Assert(parcelDetail.Id, 3)
|
||||
t.Assert(len(parcelDetail.Items), 1)
|
||||
t.Assert(parcelDetail.Items[0].Id, 2)
|
||||
t.Assert(parcelDetail.Items[0].ParcelId, 3)
|
||||
})
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user