KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > persist > impl > PersistKeyAssigner


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: PersistKeyAssigner.java,v 1.11 2006/10/30 21:14:32 bostic Exp $
7  */

8
9 package com.sleepycat.persist.impl;
10
11 import com.sleepycat.bind.tuple.TupleBase;
12 import com.sleepycat.je.DatabaseEntry;
13 import com.sleepycat.je.DatabaseException;
14 import com.sleepycat.je.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     private Catalog catalog;
29     private Format keyFormat;
30     private Format entityFormat;
31     private boolean rawAccess;
32     private Sequence sequence;
33
34     PersistKeyAssigner(PersistKeyBinding keyBinding,
35                        PersistEntityBinding entityBinding,
36                        Sequence sequence) {
37         catalog = keyBinding.catalog;
38         keyFormat = keyBinding.keyFormat;
39         entityFormat = entityBinding.entityFormat;
40         rawAccess = entityBinding.rawAccess;
41         this.sequence = sequence;
42     }
43
44     public boolean assignPrimaryKey(Object JavaDoc entity, DatabaseEntry key)
45         throws DatabaseException {
46             
47         if (entityFormat.isPriKeyNullOrZero(entity, rawAccess)) {
48             Long JavaDoc value = sequence.get(null, 1);
49             RecordOutput output = new RecordOutput(catalog, rawAccess);
50             keyFormat.writeObject(value, output, rawAccess);
51             TupleBase.outputToEntry(output, key);
52             EntityInput input = new RecordInput
53                 (catalog, rawAccess, null, 0,
54                  key.getData(), key.getOffset(), key.getSize());
55             entityFormat.getReader().readPriKey(entity, input, rawAccess);
56             return true;
57         } else {
58             return false;
59         }
60     }
61 }
62
Popular Tags