Room系列专题

Android Jetpack之Room篇

Room Entity注解说明

Room Dao注解说明

Room Fts 虚拟表模块

Room DatabaseView 视图

Room SkipQueryVerification

Room TypeConverter 属性类型转换器

定义

FTS3和FTS4是SQLite虚拟表模块,允许对一组文档执行全文搜索。

https://www.sqlite.org/fts3.html

FTS实体表总是有一个名为 rowid 的列,它相当于一个 整数主键 索引。因此,FTS实体只能有一个带有 PrimaryKey 注释的字段,它必须命名为 rowid ,并且必须具有 INTEGER affinity。可以在类中选择性地省略该字段,但仍然可以在查询中使用。

FTS实体中的所有字段都具有 TEXT 亲缘关系,“rowid”字段除外。

Fts3

将 Entity 注释的类标记为FTS3实体。这个类在数据库中有一个映射SQLite FTS3表。

字段定义

1
2
3
4
5
6
7
8
9
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS)
@RequiresApi(16)
public @interface Fts3 {
    //FTS表中使用的记号赋予器。
    String tokenizer() default TOKENIZER_SIMPLE;
    //令牌赋予器参数字符串列表
    String[] tokenizerArgs() default {};
}

例子

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@Entity
@Fts3
public class Mail {

    @PrimaryKey
    @ColumnInfo(name = "rowid")
    private final int rowId;

    private final String subject;

    private final String body;

    public Mail(int rowId, String subject, String body) {
        this.rowId = rowId;
        this.subject = subject;
        this.body = body;
    }

    public String getRowId() {
        return rowId;
    }

    public String getSubject() {
        return subject;
    }

    public void getBody() {
        return body;
    }
}

Fts4

字段定义

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS)
@RequiresApi(16)
public @interface Fts4 {
    //FTS表中使用的记号赋予器
    String tokenizer() default TOKENIZER_SIMPLE;
    //令牌赋予器参数字符串列表
    String[] tokenizerArgs() default {};
    //映射表的外部内容实体将用作FTS表的内容
    Class<?> contentEntity() default Object.class;
    //用作“languageid”的列名
    String languageId() default "";
    //FTS版本用于存储文本匹配信息,通过将此选项设置为FTS3,可以减少磁盘空间消耗
    MatchInfo matchInfo() default MatchInfo.FTS4;
    //将不被索引的FTS表上的列名列表
    String[] notIndexed() default {};
    //要索引的前缀大小列表
    int[] prefix() default {};
    //FTS表的首选“rowid”顺序,如果对FTS表运行许多查询时使用的是 ORDER BY row DESC ,那么将该选项设置为 Order.DESC 可能会提高性能,从而使FTS模块能够以rowid降序优化返回结果的方式存储数据。
    Order order() default Order.ASC;
}

例子

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@Entity
@Fts4
public class Mail {
    @PrimaryKey
    @ColumnInfo(name = "rowid")
    private final int rowId;

    private final String subject;

    private final String body;

    public Mail(int rowId, String subject, String body) {
        this.rowId = rowId;
        this.subject = subject;
        this.body = body;
    }

    public String getRowId() {
        return rowId;
    }

    public String getSubject() {
        return subject;
    }

    public void getBody() {
        return body;
    }
}

如果你喜欢我的文章,可以关注我的掘金、公众号、博客、简书或者Github!

简书: https://www.jianshu.com/u/a2591ab8eed2

GitHub: https://github.com/bugyun

Blog: https://ruoyun.vip

掘金: https://juejin.im/user/56cbef3b816dfa0059e330a8/posts

CSDN: https://blog.csdn.net/zxloveooo

欢迎关注微信公众号