| 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.io.Serializable; |
| 11 |
import java.util.Comparator; |
| 12 |
import java.util.HashMap; |
| 13 |
import java.util.Map; |
| 14 |
|
| 15 |
import com.sleepycat.compat.DbCompat; |
| 16 |
|
| 17 |
/** |
| 18 |
* The btree comparator for persistent key classes. The serialized form of |
| 19 |
* this comparator is stored in the BDB JE database descriptor so that the |
| 20 |
* comparator can be re-created during recovery. |
| 21 |
* |
| 22 |
* @author Mark Hayes |
| 23 |
*/ |
| 24 |
public class PersistComparator implements |
| 25 |
Comparator<byte[]>, Serializable { |
| 26 |
|
| 27 |
private static final long serialVersionUID = 5221576538843355317L; |
| 28 |
|
| 29 |
private String keyClassName; |
| 30 |
private String[] comositeFieldOrder; |
| 31 |
private Map<String, String[]> fieldFormatData; |
| 32 |
private transient PersistKeyBinding binding; |
| 33 |
|
| 34 |
public PersistComparator(PersistKeyBinding binding) { |
| 35 |
this.binding = binding; |
| 36 |
/* Save info necessary to recreate binding during deserialization. */ |
| 37 |
final CompositeKeyFormat format = |
| 38 |
(CompositeKeyFormat) binding.keyFormat; |
| 39 |
keyClassName = format.getClassName(); |
| 40 |
comositeFieldOrder = CompositeKeyFormat.getFieldNameArray |
| 41 |
(format.getClassMetadata().getCompositeKeyFields()); |
| 42 |
/* Currently only enum formats have per-class data. */ |
| 43 |
for (FieldInfo field : format.getFieldInfo()) { |
| 44 |
Format fieldFormat = field.getType(); |
| 45 |
if (fieldFormat.isEnum()) { |
| 46 |
EnumFormat enumFormat = (EnumFormat) fieldFormat; |
| 47 |
if (fieldFormatData == null) { |
| 48 |
fieldFormatData = new HashMap<String, String[]>(); |
| 49 |
} |
| 50 |
fieldFormatData.put(enumFormat.getClassName(), |
| 51 |
enumFormat.getFormatData()); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* In BDB JE this method will be called after the comparator is |
| 58 |
* deserialized, including during recovery. We must construct the binding |
| 59 |
* here, without access to the stored catalog since recovery is not |
| 60 |
* complete. |
| 61 |
*/ |
| 62 |
public void initialize(ClassLoader loader) { |
| 63 |
final Catalog catalog; |
| 64 |
if (fieldFormatData == null) { |
| 65 |
catalog = new ComparatorCatalog(loader, null); |
| 66 |
} else { |
| 67 |
final Map<String, Format> enumFormats = |
| 68 |
new HashMap<String, Format>(); |
| 69 |
catalog = new ComparatorCatalog(loader, enumFormats); |
| 70 |
for (Map.Entry<String, String[]> entry : |
| 71 |
fieldFormatData.entrySet()) { |
| 72 |
final String fldClassName = entry.getKey(); |
| 73 |
final String[] enumNames = entry.getValue(); |
| 74 |
final Class fldClass; |
| 75 |
try { |
| 76 |
fldClass = catalog.resolveClass(fldClassName); |
| 77 |
} catch (ClassNotFoundException e) { |
| 78 |
throw new IllegalStateException(e); |
| 79 |
} |
| 80 |
enumFormats.put(fldClassName, |
| 81 |
new EnumFormat(catalog, fldClass, enumNames)); |
| 82 |
} |
| 83 |
for (Format fldFormat : enumFormats.values()) { |
| 84 |
fldFormat.initializeIfNeeded(catalog, null /*model*/); |
| 85 |
} |
| 86 |
} |
| 87 |
final Class keyClass; |
| 88 |
try { |
| 89 |
keyClass = catalog.resolveClass(keyClassName); |
| 90 |
} catch (ClassNotFoundException e) { |
| 91 |
throw new IllegalStateException(e); |
| 92 |
} |
| 93 |
binding = new PersistKeyBinding(catalog, keyClass, |
| 94 |
comositeFieldOrder); |
| 95 |
} |
| 96 |
|
| 97 |
public int compare(byte[] b1, byte[] b2) { |
| 98 |
|
| 99 |
/* |
| 100 |
* In BDB JE, the binding is initialized by the initialize method. In |
| 101 |
* BDB, the binding is intialized by the constructor. |
| 102 |
*/ |
| 103 |
if (binding == null) { |
| 104 |
throw DbCompat.unexpectedState("Not initialized"); |
| 105 |
} |
| 106 |
|
| 107 |
try { |
| 108 |
Comparable k1 = |
| 109 |
(Comparable) binding.bytesToObject(b1, 0, b1.length); |
| 110 |
Comparable k2 = |
| 111 |
(Comparable) binding.bytesToObject(b2, 0, b2.length); |
| 112 |
|
| 113 |
return k1.compareTo(k2); |
| 114 |
} catch (RefreshException e) { |
| 115 |
|
| 116 |
/* |
| 117 |
* Refresh is not applicable to PersistComparator, which is used |
| 118 |
* during recovery. All field formats used by the comparator are |
| 119 |
* guaranteed to be predefined, because they must be primitive or |
| 120 |
* primitive wrapper types. So they are always present in the |
| 121 |
* catalog, and cannot change as the result of class evolution or |
| 122 |
* replication. |
| 123 |
*/ |
| 124 |
throw DbCompat.unexpectedException(e); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public String toString() { |
| 130 |
StringBuilder b = new StringBuilder(); |
| 131 |
b.append("[DPL comparator "); |
| 132 |
b.append(" keyClassName = ").append(keyClassName); |
| 133 |
b.append(" comositeFieldOrder = ["); |
| 134 |
for (String s : comositeFieldOrder) { |
| 135 |
b.append(s).append(','); |
| 136 |
} |
| 137 |
b.append(']'); |
| 138 |
b.append(" fieldFormatData = {"); |
| 139 |
for (Map.Entry<String, String[]> entry : fieldFormatData.entrySet()) { |
| 140 |
b.append(entry.getKey()).append(": ["); |
| 141 |
for (String s : entry.getValue()) { |
| 142 |
b.append(s).append(','); |
| 143 |
} |
| 144 |
b.append(']'); |
| 145 |
} |
| 146 |
b.append('}'); |
| 147 |
b.append(']'); |
| 148 |
return b.toString(); |
| 149 |
} |
| 150 |
} |