Home | History | Annotate | Download | only in room
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package androidx.room;
     18 
     19 import java.lang.annotation.Retention;
     20 import java.lang.annotation.RetentionPolicy;
     21 import java.lang.annotation.Target;
     22 
     23 /**
     24  * Declares an index on an Entity.
     25  * see: <a href="https://sqlite.org/lang_createindex.html">SQLite Index Documentation</a>
     26  * <p>
     27  * Adding an index usually speeds up your select queries but will slow down other queries like
     28  * insert or update. You should be careful when adding indices to ensure that this additional cost
     29  * is worth the gain.
     30  * <p>
     31  * There are 2 ways to define an index in an {@link Entity}. You can either set
     32  * {@link ColumnInfo#index()} property to index individual fields or define composite indices via
     33  * {@link Entity#indices()}.
     34  * <p>
     35  * If an indexed field is embedded into another Entity via {@link Embedded}, it is <b>NOT</b>
     36  * added as an index to the containing {@link Entity}. If you want to keep it indexed, you must
     37  * re-declare it in the containing {@link Entity}.
     38  * <p>
     39  * Similarly, if an {@link Entity} extends another class, indices from the super classes are
     40  * <b>NOT</b> inherited. You must re-declare them in the child {@link Entity} or set
     41  * {@link Entity#inheritSuperIndices()} to {@code true}.
     42  * */
     43 @Target({})
     44 @Retention(RetentionPolicy.CLASS)
     45 public @interface Index {
     46     /**
     47      * List of column names in the Index.
     48      * <p>
     49      * The order of columns is important as it defines when SQLite can use a particular index.
     50      * See <a href="https://www.sqlite.org/optoverview.html">SQLite documentation</a> for details on
     51      * index usage in the query optimizer.
     52      *
     53      * @return The list of column names in the Index.
     54      */
     55     String[] value();
     56 
     57     /**
     58      * Name of the index. If not set, Room will set it to the list of columns joined by '_' and
     59      * prefixed by "index_${tableName}". So if you have a table with name "Foo" and with an index
     60      * of {"bar", "baz"}, generated index name will be  "index_Foo_bar_baz". If you need to specify
     61      * the index in a query, you should never rely on this name, instead, specify a name for your
     62      * index.
     63      *
     64      * @return The name of the index.
     65      */
     66     String name() default "";
     67 
     68     /**
     69      * If set to true, this will be a unique index and any duplicates will be rejected.
     70      *
     71      * @return True if index is unique. False by default.
     72      */
     73     boolean unique() default false;
     74 }
     75