创建了一个注解 KVIndex。
感觉上应该是这样子的:
有一个数据库: KVStore,
里面存了好多好多数据,这些数据都是某一种类型的,数据一个类的实例就相当于一条记录,一个类就相当于一个表。
KVIndex 就是给某个表添加索引。
比如单元测试里面ArrayKeyIndexType,就存储在KVStore里面,一个数据库里面有了一张ArrayKeyIndexType的表。
这个表的某些字段需要建索引,它的key这个字段就是主键索引, 还有id键也是个索引,通过索引字段的值,可以快速的找到类的实例。
感觉KVStore就是个小小的数据库的接口。
然后还有标注时候除了给某个索引起个名字,默认名字是 __main__ , 还可以给这个索引一个 father,难道father是和数据库里面那种复合索引一样的意思吗?
/**
* Tags a field to be indexed when storing an object.
*
*
* Types are required to have a natural index that uniquely identifies instances in the store.
* The default value of the annotation identifies the natural index for the type.
*
*
*
* Indexes allow for more efficient sorting of data read from the store. By annotating a field or
* "getter" method with this annotation, an index will be created that will provide sorting based on
* the string value of that field.
*
*
*
* Note that creating indices means more space will be needed, and maintenance operations like
* updating or deleting a value will become more expensive.
*
*
*
* Indices are restricted to String, integral types (byte, short, int, long, boolean), and arrays
* of those values.
*
*/
@Private
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface KVIndex {
String NATURAL_INDEX_NAME = "__main__";
/**
* The name of the index to be created for the annotated entity. Must be unique within
* the class. Index names are not allowed to start with an underscore (that's reserved for
* internal use). The default value is the natural index name (which is always a copy index
* regardless of the annotation's values).
*/
String value() default NATURAL_INDEX_NAME;
/**
* The name of the parent index of this index. By default there is no parent index, so the
* generated data can be retrieved without having to provide a parent value.
*
*
* If a parent index is defined, iterating over the data using the index will require providing
* a single value for the parent index. This serves as a rudimentary way to provide relationships
* between entities in the store.
*
*/
String parent() default "";
/**
* Whether to copy the instance's data to the index, instead of just storing a pointer to the
* data. The default behavior is to just store a reference; that saves disk space but is slower
* to read, since there's a level of indirection.
*/
boolean copy() default false;
}
单元测试案例:
public class ArrayKeyIndexType {
@KVIndex
public int[] key; //这个是主键来的
@KVIndex("id")
public String[] id; //这个也是个索引,索引还有个名字叫 id
@Override
public boolean equals(Object o) {
if (o instanceof ArrayKeyIndexType) {
ArrayKeyIndexType other = (ArrayKeyIndexType) o;
return Arrays.equals(key, other.key) && Arrays.equals(id, other.id);
}
return false;
}
@Override
public int hashCode() {
return key.hashCode();
}
}