~ubuntu-branches/ubuntu/saucy/db/saucy-proposed

« back to all changes in this revision

Viewing changes to lang/java/src/com/sleepycat/persist/impl/PersistKeyAssigner.java

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2010-11-05 15:02:09 UTC
  • mfrom: (13.1.12 sid)
  • Revision ID: james.westby@ubuntu.com-20101105150209-ppvyn0619pu014xo
Tags: 5.1.19-1ubuntu1
* Resynchronise with Debian.  Remaining changes:
  - Pass --build/--host to configure to support cross-building, and don't
    override CC.
  - Disable the Java build when cross-building, for now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-
 
2
 * See the file LICENSE for redistribution information.
 
3
 *
 
4
 * Copyright (c) 2002, 2010 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.db.DatabaseEntry;
 
12
import com.sleepycat.db.DatabaseException;
 
13
import com.sleepycat.db.Sequence;
 
14
 
 
15
/**
 
16
 * Assigns primary keys from a Sequence.
 
17
 *
 
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.
 
22
 *
 
23
 * @author Mark Hayes
 
24
 */
 
25
public class PersistKeyAssigner {
 
26
 
 
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;
 
33
 
 
34
    PersistKeyAssigner(PersistKeyBinding keyBinding,
 
35
                       PersistEntityBinding entityBinding,
 
36
                       Sequence sequence) {
 
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;
 
43
    }
 
44
 
 
45
    public boolean assignPrimaryKey(Object entity, DatabaseEntry key)
 
46
        throws DatabaseException {
 
47
            
 
48
        /*
 
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
 
54
         * the entity object.
 
55
         */
 
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);
 
65
            return true;
 
66
        } else {
 
67
            return false;
 
68
        }
 
69
    }
 
70
 
 
71
    /**
 
72
     * See Store.refresh.
 
73
     */
 
74
    void refresh(final PersistCatalog newCatalog) {
 
75
        catalog = newCatalog;
 
76
        entityFormat = catalog.getFormat(entityFormat.getClassName());
 
77
        keyFieldFormat = catalog.getFormat(keyFieldFormat.getClassName());
 
78
    }
 
79
}