~ubuntu-branches/ubuntu/maverick/hsqldb/maverick

« back to all changes in this revision

Viewing changes to src/org/hsqldb/persist/ScaledRAFileHybrid.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-09-26 11:47:49 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060926114749-3jd0utm7w21x1iqt
Tags: 1.8.0.5-2ubuntu1
* Synchronise with Debian unstable; remaining changes:
  - build using java-gcj-compat.
* libhsqldb-java: Add gij as alternative dependency.
* Build a libhsqldb-java-gcj package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2001-2005, The HSQL Development Group
 
2
 * All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 *
 
7
 * Redistributions of source code must retain the above copyright notice, this
 
8
 * list of conditions and the following disclaimer.
 
9
 *
 
10
 * Redistributions in binary form must reproduce the above copyright notice,
 
11
 * this list of conditions and the following disclaimer in the documentation
 
12
 * and/or other materials provided with the distribution.
 
13
 *
 
14
 * Neither the name of the HSQL Development Group nor the names of its
 
15
 * contributors may be used to endorse or promote products derived from this
 
16
 * software without specific prior written permission.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
19
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
21
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
 
22
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
23
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
24
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
25
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 
26
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
28
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 */
 
30
 
 
31
 
 
32
package org.hsqldb.persist;
 
33
 
 
34
import java.io.IOException;
 
35
 
 
36
import org.hsqldb.Database;
 
37
 
 
38
/**
 
39
 * Mixe NIO / non-NIO version of ScaledRAFile.
 
40
 * This class is used only for storing a CACHED
 
41
 * TABLE .data file and cannot be used for TEXT TABLE source files.
 
42
 *
 
43
 * Due to various issues with java.nio classes, this class will use a mapped
 
44
 * channel of fixed size. After reaching this size, the file and channel are
 
45
 * closed and a new one opened, up to the maximum size.
 
46
 *
 
47
 * @author fredt@users
 
48
 * @version  1.8.0.5
 
49
 * @since 1.7.2
 
50
 */
 
51
public class ScaledRAFileHybrid implements ScaledRAInterface {
 
52
 
 
53
    final Database    database;
 
54
    final String      fileName;
 
55
    final boolean     isReadOnly;
 
56
    long              maxLength;
 
57
    ScaledRAInterface store;
 
58
 
 
59
    public ScaledRAFileHybrid(Database database, String name,
 
60
                              boolean readOnly) throws IOException {
 
61
 
 
62
        this.database   = database;
 
63
        this.fileName   = name;
 
64
        this.isReadOnly = readOnly;
 
65
 
 
66
        newStore(0);
 
67
    }
 
68
 
 
69
    public long length() throws IOException {
 
70
        return store.length();
 
71
    }
 
72
 
 
73
    public void seek(long position) throws IOException {
 
74
        checkSeek(position);
 
75
        store.seek(position);
 
76
    }
 
77
 
 
78
    public long getFilePointer() throws IOException {
 
79
        return store.getFilePointer();
 
80
    }
 
81
 
 
82
    public int read() throws IOException {
 
83
 
 
84
        checkLength(1);
 
85
 
 
86
        return store.read();
 
87
    }
 
88
 
 
89
    public void read(byte[] b, int offset, int length) throws IOException {
 
90
        checkLength(length);
 
91
        store.read(b, offset, length);
 
92
    }
 
93
 
 
94
    public void write(byte[] b, int offset, int length) throws IOException {
 
95
        checkLength(length);
 
96
        store.write(b, offset, length);
 
97
    }
 
98
 
 
99
    public int readInt() throws IOException {
 
100
 
 
101
        checkLength(4);
 
102
 
 
103
        return store.readInt();
 
104
    }
 
105
 
 
106
    public void writeInt(int i) throws IOException {
 
107
        checkLength(4);
 
108
        store.writeInt(i);
 
109
    }
 
110
 
 
111
    public long readLong() throws IOException {
 
112
 
 
113
        checkLength(8);
 
114
 
 
115
        return store.readLong();
 
116
    }
 
117
 
 
118
    public void writeLong(long i) throws IOException {
 
119
        checkLength(8);
 
120
        store.writeLong(i);
 
121
    }
 
122
 
 
123
    public void close() throws IOException {
 
124
        store.close();
 
125
    }
 
126
 
 
127
    public boolean isReadOnly() {
 
128
        return store.isReadOnly();
 
129
    }
 
130
 
 
131
    public boolean wasNio() {
 
132
        return true;
 
133
    }
 
134
 
 
135
    public boolean canAccess(int length) {
 
136
        return true;
 
137
    }
 
138
 
 
139
    public boolean canSeek(long position) {
 
140
        return true;
 
141
    }
 
142
 
 
143
    public Database getDatabase() {
 
144
        return null;
 
145
    }
 
146
 
 
147
    private void checkLength(int length) throws IOException {
 
148
 
 
149
        if (store.canAccess(length)) {
 
150
            return;
 
151
        }
 
152
 
 
153
        newStore(store.getFilePointer() + length);
 
154
    }
 
155
 
 
156
    private void checkSeek(long position) throws IOException {
 
157
 
 
158
        if (store.canSeek(position)) {
 
159
            return;
 
160
        }
 
161
 
 
162
        newStore(position);
 
163
    }
 
164
 
 
165
    void newStore(long requiredSize) throws IOException {
 
166
 
 
167
        long currentPosition = 0;
 
168
 
 
169
        if (store != null) {
 
170
            currentPosition = store.getFilePointer();
 
171
 
 
172
            store.close();
 
173
        }
 
174
 
 
175
        if (requiredSize <= ScaledRAFile.MAX_NIO_LENGTH) {
 
176
            try {
 
177
                store = new ScaledRAFileNIO(database, fileName, isReadOnly,
 
178
                                            (int) requiredSize);
 
179
 
 
180
                store.seek(currentPosition);
 
181
 
 
182
                return;
 
183
            } catch (Throwable e) {}
 
184
        }
 
185
 
 
186
        store = new ScaledRAFile(database, fileName, isReadOnly);
 
187
 
 
188
        store.seek(currentPosition);
 
189
    }
 
190
}