Skip to content

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

Closed

Description

@ceving

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"]

Activity

  1. Elbehery commented on Apr 17, 2024

    @Elbehery
    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

  2. ahrtr commented on Apr 17, 2024

    @ahrtr
    Member

    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{}.

  3. ahrtr commented on Apr 17, 2024

    @ahrtr
    Member

    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
    	})
    }
    
    
  4. ahrtr commented on Apr 17, 2024

    @ahrtr
    Member

    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.

  5. ahrtr commented on Apr 18, 2024

    @ahrtr
    Member

    Completed in #730

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions