KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > persist > impl > RecordOutput


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: RecordOutput.java,v 1.4 2006/10/30 21:14:32 bostic Exp $
7  */

8
9 package com.sleepycat.persist.impl;
10
11 import com.sleepycat.bind.tuple.TupleOutput;
12 import com.sleepycat.persist.raw.RawObject;
13
14 /**
15  * Implements EntityOutput to write record key-data pairs. Extends TupleOutput
16  * to implement the subset of TupleOutput methods that are defined in the
17  * EntityOutput interface.
18  *
19  * @author Mark Hayes
20  */

21 class RecordOutput extends TupleOutput implements EntityOutput {
22
23     private Catalog catalog;
24     private boolean rawAccess;
25     private VisitedObjects visited;
26
27     /**
28      * Creates a new output with an empty/null VisitedObjects set.
29      */

30     RecordOutput(Catalog catalog, boolean rawAccess) {
31
32         super();
33         this.catalog = catalog;
34         this.rawAccess = rawAccess;
35     }
36
37     /**
38      * @see EntityInput#writeObject
39      */

40     public void writeObject(Object JavaDoc o, Format fieldFormat) {
41
42         /* For a null instance, write a zero format ID. */
43         if (o == null) {
44             writePackedInt(Format.ID_NULL);
45             return;
46         }
47
48         /*
49          * For an already visited instance, output a reference to it. The
50          * reference is the negation of the visited offset minus one.
51          */

52         if (visited != null) {
53             int offset = visited.getOffset(o);
54             if (offset > 0) {
55                 writePackedInt(-(offset + 1));
56                 return;
57             }
58         }
59
60         /*
61          * Get and validate the format. Catalog.getFormat(Class) throws
62          * IllegalArgumentException if the class is not persistent. We don't
63          * need to check the fieldFormat (and it will be null) for non-raw
64          * access because field type checking is enforced by Java.
65          */

66         Format format;
67         if (rawAccess) {
68             format = RawAbstractInput.checkRawType(catalog, o, fieldFormat);
69         } else {
70             format = catalog.getFormat(o.getClass());
71         }
72         if (format.getProxiedFormat() != null) {
73             throw new IllegalArgumentException JavaDoc
74                 ("May not store proxy classes directly: " +
75                  format.getClassName());
76         }
77         if (format.isEntity()) {
78             throw new IllegalArgumentException JavaDoc
79                 ("References to entities are not allowed: " +
80                  o.getClass().getName());
81         }
82
83         /* Remember that we visited this instance. */
84         if (visited == null) {
85             visited = new VisitedObjects();
86         }
87         visited.add(o, size());
88
89         /* Finally, write the formatId and object value. */
90         writePackedInt(format.getId());
91         format.writeObject(o, this, rawAccess);
92     }
93
94     /**
95      * @see EntityInput#writeKeyObject
96      */

97     public void writeKeyObject(Object JavaDoc o, Format fieldFormat) {
98
99         /* Key objects must not be null and must be of the declared class. */
100         if (o == null) {
101             throw new IllegalArgumentException JavaDoc
102                 ("Key field object may not be null");
103         }
104         Format format;
105         if (rawAccess) {
106             if (o instanceof RawObject) {
107                 format = (Format) ((RawObject) o).getType();
108             } else {
109                 format = catalog.getFormat(o.getClass());
110                 /* Expect primitive wrapper class in raw mode. */
111                 if (fieldFormat.isPrimitive()) {
112                     fieldFormat = fieldFormat.getWrapperFormat();
113                 }
114             }
115         } else {
116             format = catalog.getFormat(o.getClass());
117         }
118         if (fieldFormat != format) {
119             throw new IllegalArgumentException JavaDoc
120                 ("The key field object class (" + o.getClass().getName() +
121                  ") must be the field's declared class: " +
122                  fieldFormat.getClassName());
123         }
124
125         /* Write the object value (no formatId is written for keys). */
126         fieldFormat.writeObject(o, this, rawAccess);
127     }
128
129     /**
130      * @see EntityInput#registerPriKeyObject
131      */

132     public void registerPriKeyObject(Object JavaDoc o) {
133
134         /*
135          * PRI_KEY_VISITED_OFFSET is used as the visited offset to indicate
136          * that the visited object is stored in the primary key byte array.
137          */

138         if (visited == null) {
139             visited = new VisitedObjects();
140         }
141         visited.add(o, EntityOutput.PRI_KEY_VISITED_OFFSET);
142     }
143     
144     /**
145      * @see EntityInput#writeArrayLength
146      */

147     public void writeArrayLength(int length) {
148         writePackedInt(length);
149     }
150
151     /**
152      * @see EntityInput#writeEnumConstant
153      */

154     public void writeEnumConstant(String JavaDoc[] names, int index) {
155         writePackedInt(index);
156     }
157 }
158
Popular Tags