Debian Codegraph @

jdbm1/src/main/java/jdbm/recman/PhysicalRowIdManager.java

raw
1 /**
2 * JDBM LICENSE v1.00
3 *
4 * Redistribution and use of this software and associated documentation
5 * ("Software"), with or without modification, are permitted provided
6 * that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain copyright
9 * statements and notices. Redistributions must also contain a
10 * copy of this document.
11 *
12 * 2. Redistributions in binary form must reproduce the
13 * above copyright notice, this list of conditions and the
14 * following disclaimer in the documentation and/or other
15 * materials provided with the distribution.
16 *
17 * 3. The name "JDBM" must not be used to endorse or promote
18 * products derived from this Software without prior written
19 * permission of Cees de Groot. For written permission,
20 * please contact [email protected].
21 *
22 * 4. Products derived from this Software may not be called "JDBM"
23 * nor may "JDBM" appear in their names without prior written
24 * permission of Cees de Groot.
25 *
26 * 5. Due credit should be given to the JDBM Project
27 * (http://jdbm.sourceforge.net/).
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
31 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
32 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
33 * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
34 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
40 * OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * Copyright 2000 (C) Cees de Groot. All Rights Reserved.
43 * Contributions are Copyright (C) 2000 by their associated contributors.
44 *
45 * $Id: PhysicalRowIdManager.java,v 1.3 2003/03/21 03:00:09 boisvert Exp $
46 */
47 package jdbm.recman;
48
49
50 import java.io.IOException;
51
52
53 /**
54 * This class manages physical row ids, and their data.
55 */
56 final class PhysicalRowIdManager
57 {
58 // The file we're talking to and the associated page manager.
59 private RecordFile file;
60 private PageManager pageManager;
61 private FreePhysicalRowIdPageManager freeman;
62
63 /**
64 * Creates a new rowid manager using the indicated record file.
65 * and page manager.
66 */
67 PhysicalRowIdManager( PageManager pageManager )
68 throws IOException
69 {
70 this.pageManager = pageManager;
71 this.file = pageManager.getRecordFile();
72 this.freeman = new FreePhysicalRowIdPageManager( pageManager );
73 }
74
75 /**
76 * Inserts a new record. Returns the new physical rowid.
77 */
78 Location insert( byte[] data, int start, int length )
79 throws IOException
80 {
81 Location retval = alloc( length );
82 write( retval, data, start, length );
83 return retval;
84 }
85
86 /**
87 * Updates an existing record. Returns the possibly changed
88 * physical rowid.
89 */
90 Location update( Location rowid, byte[] data, int start, int length )
91 throws IOException
92 {
93 // fetch the record header
94 BlockIo block = pageManager.getRecordFile().get( rowid.getBlock() );
95 RecordHeader head = new RecordHeader( block, rowid.getOffset() );
96
97 if ( length > head.getAvailableSize() ) {
98 // not enough space - we need to copy to a new rowid.
99 pageManager.getRecordFile().release( block );
100 free( rowid );
101 rowid = alloc( length );
102 } else {
103 pageManager.getRecordFile().release( block );
104 }
105
106 // 'nuff space, write it in and return the rowid.
107 write( rowid, data, start, length );
108 return rowid;
109 }
110
111 /**
112 * Deletes a record.
113 */
114 void delete( Location rowid )
115 throws IOException
116 {
117 free( rowid );
118 }
119
120 /**
121 * Retrieves a record.
122 */
123 byte[] fetch( Location rowid )
124 throws IOException
125 {
126 // fetch the record header
127 PageCursor curs = new PageCursor( pageManager, rowid.getBlock() );
128 BlockIo block = file.get( curs.getCurrent() );
129 RecordHeader head = new RecordHeader( block, rowid.getOffset() );
130
131 // allocate a return buffer
132 byte[] retval = new byte[ head.getCurrentSize() ];
133 if ( retval.length == 0 ) {
134 file.release( curs.getCurrent(), false );
135 return retval;
136 }
137
138 // copy bytes in
139 int offsetInBuffer = 0;
140 int leftToRead = retval.length;
141 short dataOffset = (short) (rowid.getOffset() + RecordHeader.SIZE);
142 while ( leftToRead > 0 ) {
143 // copy current page's data to return buffer
144 int toCopy = RecordFile.BLOCK_SIZE - dataOffset;
145 if ( leftToRead < toCopy ) {
146 toCopy = leftToRead;
147 }
148 System.arraycopy( block.getData(), dataOffset,
149 retval, offsetInBuffer,
150 toCopy );
151
152 // Go to the next block
153 leftToRead -= toCopy;
154 offsetInBuffer += toCopy;
155
156 file.release( block );
157
158 if ( leftToRead > 0 ) {
159 block = file.get( curs.next() );
160 dataOffset = DataPage.O_DATA;
161 }
162
163 }
164
165 return retval;
166 }
167
168 /**
169 * Allocate a new rowid with the indicated size.
170 */
171 private Location alloc( int size )
172 throws IOException
173 {
174 Location retval = freeman.get( size );
175 if ( retval == null ) {
176 // temporary work around for DIRSERVER-1459
177 retval = allocNew( size * 2, pageManager.getLast( Magic.USED_PAGE ) );
178 }
179 return retval;
180 }
181
182 /**
183 * Allocates a new rowid. The second parameter is there to
184 * allow for a recursive call - it indicates where the search
185 * should start.
186 */
187 private Location allocNew( int size, long start )
188 throws IOException
189 {
190 BlockIo curBlock;
191 DataPage curPage;
192 if ( start == 0 ) {
193 // we need to create a new page.
194 start = pageManager.allocate( Magic.USED_PAGE );
195 curBlock = file.get( start );
196 curPage = DataPage.getDataPageView( curBlock );
197 curPage.setFirst( DataPage.O_DATA );
198 RecordHeader hdr = new RecordHeader( curBlock, DataPage.O_DATA );
199 hdr.setAvailableSize( 0 );
200 hdr.setCurrentSize( 0 );
201 } else {
202 curBlock = file.get( start );
203 curPage = DataPage.getDataPageView( curBlock );
204 }
205
206 // follow the rowids on this page to get to the last one. We don't
207 // fall off, because this is the last page, remember?
208 short pos = curPage.getFirst();
209 if ( pos == 0 ) {
210 // page is exactly filled by the last block of a record
211 file.release( curBlock );
212 return allocNew( size, 0 );
213 }
214
215 RecordHeader hdr = new RecordHeader( curBlock, pos );
216 while ( hdr.getAvailableSize() != 0 && pos < RecordFile.BLOCK_SIZE ) {
217 pos += hdr.getAvailableSize() + RecordHeader.SIZE;
218 if ( pos == RecordFile.BLOCK_SIZE ) {
219 // Again, a filled page.
220 file.release( curBlock );
221 return allocNew( size, 0 );
222 }
223
224 hdr = new RecordHeader( curBlock, pos );
225 }
226
227 if ( pos == RecordHeader.SIZE ) {
228 // the last record exactly filled the page. Restart forcing
229 // a new page.
230 file.release( curBlock );
231 }
232
233 // we have the position, now tack on extra pages until we've got
234 // enough space.
235 Location retval = new Location( start, pos );
236 int freeHere = RecordFile.BLOCK_SIZE - pos - RecordHeader.SIZE;
237 if ( freeHere < size ) {
238 // check whether the last page would have only a small bit left.
239 // if yes, increase the allocation. A small bit is a record
240 // header plus 16 bytes.
241 int lastSize = (size - freeHere) % DataPage.DATA_PER_PAGE;
242 if (( DataPage.DATA_PER_PAGE - lastSize ) < (RecordHeader.SIZE + 16) ) {
243 size += (DataPage.DATA_PER_PAGE - lastSize);
244 }
245
246 // write out the header now so we don't have to come back.
247 hdr.setAvailableSize( size );
248 file.release( start, true );
249
250 int neededLeft = size - freeHere;
251 // Refactor these two blocks!
252 while ( neededLeft >= DataPage.DATA_PER_PAGE ) {
253 start = pageManager.allocate( Magic.USED_PAGE );
254 curBlock = file.get( start );
255 curPage = DataPage.getDataPageView( curBlock );
256 curPage.setFirst( (short) 0 ); // no rowids, just data
257 file.release( start, true );
258 neededLeft -= DataPage.DATA_PER_PAGE;
259 }
260 if ( neededLeft > 0 ) {
261 // done with whole chunks, allocate last fragment.
262 start = pageManager.allocate( Magic.USED_PAGE );
263 curBlock = file.get( start );
264 curPage = DataPage.getDataPageView( curBlock );
265 curPage.setFirst( (short) (DataPage.O_DATA + neededLeft) );
266 file.release( start, true );
267 }
268 } else {
269 // just update the current page. If there's less than 16 bytes
270 // left, we increase the allocation (16 bytes is an arbitrary
271 // number).
272 if ( freeHere - size <= (16 + RecordHeader.SIZE) ) {
273 size = freeHere;
274 }
275 hdr.setAvailableSize( size );
276 file.release( start, true );
277 }
278 return retval;
279
280 }
281
282
283 private void free( Location id )
284 throws IOException
285 {
286 // get the rowid, and write a zero current size into it.
287 BlockIo curBlock = file.get( id.getBlock() );
288 RecordHeader hdr = new RecordHeader( curBlock, id.getOffset() );
289 hdr.setCurrentSize( 0 );
290 file.release( id.getBlock(), true );
291
292 // write the rowid to the free list
293 freeman.put( id, hdr.getAvailableSize() );
294 }
295
296 /**
297 * Writes out data to a rowid. Assumes that any resizing has been
298 * done.
299 */
300 private void write(Location rowid, byte[] data, int start, int length )
301 throws IOException
302 {
303 PageCursor curs = new PageCursor( pageManager, rowid.getBlock() );
304 BlockIo block = file.get( curs.getCurrent() );
305 RecordHeader hdr = new RecordHeader( block, rowid.getOffset() );
306 hdr.setCurrentSize( length );
307 if ( length == 0 ) {
308 file.release( curs.getCurrent(), true );
309 return;
310 }
311
312 // copy bytes in
313 int offsetInBuffer = start;
314 int leftToWrite = length;
315 short dataOffset = (short) (rowid.getOffset() + RecordHeader.SIZE);
316 while ( leftToWrite > 0 ) {
317 // copy current page's data to return buffer
318 int toCopy = RecordFile.BLOCK_SIZE - dataOffset;
319
320 if ( leftToWrite < toCopy ) {
321 toCopy = leftToWrite;
322 }
323 System.arraycopy( data, offsetInBuffer, block.getData(),
324 dataOffset, toCopy );
325
326 // Go to the next block
327 leftToWrite -= toCopy;
328 offsetInBuffer += toCopy;
329
330 file.release( curs.getCurrent(), true );
331
332 if ( leftToWrite > 0 ) {
333 block = file.get( curs.next() );
334 dataOffset = DataPage.O_DATA;
335 }
336 }
337 }
338 }
339