| 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.bind.EntryBinding; |
| 11 |
import com.sleepycat.bind.tuple.TupleBase; |
| 12 |
import com.sleepycat.compat.DbCompat; |
| 13 |
import com.sleepycat.db.DatabaseEntry; |
| 14 |
|
| 15 |
/** |
| 16 |
* A persistence key binding for a given key class. |
| 17 |
* |
| 18 |
* @author Mark Hayes |
| 19 |
*/ |
| 20 |
public class PersistKeyBinding implements EntryBinding { |
| 21 |
|
| 22 |
/* See Store.refresh for an explanation of the use of volatile fields. */ |
| 23 |
volatile Catalog catalog; |
| 24 |
volatile Format keyFormat; |
| 25 |
final boolean rawAccess; |
| 26 |
|
| 27 |
/** |
| 28 |
* Creates a key binding for a given key class. |
| 29 |
*/ |
| 30 |
public PersistKeyBinding(Catalog catalogParam, |
| 31 |
String clsName, |
| 32 |
boolean rawAccess) { |
| 33 |
catalog = catalogParam; |
| 34 |
try { |
| 35 |
keyFormat = PersistEntityBinding.getOrCreateFormat |
| 36 |
(catalog, clsName, rawAccess); |
| 37 |
} catch (RefreshException e) { |
| 38 |
/* Must assign catalog field in constructor. */ |
| 39 |
catalog = e.refresh(); |
| 40 |
try { |
| 41 |
keyFormat = PersistEntityBinding.getOrCreateFormat |
| 42 |
(catalog, clsName, rawAccess); |
| 43 |
} catch (RefreshException e2) { |
| 44 |
throw DbCompat.unexpectedException(e2); |
| 45 |
} |
| 46 |
} |
| 47 |
if (!keyFormat.isSimple() && |
| 48 |
!keyFormat.isEnum() && |
| 49 |
!(keyFormat.getClassMetadata() != null && |
| 50 |
keyFormat.getClassMetadata().getCompositeKeyFields() != null)) { |
| 51 |
throw new IllegalArgumentException |
| 52 |
("Key class is not a simple type, an enum, or a composite " + |
| 53 |
"key class (composite keys must include @KeyField " + |
| 54 |
"annotations): " + |
| 55 |
clsName); |
| 56 |
} |
| 57 |
this.rawAccess = rawAccess; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Creates a key binding dynamically for use by PersistComparator. Formats |
| 62 |
* are created from scratch rather than using a shared catalog. |
| 63 |
*/ |
| 64 |
PersistKeyBinding(final Catalog catalog, |
| 65 |
final Class cls, |
| 66 |
final String[] compositeFieldOrder) { |
| 67 |
this.catalog = catalog; |
| 68 |
keyFormat = new CompositeKeyFormat(catalog, cls, compositeFieldOrder); |
| 69 |
keyFormat.initializeIfNeeded(catalog, null /*model*/); |
| 70 |
rawAccess = false; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Binds bytes to an object for use by PersistComparator as well as |
| 75 |
* entryToObject. |
| 76 |
*/ |
| 77 |
Object bytesToObject(byte[] bytes, int offset, int length) |
| 78 |
throws RefreshException { |
| 79 |
|
| 80 |
return readKey(keyFormat, catalog, bytes, offset, length, rawAccess); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Binds bytes to an object for use by PersistComparator as well as |
| 85 |
* entryToObject. |
| 86 |
*/ |
| 87 |
static Object readKey(Format keyFormat, |
| 88 |
Catalog catalog, |
| 89 |
byte[] bytes, |
| 90 |
int offset, |
| 91 |
int length, |
| 92 |
boolean rawAccess) |
| 93 |
throws RefreshException { |
| 94 |
|
| 95 |
EntityInput input = new RecordInput |
| 96 |
(catalog, rawAccess, null, 0, bytes, offset, length); |
| 97 |
return input.readKeyObject(keyFormat); |
| 98 |
} |
| 99 |
|
| 100 |
public Object entryToObject(DatabaseEntry entry) { |
| 101 |
try { |
| 102 |
return entryToObjectInternal(entry); |
| 103 |
} catch (RefreshException e) { |
| 104 |
e.refresh(); |
| 105 |
try { |
| 106 |
return entryToObjectInternal(entry); |
| 107 |
} catch (RefreshException e2) { |
| 108 |
throw DbCompat.unexpectedException(e2); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
private Object entryToObjectInternal(DatabaseEntry entry) |
| 114 |
throws RefreshException { |
| 115 |
|
| 116 |
return bytesToObject |
| 117 |
(entry.getData(), entry.getOffset(), entry.getSize()); |
| 118 |
} |
| 119 |
|
| 120 |
public void objectToEntry(Object object, DatabaseEntry entry) { |
| 121 |
try { |
| 122 |
objectToEntryInternal(object, entry); |
| 123 |
} catch (RefreshException e) { |
| 124 |
e.refresh(); |
| 125 |
try { |
| 126 |
objectToEntryInternal(object, entry); |
| 127 |
} catch (RefreshException e2) { |
| 128 |
throw DbCompat.unexpectedException(e2); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
private void objectToEntryInternal(Object object, DatabaseEntry entry) |
| 134 |
throws RefreshException { |
| 135 |
|
| 136 |
RecordOutput output = new RecordOutput(catalog, rawAccess); |
| 137 |
output.writeKeyObject(object, keyFormat); |
| 138 |
TupleBase.outputToEntry(output, entry); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* See Store.refresh. |
| 143 |
*/ |
| 144 |
void refresh(final PersistCatalog newCatalog) { |
| 145 |
catalog = newCatalog; |
| 146 |
keyFormat = catalog.getFormat(keyFormat.getClassName()); |
| 147 |
} |
| 148 |
} |