1 8 9 package com.sleepycat.persist.impl; 10 11 import com.sleepycat.bind.tuple.TupleOutput; 12 import com.sleepycat.persist.raw.RawObject; 13 14 21 class RecordOutput extends TupleOutput implements EntityOutput { 22 23 private Catalog catalog; 24 private boolean rawAccess; 25 private VisitedObjects visited; 26 27 30 RecordOutput(Catalog catalog, boolean rawAccess) { 31 32 super(); 33 this.catalog = catalog; 34 this.rawAccess = rawAccess; 35 } 36 37 40 public void writeObject(Object o, Format fieldFormat) { 41 42 43 if (o == null) { 44 writePackedInt(Format.ID_NULL); 45 return; 46 } 47 48 52 if (visited != null) { 53 int offset = visited.getOffset(o); 54 if (offset > 0) { 55 writePackedInt(-(offset + 1)); 56 return; 57 } 58 } 59 60 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 74 ("May not store proxy classes directly: " + 75 format.getClassName()); 76 } 77 if (format.isEntity()) { 78 throw new IllegalArgumentException 79 ("References to entities are not allowed: " + 80 o.getClass().getName()); 81 } 82 83 84 if (visited == null) { 85 visited = new VisitedObjects(); 86 } 87 visited.add(o, size()); 88 89 90 writePackedInt(format.getId()); 91 format.writeObject(o, this, rawAccess); 92 } 93 94 97 public void writeKeyObject(Object o, Format fieldFormat) { 98 99 100 if (o == null) { 101 throw new IllegalArgumentException 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 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 120 ("The key field object class (" + o.getClass().getName() + 121 ") must be the field's declared class: " + 122 fieldFormat.getClassName()); 123 } 124 125 126 fieldFormat.writeObject(o, this, rawAccess); 127 } 128 129 132 public void registerPriKeyObject(Object o) { 133 134 138 if (visited == null) { 139 visited = new VisitedObjects(); 140 } 141 visited.add(o, EntityOutput.PRI_KEY_VISITED_OFFSET); 142 } 143 144 147 public void writeArrayLength(int length) { 148 writePackedInt(length); 149 } 150 151 154 public void writeEnumConstant(String [] names, int index) { 155 writePackedInt(index); 156 } 157 } 158 | Popular Tags |