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.internal;
022:
023: import com.db4o.config.*;
024: import com.db4o.ext.*;
025: import com.db4o.foundation.*;
026:
027: /**
028: * @exclude
029: */
030: public class InMemoryObjectContainer extends LocalObjectContainer {
031:
032: private boolean _closed = false;
033: private final MemoryFile _memoryFile;
034: private int _length = 0;
035:
036: protected InMemoryObjectContainer(Configuration config,
037: ObjectContainerBase parent, MemoryFile memoryFile)
038: throws OldFormatException {
039: super (config, parent);
040: _memoryFile = memoryFile;
041: open();
042: }
043:
044: public InMemoryObjectContainer(Configuration config,
045: MemoryFile memoryFile) {
046: this (config, null, memoryFile);
047: }
048:
049: protected final void openImpl() throws OldFormatException {
050: byte[] bytes = _memoryFile.getBytes();
051: if (bytes == null || bytes.length == 0) {
052: _memoryFile
053: .setBytes(new byte[_memoryFile.getInitialSize()]);
054: configureNewFile();
055: commitTransaction();
056: writeHeader(false, false);
057: } else {
058: _length = bytes.length;
059: readThis();
060: }
061: }
062:
063: public void backup(String path) throws NotSupportedException {
064: throw new NotSupportedException();
065: }
066:
067: public void blockSize(int size) {
068: // do nothing, blocksize is always 1
069: }
070:
071: protected void freeInternalResources() {
072: // nothing to do here
073: }
074:
075: protected void shutdownDataStorage() {
076: if (!_closed) {
077: byte[] temp = new byte[_length];
078: System.arraycopy(_memoryFile.getBytes(), 0, temp, 0,
079: _length);
080: _memoryFile.setBytes(temp);
081: }
082: _closed = true;
083: dropReferences();
084: }
085:
086: protected void dropReferences() {
087: // do nothing
088: }
089:
090: public void copy(int oldAddress, int oldAddressOffset,
091: int newAddress, int newAddressOffset, int length) {
092: int fullNewAddress = newAddress + newAddressOffset;
093: ensureMemoryFileSize(fullNewAddress + length);
094: byte[] bytes = _memoryFile.getBytes();
095: System.arraycopy(bytes, oldAddress + oldAddressOffset, bytes,
096: fullNewAddress, length);
097: }
098:
099: public long fileLength() {
100: return _length;
101: }
102:
103: public String fileName() {
104: return "Memory File";
105: }
106:
107: protected boolean hasShutDownHook() {
108: return false;
109: }
110:
111: public final boolean needsLockFileThread() {
112: return false;
113: }
114:
115: public void readBytes(byte[] bytes, int address, int length) {
116: try {
117: System.arraycopy(_memoryFile.getBytes(), address, bytes, 0,
118: length);
119: } catch (Exception e) {
120: Exceptions4.throwRuntimeException(13, e);
121: }
122: }
123:
124: public void readBytes(byte[] bytes, int address, int addressOffset,
125: int length) {
126: readBytes(bytes, address + addressOffset, length);
127: }
128:
129: public void syncFiles() {
130: }
131:
132: public void writeBytes(Buffer bytes, int address, int addressOffset) {
133: int fullAddress = address + addressOffset;
134: int length = bytes.length();
135: ensureMemoryFileSize(fullAddress + length);
136: System.arraycopy(bytes._buffer, 0, _memoryFile.getBytes(),
137: fullAddress, length);
138: }
139:
140: private void ensureMemoryFileSize(int last) {
141: if (last < _length)
142: return;
143:
144: byte[] bytes = _memoryFile.getBytes();
145: if (last < bytes.length) {
146: _length = last;
147: return;
148: }
149:
150: int increment = _memoryFile.getIncrementSizeBy();
151: while (last > increment) {
152: increment <<= 1;
153: }
154:
155: byte[] newBytes = new byte[bytes.length + increment];
156: System.arraycopy(bytes, 0, newBytes, 0, bytes.length);
157: _memoryFile.setBytes(newBytes);
158: _length = newBytes.length;
159: }
160:
161: public void overwriteDeletedBytes(int a_address, int a_length) {
162: }
163:
164: public void reserve(int byteCount) {
165: throw new NotSupportedException();
166: }
167:
168: public byte blockSize() {
169: return 1;
170: }
171: }
|