ordered keys via time based UUID's

1,231 views
Skip to first unread message

Huseyn Guliyev

unread,
Mar 13, 2014, 12:44:12 AM3/13/14
to objectify...@googlegroups.com
Jeff,
I am using  UUID with timestamp to generate unique Id strings. They are ordered by time, work very well and quite generic. It seems like a common requirement, for people to have naturally ordered entity ids. 

Here is a class I use as base, to autogenerate such id strings. it is very reusable.  I think it will be useful  for people with similar requirements, if we can add such a class to Ofy  ?

public abstract class BaseTimeKeyIdModel  {

    @Id
    @Getter
    @Setter
    private String name;

    @OnSave
    public void generateId() {
        long ts = System.currentTimeMillis();
        setIdTimeStamp(ts);
    }

    public void setIdTimeStamp(long ts) {
        synchronized (this) {
            if (Strings.isNullOrEmpty(name)) {
                this.name = ts + "-" + UUID.randomUUID().toString();
            }
        }
    }
}



Nicholas Okunew

unread,
Mar 13, 2014, 12:47:38 AM3/13/14
to objectify...@googlegroups.com
There is actually such a thing as a type 1 UUID, which is naturally time ordered. Unfortunately, java only comes with type 3 built in (the above UUID).
Including a type 1 UUID implementation in objectify could be quite useful


--
You received this message because you are subscribed to the Google Groups "objectify-appengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objectify-appen...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jeff Schnitzer

unread,
Mar 13, 2014, 1:44:52 AM3/13/14
to objectify...@googlegroups.com
Since Objectify doesn't use this, I'd rather keep the core of
Objectify as minimal as possible. However, it's fair to interpret this
as a request for some sort of mechanism whereby you can define your
own annotations and behaviors in a pluggable way.

Jeff

Huseyn Guliyev

unread,
Mar 13, 2014, 8:45:40 AM3/13/14
to objectify...@googlegroups.com, je...@infohazard.org
Nick,
 yes I actually started with using JUG library (https://github.com/cowtowncoder/java-uuid-generator) which has time based type 1 UUIDs. The problem with them, is that their UUID strings are not strictly ordered. So that is why in the name i generate above i prefix uuid with timestamp, to guarantee they ordered by creation time.

Jeff, 
They might not be really core part of ofy, but are kind of essential, utility methods, patterns. You have some of that in your unsuck library. We could have an additional, ofy.extra library with these useful bits of code. They will help to promote best practices and to avoid many pitfalls.
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "objectify-appengine" group.
> To unsubscribe from this group and stop receiving emails from it, send an
Reply all
Reply to author
Forward
0 new messages