KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > datasource > ids > AbstractDataSourceBlockIdGenerator


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package org.apache.avalon.excalibur.datasource.ids;
21
22 import java.math.BigDecimal JavaDoc;
23
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26
27 /**
28  * The AbstractDataSourceBlockIdGenerator allocates blocks of ids from a DataSource
29  * and then provides them as needed. This is useful in reducing communication with
30  * the DataSource.
31  *
32  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
33  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:17 $
34  * @since 4.1
35  */

36 public abstract class AbstractDataSourceBlockIdGenerator
37     extends AbstractDataSourceIdGenerator
38 {
39     /**
40      * The first id in a batch of Ids loaded in from the DataSource.
41      */

42     private BigDecimal JavaDoc m_firstBigDecimal;
43
44     /**
45      * The first id in a batch of Ids loaded in from the DataSource.
46      */

47     private long m_firstLong;
48
49     /**
50      * The number of ids loaded in each block.
51      */

52     private int m_blockSize;
53
54     /**
55      * The number of ids which have been allocated from the current block.
56      */

57     private int m_allocated;
58
59     /*---------------------------------------------------------------
60      * Constructors
61      *-------------------------------------------------------------*/

62     public AbstractDataSourceBlockIdGenerator()
63     {
64     }
65
66     /*---------------------------------------------------------------
67      * Methods
68      *-------------------------------------------------------------*/

69     /**
70      * Allocates a block, of the given size, of ids from the database.
71      *
72      * @param blockSize number of Ids which are to be allocated.
73      *
74      * @return The first id in the allocated block.
75      *
76      * @throws IdException if there it was not possible to allocate a block of ids.
77      */

78     protected abstract BigDecimal JavaDoc allocateBigDecimalIdBlock( int blockSize )
79         throws IdException;
80
81     /**
82      * Allocates a block, of the given size, of ids from the database.
83      *
84      * @param blockSize number of Ids which are to be allocated.
85      *
86      * @return The first id in the allocated block.
87      *
88      * @throws IdException if there it was not possible to allocate a block of ids.
89      */

90     protected abstract long allocateLongIdBlock( int blockSize )
91         throws IdException;
92
93     /*---------------------------------------------------------------
94      * AbstractIdGenerator Methods
95      *-------------------------------------------------------------*/

96     /**
97      * Gets the next id as a Big Decimal. This method will only be called
98      * when synchronized and when the data type is configured to be BigDecimal.
99      *
100      * @return the next id as a BigDecimal.
101      *
102      * @throws IdException if an Id could not be allocated for any reason.
103      */

104     protected BigDecimal JavaDoc getNextBigDecimalIdInner()
105         throws IdException
106     {
107         if( m_allocated >= m_blockSize )
108         {
109             // Need to allocate a new batch of ids
110
try
111             {
112                 m_firstBigDecimal = allocateBigDecimalIdBlock( m_blockSize );
113
114                 // Reset the allocated count
115
m_allocated = 0;
116             }
117             catch( IdException e )
118             {
119                 // Set the allocated count to signal that there are not any ids available.
120
m_allocated = Integer.MAX_VALUE;
121                 throw e;
122             }
123         }
124
125         // We know that at least one id is available.
126
// Get an id out of the currently allocated block.
127
BigDecimal JavaDoc id = m_firstBigDecimal.add( new BigDecimal JavaDoc( m_allocated ) );
128         m_allocated++;
129
130         return id;
131     }
132
133     /**
134      * Gets the next id as a long. This method will only be called
135      * when synchronized and when the data type is configured to be long.
136      *
137      * @return the next id as a long.
138      *
139      * @throws IdException if an Id could not be allocated for any reason.
140      */

141     protected long getNextLongIdInner()
142         throws IdException
143     {
144         if( m_allocated >= m_blockSize )
145         {
146             // Need to allocate a new batch of ids
147
try
148             {
149                 m_firstLong = allocateLongIdBlock( m_blockSize );
150
151                 // Reset the allocated count
152
m_allocated = 0;
153             }
154             catch( IdException e )
155             {
156                 // Set the allocated count to signal that there are not any ids available.
157
m_allocated = Integer.MAX_VALUE;
158                 throw e;
159             }
160         }
161
162         // We know that at least one id is available.
163
// Get an id out of the currently allocated block.
164
long id = m_firstLong + m_allocated;
165         if( id < 0 )
166         {
167             // The value wrapped
168
String JavaDoc msg = "No more Ids are available, the maximum long value has been reached.";
169             getLogger().error( msg );
170             throw new IdException( msg );
171         }
172         m_allocated++;
173
174         return id;
175     }
176
177     /*---------------------------------------------------------------
178      * Configurable Methods
179      *-------------------------------------------------------------*/

180     /**
181      * Called by the Container to configure the component.
182      *
183      * @param configuration configuration info used to setup the component.
184      *
185      * @throws ConfigurationException if there are any problems with the configuration.
186      */

187     public void configure( Configuration configuration )
188         throws ConfigurationException
189     {
190         super.configure( configuration );
191
192         // Obtain the block size.
193
m_blockSize = configuration.getAttributeAsInteger( "block-size", 10 );
194     }
195
196     /*---------------------------------------------------------------
197      * Initializable Methods
198      *-------------------------------------------------------------*/

199     /**
200      * Called by the Container to initialize the component.
201      *
202      * @throws Exception if there were any problems durring initialization.
203      */

204     public void initialize()
205         throws Exception JavaDoc
206     {
207         super.initialize();
208
209         // Set the state so that the first request for an id will load in a block of ids.
210
m_allocated = Integer.MAX_VALUE;
211     }
212 }
213
214
Popular Tags