[Joafip-svn] SF.net SVN: joafip:[3012] trunk
Brought to you by:
luc_peuvrier
|
From: <luc...@us...> - 2012-04-17 05:06:03
|
Revision: 3012
http://joafip.svn.sourceforge.net/joafip/?rev=3012&view=rev
Author: luc_peuvrier
Date: 2012-04-17 05:05:53 +0000 (Tue, 17 Apr 2012)
Log Message:
-----------
for for storable management moved to joafip-kvstore project
Modified Paths:
--------------
trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/HeapHeader.java
trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/HeapRecord.java
trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/HeapElementManager.java
trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/IHeapElementManager.java
trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/service/HeapFileDataManager.java
trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/TestIdNode.java
trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/entity/TestAbstractFileStorable.java
trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/entity/TestMarshall.java
trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestHeapElementManager.java
trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestHeapRecordManage.java
trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestStoreRestore.java
trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/FileForStorableBridge.java
Added Paths:
-----------
trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/entity/AbstractFileStorable.java
trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/entity/IFileStorable.java
trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/entity/ToBackupRecord.java
trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/service/FileForStorable.java
trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/service/IFileForStorable.java
Removed Paths:
-------------
trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/entity/AbstractFileStorable.java
trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/entity/IFileStorable.java
trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/ToBackupRecord.java
trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/service/FileForStorable.java
trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/service/IFileForStorable.java
Deleted: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/entity/AbstractFileStorable.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/entity/AbstractFileStorable.java 2012-04-17 05:00:49 UTC (rev 3011)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/entity/AbstractFileStorable.java 2012-04-17 05:05:53 UTC (rev 3012)
@@ -1,424 +0,0 @@
-/*
- * Copyright 2007 Luc Peuvrier
- *
- * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE
- * Licensed under the LGPL License, Version 3, 29 June 2007 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.gnu.org/licenses/lgpl.html
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package net.sf.joafip.heapfile.entity;
-
-import java.util.Arrays;
-
-import net.sf.joafip.NotStorableClass;
-import net.sf.joafip.heapfile.service.FileForStorable;
-import net.sf.joafip.heapfile.service.IFileForStorable;
-import net.sf.joafip.kvstore.entity.EnumFileState;
-import net.sf.joafip.kvstore.service.HeapException;
-import net.sf.joafip.logger.JoafipLogger;
-
-/**
- * all that is common to storable in file using {@link FileForStorable}:<br>
- * <ul>
- * <li>the {@link FileForStorable} manager to use</li>
- * <li>position of data in file</li>
- * <li>flags for first creation and state change</li>
- * <li>primitive type read and write in file</li>
- * <li>crc32 compute and check for data integrity check</li>
- * </ul>
- *
- * by extension make able to create mutable or immutable object, immutable are
- * for file record unique representation, mutable make able to change the file
- * record represented<br>
- *
- * @author luc peuvrier
- *
- */
-@NotStorableClass
-public abstract class AbstractFileStorable implements IFileStorable {
-
- protected final JoafipLogger logger = JoafipLogger.getLogger(getClass());// NOPMD
-
- private static final String ALREADY_READ = "already read";
-
- private static final String RECORD_INTEGRITY_ERROR = "record integrity error";
-
- private static final String NO_VALUE_CHANGE_TO_WRITE = "no value change to write";
-
- private static final String READING_ERASE_VALUE_CHANGE = "reading erase value change";
-
- /** position in file of this in marshallized forms */
- protected final long positionInFile;
-
- /** to read/write this in file */
- protected final IFileForStorable fileForStorable;
-
- /** to compute crc32 record header */
- private transient int crc32;
-
- /** current number of byte read or wrote */
- private transient int numberOfByteReadWrote = 0;
-
- /** input output buffer */
- private transient byte[] ioBuffer;
-
- private transient int readInIoBuffer;
-
- /**
- * true if this value changed, creation implies no value to save since not
- * read and not write
- */
- protected boolean valueChangedToSave = false;
-
- /** true if just created in memory, never read from file */
- protected boolean justCreated = true;
-
- /**
- * @param fileForStorable
- * @param positionInFile
- */
- public AbstractFileStorable(final IFileForStorable fileForStorable,
- final long positionInFile) {
- super();
- this.fileForStorable = fileForStorable;
- this.positionInFile = positionInFile;
- }
-
- public void clear() {
- valueChangedToSave = false;
- justCreated = true;
- }
-
- public long getPositionInFile() {
- return positionInFile;
- }
-
- public void readFromFile() throws HeapException {
- // ASSERTX
- assert justCreated : ALREADY_READ;
- // ASSERTX
- assert !valueChangedToSave : READING_ERASE_VALUE_CHANGE;
- fileForStorable.seek(positionInFile);
- crc32 = 0;
- ioBuffer = null;// NOPMD
- unmarshallImpl();
- ioBuffer = null;// NOPMD
- justCreated = false;
- }
-
- public byte[] writeToFile() throws HeapException {
- // ASSERTX
- assert valueChangedToSave : NO_VALUE_CHANGE_TO_WRITE;
- fileForStorable.seek(positionInFile);
- crc32 = 0;
- numberOfByteReadWrote = 0;
- ioBuffer = new byte[toMarshallSize()];
- marshallImpl();
- fileForStorable.write(ioBuffer);
- valueChangedToSave = false;
- final byte[] result = ioBuffer;
- ioBuffer = null;// NOPMD discard
- return result;
- }
-
- /**
- *
- * @return number of byte to marshal
- * @throws HeapException
- */
- protected abstract int toMarshallSize() throws HeapException;
-
- /**
- * Unmarshalling implementation for read<br>
- * {@link #crc32} is set to zero<br>
- *
- * @throws HeapException
- */
- protected abstract void unmarshallImpl() throws HeapException;
-
- /**
- * marshaling implementation for write<br>
- * {@link #crc32} is set to zero<br>
- *
- * @throws HeapException
- */
- protected abstract void marshallImpl() throws HeapException;
-
- /**
- *
- * @return true if just created ( not read from file and not setted )
- */
- public boolean isJustCreated() {
- return justCreated;
- }
-
- /**
- * to know if the value state change
- *
- * @return true if value changed
- */
- public boolean isValueChangedToSave() {
- return valueChangedToSave;
- }
-
- /**
- * set this record have value changed, so to save
- *
- * @throws HeapException
- */
- public void setValueIsChangedValueToSave() throws HeapException {
- if (!valueChangedToSave) {
- valueChangedToSave = true;
- justCreated = false;
- valueChangedAction();
- }
- }
-
- protected abstract void valueChangedAction() throws HeapException;
-
- public void setValueIsNotChanged() {
- valueChangedToSave = false;
- }
-
- /**
- * update crc32 with byte value in buffer
- *
- * @param buffer
- * bytes for crc32 update
- */
- private void updateCrc32(final byte[] buffer) {
- updateCrc32(buffer, 0, buffer.length);
- }
-
- private void updateCrc32(final byte[] buffer, final int start,
- final int length) {
-
- for (int i = 0; i < length; i++) {
- final byte byteValue = buffer[start + i];
- updateCrc32(byteValue);
- }
-
- }
-
- private void updateCrc32(final byte byteValue) {
- for (int j = 0; j < 8; j++) {
- final int testbit = ((crc32 >> 31) & 1) ^ ((byteValue >> j) & 1);
- crc32 <<= 2;
- if (testbit != 0) {
- crc32 ^= 0x8005; /* 32 bit crc */
- }
- }
- }
-
- protected void clearCrc32() {
- crc32 = 0;
- }
-
- protected int getCrc32() {
- return crc32;
- }
-
- protected int readFileToIoBuffer(final int size) throws HeapException {
- ioBuffer = new byte[size];
- readInIoBuffer = fileForStorable.read(ioBuffer);
- numberOfByteReadWrote = 0;
- return readInIoBuffer;
- }
-
- /**
- * read extending ioBuffer
- *
- * @param newSize
- * ioBuffer new size
- * @throws HeapException
- */
- protected void readFileAppendToIoBuffer(final int newSize)
- throws HeapException {
- if (newSize > ioBuffer.length) {
- ioBuffer = Arrays.copyOf(ioBuffer, newSize);
- final int appendSize = newSize - readInIoBuffer;
- final byte[] tempBuffer = new byte[appendSize];
- // FIXMELUC _________________use of additional read
- // read(iobuffer,readInIoBuffer/*offset*/,appendSize/*length*/);
- fileForStorable.read(tempBuffer);
- System.arraycopy(tempBuffer, 0, ioBuffer, readInIoBuffer,
- appendSize);
- }
- }
-
- /**
- * read and check CRC32
- *
- * @throws HeapException
- * read error crc32 error in record
- */
- protected void readAndCheckCrc32() throws HeapException {
- final int computedCrc32 = crc32;
- final int readCrc32 = readInteger();
- if (computedCrc32 != readCrc32) {
- throw new HeapException(RECORD_INTEGRITY_ERROR
- + " record position=" + positionInFile + " read="
- + readCrc32 + " computed=" + computedCrc32 + " nb rd/wr="
- + numberOfByteReadWrote, EnumFileState.STATE_CORRUPTED);
- }
- }
-
- /**
- * read bytes
- *
- * @param length
- * number of bytes to read
- * @return the bytes read
- * @throws HeapException
- */
- protected byte[] readBytes(final int length) throws HeapException {
- final byte[] dataByteArray = new byte[length];
- System.arraycopy(ioBuffer, numberOfByteReadWrote, dataByteArray, 0,
- length);
- numberOfByteReadWrote += length;
- updateCrc32(dataByteArray);
- return dataByteArray;
- }
-
- /**
- * unmarshal from byte array current {@link #dataByteArrayIndex} index to
- * long<br>
- * {@link #dataByteArrayIndex} is updated to next data to read position<br>
- *
- * @param dataByteArray
- * @return the unmarshaled value
- * @throws HeapException
- */
- protected long readLong() throws HeapException {
- long value = 0;
- for (int byteIndex = 7; byteIndex >= 0; byteIndex--) {
- final byte byteValue = ioBuffer[numberOfByteReadWrote + byteIndex];
- value = (value << 8) | (((long) byteValue) & 0xff);
- }
- updateCrc32(ioBuffer, numberOfByteReadWrote, 8);
- numberOfByteReadWrote += 8;
- return value;
- }
-
- protected int readInteger() throws HeapException {
- int value = 0;
- for (int byteIndex = 3; byteIndex >= 0; byteIndex--) {
- final byte byteValue = ioBuffer[numberOfByteReadWrote + byteIndex];
- value = (value << 8) | (((int) byteValue) & 0xff);
- }
- updateCrc32(ioBuffer, numberOfByteReadWrote, 4);
- numberOfByteReadWrote += 4;
- return value;
- }
-
- protected boolean readBoolean() throws HeapException {
- final byte byteValue = ioBuffer[numberOfByteReadWrote++];
- updateCrc32(byteValue);
- return byteValue == 0 ? false : true;
- }
-
- protected byte readByte() throws HeapException {
- final byte byteValue = ioBuffer[numberOfByteReadWrote++];
- updateCrc32(byteValue);
- return byteValue;
- }
-
- /**
- * write bytes
- *
- * @param dataByteArray
- * bytes to write
- * @throws HeapException
- */
- protected void writeBytes(final byte[] dataByteArray) throws HeapException {
- System.arraycopy(dataByteArray, 0, ioBuffer, numberOfByteReadWrote,
- dataByteArray.length);
- numberOfByteReadWrote += dataByteArray.length;
- updateCrc32(dataByteArray);
- }
-
- /**
- * Marshal long value to byte array current {@link #dataByteArrayIndex}
- * index<br>
- * {@link #dataByteArrayIndex} is updated to next data to write position<br>
- *
- * @param value
- * the value to marshal
- * @throws HeapException
- */
- protected void writeLong(final long value) throws HeapException {
- long localValue = value;
- for (int byteIndex = 0; byteIndex < 8; byteIndex++) {
- updateCrc32(ioBuffer[numberOfByteReadWrote++] = (byte) localValue);
- localValue >>= 8;
- }
- }
-
- protected void writeInteger(final int value) throws HeapException {
- int localValue = value;
- for (int byteIndex = 0; byteIndex < 4; byteIndex++) {
- updateCrc32(ioBuffer[numberOfByteReadWrote++] = (byte) localValue);
- localValue >>= 8;
- }
- }
-
- protected void writeCrc32() throws HeapException {
- int localValue = crc32;
- for (int byteIndex = 0; byteIndex < 4; byteIndex++) {
- ioBuffer[numberOfByteReadWrote++] = (byte) localValue;
- localValue >>= 8;
- }
- }
-
- protected void writeBoolean(final boolean value) throws HeapException {
- updateCrc32(ioBuffer[numberOfByteReadWrote++] = (byte) (value ? 1 : 0));
- }
-
- protected void writeByte(final byte value) throws HeapException {
- updateCrc32(ioBuffer[numberOfByteReadWrote++] = value);
- }
-
- @Override
- public int hashCode() {
- return (int) (positionInFile ^ (positionInFile >>> 32));
- }
-
- @Override
- public boolean equals(final Object obj) {
- final boolean equals;
- if (obj == this) {
- equals = true;
- } else {
- assert obj != null : "do no accept null parameter";
- assert obj instanceof AbstractFileStorable : "must be instance of AbstractFileStorable";
- final AbstractFileStorable storable = (AbstractFileStorable) obj;
- equals = positionInFile == storable.positionInFile;
- }
- return equals;
- }
-
- @Override
- public String toString() {
- final StringBuilder builder = new StringBuilder();
- builder.append("AbstractFileStorable ");
- builder.append(super.toString());
- builder.append(" [fileForStorable=");
- builder.append(fileForStorable);
- builder.append(", justCreated=");
- builder.append(justCreated);
- builder.append(", positionInFile=");
- builder.append(positionInFile);
- builder.append(", valueChangedToSave=");
- builder.append(valueChangedToSave);
- builder.append("]");
- return builder.toString();
- }
-}
Deleted: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/entity/IFileStorable.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/entity/IFileStorable.java 2012-04-17 05:00:49 UTC (rev 3011)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/entity/IFileStorable.java 2012-04-17 05:05:53 UTC (rev 3012)
@@ -1,51 +0,0 @@
-/*
- * Copyright 2007 Luc Peuvrier
- *
- * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE
- * Licensed under the LGPL License, Version 3, 29 June 2007 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.gnu.org/licenses/lgpl.html
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package net.sf.joafip.heapfile.entity;
-
-import net.sf.joafip.heapfile.service.FileForStorable;
-import net.sf.joafip.kvstore.service.HeapException;
-
-/**
- * class implementing this can be store in file using {@link FileForStorable}<br>
- *
- * @author luc peuvrier
- *
- */
-public interface IFileStorable {
-
- /**
- *
- * @return position in file of this file storable
- */
- long getPositionInFile();
-
- /**
- * write this file storable to file
- *
- * @return bytes wrote
- * @throws HeapException
- */
- byte[] writeToFile() throws HeapException;
-
- /**
- * read this file storable from file
- *
- * @throws HeapException
- */
- void readFromFile() throws HeapException;
-
-}
Modified: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/HeapHeader.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/HeapHeader.java 2012-04-17 05:00:49 UTC (rev 3011)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/HeapHeader.java 2012-04-17 05:05:53 UTC (rev 3012)
@@ -17,8 +17,8 @@
package net.sf.joafip.heapfile.record.entity;
import net.sf.joafip.NotStorableClass;
-import net.sf.joafip.heapfile.entity.AbstractFileStorable;
import net.sf.joafip.heapfile.record.service.HeapElementManager;
+import net.sf.joafip.kvstore.entity.AbstractFileStorable;
import net.sf.joafip.kvstore.record.entity.DataRecordIdentifier;
import net.sf.joafip.kvstore.service.HeapException;
Modified: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/HeapRecord.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/HeapRecord.java 2012-04-17 05:00:49 UTC (rev 3011)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/HeapRecord.java 2012-04-17 05:05:53 UTC (rev 3012)
@@ -20,9 +20,9 @@
import net.sf.joafip.Fortest;
import net.sf.joafip.NotStorableClass;
-import net.sf.joafip.heapfile.entity.AbstractFileStorable;
import net.sf.joafip.heapfile.record.service.HeapElementManager;
import net.sf.joafip.heapfile.record.service.IHeapElementManager;
+import net.sf.joafip.kvstore.entity.AbstractFileStorable;
import net.sf.joafip.kvstore.record.entity.DataRecordIdentifier;
import net.sf.joafip.kvstore.record.entity.IDataRecordKey;
import net.sf.joafip.kvstore.service.HeapException;
Deleted: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/ToBackupRecord.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/ToBackupRecord.java 2012-04-17 05:00:49 UTC (rev 3011)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/ToBackupRecord.java 2012-04-17 05:05:53 UTC (rev 3012)
@@ -1,84 +0,0 @@
-/*
- * Copyright 2008 Luc Peuvrier
- *
- * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE
- * Licensed under the LGPL License, Version 3, 29 June 2007 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.gnu.org/licenses/lgpl.html
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package net.sf.joafip.heapfile.record.entity;
-
-import net.sf.joafip.NotStorableClass;
-
-/**
- *
- * @author luc peuvrier
- *
- */
-@NotStorableClass
-public class ToBackupRecord implements Comparable<ToBackupRecord> {
-
- private final long positionInFile;
-
- private final byte[] data;
-
- public ToBackupRecord(final long positionInFile, final byte[] data) {// NOPMD
- // expose
- // data
- super();
- this.positionInFile = positionInFile;
- this.data = data;
- }
-
- public byte[] getData() {
- return data;// NOPMD expose data
- }
-
- public long getPositionInFile() {
- return positionInFile;
- }
-
- public int compareTo(final ToBackupRecord other) {
- final int compareTo;
- if (positionInFile < other.positionInFile) {
- compareTo = -1;
- } else if (positionInFile > other.positionInFile) {
- compareTo = 1;
- } else {
- compareTo = 0;
- }
- return compareTo;
- }
-
- @Override
- public int hashCode() {
- final int PRIME = 31;
- int result = 1;
- result = PRIME * result
- + (int) (positionInFile ^ (positionInFile >>> 32));
- return result;
- }
-
- @Override
- @SuppressWarnings("PMD")
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- // if (obj == null)
- // return false;
- // if (getClass() != obj.getClass())
- // return false;
- final ToBackupRecord other = (ToBackupRecord) obj;
- if (positionInFile != other.positionInFile)
- return false;
- return true;
- }
-}
Modified: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/HeapElementManager.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/HeapElementManager.java 2012-04-17 05:00:49 UTC (rev 3011)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/HeapElementManager.java 2012-04-17 05:05:53 UTC (rev 3012)
@@ -27,16 +27,16 @@
import net.sf.joafip.NotStorableClass;
import net.sf.joafip.file.service.FileIOException;
import net.sf.joafip.file.service.HelperFileUtil;
-import net.sf.joafip.heapfile.entity.IFileStorable;
import net.sf.joafip.heapfile.record.entity.HeapHeader;
import net.sf.joafip.heapfile.record.entity.HeapRecord;
-import net.sf.joafip.heapfile.record.entity.ToBackupRecord;
-import net.sf.joafip.heapfile.service.FileForStorable;
-import net.sf.joafip.heapfile.service.IFileForStorable;
import net.sf.joafip.kvstore.entity.EnumFileState;
+import net.sf.joafip.kvstore.entity.IFileStorable;
+import net.sf.joafip.kvstore.entity.ToBackupRecord;
import net.sf.joafip.kvstore.record.entity.DataRecordIdentifier;
import net.sf.joafip.kvstore.record.service.IDataRecordKeyManager;
+import net.sf.joafip.kvstore.service.FileForStorable;
import net.sf.joafip.kvstore.service.HeapException;
+import net.sf.joafip.kvstore.service.IFileForStorable;
import net.sf.joafip.logger.JoafipLogger;
/**
Modified: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/IHeapElementManager.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/IHeapElementManager.java 2012-04-17 05:00:49 UTC (rev 3011)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/IHeapElementManager.java 2012-04-17 05:05:53 UTC (rev 3012)
@@ -19,10 +19,10 @@
import net.sf.joafip.Fortest;
import net.sf.joafip.heapfile.record.entity.HeapHeader;
import net.sf.joafip.heapfile.record.entity.HeapRecord;
-import net.sf.joafip.heapfile.service.IFileForStorable;
import net.sf.joafip.kvstore.record.entity.DataRecordIdentifier;
import net.sf.joafip.kvstore.record.service.IDataRecordKeyManager;
import net.sf.joafip.kvstore.service.HeapException;
+import net.sf.joafip.kvstore.service.IFileForStorable;
/**
* Heap elements manager in heap file interface.<br>
Deleted: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/service/FileForStorable.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/service/FileForStorable.java 2012-04-17 05:00:49 UTC (rev 3011)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/service/FileForStorable.java 2012-04-17 05:05:53 UTC (rev 3012)
@@ -1,387 +0,0 @@
-/*
- * Copyright 2007 Luc Peuvrier
- *
- * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE
- * Licensed under the LGPL License, Version 3, 29 June 2007 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.gnu.org/licenses/lgpl.html
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package net.sf.joafip.heapfile.service;
-
-import java.io.File;
-
-import net.sf.joafip.NotStorableClass;
-import net.sf.joafip.file.service.FileIOException;
-import net.sf.joafip.file.service.IRandomAccessFile;
-import net.sf.joafip.file.service.RandomAccessFileDirectNio;
-import net.sf.joafip.file.service.RandomAccessFileReadWriteCache;
-import net.sf.joafip.heapfile.entity.IFileStorable;
-import net.sf.joafip.heapfile.record.entity.ToBackupRecord;
-import net.sf.joafip.kvstore.service.HeapException;
-import net.sf.joafip.logger.JoafipLogger;
-
-/**
- * to read/write class implementing {@link IFileStorable} to/from file.<br>
- * {@link #open()} and {@link #close()}<br>
- *
- * @author luc peuvrier
- *
- */
-@NotStorableClass
-public class FileForStorable implements IFileForStorable {
-
- private static final String FILE_IS_OPENED = "file is opened";
-
- protected final JoafipLogger logger = JoafipLogger.getLogger(getClass());// NOPMD
-
- protected static final String FILE_ALREADY_CLOSED = "file already closed";
-
- protected static final String FILE_NOT_OPENNED = "file not opened";
-
- private static final String FILE_ALREADY_OPENED = "file already opened";
-
- protected static final String FAILED_DELETE_FILE = "failed delete file ";
-
- /** the file object for file heap */
- private final File file;
-
- /** for random read write file access */
- protected IRandomAccessFile randomAccessFile;
-
- /** true if file opened */
- private boolean opened = false;
-
- private Exception opennerTrace;
-
- private Exception closerTrace = new Exception("closer trace");
-
- /**
- * create a file to read from and write to {@link IFileStorable} object.<br>
- * no cache used for file management<br>
- *
- * @param fileName
- * the file name of file to use for storing storable object
- * @param maxRetry
- * @param retryMsDelay
- * @throws HeapException
- * if an I/O error occurs while opening the file
- */
- public FileForStorable(final String fileName, final int maxRetry,
- final int retryMsDelay) throws HeapException {
- this(fileName == null ? null : new File(fileName), maxRetry,
- retryMsDelay);
- }
-
- /**
- * create a file to read from and write to {@link IFileStorable} object<br>
- * no cache used for file management<br>
- *
- * @param file
- * the file to use for storable object storing
- * @param maxRetry
- * @param retryMsDelay
- * @throws HeapException
- * if an I/O error occurs while opening the file
- */
- public FileForStorable(final File file, final int maxRetry,
- final int retryMsDelay) throws HeapException {
- this.file = file;
- randomAccessFile = new RandomAccessFileDirectNio(file, maxRetry,
- retryMsDelay);
- }
-
- /**
- * create a file to read from and write to {@link IFileStorable} object<br>
- * file management use read/write cache<br>
- *
- * @param fileName
- * the file name of file to use for object storing
- * @param pageSize
- * page size ( number of byte ), must be greater or equals to
- * 1024
- * @param maxPage
- * maximum number of page for read cache
- * @param maxRetry
- * @param retryMsDelay
- * @throws HeapException
- */
- public FileForStorable(final String fileName, final int pageSize,
- final int maxPage, final int maxRetry, final int retryMsDelay)
- throws HeapException {
- this(fileName == null ? null : new File(fileName), pageSize, maxPage,
- maxRetry, retryMsDelay);
- }
-
- /**
- * create a file to read from and write to {@link IFileStorable} object<br>
- *
- * @param file
- * the file to use for storable object storing
- * @param pageSize
- * page size ( number of byte ), must be greater or equals to
- * 1024
- * @param maxPage
- * maximum number of page for read cache
- * @param maxRetry
- * @param retryMsDelay
- * @throws HeapException
- */
- public FileForStorable(final File file, final int pageSize,
- final int maxPage, final int maxRetry, final int retryMsDelay)
- throws HeapException {
- this.file = file;
- final RandomAccessFileDirectNio randomAccessFileDirect =
- /**/new RandomAccessFileDirectNio(file, maxRetry, retryMsDelay);
- randomAccessFile = new RandomAccessFileReadWriteCache(
- randomAccessFileDirect, pageSize, maxPage, maxRetry,
- retryMsDelay);
- }
-
- /**
- * create a file to read from and write to {@link IFileStorable} object<br>
- * file management use read/write cache<br>
- *
- * @param file
- * the file to use for storable object storing
- * @param pageSize
- * page size ( number of byte ), must be greater or equals to
- * 1024
- * @param maxPage
- * maximum number of page for read cache
- * @param haveReadCache
- * true if have read cache
- * @param maxRetry
- * @param retryMsDelay
- * @throws HeapException
- */
- public FileForStorable(final File file, final int pageSize,
- final int maxPage, final boolean haveReadCache, final int maxRetry,
- final int retryMsDelay) {
- this.file = file;
- final RandomAccessFileDirectNio randomAccessFileDirect =
- /**/new RandomAccessFileDirectNio(file, maxRetry, retryMsDelay);
- randomAccessFile = new RandomAccessFileReadWriteCache(
- randomAccessFileDirect, pageSize, maxPage, haveReadCache);
- }
-
- @Override
- public void open() throws HeapException {
- if (opened) {
- throw new HeapException(FILE_ALREADY_OPENED, opennerTrace);
- } else {
- try {
- randomAccessFile.open();
- } catch (FileIOException exception) {
- throw new HeapException(exception);
- }
- opened = true;
- opennerTrace = new Exception("openner trace");
- }
- }
-
- @Override
- public void close() throws HeapException {
- if (opened) {
- opened = false;
- closerTrace = new Exception("closer trace");
- try {
- randomAccessFile.close();
- } catch (FileIOException e) {
- throw new HeapException(e);
- }
- } else {
- throw new HeapException(FILE_ALREADY_CLOSED, closerTrace);
- }
- }
-
- @Override
- public void flush() throws HeapException {
- assertOpened();
- try {
- randomAccessFile.flush();
- } catch (FileIOException e) {
- throw new HeapException(e);
- }
- }
-
- @Override
- public void resetFileSize() throws HeapException {
- assertOpened();
- try {
- randomAccessFile.setLength(0);
- } catch (FileIOException e) {
- throw closeAfterIOException(e);
- }
- }
-
- @Override
- public void deleteFileIfExists() throws HeapException {
- if (opened) {
- throw new HeapException(FILE_IS_OPENED);
- }
- if (logger.debugEnabled) {
- logger.debug("delete " + file);
- }
- try {
- randomAccessFile.deleteIfExists();
- } catch (FileIOException exception) {
- throw new HeapException(FAILED_DELETE_FILE + file, exception);
- }
- }
-
- @Override
- public void deleteFileIfExistsRenaming() throws HeapException {
- if (opened) {
- throw new HeapException(FILE_IS_OPENED);
- }
- if (logger.debugEnabled) {
- logger.debug("delete " + file);
- }
- try {
- randomAccessFile.deleteIfExistsRenaming();
- } catch (FileIOException exception) {
- throw new HeapException(FAILED_DELETE_FILE + file, exception);
- }
- }
-
- /**
- * @throws HeapException
- *
- */
- private void assertOpened() throws HeapException {
- if (!opened) {
- throw new HeapException(FILE_NOT_OPENNED, closerTrace);
- }
- }
-
- public boolean isOpened() {
- return opened;
- }
-
- @Override
- public void seek(final long positionInFile) throws HeapException {
- assertOpened();
- try {
- randomAccessFile.seek(positionInFile);
- } catch (FileIOException e) {
- throw closeAfterIOException(e);
- }
- }
-
- @Override
- public int read(final byte[] data) throws HeapException {
- assertOpened();
- try {
- return randomAccessFile.read(data);// NOPMD
- } catch (FileIOException e) {
- throw closeAfterIOException(e);
- }
- }
-
- @Override
- public void write(final byte[] data) throws HeapException {
- assertOpened();
- try {
- randomAccessFile.write(data);
- } catch (FileIOException e) {
- throw closeAfterIOException(e);
- }
- }
-
- @Override
- public void write(final ToBackupRecord toBackupRecord) throws HeapException {
- try {
- randomAccessFile.seek(toBackupRecord.getPositionInFile());
- randomAccessFile.write(toBackupRecord.getData());
- } catch (FileIOException exception) {
- throw new HeapException(exception);
- }
- }
-
- @Override
- public File getFile() {
- return file;
- }
-
- @Override
- public long getFileSize() throws HeapException {
- long size;
- assertOpened();
- try {
- size = randomAccessFile.length();
- } catch (FileIOException e) {
- throw closeAfterIOException(e);
- }
- return size;
- }
-
- /**
- * set the size of this file
- *
- * @param newSize
- * the file new size
- * @throws HeapException
- * if an I/O error occurs.
- */
- public void setFileSize(final long newSize) throws HeapException {
- assertOpened();
- try {
- randomAccessFile.setLength(newSize);
- } catch (FileIOException e) {
- throw closeAfterIOException(e);
- }
- }
-
- /**
- * copy this file to an other file
- *
- * @param fileForStorable
- * @throws HeapException
- */
- public void copy(final FileForStorable fileForStorable)
- throws HeapException {
- try {
- randomAccessFile.copy(fileForStorable.getFile().getAbsolutePath());
- } catch (FileIOException exception) {
- throw new HeapException(exception);
- }
- }
-
- /**
- * close after io exception
- */
- protected HeapException closeAfterIOException(
- final FileIOException exception) throws HeapException {
- final HeapException fileForStorageException =
- /**/new HeapException(exception);
- try {
- close();
- } catch (HeapException exception2) {
- logger.warn("closing after exception", exception2);
- }
- return fileForStorageException;
- }
-
- public void copy(final String fileName) throws FileIOException {
- randomAccessFile.copy(fileName);
- }
-
- @Override
- public String toString() {
- final StringBuilder builder = new StringBuilder();
- builder.append("AbstractFileFor [file=");
- builder.append(file);
- builder.append(", opened=");
- builder.append(opened);
- builder.append("]");
- return builder.toString();
- }
-}
Modified: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/service/HeapFileDataManager.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/service/HeapFileDataManager.java 2012-04-17 05:00:49 UTC (rev 3011)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/service/HeapFileDataManager.java 2012-04-17 05:05:53 UTC (rev 3012)
@@ -40,6 +40,7 @@
import net.sf.joafip.kvstore.record.entity.DataRecordIdentifier;
import net.sf.joafip.kvstore.record.service.IDataRecordKeyManager;
import net.sf.joafip.kvstore.service.AbstractHeapDataManager;
+import net.sf.joafip.kvstore.service.FileForStorable;
import net.sf.joafip.kvstore.service.HeapException;
import net.sf.joafip.redblacktree.entity.IRBTNode;
import net.sf.joafip.redblacktree.service.IRBTVisitor;
Deleted: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/service/IFileForStorable.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/service/IFileForStorable.java 2012-04-17 05:00:49 UTC (rev 3011)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/service/IFileForStorable.java 2012-04-17 05:05:53 UTC (rev 3012)
@@ -1,134 +0,0 @@
-/*
- * Copyright 2010 Luc Peuvrier
- *
- * This file is a part of JOAFIP.
- *
- * JOAFIP is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License.
- *
- * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE
- * Licensed under the LGPL License, Version 3, 29 June 2007 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.gnu.org/licenses/lgpl.html
- *
- * JOAFIP is distributed in the hope that it will be useful, but
- * unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package net.sf.joafip.heapfile.service;
-
-import java.io.File;
-import java.io.RandomAccessFile;
-
-import net.sf.joafip.heapfile.record.entity.ToBackupRecord;
-import net.sf.joafip.kvstore.service.HeapException;
-import net.sf.joafip.kvstore.service.HeapRuntimeException;//NOPMD
-
-/**
- *
- * @author luc peuvrier
- *
- */
-public interface IFileForStorable {
-
- /**
- * Returns the size of this file.
- *
- * @return the size of this file, measured in bytes.
- * @exception HeapException
- * if an I/O error occurs.
- */
- long getFileSize() throws HeapException;
-
- File getFile();
-
- /**
- * delete file if exists
- *
- * @throws HeapException
- */
- void deleteFileIfExists() throws HeapException;
-
- /**
- * delete file if exists
- *
- * @throws HeapException
- */
- void deleteFileIfExistsRenaming() throws HeapException;
-
- /**
- * open heap file for access operation<br>
- *
- * @throws HeapException
- * if an I/O error occurs while opening file
- */
- void open() throws HeapException;
-
- /**
- * flush modification to disk
- *
- * @throws HeapException
- *
- */
- void flush() throws HeapException;
-
- void resetFileSize() throws HeapException;
-
- /**
- * close heap file access operation<br>
- *
- * @throws HeapException
- * if an I/O error occurs while closing the the file
- * @throws FileForStorableRuntimeException
- * file already closed
- */
- void close() throws HeapException;
-
- /**
- * delegate to {@link RandomAccessFile#seek(long)} closing the file in case
- * of I/O error<br>
- *
- * @param positionInFile
- * @throws HeapException
- * if an I/O error occurs
- * @throws HeapRuntimeException
- * file closed
- */
- void seek(long positionInFile) throws HeapException;
-
- /**
- * delegate to {@link RandomAccessFile#read(byte[])} closing the file in
- * case of I/O error<br>
- *
- * Reads up to <code>data.length</code> bytes of data from this file into an
- * array of bytes.
- *
- * @param data
- * where store data read
- * @return number of bytes read
- * @throws HeapException
- * if an I/O error occurs
- * @throws HeapRuntimeException
- * file closed
- */
- int read(byte[] ioBuffer) throws HeapException;
-
- /**
- * delegate to {@link RandomAccessFile#write(byte[]))} closing the file in
- * case of I/O error<br>
- *
- * @param data
- * @throws HeapException
- * if an I/O error occurs
- * @throws HeapRuntimeException
- * file closed
- */
- void write(byte[] ioBuffer) throws HeapException;
-
- void write(ToBackupRecord toBackupRecord) throws HeapException;
-}
Modified: trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/TestIdNode.java
===================================================================
--- trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/TestIdNode.java 2012-04-17 05:00:49 UTC (rev 3011)
+++ trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/TestIdNode.java 2012-04-17 05:05:53 UTC (rev 3012)
@@ -27,8 +27,8 @@
import net.sf.joafip.heapfile.record.entity.HeapIdNode;
import net.sf.joafip.heapfile.record.entity.HeapRecord;
import net.sf.joafip.heapfile.record.service.HeapElementManager;
-import net.sf.joafip.heapfile.service.FileForStorable;
import net.sf.joafip.kvstore.record.entity.DataRecordIdentifier;
+import net.sf.joafip.kvstore.service.FileForStorable;
import net.sf.joafip.redblacktree.service.RBTException;
@NotStorableClass
Modified: trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/entity/TestAbstractFileStorable.java
===================================================================
--- trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/entity/TestAbstractFileStorable.java 2012-04-17 05:00:49 UTC (rev 3011)
+++ trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/entity/TestAbstractFileStorable.java 2012-04-17 05:05:53 UTC (rev 3012)
@@ -23,8 +23,8 @@
import net.sf.joafip.NotStorableClass;
import net.sf.joafip.TestConstant;
import net.sf.joafip.TestException;
-import net.sf.joafip.heapfile.entity.AbstractFileStorable;
-import net.sf.joafip.heapfile.service.FileForStorable;
+import net.sf.joafip.kvstore.entity.AbstractFileStorable;
+import net.sf.joafip.kvstore.service.FileForStorable;
import net.sf.joafip.kvstore.service.HeapException;
@NotStorableClass
Modified: trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/entity/TestMarshall.java
===================================================================
--- trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/entity/TestMarshall.java 2012-04-17 05:00:49 UTC (rev 3011)
+++ trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/entity/TestMarshall.java 2012-04-17 05:05:53 UTC (rev 3012)
@@ -23,8 +23,8 @@
import net.sf.joafip.NotStorableClass;
import net.sf.joafip.TestConstant;
import net.sf.joafip.TestException;
-import net.sf.joafip.heapfile.entity.AbstractFileStorable;
-import net.sf.joafip.heapfile.service.FileForStorable;
+import net.sf.joafip.kvstore.entity.AbstractFileStorable;
+import net.sf.joafip.kvstore.service.FileForStorable;
import net.sf.joafip.kvstore.service.HeapException;
@NotStorableClass
Modified: trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestHeapElementManager.java
===================================================================
--- trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestHeapElementManager.java 2012-04-17 05:00:49 UTC (rev 3011)
+++ trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestHeapElementManager.java 2012-04-17 05:05:53 UTC (rev 3012)
@@ -25,8 +25,8 @@
import net.sf.joafip.TestException;
import net.sf.joafip.heapfile.record.entity.HeapHeader;
import net.sf.joafip.heapfile.record.entity.HeapRecord;
-import net.sf.joafip.heapfile.service.FileForStorable;
import net.sf.joafip.kvstore.record.entity.DataRecordIdentifier;
+import net.sf.joafip.kvstore.service.FileForStorable;
import net.sf.joafip.kvstore.service.HeapException;
import net.sf.joafip.redblacktree.entity.IRBTNode;
import net.sf.joafip.redblacktree.service.RBTException;
Modified: trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestHeapRecordManage.java
===================================================================
--- trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestHeapRecordManage.java 2012-04-17 05:00:49 UTC (rev 3011)
+++ trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestHeapRecordManage.java 2012-04-17 05:05:53 UTC (rev 3012)
@@ -26,8 +26,8 @@
import net.sf.joafip.heapfile.record.entity.HeapHeader;
import net.sf.joafip.heapfile.record.entity.HeapIdNode;
import net.sf.joafip.heapfile.record.entity.HeapRecord;
-import net.sf.joafip.heapfile.service.FileForStorable;
import net.sf.joafip.kvstore.record.entity.DataRecordIdentifier;
+import net.sf.joafip.kvstore.service.FileForStorable;
import net.sf.joafip.kvstore.service.HeapException;
import net.sf.joafip.redblacktree.entity.IRBTNode;
import net.sf.joafip.redblacktree.service.RBTException;
Modified: trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestStoreRestore.java
===================================================================
--- trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestStoreRestore.java 2012-04-17 05:00:49 UTC (rev 3011)
+++ trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestStoreRestore.java 2012-04-17 05:05:53 UTC (rev 3012)
@@ -27,9 +27,9 @@
import net.sf.joafip.heapfile.record.entity.HeapHeader;
import net.sf.joafip.heapfile.record.entity.HeapIdNode;
import net.sf.joafip.heapfile.record.entity.HeapRecord;
-import net.sf.joafip.heapfile.service.FileForStorable;
import net.sf.joafip.kvstore.record.entity.DataRecordIdentifier;
import net.sf.joafip.kvstore.record.service.IDataRecordKeyManager;
+import net.sf.joafip.kvstore.service.FileForStorable;
import net.sf.joafip.kvstore.service.HeapException;
import net.sf.joafip.redblacktree.service.RBTException;
Modified: trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/FileForStorableBridge.java
===================================================================
--- trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/FileForStorableBridge.java 2012-04-17 05:00:49 UTC (rev 3011)
+++ trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/FileForStorableBridge.java 2012-04-17 05:05:53 UTC (rev 3012)
@@ -2,8 +2,9 @@
import java.io.File;
-import net.sf.joafip.heapfile.record.entity.ToBackupRecord;
+import net.sf.joafip.kvstore.entity.ToBackupRecord;
import net.sf.joafip.kvstore.service.HeapException;
+import net.sf.joafip.kvstore.service.IFileForStorable;
public class FileForStorableBridge implements IFileForStorable {
Copied: trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/entity/AbstractFileStorable.java (from rev 3010, trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/entity/AbstractFileStorable.java)
===================================================================
--- trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/entity/AbstractFileStorable.java (rev 0)
+++ trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/entity/AbstractFileStorable.java 2012-04-17 05:05:53 UTC (rev 3012)
@@ -0,0 +1,423 @@
+/*
+ * Copyright 2007 Luc Peuvrier
+ *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE
+ * Licensed under the LGPL License, Version 3, 29 June 2007 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.sf.joafip.kvstore.entity;
+
+import java.util.Arrays;
+
+import net.sf.joafip.NotStorableClass;
+import net.sf.joafip.kvstore.service.FileForStorable;
+import net.sf.joafip.kvstore.service.IFileForStorable;
+import net.sf.joafip.kvstore.service.HeapException;
+import net.sf.joafip.logger.JoafipLogger;
+
+/**
+ * all that is common to storable in file using {@link FileForStorable}:<br>
+ * <ul>
+ * <li>the {@link FileForStorable} manager to use</li>
+ * <li>position of data in file</li>
+ * <li>flags for first creation and state change</li>
+ * <li>primitive type read and write in file</li>
+ * <li>crc32 compute and check for data integrity check</li>
+ * </ul>
+ *
+ * by extension make able to create mutable or immutable object, immutable are
+ * for file record unique representation, mutable make able to change the file
+ * record represented<br>
+ *
+ * @author luc peuvrier
+ *
+ */
+@NotStorableClass
+public abstract class AbstractFileStorable implements IFileStorable {
+
+ protected final JoafipLogger logger = JoafipLogger.getLogger(getClass());// NOPMD
+
+ private static final String ALREADY_READ = "already read";
+
+ private static final String RECORD_INTEGRITY_ERROR = "record integrity error";
+
+ private static final String NO_VALUE_CHANGE_TO_WRITE = "no value change to write";
+
+ private static final String READING_ERASE_VALUE_CHANGE = "reading erase value change";
+
+ /** position in file of this in marshallized forms */
+ protected final long positionInFile;
+
+ /** to read/write this in file */
+ protected final IFileForStorable fileForStorable;
+
+ /** to compute crc32 record header */
+ private transient int crc32;
+
+ /** current number of byte read or wrote */
+ private transient int numberOfByteReadWrote = 0;
+
+ /** input output buffer */
+ private transient byte[] ioBuffer;
+
+ private transient int readInIoBuffer;
+
+ /**
+ * true if this value changed, creation implies no value to save since not
+ * read and not write
+ */
+ protected boolean valueChangedToSave = false;
+
+ /** true if just created in memory, never read from file */
+ protected boolean justCreated = true;
+
+ /**
+ * @param fileForStorable
+ * @param positionInFile
+ */
+ public AbstractFileStorable(final IFileForStorable fileForStorable,
+ final long positionInFile) {
+ super();
+ this.fileForStorable = fileForStorable;
+ this.positionInFile = positionInFile;
+ }
+
+ public void clear() {
+ valueChangedToSave = false;
+ justCreated = true;
+ }
+
+ public long getPositionInFile() {
+ return positionInFile;
+ }
+
+ public void readFromFile() throws HeapException {
+ // ASSERTX
+ assert justCreated : ALREADY_READ;
+ // ASSERTX
+ assert !valueChangedToSave : READING_ERASE_VALUE_CHANGE;
+ fileForStorable.seek(positionInFile);
+ crc32 = 0;
+ ioBuffer = null;// NOPMD
+ unmarshallImpl();
+ ioBuffer = null;// NOPMD
+ justCreated = false;
+ }
+
+ public byte[] writeToFile() throws HeapException {
+ // ASSERTX
+ assert valueChangedToSave : NO_VALUE_CHANGE_TO_WRITE;
+ fileForStorable.seek(positionInFile);
+ crc32 = 0;
+ numberOfByteReadWrote = 0;
+ ioBuffer = new byte[toMarshallSize()];
+ marshallImpl();
+ fileForStorable.write(ioBuffer);
+ valueChangedToSave = false;
+ final byte[] result = ioBuffer;
+ ioBuffer = null;// NOPMD discard
+ return result;
+ }
+
+ /**
+ *
+ * @return number of byte to marshal
+ * @throws HeapException
+ */
+ protected abstract int toMarshallSize() throws HeapException;
+
+ /**
+ * Unmarshalling implementation for read<br>
+ * {@link #crc32} is set to zero<br>
+ *
+ * @throws HeapException
+ */
+ protected abstract void unmarshallImpl() throws HeapException;
+
+ /**
+ * marshaling implementation for write<br>
+ * {@link #crc32} is set to zero<br>
+ *
+ * @throws HeapException
+ */
+ protected abstract void marshallImpl() throws HeapException;
+
+ /**
+ *
+ * @return true if just created ( not read from file and not setted )
+ */
+ public boolean isJustCreated() {
+ return justCreated;
+ }
+
+ /**
+ * to know if the value state change
+ *
+ * @return true if value changed
+ */
+ public boolean isValueChangedToSave() {
+ return valueChangedToSave;
+ }
+
+ /**
+ * set this record have value changed, so to save
+ *
+ * @throws HeapException
+ */
+ public void setValueIsChangedValueToSave() throws HeapException {
+ if (!valueChangedToSave) {
+ valueChangedToSave = true;
+ justCreated = false;
+ valueChangedAction();
+ }
+ }
+
+ protected abstract void valueChangedAction() throws HeapException;
+
+ public void setValueIsNotChanged() {
+ valueChangedToSave = false;
+ }
+
+ /**
+ * update crc32 with byte value in buffer
+ *
+ * @param buffer
+ * bytes for crc32 update
+ */
+ private void updateCrc32(final byte[] buffer) {
+ updateCrc32(buffer, 0, buffer.length);
+ }
+
+ private void updateCrc32(final byte[] buffer, final int start,
+ final int length) {
+
+ for (int i = 0; i < length; i++) {
+ final byte byteValue = buffer[start + i];
+ updateCrc32(byteValue);
+ }
+
+ }
+
+ private void updateCrc32(final byte byteValue) {
+ for (int j = 0; j < 8; j++) {
+ final int testbit = ((crc32 >> 31) & 1) ^ ((byteValue >> j) & 1);
+ crc32 <<= 2;
+ if (testbit != 0) {
+ crc32 ^= 0x8005; /* 32 bit crc */
+ }
+ }
+ }
+
+ protected void clearCrc32() {
+ crc32 = 0;
+ }
+
+ protected int getCrc32() {
+ return crc32;
+ }
+
+ protected int readFileToIoBuffer(final int size) throws HeapException {
+ ioBuffer = new byte[size];
+ readInIoBuffer = fileForStorable.read(ioBuffer);
+ numberOfByteReadWrote = 0;
+ return readInIoBuffer;
+ }
+
+ /**
+ * read extending ioBuffer
+ *
+ * @param newSize
+ * ioBuffer new size
+ * @throws HeapException
+ */
+ protected void readFileAppendToIoBuffer(final int newSize)
+ throws HeapException {
+ if (newSize > ioBuffer.length) {
+ ioBuffer = Arrays.copyOf(ioBuffer, newSize);
+ final int appendSize = newSize - readInIoBuffer;
+ final byte[] tempBuffer = new byte[appendSize];
+ // FIXMELUC _________________use of additional read
+ // read(iobuffer,readInIoBuffer/*offset*/,appendSize/*length*/);
+ fileForStorable.read(tempBuffer);
+ System.arraycopy(tempBuffer, 0, ioBuffer, readInIoBuffer,
+ appendSize);
+ }
+ }
+
+ /**
+ * read and check CRC32
+ *
+ * @throws HeapException
+ * read error crc32 error in record
+ */
+ protected void readAndCheckCrc32() throws HeapException {
+ final int computedCrc32 = crc32;
+ final int readCrc32 = readInteger();
+ if (computedCrc32 != readCrc32) {
+ throw new HeapException(RECORD_INTEGRITY_ERROR
+ + " record position=" + positionInFile + " read="
+ + readCrc32 + " computed=" + computedCrc32 + " nb rd/wr="
+ + numberOfByteReadWrote, EnumFileState.STATE_CORRUPTED);
+ }
+ }
+
+ /**
+ * read bytes
+ *
+ * @param length
+ * number of bytes to read
+ * @return the bytes read
+ * @throws HeapException
+ */
+ protected byte[] readBytes(final int length) throws HeapException {
+ final byte[] dataByteArray = new byte[length];
+ System.arraycopy(ioBuffer, numberOfByteReadWrote, dataByteArray, 0,
+ length);
+ numberOfByteReadWrote += length;
+ updateCrc32(dataByteArray);
+ return dataByteArray;
+ }
+
+ /**
+ * unmarshal from byte array current {@link #dataByteArrayIndex} index to
+ * long<br>
+ * {@link #dataByteArrayIndex} is updated to next data to read position<br>
+ *
+ * @param dataByteArray
+ * @return the unmarshaled value
+ * @throws HeapException
+ */
+ protected long readLong() throws HeapException {
+ long value = 0;
+ for (int byteIndex = 7; byteIndex >= 0; byteIndex--) {
+ final byte byteValue = ioBuffer[numberOfByteReadWrote + byteIndex];
+ value = (value << 8) | (((long) byteValue) & 0xff);
+ }
+ updateCrc32(ioBuffer, numberOfByteReadWrote, 8);
+ numberOfByteReadWrote += 8;
+ return value;
+ }
+
+ protected int readInteger() throws HeapException {
+ int value = 0;
+ for (int byteIndex = 3; byteIndex >= 0; byteIndex--) {
+ final byte byteValue = ioBuffer[numberOfByteReadWrote + byteIndex];
+ value = (value << 8) | (((int) byteValue) & 0xff);
+ }
+ updateCrc32(ioBuffer, numberOfByteReadWrote, 4);
+ numberOfByteReadWrote += 4;
+ return value;
+ }
+
+ protected boolean readBoolean() throws HeapException {
+ final byte byteValue = ioBuffer[numberOfByteReadWrote++];
+ updateCrc32(byteValue);
+ return byteValue == 0 ? false : true;
+ }
+
+ protected byte readByte() throws HeapException {
+ final byte byteValue = ioBuffer[numberOfByteReadWrote++];
+ updateCrc32(byteValue);
+ return byteValue;
+ }
+
+ /**
+ * write bytes
+ *
+ * @param dataByteArray
+ * bytes to write
+ * @throws HeapException
+ */
+ protected void writeBytes(final byte[] dataByteArray) throws HeapException {
+ System.arraycopy(dataByteArray, 0, ioBuffer, numberOfByteReadWrote,
+ dataByteArray.length);
+ numberOfByteReadWrote += dataByteArray.length;
+ updateCrc32(dataByteArray);
+ }
+
+ /**
+ * Marshal long value to byte array current {@link #dataByteArrayIndex}
+ * index<br>
+ * {@link #dataByteArrayIndex} is updated to next data to write position<br>
+ *
+ * @param value
+ * the value to marshal
+ * @throws HeapException
+ */
+ protected void writeLong(final long value) throws HeapException {
+ long localValue = value;
+ for (int byteIndex = 0; byteIndex < 8; byteIndex++) {
+ updateCrc32(ioBuffer[numberOfByteReadWrote++] = (byte) localValue);
+ localValue >>= 8;
+ }
+ }
+
+ protected void writeInteger(final int value) throws HeapException {
+ int localValue = value;
+ for (int byteIndex = 0; byteIndex < 4; byteIndex++) {
+ updateCrc32(ioBuffer[numberOfByteReadWrote++] = (byte) localValue);
+ localValue >>= 8;
+ }
+ }
+
+ protected void writeCrc32() throws HeapException {
+ int localValue = crc32;
+ for (int byteIndex = 0; byteIndex < 4; byteIndex++) {
+ ioBuffer[numberOfByteReadWrote++] = (byte) localValue;
+ localValue >>= 8;
+ }
+ }
+
+ protected void writeBoolean(final boolean value) throws HeapException {
+ updateCrc32(ioBuffer[numberOfByteReadWrote++] = (byte) (value ? 1 : 0));
+ }
+
+ protected void writeByte(final byte value) throws HeapException {
+ updateCrc32(ioBuffer[numberOfByteReadWrote++] = value);
+ }
+
+ @Override
+ public int hashCode() {
+ return (int) (positionInFile ^ (positionInFile >>> 32));
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ final boolean equals;
+ if (obj == this) {
+ equals = true;
+ } else {
+ assert obj != null : "do no accept null parameter";
+ assert obj instanceof AbstractFileStorable : "must be instance of AbstractFileStorable";
+ final AbstractFileStorable storable = (AbstractFileStorable) obj;
+ equals = positionInFile == storable.positionInFile;
+ }
+ return equals;
+ }
+
+ @Override
+ public String toString() {
+ final StringBuilder builder = new StringBuilder();
+ builder.append("AbstractFileStorable ");
+ builder.append(super.toString());
+ builder.append(" [fileForStorable=");
+ builder.append(fileForStorable);
+ builder.append(", justCreated=");
+ builder.append(justCreated);
+ builder.append(", positionInFile=");
+ builder.append(positionInFile);
+ builder.append(", valueChangedToSave=");
+ builder.append(valueChangedToSave);
+ builder.append("]");
+ return builder.toString();
+ }
+}
Copied: trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/entity/IFileStorable.java (from rev 3010, trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/entity/IFileStorable.java)
===================================================================
--- trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/entity/IFileStorable.java (rev 0)
+++ trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/entity/IFileStorable.java 2012-04-17 05:05:53 UTC (rev 3012)
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2007 Luc Peuvrier
+ *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE
+ * Licensed under the LGPL License, Version 3, 29 June 2007 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/lgpl.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.sf.joafip.kvstore.entity;
+
+import net.sf.joafip.kvstore.service.FileForStorable;
+import net.sf.joafip.kvstore.service.HeapException;
+
+/**
+ * class implementing this can be store in file using {@link FileForStorable}<br>
+ *
+ * @author luc peuvrier
+ *
+ */
+public interface IFileStorable {
+
+ /**
+ *
+ * @return position in file of this file storable
+ */
+ long getPositionInFile();
+
+ /**
+ * write this file storable to file
+...
[truncated message content] |