| 1 |
/*- |
| 2 |
* See the file LICENSE for redistribution information. |
| 3 |
* |
| 4 |
* Copyright (c) 2002, 2013 Oracle and/or its affiliates. All rights reserved. |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
package com.sleepycat.persist.model; |
| 9 |
|
| 10 |
import static java.lang.annotation.ElementType.FIELD; |
| 11 |
import static java.lang.annotation.RetentionPolicy.RUNTIME; |
| 12 |
|
| 13 |
import java.lang.annotation.Documented; |
| 14 |
import java.lang.annotation.Retention; |
| 15 |
import java.lang.annotation.Target; |
| 16 |
|
| 17 |
import com.sleepycat.db.Environment; |
| 18 |
|
| 19 |
/** |
| 20 |
* Indicates the sorting position of a key field in a composite key class when |
| 21 |
* the {@code Comparable} interface is not implemented. The {@code KeyField} |
| 22 |
* integer value specifies the sort order of this field within the set of |
| 23 |
* fields in the composite key. |
| 24 |
* |
| 25 |
* <p>If the field type of a {@link PrimaryKey} or {@link SecondaryKey} is a |
| 26 |
* composite key class containing more than one key field, then a {@code |
| 27 |
* KeyField} annotation must be present on each non-transient instance field of |
| 28 |
* the composite key class. The {@code KeyField} value must be a number |
| 29 |
* between one and the number of non-transient instance fields declared in the |
| 30 |
* composite key class.</p> |
| 31 |
* |
| 32 |
* <p>Note that a composite key class is a flat container for one or more |
| 33 |
* simple type fields. All non-transient instance fields in the composite key |
| 34 |
* class are key fields, and its superclass must be {@code Object}.</p> |
| 35 |
* |
| 36 |
* <p>For example:</p> |
| 37 |
* <pre class="code"> |
| 38 |
* {@literal @Entity} |
| 39 |
* class Animal { |
| 40 |
* {@literal @PrimaryKey} |
| 41 |
* Classification classification; |
| 42 |
* ... |
| 43 |
* } |
| 44 |
* |
| 45 |
* {@literal @Persistent} |
| 46 |
* class Classification { |
| 47 |
* {@literal @KeyField(1) String kingdom;} |
| 48 |
* {@literal @KeyField(2) String phylum;} |
| 49 |
* {@literal @KeyField(3) String clazz;} |
| 50 |
* {@literal @KeyField(4) String order;} |
| 51 |
* {@literal @KeyField(5) String family;} |
| 52 |
* {@literal @KeyField(6) String genus;} |
| 53 |
* {@literal @KeyField(7) String species;} |
| 54 |
* {@literal @KeyField(8) String subspecies;} |
| 55 |
* ... |
| 56 |
* }</pre> |
| 57 |
* |
| 58 |
* <p>This causes entities to be sorted first by {@code kingdom}, then by |
| 59 |
* {@code phylum} within {@code kingdom}, and so on.</p> |
| 60 |
* |
| 61 |
* <p>The fields in a composite key class may not be null.</p> |
| 62 |
* |
| 63 |
* <p><a name="comparable"><strong>Custom Sort Order</strong></a></p> |
| 64 |
* |
| 65 |
* <p>To override the default sort order, a composite key class may implement |
| 66 |
* the {@link Comparable} interface. This allows overriding the sort order and |
| 67 |
* is therefore useful even when there is only one key field in the composite |
| 68 |
* key class. For example, the following class sorts Strings using a Canadian |
| 69 |
* collator:</p> |
| 70 |
* |
| 71 |
* <pre class="code"> |
| 72 |
* import java.text.Collator; |
| 73 |
* import java.util.Locale; |
| 74 |
* |
| 75 |
* {@literal @Entity} |
| 76 |
* class Animal { |
| 77 |
* ... |
| 78 |
* {@literal @SecondaryKey(relate=ONE_TO_ONE)} |
| 79 |
* CollatedString canadianName; |
| 80 |
* ... |
| 81 |
* } |
| 82 |
* |
| 83 |
* {@literal @Persistent} |
| 84 |
* {@literal class CollatedString implements Comparable<CollatedString>} { |
| 85 |
* |
| 86 |
* static Collator collator = Collator.getInstance(Locale.CANADA); |
| 87 |
* |
| 88 |
* {@literal @KeyField(1)} |
| 89 |
* String value; |
| 90 |
* |
| 91 |
* CollatedString(String value) { this.value = value; } |
| 92 |
* |
| 93 |
* private CollatedString() {} |
| 94 |
* |
| 95 |
* public int compareTo(CollatedString o) { |
| 96 |
* return collator.compare(value, o.value); |
| 97 |
* } |
| 98 |
* }</pre> |
| 99 |
* |
| 100 |
* <p>Several important rules should be considered when implementing a custom |
| 101 |
* comparison method. Failure to follow these rules may result in the primary |
| 102 |
* or secondary index becoming unusable; in other words, the store will not be |
| 103 |
* able to function.</p> |
| 104 |
* <ol> |
| 105 |
* <li>The comparison method must always return the same result, given the same |
| 106 |
* inputs. The behavior of the comparison method must not change over |
| 107 |
* time.</li> |
| 108 |
* <br> |
| 109 |
* <li>A corollary to the first rule is that the behavior of the comparison |
| 110 |
* method must not be dependent on state which may change over time. For |
| 111 |
* example, if the above collation method used the default Java locale, and the |
| 112 |
* default locale is changed, then the sort order will change.</li> |
| 113 |
* <br> |
| 114 |
* <li>The comparison method must not assume that it is called after the store |
| 115 |
* has been opened. With Berkeley DB Java Edition, the comparison method is |
| 116 |
* called during database recovery, which occurs in the {@link Environment} |
| 117 |
* constructor.</li> |
| 118 |
* <br> |
| 119 |
* <li>The comparison method must not assume that it will only be called with |
| 120 |
* keys that are currently present in the database. The comparison method will |
| 121 |
* occasionally be called with deleted keys or with keys for records that were |
| 122 |
* not part of a committed transaction.</li> |
| 123 |
* </ol> |
| 124 |
* |
| 125 |
* @author Mark Hayes |
| 126 |
*/ |
| 127 |
@Documented @Retention(RUNTIME) @Target(FIELD) |
| 128 |
public @interface KeyField { |
| 129 |
|
| 130 |
int value(); |
| 131 |
} |