| 1 |
/*- |
| 2 |
* See the file LICENSE for redistribution information. |
| 3 |
* |
| 4 |
* Copyright (c) 2002, 2013 Oracle and/or its affiliates. All rights reserved. |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
package com.sleepycat.persist.impl; |
| 9 |
|
| 10 |
import java.util.Collections; |
| 11 |
import java.util.HashMap; |
| 12 |
import java.util.List; |
| 13 |
|
| 14 |
import com.sleepycat.compat.DbCompat; |
| 15 |
import com.sleepycat.persist.raw.RawObject; |
| 16 |
|
| 17 |
/** |
| 18 |
* Implements Accessor for RawObject access. |
| 19 |
* |
| 20 |
* @author Mark Hayes |
| 21 |
*/ |
| 22 |
class RawAccessor implements Accessor { |
| 23 |
|
| 24 |
private Format parentFormat; |
| 25 |
private Accessor superAccessor; |
| 26 |
private FieldInfo priKeyField; |
| 27 |
private List<FieldInfo> secKeyFields; |
| 28 |
private List<FieldInfo> nonKeyFields; |
| 29 |
private boolean isCompositeKey; |
| 30 |
|
| 31 |
RawAccessor(Format parentFormat, |
| 32 |
Accessor superAccessor, |
| 33 |
FieldInfo priKeyField, |
| 34 |
List<FieldInfo> secKeyFields, |
| 35 |
List<FieldInfo> nonKeyFields) { |
| 36 |
this.parentFormat = parentFormat; |
| 37 |
this.superAccessor = superAccessor; |
| 38 |
this.priKeyField = priKeyField; |
| 39 |
this.secKeyFields = secKeyFields; |
| 40 |
this.nonKeyFields = nonKeyFields; |
| 41 |
} |
| 42 |
|
| 43 |
RawAccessor(Format parentFormat, |
| 44 |
List<FieldInfo> nonKeyFields) { |
| 45 |
this.parentFormat = parentFormat; |
| 46 |
this.nonKeyFields = nonKeyFields; |
| 47 |
secKeyFields = Collections.emptyList(); |
| 48 |
isCompositeKey = true; |
| 49 |
} |
| 50 |
|
| 51 |
public Object newInstance() { |
| 52 |
RawObject superObject; |
| 53 |
if (superAccessor != null) { |
| 54 |
superObject = ((RawObject) superAccessor.newInstance()); |
| 55 |
} else { |
| 56 |
superObject = null; |
| 57 |
} |
| 58 |
return new RawObject |
| 59 |
(parentFormat, new HashMap<String, Object>(), superObject); |
| 60 |
} |
| 61 |
|
| 62 |
public Object newArray(int len) { |
| 63 |
throw DbCompat.unexpectedState(); |
| 64 |
} |
| 65 |
|
| 66 |
public boolean isPriKeyFieldNullOrZero(Object o) { |
| 67 |
if (priKeyField != null) { |
| 68 |
Object val = getValue(o, priKeyField); |
| 69 |
Format format = priKeyField.getType(); |
| 70 |
if (format.isPrimitive()) { |
| 71 |
return ((Number) val).longValue() == 0L; |
| 72 |
} else { |
| 73 |
return val == null; |
| 74 |
} |
| 75 |
} else if (superAccessor != null) { |
| 76 |
return superAccessor.isPriKeyFieldNullOrZero(getSuper(o)); |
| 77 |
} else { |
| 78 |
throw DbCompat.unexpectedState("No primary key field"); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public void writePriKeyField(Object o, EntityOutput output) |
| 83 |
throws RefreshException { |
| 84 |
|
| 85 |
if (priKeyField != null) { |
| 86 |
Object val = getValue(o, priKeyField); |
| 87 |
Format format = priKeyField.getType(); |
| 88 |
output.writeKeyObject(val, format); |
| 89 |
} else if (superAccessor != null) { |
| 90 |
superAccessor.writePriKeyField(getSuper(o), output); |
| 91 |
} else { |
| 92 |
throw DbCompat.unexpectedState("No primary key field"); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
public void readPriKeyField(Object o, EntityInput input) |
| 97 |
throws RefreshException { |
| 98 |
|
| 99 |
if (priKeyField != null) { |
| 100 |
Format format = priKeyField.getType(); |
| 101 |
Object val = input.readKeyObject(format); |
| 102 |
setValue(o, priKeyField, val); |
| 103 |
} else if (superAccessor != null) { |
| 104 |
superAccessor.readPriKeyField(getSuper(o), input); |
| 105 |
} else { |
| 106 |
throw DbCompat.unexpectedState("No primary key field"); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
public void writeSecKeyFields(Object o, EntityOutput output) |
| 111 |
throws RefreshException { |
| 112 |
|
| 113 |
if (priKeyField != null && |
| 114 |
!priKeyField.getType().isPrimitive() && |
| 115 |
priKeyField.getType().getId() != Format.ID_STRING) { |
| 116 |
output.registerPriKeyObject(getValue(o, priKeyField)); |
| 117 |
} |
| 118 |
if (superAccessor != null) { |
| 119 |
superAccessor.writeSecKeyFields(getSuper(o), output); |
| 120 |
} |
| 121 |
for (int i = 0; i < secKeyFields.size(); i += 1) { |
| 122 |
writeField(o, secKeyFields.get(i), output); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
public void readSecKeyFields(Object o, |
| 127 |
EntityInput input, |
| 128 |
int startField, |
| 129 |
int endField, |
| 130 |
int superLevel) |
| 131 |
throws RefreshException { |
| 132 |
|
| 133 |
if (priKeyField != null && |
| 134 |
!priKeyField.getType().isPrimitive() && |
| 135 |
priKeyField.getType().getId() != Format.ID_STRING) { |
| 136 |
input.registerPriKeyObject(getValue(o, priKeyField)); |
| 137 |
} else if (priKeyField != null && |
| 138 |
priKeyField.getType().getId() == Format.ID_STRING) { |
| 139 |
input.registerPriStringKeyObject(getValue(o, priKeyField)); |
| 140 |
} |
| 141 |
if (superLevel != 0 && superAccessor != null) { |
| 142 |
superAccessor.readSecKeyFields |
| 143 |
(getSuper(o), input, startField, endField, superLevel - 1); |
| 144 |
} else { |
| 145 |
if (superLevel > 0) { |
| 146 |
throw DbCompat.unexpectedState("Super class does not exist"); |
| 147 |
} |
| 148 |
} |
| 149 |
if (superLevel <= 0) { |
| 150 |
for (int i = startField; |
| 151 |
i <= endField && i < secKeyFields.size(); |
| 152 |
i += 1) { |
| 153 |
readField(o, secKeyFields.get(i), input); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
public void writeNonKeyFields(Object o, EntityOutput output) |
| 159 |
throws RefreshException { |
| 160 |
|
| 161 |
if (superAccessor != null) { |
| 162 |
superAccessor.writeNonKeyFields(getSuper(o), output); |
| 163 |
} |
| 164 |
for (int i = 0; i < nonKeyFields.size(); i += 1) { |
| 165 |
writeField(o, nonKeyFields.get(i), output); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
public void readNonKeyFields(Object o, |
| 170 |
EntityInput input, |
| 171 |
int startField, |
| 172 |
int endField, |
| 173 |
int superLevel) |
| 174 |
throws RefreshException { |
| 175 |
|
| 176 |
if (superLevel != 0 && superAccessor != null) { |
| 177 |
superAccessor.readNonKeyFields |
| 178 |
(getSuper(o), input, startField, endField, superLevel - 1); |
| 179 |
} else { |
| 180 |
if (superLevel > 0) { |
| 181 |
throw DbCompat.unexpectedState("Super class does not exist"); |
| 182 |
} |
| 183 |
} |
| 184 |
if (superLevel <= 0) { |
| 185 |
for (int i = startField; |
| 186 |
i <= endField && i < nonKeyFields.size(); |
| 187 |
i += 1) { |
| 188 |
readField(o, nonKeyFields.get(i), input); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
public void writeCompositeKeyFields(Object o, EntityOutput output) |
| 194 |
throws RefreshException { |
| 195 |
|
| 196 |
for (int i = 0; i < nonKeyFields.size(); i += 1) { |
| 197 |
writeField(o, nonKeyFields.get(i), output); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
public void readCompositeKeyFields(Object o, EntityInput input) |
| 202 |
throws RefreshException { |
| 203 |
|
| 204 |
for (int i = 0; i < nonKeyFields.size(); i += 1) { |
| 205 |
readField(o, nonKeyFields.get(i), input); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
public Object getField(Object o, |
| 210 |
int field, |
| 211 |
int superLevel, |
| 212 |
boolean isSecField) { |
| 213 |
if (superLevel > 0) { |
| 214 |
return superAccessor.getField |
| 215 |
(getSuper(o), field, superLevel - 1, isSecField); |
| 216 |
} |
| 217 |
FieldInfo fld = |
| 218 |
isSecField ? secKeyFields.get(field) : nonKeyFields.get(field); |
| 219 |
return getValue(o, fld); |
| 220 |
} |
| 221 |
|
| 222 |
public void setField(Object o, |
| 223 |
int field, |
| 224 |
int superLevel, |
| 225 |
boolean isSecField, |
| 226 |
Object value) { |
| 227 |
if (superLevel > 0) { |
| 228 |
superAccessor.setField |
| 229 |
(getSuper(o), field, superLevel - 1, isSecField, value); |
| 230 |
return; |
| 231 |
} |
| 232 |
FieldInfo fld = |
| 233 |
isSecField ? secKeyFields.get(field) : nonKeyFields.get(field); |
| 234 |
setValue(o, fld, value); |
| 235 |
} |
| 236 |
|
| 237 |
public void setPriField(Object o, Object value) { |
| 238 |
if (priKeyField != null) { |
| 239 |
setValue(o, priKeyField, value); |
| 240 |
} else if (superAccessor != null) { |
| 241 |
superAccessor.setPriField(getSuper(o), value); |
| 242 |
} else { |
| 243 |
throw DbCompat.unexpectedState("No primary key field"); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
private RawObject getSuper(Object o) { |
| 248 |
return ((RawObject) o).getSuper(); |
| 249 |
} |
| 250 |
|
| 251 |
private Object getValue(Object o, FieldInfo field) { |
| 252 |
return ((RawObject) o).getValues().get(field.getName()); |
| 253 |
} |
| 254 |
|
| 255 |
private void setValue(Object o, FieldInfo field, Object val) { |
| 256 |
((RawObject) o).getValues().put(field.getName(), val); |
| 257 |
} |
| 258 |
|
| 259 |
private void writeField(Object o, FieldInfo field, EntityOutput output) |
| 260 |
throws RefreshException { |
| 261 |
|
| 262 |
Object val = getValue(o, field); |
| 263 |
Format format = field.getType(); |
| 264 |
if (isCompositeKey || format.isPrimitive()) { |
| 265 |
output.writeKeyObject(val, format); |
| 266 |
} else if (format.getId() == Format.ID_STRING) { |
| 267 |
output.writeString((String) val); |
| 268 |
} else { |
| 269 |
output.writeObject(val, format); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
private void readField(Object o, FieldInfo field, EntityInput input) |
| 274 |
throws RefreshException { |
| 275 |
|
| 276 |
Format format = field.getType(); |
| 277 |
Object val; |
| 278 |
if (isCompositeKey || format.isPrimitive()) { |
| 279 |
val = input.readKeyObject(format); |
| 280 |
} else if (format.getId() == Format.ID_STRING) { |
| 281 |
val = input.readStringObject(); |
| 282 |
} else { |
| 283 |
val = input.readObject(); |
| 284 |
} |
| 285 |
setValue(o, field, val); |
| 286 |
} |
| 287 |
|
| 288 |
public FieldInfo getPriKeyField() { |
| 289 |
return priKeyField; |
| 290 |
} |
| 291 |
} |