1
/* Copyright (c) 2001-2005, The HSQL Development Group
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* Redistributions of source code must retain the above copyright notice, this
8
* list of conditions and the following disclaimer.
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.
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.
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.
32
package org.hsqldb.persist;
34
import java.io.IOException;
36
import org.hsqldb.Database;
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.
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.
51
public class ScaledRAFileHybrid implements ScaledRAInterface {
53
final Database database;
54
final String fileName;
55
final boolean isReadOnly;
57
ScaledRAInterface store;
59
public ScaledRAFileHybrid(Database database, String name,
60
boolean readOnly) throws IOException {
62
this.database = database;
64
this.isReadOnly = readOnly;
69
public long length() throws IOException {
70
return store.length();
73
public void seek(long position) throws IOException {
78
public long getFilePointer() throws IOException {
79
return store.getFilePointer();
82
public int read() throws IOException {
89
public void read(byte[] b, int offset, int length) throws IOException {
91
store.read(b, offset, length);
94
public void write(byte[] b, int offset, int length) throws IOException {
96
store.write(b, offset, length);
99
public int readInt() throws IOException {
103
return store.readInt();
106
public void writeInt(int i) throws IOException {
111
public long readLong() throws IOException {
115
return store.readLong();
118
public void writeLong(long i) throws IOException {
123
public void close() throws IOException {
127
public boolean isReadOnly() {
128
return store.isReadOnly();
131
public boolean wasNio() {
135
public boolean canAccess(int length) {
139
public boolean canSeek(long position) {
143
public Database getDatabase() {
147
private void checkLength(int length) throws IOException {
149
if (store.canAccess(length)) {
153
newStore(store.getFilePointer() + length);
156
private void checkSeek(long position) throws IOException {
158
if (store.canSeek(position)) {
165
void newStore(long requiredSize) throws IOException {
167
long currentPosition = 0;
170
currentPosition = store.getFilePointer();
175
if (requiredSize <= ScaledRAFile.MAX_NIO_LENGTH) {
177
store = new ScaledRAFileNIO(database, fileName, isReadOnly,
180
store.seek(currentPosition);
183
} catch (Throwable e) {}
186
store = new ScaledRAFile(database, fileName, isReadOnly);
188
store.seek(currentPosition);