server icon indicating copy to clipboard operation
server copied to clipboard

feat(snowflake): extend Entity class to support snowflakes

Open miaulalala opened this issue 1 month ago • 4 comments

RFC: extending the Entity class to support snowflake IDs automagically

Good ides - yes / no?

Benefits:

  • every entity that supports snowflake IDs can automagically handle them by implementing the SnowflakeAwareEntity instead of the regular Entity
  • No adding IGenerator / IDecoder in every entity class
  • centralised method support for createdAt, setId, and anything else we would like to offer separately
  • get entire decoded snowflake id from the entity

Drawbacks:

  • Will it work the way I think it should?
  • Resolves: #

TODO

  • [ ] ...

Checklist

miaulalala avatar Dec 02 '25 13:12 miaulalala

We should rename IGenerator and IDecoder to ISnowflakeGenerator and ISnowflakeDecoder, otherwise after a use all code is ambiguous. A value object for decoded snowflakes would be good as well.

come-nc avatar Dec 02 '25 16:12 come-nc

We should rename IGenerator and IDecoder to ISnowflakeGenerator and ISnowflakeDecoder, otherwise after a use all code is ambiguous. A value object for decoded snowflakes would be good as well.

Added

miaulalala avatar Dec 03 '25 22:12 miaulalala

Would it make sense to cache the decoded snowflake ID? Seems like there's quite a few expensive calculations going on.

miaulalala avatar Dec 04 '25 12:12 miaulalala

We can still add a cache later if needed but decoding on 64 bits mainly involve binary operations, it should be fast. 32 bit is more complicated :sweat:

Altahrim avatar Dec 04 '25 13:12 Altahrim

@Altahrim should the mapper also handle IDs on insert?

Currently it's doing the following:

https://github.com/nextcloud/server/blob/bba96678826ba9b6726484ff05a1d7fd48cfa8b7/lib/public/AppFramework/Db/QBMapper.php#L117-L120

I'm thinking it might be possible to add a if ($entity instanceof SnowflakeAwareEntity) { $entity->setId()} ?

miaulalala avatar Dec 11 '25 14:12 miaulalala