SecondaryIndex
SecondaryIndex是一个实体类和一个次关键字
SecondaryIndex是线程安全的。
SecondaryIndex里存放的是封装好的由secondary key type(SK)和entity type(E)组装的map对象
如果想插入或删除一个实体必须通过PrimaryIndex,不能通过SecondaryIndex
1. @SecondaryKey被用来定义Secondary Key(次关键字)
如:
- @Entity
- class Employee {
- @PrimaryKey
- long id;
- @SecondaryKey(relate=MANY_TO_ONE)
- String department;
- String name;
- private Employee() {}
- }
2。要想获得SecondaryIndex,必须要先获得PrimaryIndex. 可以通过EntityStore.getSecondaryIndex获得SecondaryIndex.
该方法有三个参数:PrimaryIndex,Secondary key的类型和secondary key的名字 。例如:
- EntityStore store = new EntityStore(...);
- PrimaryIndex primaryIndex =
- store.getPrimaryIndex(Long.class, Employee.class);
- SecondaryIndex secondaryIndex =
- store.getSecondaryIndex(primaryIndex, String.class, "department");
注意:SecondaryIndex 有3个类型参数< SK,PK,E >
,即:< String,Long,Employee >,
但是PrimaryIndex则只有2个参数 < PK,E >
,即: < Long,Employee >
. . 这是因为SecondaryIndex有一个额外的mapping,这个map是从secondary key 定位到 primary key,然后再从primary key 定位到 实体(entity). 例如,有这样一个实体:
ID | Department | Name |
---|---|---|
1 | Engineering | Jane Smith |
PrimaryIndex maps 直接从id定位到实体,也就是说从 关键字1定位到"Jane Smith"实体。
SecondaryIndex map从department定位到id(在这里是从"Engineering"定位到主关键字key 1),然后使用PrimaryIndex的map,从primary key定位到实体。
因为额外的mapping,SecondaryIndex能够提供多于一个mapping.这主要的mapping是从secondary key (SK) 定位到entity(E).也就是从department key直接定位到Employee实体,SecondaryIndex它本身靠实现EntityIndex,来提供这样的mapping.
SecondaryIndex 的keysIndex方法提供from secondary key (SK) to primary key (PK)
subIndex(SK)提供from primary key (PK) to entity (E)。
3.one-to-one关系
one-to-one关系:the secondary key必须是唯一的,换句话说,没有2个实体有同意的secondary key值,如果存储时,已经有相同的key值存在了,会抛出DatabaseException异常。
例如:
- @Entity
- class Employee {
- @PrimaryKey
- long id;
- @SecondaryKey(relate=ONE_TO_ONE)
- String ssn;
- String name;
- private Employee() {}
- }
- SecondaryIndex employeeBySsn =
- store.getSecondaryIndex(primaryIndex, String.class, "ssn");
因为Secondary key是唯一的。它很容易用secondary key去查找实体,如:
- Employee employee = employeeBySsn.get(mySsn);
4.many-to-one
SecondaryKey上设定many-to-one关系
例如:
- @Entity
- class Employee {
- @PrimaryKey
- long id;
- @SecondaryKey(relate=MANY_TO_ONE)
- String department;
- String name;
- private Employee() {}
- }
- SecondaryIndex employeeByDepartment =
- store.getSecondaryIndex(primaryIndex, String.class, "department");
这种情况SecondaryKey不需要是唯一的。
使用subIndex可以很方面的获得属于某个departement的雇员
如:
- EntityIndex subIndex = employeeByDepartment.subIndex(myDept);
- EntityCursor<employee></employee> cursor = subIndex.entities();
- try {
- for (Employee entity : cursor) {
- // Do something with the entity...
- }
- } finally {
- cursor.close();
- }
5。one-to-many
SecondaryKey上设定one-to-many关系
SecondaryKey必须是唯一的。如果存储时,已经有相同的secondarykey存在的话,会抛出DatabaseException 异常
如:
- @Entity
- class Employee {
- @PrimaryKey
- long id;
- @SecondaryKey(relate=ONE_TO_MANY)
- Set<string></string> emailAddresses = new HashSet<string></string>;
- String name;
- private Employee() {}
- }
- SecondaryIndex employeeByEmail =
- store.getSecondaryIndex(primaryIndex, String.class, "emailAddresses");
因为Secondary key是唯一的。它很容易用secondary key去查找实体,如:
- Employee employee = employeeByEmail.get(myEmailAddress);
SecondaryKey必须是一个array 或Collection类型。为了访问这个employee的addresses ,直接访问这个collection field.
如:
- Employee employee = primaryIndex.get(1); // Get the entity by primary key
- employee.emailAddresses.add(myNewEmail); // Add an email address
- primaryIndex.putNoReturn(1, employee); // Update the entity
6.many-to-many
SencondaryKey上设定many-to-many
如:
- @Entity
- class Employee {
- @PrimaryKey
- long id;
- @SecondaryKey(relate=MANY_TO_MANY)
- Set<string></string> organizations = new HashSet<string></string>;
- String name;
- private Employee() {}
- }
- SecondaryIndex employeeByOrganization =
- store.getSecondaryIndex(primaryIndex, String.class, "organizations");
SencondaryKey不需要是唯一的。
可以很方便的通过subIndex获得某个组织(organization)里的雇员(employees)
如:
- EntityIndex subIndex = employeeByOrganization.subIndex(myOrg);
- EntityCursor<employee></employee> cursor = subIndex.entities();
- try {
- for (Employee entity : cursor) {
- // Do something with the entity...
- }
- } finally {
- cursor.close();
- }
可以直接访问某个employee的organizations
如:
- Employee employee = primaryIndex.get(1); // Get the entity by primary key
- employee.organizations.remove(myOldOrg); // Remove an organization
- primaryIndex.putNoReturn(1, employee); // Update the entity