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

Can not distinguish between "does not exist" and "has no value" #726

Closed
ceving opened this issue Apr 17, 2024 · 5 comments
Closed

Can not distinguish between "does not exist" and "has no value" #726

ceving opened this issue Apr 17, 2024 · 5 comments

Comments

@ceving
Copy link

ceving commented Apr 17, 2024

I wrote the pwned passwords into a bucket. Because I only need to know if the hash exists, I wrote an empty value into the bucket.

err := hashes.Put(hash, []byte{})

Now I tried to find a hash, but I had to realize that Go does not distinguish between an empty array and nil. The problem is: Bolt returns nil to indicate that a key does not exist. This is a problem because of Go's limitation.

How to store keys without values in order to check just for existence?

I think Bucket.Get needs to return an additional boolean or Bucket needs an additional function to check just for existence.

Go itself also indicates the existence of a key in a map by an additional boolean:

i, ok := m["route"]
@Elbehery
Copy link
Member

@ceving Get returns nil if the key does not exist, or the value is a nested bucket

this could be a feature cc @ahrtr @fuweid @tjungblu @ivanvc

@ahrtr
Copy link
Member

ahrtr commented Apr 17, 2024

For the case of "does not exist", bucket.Get returns nil.

For the case "has no value", bucket.Get returns []byte{}, which is obviously not nil.

The only known minor problem is that even you write a nil value for a key, when you read it back in a separate transaction, it still returns []byte{}.

@ahrtr
Copy link
Member

ahrtr commented Apr 17, 2024

For example,

func TestBucket_Get_NonExistent(t *testing.T) {
	db := btesting.MustCreateDB(t)

	if err := db.Update(func(tx *bolt.Tx) error {
		b, err := tx.CreateBucket([]byte("widgets"))
		if err != nil {
			t.Fatal(err)
		}
		return b.Put([]byte("foo"), nil)
	}); err != nil {
		t.Fatal(err)
	}

	db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte("widgets"))
		if v := b.Get([]byte("foo")); v != nil {
			t.Fatal("expected nil value")  // The test case will fail here.
		}
		return nil
	})
}

@ahrtr
Copy link
Member

ahrtr commented Apr 17, 2024

In short, no matter what value you write into bbolt, when you read it back, you will always get []byte{} as long as len(value) == 0 is true.

The only exception is that when you write a nil value, and you will still get a nil value when you read it back in the same transaction.

Will raise a PR to add a known issue.

@ahrtr
Copy link
Member

ahrtr commented Apr 18, 2024

Completed in #730

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

No branches or pull requests

3 participants