索引与游标
正如“索引和游标”页面中所解释的,SmartEDB 支持多种索引类型。以下各节提供了用于管理这些索引类型的 C# API 的实现细节。
B 树索引
正如 B 树索引页面所解释的那样,B 树索引可用于有序(排序)检索和范围检索。
在数据库类定义中,可以通过指定具有可选修饰符的 [Indexable] 字段属性来指定 B 树索引:
- Descending (降序) - 以相反顺序排序 - 若未指定降序,则默认为升序
- Thick (密集) - 优化处理大量重复值
- Unique (唯一) - true:不允许重复值,或 false:允许重复值(若未指定唯一,则为默认值)
例如:
[Persistent]
class Obj
{
[Indexable(Type=Database.IndexType.BTree, Unique=true)]
public int value;
}
创建 BTree 索引的另一种方法是指定 [Index] 类属性,该属性具有相同的可选修饰符。例如:
[Persistent]
[Index("byDept_EmployeeName", Keys = new string[] { "dept_no", "name" }, Unique = false)]
class Employee
{
[Indexable(Type = Database.IndexType.BTree, Unique = true)]
public String name;
public int dept_no;
}
请注意,*[Index]*类属性通常用于多字段或复合索引(如上面的情况),但它也可以用于单个字段索引。
游标和搜索
要为唯一 B 树索引(强制唯一性约束)启动“精确匹配”搜索,需使用游标方法 Find() 。例如:
Connection con = new Connection(db);
con.StartTransaction(Database.TransactionType.ReadWrite);
Cursor<Employee> cursor = new Cursor<Employee>(con, "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, "byDept_EmployeeName");
if (cursor.Search(Operation.GreaterOrEquals, emp1.dept_no, "") )
{
foreach (Employee e in cursor)
{
// Process Employee object
}
}
con. CommitTransaction();
Cursor 方法 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");
AreaCode ac = cursor.Find("360");
con.CommitTransaction();
To initiate a search for non-Unique Patricia
indexes, the Cursor method Search()
is used. For example:
Connection con = new Connection(db);
con.StartTransaction(Database.TransactionType.ReadWrite);
Cursor<AreaCode> cursor = new Cursor<AreaCode>(con, "areaCode");
if (cursor.Search(Operation.GreaterOrEquals, "360") )
{
foreach (AreaCode a in cursor)
{
// Process AreaCode object
}
}
con.CommitTransaction();
Cursor 方法 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() 并结合四种搜索操作类型之一(相等、包含、重叠或邻域)来执行 r 树搜索。有关实现细节,请参阅搜索页面。
三元索引
正如三元组索引页面所解释的那样,当目标对象的确切拼写不完全清楚时,三元组索引非常适合文本搜索。三元组索引通常定义为字符串字段。例如:
[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
public int iIdx;
[Indexable(Type=Database.IndexType.Hashtable, Unique=false, InitSize=10000)] // Declare non-unique hash index
public 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;
}