| 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 com.sleepycat.persist.model.EntityModel; |
| 11 |
import com.sleepycat.persist.evolve.Converter; |
| 12 |
import com.sleepycat.persist.raw.RawObject; |
| 13 |
|
| 14 |
/** |
| 15 |
* Reader for invoking a class Converter mutation. |
| 16 |
* |
| 17 |
* @author Mark Hayes |
| 18 |
*/ |
| 19 |
public class ConverterReader implements Reader { |
| 20 |
|
| 21 |
private static final long serialVersionUID = -305788321064984348L; |
| 22 |
|
| 23 |
private Converter converter; |
| 24 |
private transient Format oldFormat; |
| 25 |
|
| 26 |
ConverterReader(Converter converter) { |
| 27 |
this.converter = converter; |
| 28 |
} |
| 29 |
|
| 30 |
public void initializeReader(Catalog catalog, |
| 31 |
EntityModel model, |
| 32 |
int initVersion, |
| 33 |
Format oldFormat) { |
| 34 |
this.oldFormat = oldFormat; |
| 35 |
} |
| 36 |
|
| 37 |
public Object newInstance(EntityInput input, boolean rawAccess) |
| 38 |
throws RefreshException { |
| 39 |
|
| 40 |
/* Create the old format RawObject. */ |
| 41 |
return oldFormat.newInstance(input, true); |
| 42 |
} |
| 43 |
|
| 44 |
public void readPriKey(Object o, EntityInput input, boolean rawAccess) |
| 45 |
throws RefreshException { |
| 46 |
|
| 47 |
/* Read the old format RawObject's primary key. */ |
| 48 |
oldFormat.readPriKey(o, input, true); |
| 49 |
} |
| 50 |
|
| 51 |
public Object readObject(Object o, EntityInput input, boolean rawAccess) |
| 52 |
throws RefreshException { |
| 53 |
|
| 54 |
Catalog catalog = input.getCatalog(); |
| 55 |
|
| 56 |
/* Read the old format RawObject and convert it. */ |
| 57 |
boolean currentRawMode = input.setRawAccess(true); |
| 58 |
try { |
| 59 |
o = oldFormat.readObject(o, input, true); |
| 60 |
} finally { |
| 61 |
input.setRawAccess(currentRawMode); |
| 62 |
} |
| 63 |
o = converter.getConversion().convert(o); |
| 64 |
|
| 65 |
/* Convert the current format RawObject to a live Object. */ |
| 66 |
if (!rawAccess && o instanceof RawObject) { |
| 67 |
o = catalog.convertRawObject((RawObject) o, null); |
| 68 |
} |
| 69 |
return o; |
| 70 |
} |
| 71 |
|
| 72 |
public Accessor getAccessor(boolean rawAccess) { |
| 73 |
return oldFormat.getAccessor(rawAccess); |
| 74 |
} |
| 75 |
} |