Coder Social home page Coder Social logo

Allow Index Keys about iavl HOT 14 CLOSED

cosmos avatar cosmos commented on August 26, 2024
Allow Index Keys

from iavl.

Comments (14)

alexanderbez avatar alexanderbez commented on August 26, 2024 1

Essentially, it's kind of acting like a set right? I don't see anything wrong with storing nil values or an empty byte slice. Just gotta be careful it doesn't break any existing internal API's (e.g. operating a nil value).

from iavl.

liamsi avatar liamsi commented on August 26, 2024 1

What if we made your workaround the default behaviour? If you set(key, nil) you will get(key)=[]byte{}? Then, if you try to "get(non-exisiting-key)" you receive nil?
Hmm, then we can't differentiate the case where (for whatever reason) someone uses set(key, []byte{}).

from iavl.

liamsi avatar liamsi commented on August 26, 2024

I think of the IAVL tree basically as a map. There is no obvious reason for me to not allow for nil values but from an application point of view it sounds like misusing the map like an array (if that makes sense). Can you elaborate a little on the use-case?

from iavl.

rigelrozanski avatar rigelrozanski commented on August 26, 2024

Sometimes we want to take advantage of the IAVL sorting to gain the subset of records - for instance in staking we want to hold all the delegations, (key = {delegationAddr, validatorAddr}) and want to be able to access both all the delegations from one delegator, and all the delegations to one validator, we've been doing this by holding an index record with the key {validatorAddr, delegationAddr} in order to sort by validator - this second key however doesn't need to store the record, because once you know of it's existence you can go lookup the first key-value pair. Thus we don't need it to actually have a value, only the key - does this make sense?

from iavl.

liamsi avatar liamsi commented on August 26, 2024

Yeah, I was worried that we'd need to be very careful with handling this case. From looking into how nodes are persisted, this doesn't seem to hold. Hence it should be fine.

from iavl.

rigelrozanski avatar rigelrozanski commented on August 26, 2024

Thinking about this a bit more, there is one kind of weird situation: When you use Get to find a key, if the object is not there we return a value of Nil - so to distinguish this case we would need to also return a found variable which kind of stinks... So it sounds like the better option would be to create special functions which are exclusively used for Indexes which don't have Values?

currently to store an index I'm using an empty byte array ([]byte{}) instead of Nil seems to do the trick

from iavl.

liamsi avatar liamsi commented on August 26, 2024

Hmm, I knew there must be sth weird about how to implement this :-D

So it sounds like the better option would be to create special functions which are exclusively used for Indexes which don't have Values?

I think that is not really optimal, too.

currently to store an index I'm using an empty byte array ([]byte{}) instead of Nil seems to do the trick

This, or somehow else encoding / saying "nope, no value", without introducing another var or function, would be better.

from iavl.

alexanderbez avatar alexanderbez commented on August 26, 2024

Yeah I agree @liamsi. I'm seeming to be more inclined to not store nil, but []byte{} instead. I see two options of Get(key):

  1. You get nil back which means the key does not exist (meaning we'd store []byte{} for keys with nil values).
  2. Change the API to also return an error to signify that indeed the key does not exist or it does exist, but just has a nil value.

from iavl.

rigelrozanski avatar rigelrozanski commented on August 26, 2024

If anything we'd wouldn't want an error but a bool found - but this introduces a whole bunch of extra crap everywhere - How about this

  • Allow passing in a nil value, which in the store will get actually stored as a nil value (which is the desired functionality we're looking for)
  • When Getting from a store, if the record exists but has a value of nil an pass back []byte{} to indicate that is does exist but doesn't contain any information
  • Just leave it indistinguishable if you actually save a []byte to the record... when does this actually matter even? Can't think of a time

from iavl.

alexanderbez avatar alexanderbez commented on August 26, 2024

Ok, so a client will never get nil back from a Get(key) as long as the key exists. It'll only get nil back when the key does not actually exist. I'm ok with that.

from iavl.

rigelrozanski avatar rigelrozanski commented on August 26, 2024

correct

from iavl.

rigelrozanski avatar rigelrozanski commented on August 26, 2024

Additionally we could use []byte(nil) which is distinct from []byte{}

from iavl.

silasdavis avatar silasdavis commented on August 26, 2024

Perhaps this is out-of-date, but you can Set(key, []byte{}) which I think acts as the sentinel you need. I don't think we should implicitly convert []byte{} to nil. The current behaviour seems the most elegant in context.

from iavl.

erikgrinaker avatar erikgrinaker commented on August 26, 2024

The current behavior seems to be in line with this discussion: []byte{} values are accepted, but nil is rejected since it is used to signal absence in Get(). This is consistent with recent cleanups in tm-db.

from iavl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.