索引和游标
正如“索引和游标”页面中所解释的,SmartEDB 支持多种索引类型。以下各节提供了用于管理这些索引类型的 Java API 的实现细节。
B 树索引
正如我们在 B 树索引页面中所介绍的,B 树索引能够帮助我们进行有序(即排序)检索和范围检索。指定 B 树索引的方法有多种,接下来我们将详细介绍其中的一些方式。
首先,在数据库类定义中,我们可以通过添加 @Indexable 字段注解来创建 BTree 索引。这个注解支持一些可选的修饰符,以满足不同的需求:
- 降序:如果您希望数据按相反顺序排序,可以使用此选项。默认情况下,排序是升序的。
- 密集:当您的数据中有大量重复值时,选择此选项可以优化性能。
- 唯一:您可以指定是否允许重复值。默认情况下,系统允许重复值。
此外,默认情况下,索引名称将与字段名称相同。
例如:
@Persistent
class Obj
{
@Indexable(Type=Database.IndexType.BTree, Unique=true)
public int value;
}
创建BTree索引的另一种方法是指定@Index类注释,它具有相同的可选修饰符。例如:
@Persistent
@Index("byDept_EmployeeName", keys={@Key("dept_no"), @Key("name")}, unique=true)
class Employee
{
@Indexable(type = Database.IndexType.BTree, unique = true)
public String name;
public int dept_no;
}
请注意,@Index 类注解通常用于多字段或复合索引(如上述情况),但它也可以用于单字段索引。 最后,多个索引(包括复合索引)可以在 @Indexes(...) 注解内一次性声明,例如:
@Indexes({
@Index(name="byName", keys={@Key("lastName"), @Key("firstName")}, unique=true, initSize=100),
@Index(name="byAddress", keys={@Key("address.country"), @Key("address.city"), @Key("address.street")}, unique=false, initSize=100),
@Index(name="bySalary", keys={@Key(value="salary", descending=true)}, initSize=100)
})
class Employee
{
String firstName;
String lastName;
Address address;
long salary;
...
}
游标和搜索
要为唯一 B 树索引(强制唯一性约束)启动“精确匹配”搜索,需使用游标方法 find() 。例如:
Connection con = new Connection(db);
con.startTransaction(Database.TransactionType.ReadWrite);
Cursor<Employee> cursor = new Cursor<Employee>(con, Employee.class, "name");
Employee emp = cursor.find("William");
con. commitTransaction();
要开始搜索非唯一 B 树索引,需使用游标方法 search() 。例如:
Connection con = new Connection(db);
con.startTransaction(Database.TransactionType.ReadWrite);
Cursor<Employee> cursor = new Cursor<Employee>(con, Employee.class, "byDept_EmployeeName");
if (cursor.search(Operation.GreaterOrEquals, emp1.dept_no, "") )
{
for (Employee e : cursor)
{
// Process Employee object
}
}
con. commitTransaction();
游标方法 moveFirst()、moveLast()、moveNext() 和 movePrev() 用于在搜索操作的结果集中进行导航。有关进一步的实现细节,请参阅“搜索”页面。
Patricia trie索引
正如 Patricia 索引页面中所解释的那样,SmartEDB 的 Patricia 索引对于网络和电信应用特别有用。通过指定 @Indexable 字段属性,可以在字符串字段上声明 Patricia 索引。它还可以声明为唯一;若未使用 Unique 关键字,则默认允许重复。与 SmartEDB 的其他索引不同,Patricia 索引不能是复合索引;它始终针对单个字段进行声明。
例如:
@Persistent
class AreaCode
{
@Indexable(type=Database.IndexType.Patricia) // Declare patricia index by "areaCode" field
public String areaCode;
public int value;
}
游标和搜索
要为强制唯一性约束的“Unique Patricia”索引启动“精确匹配”搜索,需使用游标方法 find() 。例如:
Connection con = new Connection(db);
con.startTransaction(Database.TransactionType.ReadWrite);
Cursor<AreaCode> cursor = new Cursor<AreaCode> (con, AreaCode.class, "areaCode");
AreaCode ac = cursor.find("360");
con.commitTransaction();
要开始搜索非唯一的 Patricia
索引,需使用游标方法 search()
。例如:
Connection con = new Connection(db);
con.startTransaction(Database.TransactionType.ReadWrite);
Cursor<AreaCode> cursor = new Cursor<AreaCode> (con, AreaCode.class, "areaCode");
if (cursor.search(Operation.GreaterOrEquals, "360") )
{
for (AreaCode a : cursor)
{
// Process AreaCode object
}
}
con.commitTransaction();
游标方法 moveFirst()、moveLast()、moveNext() 和 movePrev() 用于在搜索操作的结果集中进行导航。有关进一步的实现细节,请参阅“搜索”页面。
R 树索引
正如 R 树索引页面中所解释的那样,R 树索引通常用于加快空间搜索的速度。R 树索引通常定义为包含描述“矩形”所需坐标数量的数组字段。例如:
@Persistent(List=true)
class Rect
{
@Dimension(4)
@Indexable(type=Database.IndexType.RTree) // Declare rtree index on "square" field
public short[] square;
}
游标和搜索
正如 R 树索引页面中所解释的那样,使用游标方法 Search() 来执行 rtree 搜索,该方法与四种搜索操作之一一起使用:等于、包含、重叠或邻域。有关实现细节,请参阅搜索页面。
KD 树索引
KD 树索引在 Java API 中不受支持。
三元索引
正如三元组索引页面所解释的那样,当目标对象的确切拼写不完全清楚时,三元组索引非常适合文本搜索。三元组索引通常定义为字符串字段。例如:
@Persistent(List=true)]
class Obj
{
[Indexable(Type=Database.IndexType.Trigram)]
public String str;
}
游标和搜索
正如三元组索引页面所解释的那样,三元组搜索是使用游标方法 search() 来执行的,该方法使用搜索操作类型:包含。有关实现细节,请参阅搜索页面。
哈希索引
正如“哈希和自动标识索引”页面中所解释的那样,哈希表索引非常适合快速查找单个数据库对象。哈希表索引可以声明为 unique=true 或 unique=false,并且需要一个额外的参数 initSize。这是一个整数,运行时会使用它来为索引分配初始哈希表。必须指定此参数,但不必精确。例如:
@Persistent
class Record
{
@Indexable(type=Database.IndexType.Hashtable, unique=true, initSize=10000) // Declare unique hash index
int iIdx;
@Indexable(type=Database.IndexType.Hashtable, unique=false, initSize=10000) // Declare non-unique hash index
int iSeries;
}
请注意,索引 iIdx 和 iSeries 的 [10000] 规范会导致运行时分配初始哈希表,其中包含 10000 个“桶”。
游标和搜索
正如“哈希和自动标识索引”页面中所解释的那样,哈希表搜索会根据唯一声明是真还是假,使用游标方法 find() 或 search() 来执行。有关实现细节,请参阅“搜索”页面。
列表索引
列表索引与非唯一的哈希表类似,允许在无序对象列表中按顺序(从首到尾或从尾到首)进行导航。要在类上创建列表索引,请使用 @Persistent(list=true) 注解。例如:
@Persistent(list=true)
class Rect
{
@Dimension(4)
@Indexable(type=Database.IndexType.RTree) // Declare rtree index on "square" field
public short[] square;
}