hive icon indicating copy to clipboard operation
hive copied to clipboard

Add default key support to HiveType adapters

Open MaGaroo opened this issue 3 years ago • 3 comments

Hi,

@janniklind had requested(#578) a feature to add isKey field to HiveField annotation and use the field with isKey=true as default key of that class instances in add function.

So I implemented it. Before this, the add method of the base box class was using keystore.autoIncrement as default key. Now there's ability for people to set one of that class getters as default key.

For example:

@HiveType(typeId: 1)
class Dog {
    @HiveField(0, isKey: true)
    int id; 

    @HiveField(1)
    String name;

    Dog(this.id, this.name);
}

and therefore in main function:

  var dog = Dog(26, "mydog");
  box.add(dog);

It will use 26 as the key of inserted dog.

MaGaroo avatar Jul 14 '21 00:07 MaGaroo

Thanks for your contribution 😇

However I have a bit of concern about the implementation. Wouldn't adapter lookup when adding new entries affect the speed of writes?

For example if you've putAll N entries to the box, there'll be N lookups for each item and that's not limited to just complex types (which requires custom adapters) also putting plain values like strings, numbers will also use findAdapterForValue. I think we might somehow optimize this behavior.

themisir avatar Jul 14 '21 21:07 themisir

You're right. I'll go into it in a few hours.

MaGaroo avatar Jul 15 '21 16:07 MaGaroo

You're right. I'll go into it in a few hours.

Let's discuss it before implementation. Maybe it's not worth to reduce performance in favour of implementing this feature because users can extend their models from HiveObject to support key fields.

themisir avatar Jul 15 '21 17:07 themisir