Skip to content

Commit

Permalink
Merge pull request #9 from guojun1992/main
Browse files Browse the repository at this point in the history
fix Foreach UnMarshal op bug
  • Loading branch information
heiyeluren authored Aug 31, 2022
2 parents 84303b1 + b6411ed commit 970881e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 33 deletions.
38 changes: 9 additions & 29 deletions example/xslice_test0.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"fmt"
// "github.com/heiyeluren/xmm"
// "github.com/heiyeluren/xmm"
"github.com/heiyeluren/xds"
"github.com/heiyeluren/xds/xslice"
)
Expand All @@ -16,7 +16,7 @@ func main() {
//创建XSlice对象, 类似于 s0 []Int64 = make([]Int64, 1)
s0 := xslice.NewXslice(xds.Int,1)

//Set压入数据, 类似于 s0 :=[] int { 11, 22, 33 }
//Set压入数据, 类似于 s0 :=[] int { 11, 22, 33 }
s0.Set(1, 11)
s0.Set(2, 22)
s0.Set(3, 33)
Expand All @@ -36,40 +36,20 @@ func main() {
s0.Free()

//批量压入Int ( 类似于 s1 []Int64 = make([]Int64, 1) )
s1 := xslice.NewXslice(xds.Int, 10)
s1 := xslice.NewXslice(xds.Int, 1)
for i:=50; i<=55; i++ {
s1.Append(i)
}
//使用ForEach读取所有数据
s1.ForEach(func(i1 int, v1 []byte) error {
fmt.Println("XSlice foreach data i: ",i1, " v: ", string(v1))
return nil
})
// fmt.Println(s1.s)

//读取整个slice长度, 类似于 len()
fmt.Println("XSlice data size: ", s1.Len(), "\n")

//使用 Append压入一批String数据 ( 类似于 s2 []String = make([]String, 1) )
s2 := xslice.NewXslice(xds.String, 10)
s2.Append("aaa")
s2.Append("bbb")
s2.Append("ccc")
s2.Append("ddd")
s2.Append("eee")
//fmt.Println(s1.Get(2))

//使用ForEach读取所有数据
s2.ForEach(func(i2 int, v2 []byte) error {
fmt.Println("XSlice foreach data i: ", i2, " v: ", string(v2))
s1.ForEach(func(i int, v interface{}) error {
fmt.Println("XSlice foreach data i: ",i, " v: ", v)
return nil
})
//fmt.Println(s1)

//读取整个slice长度, 类似于 len()
fmt.Println("XSlice data size: ", s2.Len(), "\n")

//释放资源
s2.Free()



////读取整个slice长度, 类似于 len()
fmt.Println("XSlice data size: ", s1.Len(), "\n")
}
12 changes: 8 additions & 4 deletions xslice/xslice.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ func(xs *Xslice) Append(v interface{}) (*Xslice,error) {
xs.s = append(xs.s,newSlot)
xs.curWSlotPos += 1
}

b,err := xds.Marshal(xs._stype,v)
if err != nil{
return xs,err
Expand Down Expand Up @@ -126,7 +125,6 @@ func (xs *Xslice) Get(n int) (interface{}, error) {
var gapPos int64 = 0
slotCap := float64(n % _blockSize)
gapPos = int64(slotCap)

b := xs.s[xs.curWSlotPos][gapPos]
v,err := xds.UnMarshal(xs._stype,b)
if err != nil{
Expand All @@ -148,7 +146,7 @@ func (xs *Xslice) Free () {
xs.lock.Unlock()
}

func(xs *Xslice) ForEach(f func(i int, v []byte) error) error{
func(xs *Xslice) ForEach(f func(i int, v interface{}) error) error{
xs.lock.Lock()
defer xs.lock.Unlock()

Expand All @@ -160,7 +158,13 @@ func(xs *Xslice) ForEach(f func(i int, v []byte) error) error{
return nil
}
c = c + 1
if err := f(c,b);err != nil{

v,err := xds.UnMarshal(xs._stype,b)
if err != nil{
return nil
}

if err := f(c,v);err != nil{
return err
}
}
Expand Down

0 comments on commit 970881e

Please sign in to comment.