KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > dbms > Sequence


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 /*
24  * Sequence.java
25  *
26  * Created on 11 septembre 2002, 09:58
27  */

28
29 package org.xquark.mapper.dbms;
30
31 import java.sql.SQLException JavaDoc;
32
33 import org.xquark.mapper.RepositoryException;
34
35 /**
36  * This class encapsulates a database unique integers generators usually knows
37  * as sequences. This objects allows ID preallocation to limit access to the
38  * underlying database. Note that if the preallocation size becomes 0, the
39  * sequence becomes all-in-memory (useful for monouser).
40  *
41  */

42 public class Sequence
43 {
44     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
45     private static final String JavaDoc RCSName = "$Name: $";
46
47     private TableInfo seq;
48     private short preAllocation;
49     private long lastDBValue;
50     private long currentValue;
51     private long maxValue;
52     private boolean initialized = false;
53     
54     /** Creates a new instance of Sequence */
55     public Sequence(TableInfo seq, short preAllocation)
56     {
57         this.seq = seq;
58         this.preAllocation = preAllocation;
59         maxValue = (long) (Math.pow(10, seq.getColumns()[0].getPrecision()) - 1f);
60     }
61     
62     /** Get a new sequence value */
63     public long nextValue(AbstractConnection connection) throws RepositoryException
64     {
65         if (currentValue == maxValue)
66             throw new RepositoryException(RepositoryException.NOT_ALLOWED,
67             "The sequence " + seq.getName() + " has reached its maximum value.");
68         if (!initialized || (currentValue == (lastDBValue + preAllocation - 1)))
69             currentValue = lastDBValue = connection.nextSequenceValue(seq, preAllocation);
70         else
71             currentValue++;
72         initialized = true;
73         return currentValue;
74     }
75     
76     /** Get the current sequence value for the current object (not the absolute
77      * last value allocated by any Repository instance.
78      */

79     public long currentValue() throws RepositoryException
80     {
81         if (initialized)
82             return currentValue;
83         else
84             throw new RepositoryException(RepositoryException.NOT_ALLOWED,
85             "The sequence was not initialized.");
86     }
87     
88     /** Create.
89      */

90     public void create(AbstractConnection connection) throws RepositoryException
91     {
92         if (initialized)
93             throw new RepositoryException(RepositoryException.NOT_ALLOWED,
94             "The sequence is already initialized.");
95         else
96         {
97             try
98             {
99                 connection.createSequence(seq, preAllocation);
100             }
101             catch (SQLException JavaDoc e)
102             {
103                 throw new RepositoryException(RepositoryException.DB_ERROR,
104                 "JDBC error while creating the "+ seq.getName() + " sequence.");
105             }
106             initialized = true;
107         }
108     }
109     
110     public TableInfo getTableInfo()
111     {
112         return seq;
113     }
114 }
115
Popular Tags