etcd-io / bbolt

An embedded key/value database for Go.
https://go.etcd.io/bbolt
MIT License
8.24k stars 640 forks source link

Custom comparison function #156

Closed achille-roussel closed 1 month ago

achille-roussel commented 5 years ago

Hello,

Today the key comparison function used by the database is bytes.Compare. In cases where we're dealing with fixed-size integer or ascii keys this makes a lot of sense, but I have a use case for using variable size integers as keys (similar to big.Int), and the order of the keys as defined by bytes.Compare is not the natural order of the integer values.

I'd like to explore modifying the package to make the comparison function configurable on a per-bucket basis, but I wanted to get feedback from someone more familiar with the implementation in case there were any major concerns.

pixelrazor commented 5 years ago

Gonna spitball some stuff here and maybe try to test it, but it seems like it would be as simple as having the bucket struct have a new member (func(a, b []byte)int) that will default to bytes.Compare, but setable with a new CreateBucket func, then passing that func into the cursor struct when you make a cursor. cursor will then reference its stored comparison func instead of always using bytes.compare. Does anyone think there's something missing with all that?

EDIT: Persisting the comparison function will be an issue. the responsibility COULD be placed on the user, and they would have to keep track of which buckets use which comparison function and pass it in each time they open the bucket. I think a better way, though unsure how to do it, would be to copy the comparison function to the disk with the bucket and load it from there when the bucket is opened. Currently unaware of any way to do that though