简体   繁体   中英

How to serialize a java object which contains object references without serializing the objects referred to?

I am implementing B Plus Tree in java . I have a node class in which I am maintaining references to child object Nodes . Now When I serialize any node , it also serialize all the child nodes also. What I want is to serialize only that node and the references to the child nodes.I tried writing the node object as a byte stream but on de-serializing it is not working.

public class BNode implements Serializable
{
    LinkedList<Float> keys;
    LinkedList<BNode> childPointers;
    BNode parent;
 ...
}

In B+ tree , the nodes are saved in disk and I have to simulate that action. Now each page is of 2 KB(say) so in my each node I am saving data of around 2044 bytes( 255 float values, and 256 node references - total 255*4 + 255*4 + some other data of 10 bytes)in a single file simulating a single node. Now if I serialize the parent node, it is serializing whole tree into single file thus defeating the whole purpose

You have to use transient . This excludes variables from serializing.

public class BNode implements Serializable
{
    LinkedList<Float> keys;
    LinkedList<BNode> childPointers;
    transient BNode parent;
 ...
}

If you need more sophisticated rules you can overwrite de default behaviour for (de)serialization by implementing this mehtods:

private void writeObject(ObjectOutputStream out) throws IOException;
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;

Check http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html for an indepth description.

In any practical disk-based B-tree, each index block will need to contain disk offsets of the blocks corresponding to the keys in this block. So you will need to make the reference array transient, and add a non-transient array of long for the offsets.

I would also suggest that you don't keep a parent reference or offset if you can possibly avoid it. If you memorise each search path you can avoid it. In large trees when you split an interior node, you don't want to have to update the parent links in N/2 child nodes as well as all the other things you have to do. It's a very significant performance hit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM