001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.io;
022:
023: import com.db4o.*;
024:
025: /**
026: * Base class for database file adapters, both for file and memory databases.
027: */
028: public abstract class IoAdapter {
029:
030: private static final int COPY_SIZE = 4096;
031:
032: private int _blockSize;
033:
034: /**
035: * converts address and address offset to an absolute address
036: */
037: protected final long regularAddress(int blockAddress,
038: int blockAddressOffset) {
039: if (0 == _blockSize) {
040: throw new IllegalStateException();
041: }
042: return (long) blockAddress * _blockSize + blockAddressOffset;
043: }
044:
045: /**
046: * copies a block within a file in block mode
047: */
048: public void blockCopy(int oldAddress, int oldAddressOffset,
049: int newAddress, int newAddressOffset, int length)
050: throws Db4oIOException {
051: copy(regularAddress(oldAddress, oldAddressOffset),
052: regularAddress(newAddress, newAddressOffset), length);
053: }
054:
055: /**
056: * sets the read/write pointer in the file using block mode
057: */
058: public void blockSeek(int address) throws Db4oIOException {
059: blockSeek(address, 0);
060: }
061:
062: /**
063: * sets the read/write pointer in the file using block mode
064: */
065: public void blockSeek(int address, int offset)
066: throws Db4oIOException {
067: seek(regularAddress(address, offset));
068: }
069:
070: /**
071: * outside call to set the block size of this adapter
072: */
073: public void blockSize(int blockSize) {
074: if (blockSize < 1) {
075: throw new IllegalArgumentException();
076: }
077: _blockSize = blockSize;
078: }
079:
080: /**
081: * implement to close the adapter
082: */
083: public abstract void close() throws Db4oIOException;
084:
085: /**
086: * copies a block within a file in absolute mode
087: */
088: public void copy(long oldAddress, long newAddress, int length)
089: throws Db4oIOException {
090:
091: if (DTrace.enabled) {
092: DTrace.IO_COPY.logLength(newAddress, length);
093: }
094:
095: if (length > COPY_SIZE) {
096: byte[] buffer = new byte[COPY_SIZE];
097: int pos = 0;
098: while (pos + COPY_SIZE < length) {
099: copy(buffer, oldAddress + pos, newAddress + pos);
100: pos += COPY_SIZE;
101: }
102: oldAddress += pos;
103: newAddress += pos;
104: length -= pos;
105: }
106:
107: copy(new byte[length], oldAddress, newAddress);
108: }
109:
110: private void copy(byte[] buffer, long oldAddress, long newAddress)
111: throws Db4oIOException {
112: seek(oldAddress);
113: read(buffer);
114: seek(newAddress);
115: write(buffer);
116: }
117:
118: /**
119: * deletes the given path from whatever 'file system' is addressed
120: */
121: public abstract void delete(String path);
122:
123: /**
124: * checks whether a file exists
125: */
126: public abstract boolean exists(String path);
127:
128: /**
129: * implement to return the absolute length of the file
130: */
131: public abstract long getLength() throws Db4oIOException;
132:
133: /**
134: * implement to open the file
135: */
136: public abstract IoAdapter open(String path, boolean lockFile,
137: long initialLength, boolean readOnly)
138: throws Db4oIOException;
139:
140: /**
141: * reads a buffer at the seeked address
142: *
143: * @return the number of bytes read and returned
144: */
145: public int read(byte[] buffer) throws Db4oIOException {
146: return read(buffer, buffer.length);
147: }
148:
149: /**
150: * implement to read a buffer at the seeked address
151: */
152: public abstract int read(byte[] bytes, int length)
153: throws Db4oIOException;
154:
155: /**
156: * implement to set the read/write pointer in the file, absolute mode
157: */
158: public abstract void seek(long pos) throws Db4oIOException;
159:
160: /**
161: * implement to flush the file contents to storage
162: */
163: public abstract void sync() throws Db4oIOException;
164:
165: /**
166: * writes a buffer to the seeked address
167: */
168: public void write(byte[] bytes) throws Db4oIOException {
169: write(bytes, bytes.length);
170: }
171:
172: /**
173: * implement to write a buffer at the seeked address
174: */
175: public abstract void write(byte[] buffer, int length)
176: throws Db4oIOException;
177:
178: /**
179: * returns the block size currently used
180: */
181: public int blockSize() {
182: return _blockSize;
183: }
184:
185: /**
186: * Delegated IO Adapter
187: * @return reference to itself
188: */
189: public IoAdapter delegatedIoAdapter() {
190: return this;
191: }
192: }
|