CqEngine添加联合索引和复合唯一索引

一.实体类

@Data
public class CategoryT {
    private Integer id;
    private String oneCategory;
    private String twoCategory;
    private String createTime;
    private String updateTime;

    public String uniKey() {
        return oneCategory + "/" + twoCategory;
    }
}

二.集合

@SuppressWarnings("unchecked")
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class CategoryDb {
    private static final IndexedCollection<CategoryT> CACHE;

    static {
        //堆内持久化
        CACHE = new TransactionalIndexedCollection<>(CategoryT.class);
        //添加索引
        CACHE.addIndex(UniqueIndex.onAttribute(Index.ID));
        CACHE.addIndex(HashIndex.onAttribute(Index.ONE_CATEGORY));
        CACHE.addIndex(HashIndex.onAttribute(Index.TWO_CATEGORY));
        //联合索引
        CACHE.addIndex(CompoundIndex.onAttributes(Index.ONE_CATEGORY, Index.TWO_CATEGORY));
        //复合唯一索引
        CACHE.addIndex(UniqueIndex.onAttribute(Index.UNI_KEY));
    }

    private static final class Index {
        //创建属性的访问者对象
        private static final Attribute<CategoryT, Integer> ID = QueryFactory.attribute("ID", CategoryT::getId);
        private static final Attribute<CategoryT, String> ONE_CATEGORY = QueryFactory.attribute("ONE_CATEGORY", CategoryT::getOneCategory);
        private static final Attribute<CategoryT, String> TWO_CATEGORY = QueryFactory.attribute("TWO_CATEGORY", CategoryT::getTwoCategory);
        private static final Attribute<CategoryT, String> UNI_KEY = QueryFactory.attribute("UNI_KEY", CategoryT::uniKey);
        private static final Attribute<CategoryT, String> CREATE_TIME = QueryFactory.attribute("CREATE_TIME", CategoryT::getCreateTime);
        private static final Attribute<CategoryT, String> UPDATE_TIME = QueryFactory.attribute("UPDATE_TIME", CategoryT::getUpdateTime);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值