Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Support Iterator for fastcache #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ehnuje
Copy link

@ehnuje ehnuje commented Jan 31, 2021

func TestCacheIterator(t *testing.T) {
	c := New(1024)
	defer c.Reset()

	numEntries := 100
	keyValMap := make(map[string][]byte)
	for i := 0; i < numEntries; i++ {
		k := []byte(fmt.Sprintf("key %d", i))
		v := []byte(fmt.Sprintf("value %d", i))
		c.Set(k, v)
		keyValMap[string(k)] = v
		vv := c.Get(nil, k)
		if string(vv) != string(v) {
			t.Fatalf("unexpected value for key %q; got %q; want %q", k, vv, v)
		}
	}

	itr := c.Iterator()
	for itr.SetNext() {
		entry, err := itr.Value()
		if err != nil {
			t.Fatal("unexpected error from itr.Value()", "err", err)
		}

		val, exist := keyValMap[string(entry.key)]
		if !exist {
			t.Fatal("failed to retrieve an entry from cache which should exist")
		}
		if !bytes.Equal(val, entry.value) {
			t.Fatalf("value from iterator is not the same as the expected one for key %q; got %q; want %q",
				entry.key, entry.value, val)
		}
	}
}

@elimist3
Copy link

LTGM!

From the looks of it, this is a highly desirable feature and this PR looks pretty solid!

@hhhhax
Copy link

hhhhax commented Jul 26, 2023

When the number of traversals I traverse is large enough, it will cause the array to go out of bounds and cause a panic. Can you help me check it out?
`c := New(2 * 1024 * 1024 * 1024 * 1024)
defer c.Reset()

numEntries := 10000000
keyValMap := make(map[string][]byte)
for i := 0; i < numEntries; i++ {
	k := []byte(fmt.Sprintf("key %d", i))
	v := []byte(fmt.Sprintf("value %d", i))
	c.Set(k, v)
	keyValMap[string(k)] = v
	vv := c.Get(nil, k)
	if string(vv) != string(v) {
		t.Fatalf("unexpected value for key %q; got %q; want %q", k, vv, v)
	}
}

go func() {
	for {
		numEntries := 10000000
		keyValMap := make(map[string][]byte)
		for i := 0; i < numEntries; i++ {
			k := []byte(fmt.Sprintf("key %d", i))
			v := []byte(fmt.Sprintf("value %d", i))
			keyValMap[string(k)] = v
			vv := c.Get(nil, k)
			if string(vv) != string(v) {
				t.Fatalf("unexpected value for key %q; got %q; want %q", k, vv, v)
			}
		}

	}
}()

for {
	time.Sleep(1 * time.Second)
	itr := c.Iterator()
	count := 0
	for itr.HasNext() {
		count++
		_, _, err := itr.Value()
		if err != nil {
			t.Fatal("unexpected error from itr.Value()", "err", err)
		}

		//val, exist := keyValMap[string(key)]
		//if !exist {
		//	t.Fatal("failed to retrieve an entry from cache which should exist")
		//}
		//if !bytes.Equal(val, value) {
		//	t.Fatalf("value from iterator is not the same as the expected one for key %q; got %q; want %q",
		//		key, value, val)
		//}
	}
	fmt.Printf("count:%v\n", count)
}`

chunkIdx := idx / chunkSize
chunk := b.chunks[chunkIdx]

kvLenBuf := chunk[idx : idx+4]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may cause the array to go out of bounds and generate Panic

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this line of code may solve the problem, can you help me take a look? idx = idx % chunkSize

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

Successfully merging this pull request may close these issues.

None yet

3 participants