2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 2002, 2010 Oracle and/or its affiliates. All rights reserved.
8
package com.sleepycat.persist.impl;
10
import com.sleepycat.bind.tuple.TupleBase;
11
import com.sleepycat.db.DatabaseEntry;
12
import com.sleepycat.db.DatabaseException;
13
import com.sleepycat.db.Sequence;
16
* Assigns primary keys from a Sequence.
18
* This class is used directly by PrimaryIndex, not via an interface. To avoid
19
* making a public interface, the PersistEntityBinding contains a reference to
20
* a PersistKeyAssigner, and the PrimaryIndex gets the key assigner from the
21
* binding. See the PrimaryIndex constructor for more information.
25
public class PersistKeyAssigner {
27
/* See Store.refresh for an explanation of the use of volatile fields. */
28
private volatile Catalog catalog;
29
private volatile Format keyFieldFormat;
30
private volatile Format entityFormat;
31
private final boolean rawAccess;
32
private final Sequence sequence;
34
PersistKeyAssigner(PersistKeyBinding keyBinding,
35
PersistEntityBinding entityBinding,
37
catalog = keyBinding.catalog;
38
/* getSequenceKeyFormat will validate the field type for a sequence. */
39
keyFieldFormat = keyBinding.keyFormat.getSequenceKeyFormat();
40
entityFormat = entityBinding.entityFormat;
41
rawAccess = entityBinding.rawAccess;
42
this.sequence = sequence;
45
public boolean assignPrimaryKey(Object entity, DatabaseEntry key)
46
throws DatabaseException {
49
* The keyFieldFormat is the format of a simple integer field. For a
50
* composite key class it is the contained integer field. By writing
51
* the Long sequence value using that format, the output data can then
52
* be read to construct the actual key instance, whether it is a simple
53
* or composite key class, and assign it to the primary key field in
56
if (entityFormat.isPriKeyNullOrZero(entity, rawAccess)) {
57
Long value = sequence.get(null, 1);
58
RecordOutput output = new RecordOutput(catalog, rawAccess);
59
keyFieldFormat.writeObject(value, output, rawAccess);
60
TupleBase.outputToEntry(output, key);
61
EntityInput input = new RecordInput
62
(catalog, rawAccess, null, 0,
63
key.getData(), key.getOffset(), key.getSize());
64
entityFormat.getReader().readPriKey(entity, input, rawAccess);
74
void refresh(final PersistCatalog newCatalog) {
76
entityFormat = catalog.getFormat(entityFormat.getClassName());
77
keyFieldFormat = catalog.getFormat(keyFieldFormat.getClassName());