对象序列化和持久化内容摘要:

ses) 2020/11/4 Institute of Computer Software Nanjing University 40 Approaches to manipulate persistent objects  Persistence mechanisms from programming languages  Relational databases  Objectoriented databases 2020/11/4 Institute of Computer Software Nanjing University 41 Persistence from programming languages  Mechanisms for storing objects in files and retrieving them  Simple objects:  . integers, characters  conventional methods usable  Composite objects:  contain references to other objects  Persistence Closure principle:  Any storage and retrieval mechanism must handle the object and all its dependents.  otherwise: dangling references 2020/11/4 Institute of Computer Software Nanjing University 42 对象结构的存储与提取  对象持久化的难点之一: 对象之间的引用 2020/11/4 Institute of Computer Software Nanjing University 43 对象结构的存储与提取  需持久化整个对象引用闭包 Persistence closure Java的 serialization规则 缺省规则:非 static 非 transient 的数据成员 用户定义 class List implements Serializable { List next。 private static final ObjectStreamField[] serialPersistentFields = {new ObjectStreamField(next, )}。 } 2020/11/4 Institute of Computer Software Nanjing University 44 对象结构的存储与提取  闭包可能太大 小对象引用(共享的)大对象 2020/11/4 Institute of Computer Software Nanjing University 45 对象结构的存储与提取  Java 的 transient 修饰子 Transient fields 不被序列化 Static fields 也不被序列化 开发者负责维护 2020/11/4 Institute of Computer Software Nanjing University 46 Schema evolution  Fact: Classes change  Problem: Objects are stored of which class descriptions have changed  Schema evolution:  At least one class used by the retrieving system differs from its counterpart stored by the storing system.  Object retrieval mismatch (Object mismatch):  The retrieving system retrieves a particular object whose own generating class was different in the storing system.  No fully satisfactory solution 2020/11/4 Institute of Computer Software Nanjing University 47 Different approaches  Naive, extreme approaches:  Forsake previously stored objects  Over a migration path from old format to new  a onetime, en masse conversion of old objects  not applicable to a large persistent store or to one that must be available continuously  Most general solution: Onthefly conversion  Note: We cover only the retrieval part. Whether to write back the converted object is a separate issue. 2020/11/4 Institute of Computer Software Nanjing University 48 Onthefly object conversion  Three separate issues: Detection: Catch object mismatch Notification: Make retrieving system aware of object mismatch Correction: Bring mismatched object to a consistent state Make it a correct instance of the new class version 2020/11/4 Institute of Computer Software Nanjing University 49 Detection  Detect a mismatch between two versions of an object‟s generating class  Two categories of detection policy:  Nominal approach:  Each class version has a version name  Central registration mechanism necessary  Structural approach:  Deduce class descriptor from actual class structure  Store class descriptor  Simple detection: pare class descriptors of retrieved object with new class descriptor 2020/11/4 Institute of Computer Software Nanjing University 50 Detection: Structural Approach  What does the class descriptor need to contain?  Tradeoff between efficiency and reliability  Two extreme approaches:  C1: class name  C2: entire class text (. abstract syntax tree)  Reasonable approaches:  C3: class name, list of attributes (name and type)  C4: in addition to C3: class invariant 2020/11/4 Institute of Computer Software Nanjing University 51 Notification  What happens when the detection mechanism has caught an object mismatch?  Language level mechanism  Class ANY could include a procedure: correct_mismatch is Handle object retrieval mismatch. local exception: EXCEPTIONS do create exception („„[ Routine failure: Object mismatch during retrieval ]‟‟) end 2020/11/4 Institute of Computer Software Nanjing University 52 Correction  How do we correct an object that caused a mismatch?  Current situation: Retrieval mechanism has created a new object (deduced from a stored object with same generating class) A mismatch has been detected  new object is in temporary (maybe inconsistent) state 2020/11/4 Institute of Computer Software Nanjing University 53 Correction 增加 attribute 删除 attribute 2020/11/4 Institute of Computer Software Nanjing University 54 Attribute was not in stored version. Field is initialized to default value of attribute type Stored version had a field. New version has removed attribute. Attributes have not changed. Correction correct_mismatch is Handle object retrieval mismatch by correctly setting up balance. do balance := ensure consistent: balance = end 2020/11/4 Institute of Computer Software Nanjing University 55 deposits withdrawals balance 900 100 200 240 300 new field (initialized to default value) old fields Wrong! 维护不变式 自动对象转换: Java  serialVersionUID 自动定义 (根据类文件生成)  1. Class name 2. The class modifiers 3. The name of each interface 4. For each field of the class (except private static and private transient fields):  The name of the field  The modifiers of the field  The。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。