Debian Codegraph @

jdbm1/src/main/java/jdbm/recman/LogicalRowIdManager.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: LogicalRowIdManager.java,v 1.3 2005/06/25 23:12:32 doomdark Exp $
46 */
47
48 package jdbm.recman;
49
50
51 import java.io.IOException;
52
53 import jdbm.I18n;
54
55
56 /**
57 * This class manages the linked lists of logical rowid pages.
58 */
59 final class LogicalRowIdManager
60 {
61 // our record recordFile and associated page manager
62 private RecordFile recordFile;
63 private PageManager pageManager;
64 private FreeLogicalRowIdPageManager freeman;
65
66
67 /**
68 * Creates a log rowid manager using the indicated record recordFile and
69 * page manager
70 */
71 LogicalRowIdManager( PageManager pageManager ) throws IOException
72 {
73 this.recordFile = pageManager.getRecordFile();
74 this.pageManager = pageManager;
75 this.freeman = new FreeLogicalRowIdPageManager( pageManager );
76 }
77
78
79 /**
80 * Creates a new logical rowid pointing to the indicated physical
81 * id
82 */
83 Location insert( Location loc ) throws IOException
84 {
85 // check whether there's a free rowid to reuse
86 Location retval = freeman.get();
87 if ( retval == null )
88 {
89 // no. This means that we bootstrap things by allocating
90 // a new translation page and freeing all the rowids on it.
91 long firstPage = pageManager.allocate( Magic.TRANSLATION_PAGE );
92 short curOffset = TranslationPage.O_TRANS;
93 for ( int i = 0; i < TranslationPage.ELEMS_PER_PAGE; i++ )
94 {
95 freeman.put( new Location( firstPage, curOffset ) );
96 curOffset += PhysicalRowId.SIZE;
97 }
98 retval = freeman.get();
99 if ( retval == null )
100 {
101 throw new Error( I18n.err( I18n.ERR_545 ) );
102 }
103 }
104 // write the translation.
105 update( retval, loc );
106 return retval;
107 }
108
109
110 /**
111 * Releases the indicated logical rowid.
112 */
113 void delete( Location rowid ) throws IOException
114 {
115
116 freeman.put( rowid );
117 }
118
119
120 /**
121 * Updates the mapping
122 *
123 * @param rowid The logical rowid
124 * @param loc The physical rowid
125 */
126 void update( Location rowid, Location loc ) throws IOException
127 {
128
129 TranslationPage xlatPage = TranslationPage.getTranslationPageView( recordFile.get( rowid.getBlock() ) );
130 PhysicalRowId physid = xlatPage.get( rowid.getOffset() );
131 physid.setBlock( loc.getBlock() );
132 physid.setOffset( loc.getOffset() );
133 recordFile.release( rowid.getBlock(), true );
134 }
135
136
137 /**
138 * Returns a mapping
139 *
140 * @param rowid The logical rowid
141 * @return The physical rowid
142 */
143 Location fetch( Location rowid ) throws IOException
144 {
145
146 TranslationPage xlatPage = TranslationPage.getTranslationPageView( recordFile.get( rowid.getBlock() ) );
147 try
148 {
149 Location retval = new Location( xlatPage.get( rowid.getOffset() ) );
150 return retval;
151 }
152 finally
153 {
154 recordFile.release( rowid.getBlock(), false );
155 }
156 }
157
158 }