aerospike-client-go icon indicating copy to clipboard operation
aerospike-client-go copied to clipboard

Accepting interface{} instead of []interface{} in BatchGetObjects

Open sashayakovtseva opened this issue 4 years ago • 5 comments

Version: v3.0+

What we have now:

BatchGetObjects(policy *BatchPolicy, keys []*Key, objects []interface{}) (found []bool, err error)

So, to get objects into []myStruct one have to first get them into []interface{}, then convert each interface{} into myStruct.

I suggest:

BatchGetObjects(policy *BatchPolicy, keys []*Key, objects interface{}) (found []bool, err error)

This way one can pass []myStruct directly into BatchGetObjects. Inside BatchGetObjects reflect package can be used to check whether a slice type of appropriate length is passed.

As this is a breaking change, I suppose it may be implemented v4.0+.

sashayakovtseva avatar Jul 10 '20 10:07 sashayakovtseva

Makes sense. Will do.

khaf avatar Jul 10 '20 10:07 khaf

Btw, I also think that accepting empty/nil slice and appending to it found object might be more convenient.

So that the final signature is the following:

BatchGetObjects(policy *BatchPolicy, keys []*Key, objects interface{}) error

That way package users won't have to iterate found []bool slice to find out what was actually fetched. Not sure this is the expected API from aerospike client though, but definitely this is the common pattern I've seen a lot.

sashayakovtseva avatar Jul 10 '20 11:07 sashayakovtseva

That way you won't be able to reuse a pre-allocated slice.

khaf avatar Jul 10 '20 11:07 khaf

you will be able, when truncating it to zero length. That way append won't allocate memory, but reuse underlying slice's capacity.

sashayakovtseva avatar Jul 10 '20 11:07 sashayakovtseva

ah, you literally meant append. That won't work due to race condition. We can only set at index without race, not append.

khaf avatar Jul 10 '20 11:07 khaf