| 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.tuple.TupleBase; |
| 11 |
import com.sleepycat.compat.DbCompat; |
| 12 |
import com.sleepycat.db.DatabaseEntry; |
| 13 |
import com.sleepycat.db.DatabaseException; |
| 14 |
import com.sleepycat.db.Sequence; |
| 15 |
|
| 16 |
/** |
| 17 |
* Assigns primary keys from a Sequence. |
| 18 |
* |
| 19 |
* This class is used directly by PrimaryIndex, not via an interface. To avoid |
| 20 |
* making a public interface, the PersistEntityBinding contains a reference to |
| 21 |
* a PersistKeyAssigner, and the PrimaryIndex gets the key assigner from the |
| 22 |
* binding. See the PrimaryIndex constructor for more information. |
| 23 |
* |
| 24 |
* @author Mark Hayes |
| 25 |
*/ |
| 26 |
public class PersistKeyAssigner { |
| 27 |
|
| 28 |
/* See Store.refresh for an explanation of the use of volatile fields. */ |
| 29 |
private volatile Catalog catalog; |
| 30 |
private volatile Format keyFieldFormat; |
| 31 |
private volatile Format entityFormat; |
| 32 |
private final boolean rawAccess; |
| 33 |
private final Sequence sequence; |
| 34 |
|
| 35 |
PersistKeyAssigner(PersistKeyBinding keyBinding, |
| 36 |
PersistEntityBinding entityBinding, |
| 37 |
Sequence sequence) { |
| 38 |
catalog = keyBinding.catalog; |
| 39 |
/* getSequenceKeyFormat will validate the field type for a sequence. */ |
| 40 |
keyFieldFormat = keyBinding.keyFormat.getSequenceKeyFormat(); |
| 41 |
entityFormat = entityBinding.entityFormat; |
| 42 |
rawAccess = entityBinding.rawAccess; |
| 43 |
this.sequence = sequence; |
| 44 |
} |
| 45 |
|
| 46 |
public boolean assignPrimaryKey(Object entity, DatabaseEntry key) |
| 47 |
throws DatabaseException { |
| 48 |
|
| 49 |
try { |
| 50 |
return assignPrimaryKeyInternal(entity, key); |
| 51 |
} catch (RefreshException e) { |
| 52 |
e.refresh(); |
| 53 |
try { |
| 54 |
return assignPrimaryKeyInternal(entity, key); |
| 55 |
} catch (RefreshException e2) { |
| 56 |
throw DbCompat.unexpectedException(e2); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
private boolean assignPrimaryKeyInternal(Object entity, DatabaseEntry key) |
| 62 |
throws DatabaseException, RefreshException { |
| 63 |
|
| 64 |
/* |
| 65 |
* The keyFieldFormat is the format of a simple integer field. For a |
| 66 |
* composite key class it is the contained integer field. By writing |
| 67 |
* the Long sequence value using that format, the output data can then |
| 68 |
* be read to construct the actual key instance, whether it is a simple |
| 69 |
* or composite key class, and assign it to the primary key field in |
| 70 |
* the entity object. |
| 71 |
*/ |
| 72 |
if (entityFormat.isPriKeyNullOrZero(entity, rawAccess)) { |
| 73 |
Long value = sequence.get(null, 1); |
| 74 |
RecordOutput output = new RecordOutput(catalog, rawAccess); |
| 75 |
keyFieldFormat.writeObject(value, output, rawAccess); |
| 76 |
TupleBase.outputToEntry(output, key); |
| 77 |
EntityInput input = new RecordInput |
| 78 |
(catalog, rawAccess, null, 0, |
| 79 |
key.getData(), key.getOffset(), key.getSize()); |
| 80 |
entityFormat.getReader().readPriKey(entity, input, rawAccess); |
| 81 |
return true; |
| 82 |
} else { |
| 83 |
return false; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* See Store.refresh. |
| 89 |
*/ |
| 90 |
void refresh(final PersistCatalog newCatalog) { |
| 91 |
catalog = newCatalog; |
| 92 |
entityFormat = catalog.getFormat(entityFormat.getClassName()); |
| 93 |
keyFieldFormat = catalog.getFormat(keyFieldFormat.getClassName()); |
| 94 |
} |
| 95 |
} |