14. IDs标识
The db4o team recommends, not to use object IDs where this is not necessary. db4o keeps track of object identities in a transparent way, by identifying "known" objects on updates. The reference system also makes sure that every persistent object is instantiated only once, when a graph of objects is retrieved from the database, no matter which access path is chosen. If an object is accessed by multiple queries or by multiple navigation access paths, db4o will always return the one single object, helping you to put your object graph together exactly the same way as it was when it was stored, without having to use IDs.
db4o团队建议:如非必要,请不要使用对象标识(object ID)。
db4o用一种透明的方式跟踪对象标识:在更新时辨别"已知"对象。当从数据库中读取对象图表(a graph of objects)时,无论选用何种访问途经,引用系统(reference system)都能确保每个持久化对象仅被实例化一次。如果一个对象是通过多重检索或多重导航的途经被访问的,db4o将总会返回一个单独的对象,来帮助你像在存储对象图表时那样将它们放到一起,而无需使用标识。
The use of IDs does make sense when object and database are disconnected, for instance in stateless applications.
当对象和数据库断开连接的时候,标识的使用才是有意思的,比如在无状态程序中。
db4o provides two types of ID systems.
db4o提供了两种标识系统。
14.1. Internal IDs内部标识
The internal db4o ID is a physical pointer into the database with only one indirection in the file to the actual object so it is the fastest external access to an object db4o provides. The internal ID of an object is available with
db4o的内部标识是指向数据库内部的物理指针,它仅表示了从文件到实际对象的一种间接关系,因此它是db4o提供的访问对象的最快外部方法。这个内部标识可以通过如下方法获得:
objectContainer.ext().getID(object);
To get an object for an internal ID use
要根据内部标识获得相应对象,则使用如下方法:
objectContainer.ext().getByID(id);
Note that #getByID() does not activate objects. If you want to work with objects that you get with #getByID(), your code would have to make sure the object is activated by calling
请注意:#getByID()并不会激活对象。如果你想使用通过#getByID()方法获得的对象,就必须确保在代码里通过调用方法来激活该对象:
objectContainer.activate(object, depth);
db4o assigns internal IDs to any stored first class object. These internal IDs are guaranteed to be unique within one ObjectContainer/ObjectServer and they will stay the same for every object when an ObjectContainer/ObjectServer is closed and reopened. Internal IDs will change when an object is moved from one ObjectContainer to another, as it happens during Defragment.
db4o给每个已存储的头等(first class)对象分配一个内部标识。这些产生的内部标识是在一个ObjectContainer/ObjectServer里是独一无二的,而且当ObjectContainer/ObjectServer关闭和重新打开的时候,它们会保持不变。当对象从一个ObjectContainer移动到另一个时,内部标识才会改变,比如在整理磁盘碎片(Defragment)时。
14.2. Unique Universal IDs (UUIDs)
For long term external references and to identify an object even after it has been copied or moved to another ObjectContainer, db4o supplies UUIDs. These UUIDs are not generated by default, since they occupy some space and consume some performance for maintaining their index. UUIDs can be turned on globally or for individual classes:
对于长期的外部引用,为了在其被拷贝或移动到另外一个ObjectContainer时还能够被识别,db4o提供了UUIDs。这些UUIDs不会默认产生,因为维护它们的索引会占据一些空间和消耗一定的性能。
UUIDs可以用于全局或者只针对某些单独的类:
Db4o.configure().generateUUIDs(Integer.MAX_VALUE);
Db4o.configure().objectClass(Foo.class).generateUUIDs(true);
The respective methods for working with UUIDs are:
使用UUIDs的方法几种方法:
ExtObjectContainer#getObjectInfo(Object)
ObjectInfo#getUUID();
ExtObjectContainer#getByUUID(Db4oUUID);