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.ext;
022:
023: /**
024: * carries in-memory data for db4o in-memory operation.
025: * <br><br>In-memory ObjectContainers are useful for maximum performance
026: * on small databases, for swapping objects or for storing db4o format data
027: * to other media or other databases.<br><br>Be aware of the danger of running
028: * into OutOfMemory problems or complete loss of all data, in case of hardware
029: * or JVM failures.
030: * <br><br>
031: * @see ExtDb4o#openMemoryFile
032: */
033: public class MemoryFile {
034:
035: private byte[] i_bytes;
036: private final static int INITIAL_SIZE_AND_INC = 1024 * 64;
037: private int i_initialSize = INITIAL_SIZE_AND_INC;
038: private int i_incrementSizeBy = INITIAL_SIZE_AND_INC;
039:
040: /**
041: * constructs a new MemoryFile without any data.
042: * @see ExtDb4o#openMemoryFile
043: */
044: public MemoryFile() {
045: }
046:
047: /**
048: * constructs a MemoryFile to use the byte data from a previous
049: * MemoryFile.
050: * @param bytes the raw byte data.
051: * @see ExtDb4o#openMemoryFile
052: */
053: public MemoryFile(byte[] bytes) {
054: i_bytes = bytes;
055: }
056:
057: /**
058: * returns the raw byte data.
059: * <br><br>Use this method to get the byte data from the MemoryFile
060: * to store it to other media or databases, for backup purposes or
061: * to create other MemoryFile sessions.
062: * <br><br>The byte data from a MemoryFile should only be used
063: * after it is closed.<br><br>
064: * @return bytes the raw byte data.
065: */
066: public byte[] getBytes() {
067: if (i_bytes == null) {
068: return new byte[0];
069: }
070: return i_bytes;
071: }
072:
073: /**
074: * returns the size the MemoryFile is to be enlarged, if it grows beyond
075: * the current size.
076: * @return size in bytes
077: */
078: public int getIncrementSizeBy() {
079: return i_incrementSizeBy;
080: }
081:
082: /**
083: * returns the initial size of the MemoryFile.
084: * @return size in bytes
085: */
086: public int getInitialSize() {
087: return i_initialSize;
088: }
089:
090: /**
091: * sets the raw byte data.
092: * <br><br><b>Caution!</b><br>Calling this method during a running
093: * Memory File session may produce unpreditable results.
094: * @param bytes the raw byte data.
095: */
096: public void setBytes(byte[] bytes) {
097: i_bytes = bytes;
098: }
099:
100: /**
101: * configures the size the MemoryFile is to be enlarged by, if it grows
102: * beyond the current size.
103: * <br><br>Call this method before passing the MemoryFile to
104: * {@link com.db4o.ext.ExtDb4o#openMemoryFile(MemoryFile)}.
105: * <br><br>
106: * This parameter can be modified to tune the maximum performance of
107: * a MemoryFile for a specific usecase. To produce the best results,
108: * test the speed of your application with real data.<br><br>
109: * @param byteCount the desired size in bytes
110: */
111: public void setIncrementSizeBy(int byteCount) {
112: i_incrementSizeBy = byteCount;
113: }
114:
115: /**
116: * configures the initial size of the MemoryFile.
117: * <br><br>Call this method before passing the MemoryFile to
118: * {@link com.db4o.ext.ExtDb4o#openMemoryFile(MemoryFile)}.
119: * <br><br>
120: * This parameter can be modified to tune the maximum performance of
121: * a MemoryFile for a specific usecase. To produce the best results,
122: * test speed and memory consumption of your application with
123: * real data.<br><br>
124: * @param byteCount the desired size in bytes
125: */
126: public void setInitialSize(int byteCount) {
127: i_initialSize = byteCount;
128: }
129: }
|