CQengine高性能內存數據緩存查找框架


CQengine可實現高性能內存數據緩存查找

 

CQEngine 需要設置字段對應的屬性以方便訪問與查詢

主要有屬性鏈接

  • SimpleAttribute(不能為空)

  • SimpleNullableAttribute(可以為空)

  • MultiValueAttribute(集合類型字段,不為空)

  • MultiValueNullableAttribute(集合類型的字段,可以為空)

另外SimpleAttribute下有幾個重要的子類

OrderControlAttribute : 排序相關

ReflectiveAttribute : 動態反射創建Attribute ,性能沒有直接在類中定義好

SelfAttribute:集合里中查找元素, 如["a","b","c"] 中查找b

自動生成實體類的 Attribute

cqengine 查詢對象,根據字段查詢,生成索引都需要用到SimpleAttribute,為每個實體的字段建 SimpleAttribute比較費力,所以cqengine 默認提供了自動生成屬性代碼
String code = AttributeSourceGenerator.generateAttributesForPastingIntoTargetClass(clazz);//傳入實體類的CLASS,生成實體類下的所有屬性對應的SimpleAttribute
如下:
public static final SimpleAttribute<Car, String> MODEL = new SimpleAttribute<Car, String>("model") {
public String getValue(Car car, QueryOptions queryOptions) { return car.model; }
};

AttributeSourceGenerator 生成源代碼
AttributeBytecodeGenerator 運行時通過反射讀取字段並反映創建Attribute對象添加到CLASS中

 

 

支持sql和cqn查詢

示例鏈接

SQL

public static void main(String[] args) {
    SQLParser<Car> parser = SQLParser.forPojoWithAttributes(Car.class, createAttributes(Car.class));
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();
    cars.addAll(CarFactory.createCollectionOfCars(10));
 
    ResultSet<Car> results = parser.retrieve(cars, "SELECT * FROM cars WHERE (" +
                                    "(manufacturer = 'Ford' OR manufacturer = 'Honda') " +
                                    "AND price <= 5000.0 " +
                                    "AND color NOT IN ('GREEN', 'WHITE')) " +
                                    "ORDER BY manufacturer DESC, price ASC");
    for (Car car : results) {
        System.out.println(car); // Prints: Honda Accord, Ford Fusion, Ford Focus
    }
}

注意:

  1. sql 查詢時,SQLParser必須注冊字段registerAttribute
  2. 注冊的字段只能是引用類型(包裝類),不然為報錯

CQN


public static void main(String[] args) {
    CQNParser<Car> parser = CQNParser.forPojoWithAttributes(Car.class, createAttributes(Car.class));
    IndexedCollection<Car> cars = new ConcurrentIndexedCollection<Car>();
    cars.addAll(CarFactory.createCollectionOfCars(10));
 
    ResultSet<Car> results = parser.retrieve(cars,
                                    "and(" +
                                        "or(equal(\"manufacturer\", \"Ford\"), equal(\"manufacturer\", \"Honda\")), " +
                                        "lessThanOrEqualTo(\"price\", 5000.0), " +
                                        "not(in(\"color\", GREEN, WHITE))" +
                                    ")");
    for (Car car : results) {
        System.out.println(car); // Prints: Ford Focus, Ford Fusion, Honda Accord
    }
}

支持索引

支持的索引類型有:
HashIndex,NavigableIndex,RadixTreeIndex,ReversedRadixTreeIndex,InvertedRadixTreeIndex,SuffixTreeIndex

HashIndex:
索引依賴ConcurrentHashMap ,適用於Equal 來比較。一般適用於字段為字符串,枚舉。

NavigableIndex:
依賴ConcurrentSkipListMap,適用於Equal,LessThan,GreaterThan,Between;一般適用於字段為數字類型。

RadixTreeIndex:
依賴ConcurrentRadixTree,適用於Equal,StringStartsWith ;一般適用於字段為字符串需要StartsWith模糊匹配。

ReversedRadixTreeIndex:
依賴ConcurrentReversedRadixTree,適用於Equal,StringEndsWith;一般適用於字段為符串需要EndsWith模糊匹配。

InvertedRadixTreeIndex:
依賴ConcurrentInvertedRadixTree,適用於Equal,StringIsContainedIn;一般適用於字段為字符串是否包含XX字符char

SuffixTreeIndex:
依賴ConcurrentSuffixTree,適用於Equal,StringEndsWith,StringContains ; 一般適用於字段為需要 in(a,b,c....),容器是否包含字符串string。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM