Primary keys

Primary key support added through @PrimaryKey annotation.
Primary keys are only allowed on String, short, int, long.
Only 1 field can be a primary key. 
Primary key is automatically indexed if possible.

Some performance numbers (each test was run 10 times):

Label | Result | All results
------------ | -------------| -------------
Insert(10K) - nopk | mean: 0.193 s. - avg: 0.200 s. | [275, 194, 202, 192, 187, 199, 188, 189, 198, 184]
Insert(10K) - pk,string | mean: 1.526 s. - avg: 1.540 s. | [1654, 1559, 1559, 1530, 1514, 1522, 1512, 1495, 1541, 1522]
Insert(10K) - pk,long | mean: 0.303 s. - avg: 0.305 s. | [338, 310, 306, 304, 295, 301, 308, 297, 302, 290]
Insert(10K) - pk,string,index | mean: 0.939 s. - avg: 0.941 s. | [975, 904, 928, 926, 940, 939, 927, 968, 956, 950]
Insert(50K) - nopk | mean: 0.935 s. - avg: 0.938 s. | [1006, 946, 932, 934, 936, 911, 916, 939, 922, 945]
Insert(50K) - pk,string | mean: 19.657 s. - avg: 19.840 s. | [19684, 19582, 19671, 19587, 19636, 19644, 19614, 20202, 20041, 20744]
Insert(50K) - pk,long | mean: 3.593 s. - avg: 3.642 s. | [3838, 3714, 3529, 3567, 3522, 3491, 3562, 3620, 3761, 3819]
Insert(50K) - pk,string,index | mean: 4.891 s. - avg: 4.887 s. | [5189, 4638, 4812, 4892, 4905, 4891, 4672, 4854, 5005, 5012]





I agree on the fact that we cannot ship it in the current state, also
considering the compatibility of realm files among platforms. Let's add
support for standalone objects soon! :)

On Tue, Nov 18, 2014 at 7:29 PM, Christian Melchior <
notifications@github.com> wrote:

> In
> examples/introExample/src/main/java/io/realm/examples/intro/RealmIntroExampleActivity.java:
>
> > @@ -122,8 +123,9 @@ private String complexReadWrite() {
> >          realm.beginTransaction();
> >          Dog fido = realm.createObject(Dog.class);
> >          fido.setName("fido");
> > -        for (int i = 0; i < 10; i++) {
> > +        for (int i = 1; i <= 10; i++) {
>
> I agree it is not ideal, and we risk that some server side API send our
> default values as their primary keys, but there are ways around it, eg.
> generating primary keys only used locally or adding 1 to all primary key
> values. We have the same problem with Null values currently (where people
> have to maintain a separate boolean for it).
>
> Having a method like
>
> Realm.createObject(Class<? extends RealmObject> clazz, Object primaryKeyValue)
>
> will be a very strange API on Java/Android specially since it is only
> temporary.
>
> Once we support stand alone object this problem will go away as we then
> replace createObject() with addObject() and addOrUpdateObject() just like
> Cocoa. So if we think that we cannot ship without support for 0 or "" as
> primary key values, we should instead focus on getting stand alone objects
> done (which would incidentally also fix our JSON import through GSON).
>
> —
> Reply to this email directly or view it on GitHub
> <https://github.com/realm/realm-java/pull/565/files#r20530910>.
>



-- 
*Emanuele Zattin* (@emanuelez <https://twitter.com/emanuelez>)
SW Engineer & CI Specialist