Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

HI, is it to delete element in iterator? #107

Open
roooneychina opened this issue Apr 1, 2022 · 1 comment
Open

HI, is it to delete element in iterator? #107

roooneychina opened this issue Apr 1, 2022 · 1 comment

Comments

@roooneychina
Copy link

HI, would you like kindly to tell me about that :is it to delete element in iterator? many thanks.

@roooneychina
Copy link
Author

it seems that it is statisfied me.
great
func TestIterator(t *testing.T) {
cache := NewCache(1024)
count := 5
for i := 0; i < count; i++ {
err := cache.Set([]byte(fmt.Sprintf("%d", i)), []byte(fmt.Sprintf("val%d", i)), 0)
if err != nil {
t.Error(err)
}
}
//fmt.Println(cache.EntryCount())
go func() {
res, err33 := cache.Get([]byte(fmt.Sprintf("%d", 3)))
if err33 != nil{
t.Error((err33))
}
fmt.Println(string(res))
cache.Del([]byte(fmt.Sprintf("%d", 3)))
fmt.Println(cache.EntryCount())
cache.Del([]byte(fmt.Sprintf("%d", 2)))
cache.Del([]byte(fmt.Sprintf("%d", 0)))
cache.Del([]byte(fmt.Sprintf("%d", 1)))
cache.Del([]byte(fmt.Sprintf("%d", 1)))
}()

go func() {
	time.Sleep(time.Second*10)

	err := cache.Set([]byte(fmt.Sprintf("%d", 6)), []byte(fmt.Sprintf("val%d", 6)), 0)
	if err!= nil{
		t.Error((err))
	}
//	fmt.Println(string(res))
	//cache.Del([]byte(fmt.Sprintf("%d", 3)))
	fmt.Println(cache.EntryCount())
}()
// Set some value that expires to make sure expired entry is not returned.
//cache.Set([]byte("abc"), []byte("def"), 1)
//time.Sleep(5 * time.Second)
it := cache.NewIterator()
var ii int64
for ii=0; ii < cache.EntryCount(); ii++ {
	entry := it.Next()
	time.Sleep(time.Second*5)
	if entry == nil {
		continue;
		//t.Fatalf("entry is nil for %d", i)
	}
	go func() {
		_, err33 := cache.Get([]byte(fmt.Sprintf("%d", 2)))
		if err33 != nil{
			fmt.Println("not found")
		}

	}()

	fmt.Println("start")
	fmt.Println(string(entry.Value))
	if string(entry.Value) != "val"+string(entry.Key) {
		t.Fatalf("entry key value not match %s %s", entry.Key, entry.Value)
	}
	err := cache.Set([]byte(fmt.Sprintf("%d", 4)), []byte(fmt.Sprintf("val%d", 18)), 0)
	if err!= nil{
		t.Error((err))
	}
	cache.Del([]byte(fmt.Sprintf("%d", 4)))
	cache.Del([]byte(fmt.Sprintf("%d", 4)))

}
e := it.Next()
if e != nil {
	t.Fail()
}
time.Sleep(time.Second*20)
fmt.Println("second round")
it2 := cache.NewIterator()
var i int64
for i = 0; i < cache.EntryCount(); i++ {
	entry := it2.Next()

	if entry == nil {
		break
	}
	fmt.Println(string(entry.Value))
}

}

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant