[Joafip-svn] SF.net SVN: joafip:[3016] trunk
Brought to you by:
luc_peuvrier
|
From: <luc...@us...> - 2012-04-18 03:29:26
|
Revision: 3016
http://joafip.svn.sourceforge.net/joafip/?rev=3016&view=rev
Author: luc_peuvrier
Date: 2012-04-18 03:29:16 +0000 (Wed, 18 Apr 2012)
Log Message:
-----------
HeapElementManager no more dependent of HeapRecord
Modified Paths:
--------------
trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/AbstractHeapRBTNode.java
trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/HeapFreeNode.java
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/HeapFreeNodeManager.java
trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/HeapIdNodeManager.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/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/FileDataManagerIntegrityChecker.java
trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/HeapRecordIterator.java
trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/entity/IFileStorable.java
trunk/joafip-kvstore/src/test/java/net/sf/joafip/heapfile/record/entity/TestAbstractFileStorable.java
trunk/joafip-kvstore/src/test/java/net/sf/joafip/heapfile/record/entity/TestMarshall.java
Added Paths:
-----------
trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/IHeapRecordFactory.java
Modified: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/AbstractHeapRBTNode.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/AbstractHeapRBTNode.java 2012-04-18 00:29:55 UTC (rev 3015)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/AbstractHeapRBTNode.java 2012-04-18 03:29:16 UTC (rev 3016)
@@ -132,12 +132,12 @@
// }
public HeapRecord getHeapRecord() throws HeapException {
- return heapElementManager.readHeapFileDataRecord(positionInFile);
+ return readHeapFileDataRecord(positionInFile);
}
/**
* get this node mirroring in heap file position <br>
- * should be used only by heap red black tree node manager ( sp�cialization
+ * should be used only by heap red black tree node manager ( specialization
* of {@link AbstractHeapNodeManager}) and this<br>
*
* @return the position of this node mirroring in heap file
@@ -283,8 +283,7 @@
*/
HeapRecord heapRecord;
try {
- heapRecord = heapElementManager
- .readHeapFileDataRecord(parentPositionInFile);
+ heapRecord = readHeapFileDataRecord(parentPositionInFile);
} catch (HeapException exception) {
throw new RBTException(exception);
}
@@ -316,8 +315,7 @@
*/
HeapRecord heapRecord;
try {
- heapRecord = heapElementManager
- .readHeapFileDataRecord(leftPositionInFile);
+ heapRecord = readHeapFileDataRecord(leftPositionInFile);
} catch (HeapException exception) {
throw new RBTException(exception);
}
@@ -351,8 +349,7 @@
*/
HeapRecord heapRecord;
try {
- heapRecord = heapElementManager
- .readHeapFileDataRecord(rightPositionInFile);
+ heapRecord = readHeapFileDataRecord(rightPositionInFile);
} catch (HeapException exception) {
throw new RBTException(exception);
}
@@ -507,6 +504,12 @@
}
}
+ private HeapRecord readHeapFileDataRecord(final long positionInFile)
+ throws HeapException {
+ return (HeapRecord) heapElementManager
+ .readHeapFileDataRecord(positionInFile);
+ }
+
@Override
public void detach() {
valueChanged = false;
Modified: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/HeapFreeNode.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/HeapFreeNode.java 2012-04-18 00:29:55 UTC (rev 3015)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/HeapFreeNode.java 2012-04-18 03:29:16 UTC (rev 3016)
@@ -51,7 +51,7 @@
public int compareTo(final IRBTComparableNode<Integer> node)
throws RBTException {
int nodeSize;
- nodeSize = ((HeapFreeNode) node).getAreaSize();
+ nodeSize = ((HeapFreeNode) node).getRecordSize();
final int compareTo;
if (areaSize > nodeSize) {
compareTo = 1;
@@ -75,7 +75,7 @@
return compareTo;
}
- public int getAreaSize() {
+ public int getRecordSize() {
return areaSize;
}
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-18 00:29:55 UTC (rev 3015)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/HeapHeader.java 2012-04-18 03:29:16 UTC (rev 3016)
@@ -84,6 +84,16 @@
}
@Override
+ public long getPreviousRecordPositionInFile() throws HeapException {
+ return -1L;
+ }
+
+ @Override
+ public int getRecordSize() throws HeapException {
+ return HEAP_HEADER_SIZE;
+ }
+
+ @Override
protected void valueChangedAction() throws HeapException {
// no implementation
}
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-18 00:29:55 UTC (rev 3015)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/HeapRecord.java 2012-04-18 03:29:16 UTC (rev 3016)
@@ -42,11 +42,11 @@
private static final String DATA_SIZE_MISMATCH =
/**/"data size mismatch";
- private static final String CAN_NOT_INCREASE_RECORD_AREA_SIZE =
- /**/"can not increase record area size";
+ private static final String CAN_NOT_INCREASE_RECORD_RECORD_SIZE =
+ /**/"can not increase record size";
- private static final String AREA_SIZE_TO_SMALL_FOR_DATA =
- /**/"area size to small for data";
+ private static final String RECORD_SIZE_TO_SMALL_FOR_DATA =
+ /**/"record size to small for data";
private static final String NO_DATA_TO_WRITE_SET =
/**/"no data to write set";
@@ -129,8 +129,8 @@
private HeapFreeNode freeNode;
- /** the area size in file ( record header size + data size ) */
- private Integer areaSize;
+ /** the record size ( record header size + data size ) */
+ private int recordSize;
private int dataRecordHeaderSize;
@@ -169,15 +169,15 @@
* @param dataAssociatedSize
* size of associated data if not a free record, else it is the
* free space
- * @param areaSize
+ * @param recordSize
* area size for this record
* @throws HeapException
*/
public HeapRecord(final IHeapElementManager heapElementManager,
final long positionInFile, final Long previousRecordPositionInFile,
final DataRecordIdentifier nodeIdentifier,
- final boolean freeRecord, final Integer dataAssociatedSize,
- final Integer areaSize) throws HeapException {
+ final boolean freeRecord, final int dataAssociatedSize,
+ final int recordSize) throws HeapException {
/* length=areaSize */
super(heapElementManager.getFileForStorable(), positionInFile);
this.heapElementManager = heapElementManager;
@@ -190,28 +190,49 @@
*/
// ASSERTX
assert assertAreaSizeForData(freeRecord, nodeIdentifier,
- dataAssociatedSize, areaSize);
- this.areaSize = areaSize;
+ dataAssociatedSize, recordSize);
+ this.recordSize = recordSize;
this.nodeIdentifier = nodeIdentifier;
clearNodeData();
// ASSERTX
assert assertPreviousPositionInFile();
}
+ /**
+ * construction for reading, unknown data associated size, and unknown node
+ * identification<br>
+ * should be only used bye {@link HeapElementManager}<br>
+ *
+ * @param heapElementManager
+ * heap header and record manager in heap file
+ * @param positionInFile
+ * record position in file
+ * @throws HeapException
+ */
+ public HeapRecord(final IHeapElementManager heapElementManager,
+ final long positionInFile) throws HeapException {
+ this(heapElementManager, positionInFile,
+ /**/null/* previous record position in file unknown */,
+ /**/null/* node identification unknown */,
+ /**/false/* a data record is not a free record */,
+ /**/0/* Unknown data Associated Size */,
+ /**/0/* Unknown areaSize */);
+ }
+
private boolean assertAreaSizeForData(final boolean freeRecord,
final DataRecordIdentifier nodeIdentifier,
- final Integer dataAssociatedSize, final Integer areaSize)
+ final int dataAssociatedSize, final int recordSize)
throws HeapException {
// if (!freeRecord && areaSize != null
// && areaSize < MAX_RECORD_HEADER_SIZE + dataAssociatedSize + 4) {
if (!freeRecord
- && areaSize != null
- && areaSize < DATA_RECORD_HEADER_SIZE
+ && recordSize != 0
+ && recordSize < DATA_RECORD_HEADER_SIZE
+ nodeIdentifier.getKeyDataSize() + dataAssociatedSize
+ 4) {
- logger.fatal(AREA_SIZE_TO_SMALL_FOR_DATA);
+ logger.fatal(RECORD_SIZE_TO_SMALL_FOR_DATA);
throw new HeapException(new HeapException(
- AREA_SIZE_TO_SMALL_FOR_DATA));
+ RECORD_SIZE_TO_SMALL_FOR_DATA));
}
return true;
}
@@ -235,27 +256,6 @@
return true;
}
- /**
- * construction for reading, unknown data associated size, and unknown node
- * identification<br>
- * should be only used bye {@link HeapElementManager}<br>
- *
- * @param heapElementManager
- * heap header and record manager in heap file
- * @param positionInFile
- * record position in file
- * @throws HeapException
- */
- public HeapRecord(final IHeapElementManager heapElementManager,
- final long positionInFile) throws HeapException {
- this(heapElementManager, positionInFile,
- /**/null/* previous record position in file unknown */,
- /**/null/* node identification unknown */,
- /**/false/* a data record is not a free record */,
- /**/null/* Unknown data Associated Size */,
- /**/null/* Unknown areaSize */);
- }
-
@Override
protected void valueChangedAction() throws HeapException {
/*
@@ -273,26 +273,26 @@
clearNodeData();
}
- public void unfreeRecord(final int dataAssociatedSize, final int areaSize,
- final DataRecordIdentifier nodeIdentifier, final boolean manageData)
- throws HeapException {
+ public void unfreeRecord(final int dataAssociatedSize,
+ final int recordSize, final DataRecordIdentifier nodeIdentifier,
+ final boolean manageData) throws HeapException {
// ASSERTX
- assert assertCanUnFreeRecordForAreaSize(areaSize);
- this.areaSize = areaSize;
+ assert assertCanUnFreeRecordForRecordSize(recordSize);
+ this.recordSize = recordSize;
assertedUnfreeRecord(dataAssociatedSize, nodeIdentifier);
}
- private boolean assertCanUnFreeRecordForAreaSize(final int areaSize)
+ private boolean assertCanUnFreeRecordForRecordSize(final int recordSize)
throws HeapException {
if (!freeRecord) {
throw new HeapException("already a free record");
}
- assertAreaSizeKnew();
- if (areaSize > this.areaSize) {
- logger.fatal(CAN_NOT_INCREASE_RECORD_AREA_SIZE + " current size="
- + this.areaSize + " asked size=" + areaSize);
+ assertRecordSizeKnew();
+ if (recordSize > this.recordSize) {
+ logger.fatal(CAN_NOT_INCREASE_RECORD_RECORD_SIZE + " current size="
+ + this.recordSize + " asked size=" + recordSize);
throw new HeapException(new HeapException(
- CAN_NOT_INCREASE_RECORD_AREA_SIZE));
+ CAN_NOT_INCREASE_RECORD_RECORD_SIZE));
}
return true;
}
@@ -309,12 +309,12 @@
if (!freeRecord) {
throw new HeapException("already a free record");
}
- assertAreaSizeKnew();
+ assertRecordSizeKnew();
// dataAssociatedSize + 4 : +4 for crc32
- if (areaSize < MAX_RECORD_HEADER_SIZE + dataAssociatedSize + 4) {
- logger.fatal(AREA_SIZE_TO_SMALL_FOR_DATA);
+ if (recordSize < MAX_RECORD_HEADER_SIZE + dataAssociatedSize + 4) {
+ logger.fatal(RECORD_SIZE_TO_SMALL_FOR_DATA);
throw new HeapException(new HeapException(
- AREA_SIZE_TO_SMALL_FOR_DATA));
+ RECORD_SIZE_TO_SMALL_FOR_DATA));
}
return true;
}
@@ -331,12 +331,12 @@
@Override
protected int toMarshallSize() throws HeapException {
// ASSERTX
- assert assertAreaSizeKnew();
+ assert assertRecordSizeKnew();
int toMarshallSize;
if (freeRecord) {
toMarshallSize = FREE_RECORD_HEADER_SIZE;
} else if (dataValueChanged) {
- toMarshallSize = areaSize;
+ toMarshallSize = recordSize;
} else {
// only header will be write
final byte[] keyData = nodeIdentifier.getKeyData();
@@ -358,8 +358,8 @@
assert previousRecordPositionInFile != null : "previous position in file not defined";
writeLong(previousRecordPositionInFile);
// ASSERTX
- assert assertAreaSizeValue(freeRecord);
- writeInteger(areaSize);
+ assert assertRecordSizeValue(freeRecord);
+ writeInteger(recordSize);
if (freeRecord) {
marshallFreeRecord();
} else {
@@ -493,9 +493,9 @@
freeRecord = readBoolean();
previousRecordPositionInFile = readLong();
assertPreviousPositionInFile();
- areaSize = readInteger();
+ recordSize = readInteger();
// ASSERTX
- assert assertAreaSizeValue(freeRecord);
+ assert assertRecordSizeValue(freeRecord);
if (freeRecord) {
// ASSERTX
assert read >= FREE_RECORD_HEADER_SIZE : "expected a minimum of "
@@ -588,10 +588,10 @@
* @param freeRecord
* @throws HeapException
*/
- private boolean assertAreaSizeValue(final boolean freeRecord)
+ private boolean assertRecordSizeValue(final boolean freeRecord)
throws HeapException {
- if (areaSize == null) {
- throw new HeapException("area size must be defined");
+ if (recordSize <= 0) {
+ throw new HeapException("record size must be defined");
}
final int minimal;
if (freeRecord) {
@@ -599,9 +599,10 @@
} else {
minimal = DATA_RECORD_HEADER_SIZE;
}
- if (areaSize < minimal) {
- throw new HeapException("area size too small: areaSize=" + areaSize
- + ", minimal=" + minimal + ", free record=" + freeRecord);
+ if (recordSize < minimal) {
+ throw new HeapException("record size too small: areaSize="
+ + recordSize + ", minimal=" + minimal + ", free record="
+ + freeRecord);
}
return true;
}
@@ -635,7 +636,7 @@
freeNode = new HeapFreeNode(heapElementManager, positionInFile,
parentPositionInFile, leftPositionInFile,
rightPositionInFile, colorSetted, color, numberOfChild,
- areaSize);
+ recordSize);
}
return freeNode;
}
@@ -649,6 +650,7 @@
return freeRecord;
}
+ @Override
public long getPreviousRecordPositionInFile() throws HeapException {
// ASSERTX
assert previousRecordPositionInFile != null : "previous position in file not defined";
@@ -663,8 +665,8 @@
}
public long getNextRecordFilePosition() throws HeapException {
- assertAreaSizeKnew();
- long nextRecordFilePosition = positionInFile + areaSize;
+ assertRecordSizeKnew();
+ long nextRecordFilePosition = positionInFile + recordSize;
final HeapHeader header = (HeapHeader) heapElementManager
.getHeapHeader();
if (nextRecordFilePosition >= header.getEndOfRecordPositionInFile()) {
@@ -676,9 +678,9 @@
/**
* @throws HeapException
*/
- private boolean assertAreaSizeKnew() throws HeapException {
- if (areaSize == null) {
- throw new HeapException("area size is unknow");
+ private boolean assertRecordSizeKnew() throws HeapException {
+ if (recordSize <= 0) {
+ throw new HeapException("record size is unknow");
}
return true;
}
@@ -693,23 +695,18 @@
return true;
}
- /**
- * get the area in file size ( data size or may be more add to header size )
- *
- * @return the area size in file, null if not defined
- * @throws HeapException
- */
- public int getAreaSize() throws HeapException {
+ @Override
+ public int getRecordSize() throws HeapException {
// ASSERTX
- assert assertAreaSizeKnew();
- return areaSize;
+ assert assertRecordSizeKnew();
+ return recordSize;
}
- public void setAreaSize(final int areaSize) {
+ public void setRecordSize(final int recordSize) {
// only for free node update
- this.areaSize = areaSize;
+ this.recordSize = recordSize;
if (freeNode != null) {
- freeNode.setAreaSize(areaSize);
+ freeNode.setAreaSize(recordSize);
}
}
@@ -821,7 +818,7 @@
// (freeNode != null && freeNode
// .equals(heapRecord.freeNode)))
&&
- /**/integerEquals(areaSize, heapRecord.areaSize)
+ /**/integerEquals(recordSize, heapRecord.recordSize)
&&
/**/integerEquals(dataAssociatedSize,
heapRecord.dataAssociatedSize)
@@ -877,8 +874,8 @@
buffer.append(dataAssociatedSize);
buffer.append("\nnodeIdentifier=");
buffer.append(getNodeIdentifier());
- buffer.append("\nareaSize=");
- buffer.append(areaSize);
+ buffer.append("\nrecordSize=");
+ buffer.append(recordSize);
buffer.append("\ndataAssociated=");
buffer.append(dataAssociated);
return buffer.toString();
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-18 00:29:55 UTC (rev 3015)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/HeapElementManager.java 2012-04-18 03:29:16 UTC (rev 3016)
@@ -28,11 +28,9 @@
import net.sf.joafip.file.service.FileIOException;
import net.sf.joafip.file.service.HelperFileUtil;
import net.sf.joafip.heapfile.record.entity.HeapHeader;
-import net.sf.joafip.heapfile.record.entity.HeapRecord;
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;
@@ -41,7 +39,7 @@
/**
* Heap elements manager in heap file.<br>
- * elements are {@link HeapHeader} and {@link HeapRecord}<br>
+ * elements are one heap header and the heap records<br>
* see {@link IHeapElementManager} for role description<br>
* implement a cache for elements read and to write<br>
* data in the cache that was modified will be saved when closing transaction<br>
@@ -202,11 +200,11 @@
* read are put in cache to guaranty that same object for same record is
* returned in same transaction<br>
*/
- private final Map<Long, HeapRecord> heapRecordToWriteMap =
- /**/new TreeMap<Long, HeapRecord>();
+ private final Map<Long, IFileStorable> heapRecordToWriteMap =
+ /**/new TreeMap<Long, IFileStorable>();
- private final Map<Long, HeapRecord> readHeapRecordMap =
- /**/new TreeMap<Long, HeapRecord>();
+ private final Map<Long, IFileStorable> readHeapRecordMap =
+ /**/new TreeMap<Long, IFileStorable>();
private final boolean fileOpenedInTransaction;
@@ -218,11 +216,14 @@
private final int fileOperationRetryMsDelay;
+ private final IHeapRecordFactory heapRecordFactory;
+
private IDataRecordKeyManager dataRecordKeyManager;
/**
* construction for crash safe mode disabled<br>
*
+ * @param heapRecordFactory
* @param fileForStorable
* data file
* @param deleteRenaming
@@ -231,11 +232,13 @@
* @param fileOperationRetryMsDelay
* @param openFileTraceFile
*/
- public HeapElementManager(final IFileForStorable fileForStorable,
+ public HeapElementManager(final IHeapRecordFactory heapRecordFactory,
+ final IFileForStorable fileForStorable,
final boolean deleteRenaming, final boolean clearResizeFile,
final int maxFileOperationRetry,
final int fileOperationRetryMsDelay, final File openFileTraceFile) {
super();
+ this.heapRecordFactory = heapRecordFactory;
crashSafeMode = false;
fileOpenedInTransaction = false;
this.fileForStorable = fileForStorable;
@@ -253,6 +256,7 @@
/**
* construction for crash safe mode enabled<br>
*
+ * @param heapRecordFactory
* @param fileForStorable
* data file
* @param stateOkFlagFile
@@ -271,14 +275,15 @@
* @throws HeapException
* no files in stable state
*/
- public HeapElementManager(final FileForStorable fileForStorable,
- final File stateOkFlagFile,
+ public HeapElementManager(final IHeapRecordFactory heapRecordFactory,
+ final FileForStorable fileForStorable, final File stateOkFlagFile,
final FileForStorable fileForStorableBackup,
final File stateBackupOkFlagFile, final File globalStateFlagFile,
final boolean deleteRenaming, final boolean clearResizeFile,
final int maxFileOperationRetry,
final int fileOperationRetryMsDelay, final File openFileTraceFile) {
super();
+ this.heapRecordFactory = heapRecordFactory;
crashSafeMode = true;
fileOpenedInTransaction = true;
this.fileForStorable = fileForStorable;
@@ -785,9 +790,9 @@
throws HeapException {
long lastWrotePositionInFile = HeapHeader.HEAP_HEADER_SIZE - 1;
long lastRecordPositionInFile = 0;
- for (Map.Entry<Long, HeapRecord> entry : heapRecordToWriteMap
+ for (Map.Entry<Long, IFileStorable> entry : heapRecordToWriteMap
.entrySet()) {
- final HeapRecord heapRecord = entry.getValue();
+ final IFileStorable heapRecord = entry.getValue();
try {
assert false;
} catch (AssertionError error) {
@@ -821,7 +826,7 @@
previousRecordPositionInFile);
}
lastWrotePositionInFile = recordPositionInFile
- + heapRecord.getAreaSize() - 1;
+ + heapRecord.getRecordSize() - 1;
lastRecordPositionInFile = recordPositionInFile;
}
saveHeapRecord(heapRecord, toBackupList);
@@ -915,7 +920,7 @@
}
}
- private void saveHeapRecord(final HeapRecord heapRecord,
+ private void saveHeapRecord(final IFileStorable heapRecord,
final Set<ToBackupRecord> toBackupList) throws HeapException {
// ASSERTX
assert heapRecord.isValueChangedToSave() : "value not change for:\n"
@@ -943,28 +948,27 @@
final long previousRecordPositionInFile) throws HeapException {
String nextOfPrevious;
try {
- final HeapRecord heapRecord2 = readHeapRecordInFile(previousRecordPositionInFile);
+ final IFileStorable heapRecord = readHeapRecordInFile(previousRecordPositionInFile);
final long nNextOfPrevious = previousRecordPositionInFile
- + heapRecord2.getAreaSize();
+ + heapRecord.getRecordSize();
nextOfPrevious = String.valueOf(nNextOfPrevious);
} catch (Exception exception) {
nextOfPrevious = exception.getMessage();
}
- final HeapRecord heapRecord2 = heapRecordToWriteMap
+ final IFileStorable heapRecord = heapRecordToWriteMap
.get(lastRecordPositionInFile);
final long nEndOfFilePosition = lastRecordPositionInFile
- + heapRecord2.getAreaSize();
+ + heapRecord.getRecordSize();
throw new HeapException("for record at " + recordPositionInFile
+ ", bad previous position in file "
+ previousRecordPositionInFile + ", last record position is "
+ lastRecordPositionInFile + ",next of previous position "
+ nextOfPrevious + ",next of last " + nEndOfFilePosition
- + ", last is free " + heapRecord2.isFreeRecord()
- + ", last size " + heapRecord2.getAreaSize() + ", "
- + Thread.currentThread().getName());
+ + Thread.currentThread().getName() + "\nrecord:\n"
+ + heapRecord.toString());
}
/**
@@ -975,9 +979,10 @@
* @return
* @throws HeapException
*/
- private HeapRecord readHeapRecordInFile(final long positionInFile)
+ private IFileStorable readHeapRecordInFile(final long positionInFile)
throws HeapException {
- final HeapRecord heapRecord = new HeapRecord(this, positionInFile);
+ final IFileStorable heapRecord = heapRecordFactory.createHeapRecord(
+ this, positionInFile);
try {
heapRecord.readFromFile();
} catch (HeapException exception) {
@@ -1002,7 +1007,7 @@
}
@Override
- public HeapRecord readHeapFileDataRecord(final long positionInFile)
+ public IFileStorable readHeapFileDataRecord(final long positionInFile)
throws HeapException {
assertTransactionOpened();
// ASSERTX
@@ -1012,13 +1017,14 @@
* same record is returned in same transaction<br>
*/
- HeapRecord heapRecord;
+ IFileStorable heapRecord;
heapRecord = heapRecordToWriteMap.get(positionInFile);
if (heapRecord == null) {
heapRecord = readHeapRecordMap.get(positionInFile);
}
if (heapRecord == null) {
- heapRecord = new HeapRecord(this, positionInFile);
+ heapRecord = heapRecordFactory.createHeapRecord(this,
+ positionInFile);
try {
heapRecord.readFromFile();
} catch (HeapException exception) {
@@ -1035,22 +1041,26 @@
private boolean assertPositionInFile(final long positionInFile)
throws HeapException {
- if (positionInFile < HeapHeader.HEAP_HEADER_SIZE) {
+ if (positionInFile < header.getRecordSize()) {
LOGGER.fatal(READ_HEAP_FILE_NODE_FAILED + " position in heap file "
+ positionInFile + " in header of size="
- + HeapHeader.HEAP_HEADER_SIZE);
+ + header.getRecordSize());
throw new HeapException(READ_HEAP_FILE_NODE_FAILED);
}
return true;
}
@Override
- public void appendHeapFileRecord(final HeapRecord heapRecord)
+ public void appendHeapFileRecord(final IFileStorable heapRecord)
throws HeapException {
final long positionInFile = heapRecord.getPositionInFile();
- HeapRecord previous;
- previous = heapRecordToWriteMap.put(positionInFile, heapRecord);
// ASSERTX
+ assert assertTransactionOpened();
+ // ASSERTX
+ assert assertPositionInFile(positionInFile);
+ IFileStorable previous = heapRecordToWriteMap.put(positionInFile,
+ heapRecord);
+ // ASSERTX
assert assertNoPreviousOrPreviousSameAsCurrent(previous, heapRecord);
if (previous == null) {
previous = readHeapRecordMap.remove(positionInFile);
@@ -1062,38 +1072,8 @@
}
}
- @Override
- public HeapRecord newHeapFileRecord(final long positionInFile,
- final long previousRecordPositionInFile,
- final DataRecordIdentifier nodeIdentification,
- final boolean freeRecord, final Integer dataAssociatedSize,
- final int areaSize) throws HeapException {
- // ASSERTX
- assert assertTransactionOpened();
- // ASSERTX
- assert assertPositionInFile(positionInFile);
- // ASSERTX
- assert !readHeapRecordMap.containsKey(positionInFile) : "must not exist in read cache";
- final HeapRecord heapRecord = new HeapRecord(this, positionInFile,
- previousRecordPositionInFile, nodeIdentification, freeRecord,
- dataAssociatedSize, areaSize);
- final HeapRecord previous = heapRecordToWriteMap.put(positionInFile,
- heapRecord);
- // ASSERTX
- assert assertNoPrevious(heapRecord, previous);
- return heapRecord;
- }
-
- private boolean assertNoPrevious(final HeapRecord heapRecord,
- final HeapRecord previous) throws HeapException {
- if (previous != null) {
- mustNotHavePreviousException(previous, heapRecord);
- }
- return true;
- }
-
private boolean assertNoPreviousOrPreviousSameAsCurrent(
- final HeapRecord previous, final HeapRecord current)
+ final IFileStorable previous, final IFileStorable current)
throws HeapException {
if (previous != null && previous != current) {// NOPMD I want to compare
// reference
@@ -1102,8 +1082,8 @@
return true;
}
- private void mustNotHavePreviousException(final HeapRecord previous,
- final HeapRecord current) throws HeapException {
+ private void mustNotHavePreviousException(final IFileStorable previous,
+ final IFileStorable current) throws HeapException {
throw new HeapException(
"must not have a previous heap record: current=\n"
+ current.toString() + "\nprevious=\n"
@@ -1141,7 +1121,7 @@
* @return heap record in read cache for position in file, null if none
*/
@Fortest
- public HeapRecord getHeapFileRecordInReadCache(final long positionInFile) {
+ public IFileStorable getHeapFileRecordInReadCache(final long positionInFile) {
return readHeapRecordMap.get(positionInFile);
}
@@ -1153,7 +1133,7 @@
* @return heap record in write cache for position in file, null if none
*/
@Fortest
- public HeapRecord getHeapFileRecordInWriteCache(final long positionInFile) {
+ public IFileStorable getHeapFileRecordInWriteCache(final long positionInFile) {
return heapRecordToWriteMap.get(positionInFile);
}
}
Modified: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/HeapFreeNodeManager.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/HeapFreeNodeManager.java 2012-04-18 00:29:55 UTC (rev 3015)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/HeapFreeNodeManager.java 2012-04-18 03:29:16 UTC (rev 3016)
@@ -57,7 +57,7 @@
if (freeRootNodeFilePosition == -1) {
heapFreeRootNode = null;
} else {
- final HeapRecord heapRecord = heapElementManager
+ final HeapRecord heapRecord = (HeapRecord) heapElementManager
.readHeapFileDataRecord(freeRootNodeFilePosition);
heapFreeRootNode = (HeapFreeNode) heapRecord.getFreeNode();
}
Modified: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/HeapIdNodeManager.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/HeapIdNodeManager.java 2012-04-18 00:29:55 UTC (rev 3015)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/HeapIdNodeManager.java 2012-04-18 03:29:16 UTC (rev 3016)
@@ -64,7 +64,7 @@
if (idRootNodeFilePosition == -1) {
heapIdRootNode = null;
} else {
- final HeapRecord heapRecord = heapElementManager
+ final HeapRecord heapRecord = (HeapRecord) heapElementManager
.readHeapFileDataRecord(idRootNodeFilePosition);
heapIdRootNode = (HeapIdNode) heapRecord.getIdNode();
}
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-18 00:29:55 UTC (rev 3015)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/IHeapElementManager.java 2012-04-18 03:29:16 UTC (rev 3016)
@@ -17,16 +17,14 @@
package net.sf.joafip.heapfile.record.service;
import net.sf.joafip.heapfile.record.entity.HeapHeader;
-import net.sf.joafip.heapfile.record.entity.HeapRecord;
import net.sf.joafip.kvstore.entity.IFileStorable;
-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>
- * elements are {@link HeapHeader} and {@link HeapRecord}<br>
+ * elements are one header and the heap records<br>
* heap element are heap header, heap record, and data block<br>
* make able to get the heap element for read and possible modification<br>
* the closing save the header and records modification<br>
@@ -144,7 +142,7 @@
* @throws HeapException
*
*/
- HeapRecord readHeapFileDataRecord(final long positionInFile)
+ IFileStorable readHeapFileDataRecord(final long positionInFile)
throws HeapException;
/**
@@ -154,35 +152,13 @@
* the heap file record to append
* @throws HeapException
*/
- void appendHeapFileRecord(final HeapRecord heapRecord) throws HeapException;
+ void appendHeapFileRecord(IFileStorable heapRecord) throws HeapException;
/**
*
* @param positionInFile
- * record position in file
- * @param previousRecordPositionInFile
- * previous record position in file
- * @param nodeIdentification
- * data node identification number
- * @param freeRecord
- * true if free record
- * @param dataAssociatedSize
- * size of associated data if not a free record, else it is null
- * @param areaSize
- * area size for this record
- * @return
* @throws HeapException
*/
- HeapRecord newHeapFileRecord(long positionInFile,
- long previousRecordPositionInFile,
- DataRecordIdentifier nodeIdentification, boolean freeRecord,
- Integer dataAssociatedSize, int areaSize) throws HeapException;
-
- /**
- *
- * @param positionInFile
- * @throws HeapException
- */
void delete(long positionInFile) throws HeapException;
/**
Added: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/IHeapRecordFactory.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/IHeapRecordFactory.java (rev 0)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/IHeapRecordFactory.java 2012-04-18 03:29:16 UTC (rev 3016)
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2012 Luc Peuvrier
+ * All rights reserved.
+ *
+ * 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.record.service;
+
+import net.sf.joafip.kvstore.entity.IFileStorable;
+import net.sf.joafip.kvstore.service.HeapException;
+
+/**
+ *
+ * @author luc peuvrier
+ *
+ */
+public interface IHeapRecordFactory {
+
+ IFileStorable createHeapRecord(HeapElementManager heapElementManager,
+ long positionInFile) throws HeapException;
+}
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-18 00:29:55 UTC (rev 3015)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/service/HeapFileDataManager.java 2012-04-18 03:29:16 UTC (rev 3016)
@@ -36,7 +36,9 @@
import net.sf.joafip.heapfile.record.service.HeapFreeNodeManager;
import net.sf.joafip.heapfile.record.service.HeapIdNodeManager;
import net.sf.joafip.heapfile.record.service.IHeapElementManager;
+import net.sf.joafip.heapfile.record.service.IHeapRecordFactory;
import net.sf.joafip.kvstore.entity.EnumFileState;
+import net.sf.joafip.kvstore.entity.IFileStorable;
import net.sf.joafip.kvstore.record.entity.DataRecordIdentifier;
import net.sf.joafip.kvstore.record.service.IDataRecordKeyManager;
import net.sf.joafip.kvstore.service.AbstractHeapDataManager;
@@ -54,7 +56,8 @@
*
*/
@NotStorableClass
-public class HeapFileDataManager extends AbstractHeapDataManager {// NOPMD
+public class HeapFileDataManager extends AbstractHeapDataManager implements
+ IHeapRecordFactory {// NOPMD
// FIXMELUC ______HeapFileDataManager not accept empty data
@@ -182,13 +185,13 @@
}
final File openFileTraceFile = setup.getOpenFileTraceFile();
if (crashSafeMode) {
- heapElementManager = new HeapElementManager(fileForStorable,
+ heapElementManager = new HeapElementManager(this, fileForStorable,
stateOkFlagFile, fileForStorableBackup,
stateBackupOkFlagFile, globalStateFlagFile, deleteRenaming,
clearResizeFile, maxFileOperationRetry,
fileOperationRetryMsDelay, openFileTraceFile);
} else {
- heapElementManager = new HeapElementManager(fileForStorable,
+ heapElementManager = new HeapElementManager(this, fileForStorable,
deleteRenaming, clearResizeFile, maxFileOperationRetry,
fileOperationRetryMsDelay, openFileTraceFile);
}
@@ -365,22 +368,21 @@
/* will update heap header for free and used size */
final HeapHeader heapHeader = (HeapHeader) heapElementManager
.getHeapHeader();
- heapHeader.addFreeSize(heapRecord.getAreaSize());
+ heapHeader.addFreeSize(heapRecord.getRecordSize());
long positionInFile = heapRecord.getPreviousRecordPositionInFile();
final HeapRecord previous;
if (positionInFile == -1) {
previous = null;
} else {
- previous = heapElementManager
- .readHeapFileDataRecord(positionInFile);
+ previous = readHeapFileDataRecord(positionInFile);
}
positionInFile = heapRecord.getNextRecordFilePosition();
final HeapRecord next;
if (positionInFile == -1) {
next = null;
} else {
- next = heapElementManager.readHeapFileDataRecord(positionInFile);
+ next = readHeapFileDataRecord(positionInFile);
}
if (canMergeWithOnlyPrevious(heapRecord, previous, next)) {
@@ -415,22 +417,26 @@
&& previous.isFreeRecord()
&& next != null
&& next.isFreeRecord()
- && !intOverflow(heapRecord.getAreaSize(),
- previous.getAreaSize(), next.getAreaSize());
+ && !intOverflow(heapRecord.getRecordSize(),
+ previous.getRecordSize(), next.getRecordSize());
}
private boolean canMergeWithOnlyNext(final HeapRecord heapRecord,
final HeapRecord previous, final HeapRecord next)
throws HeapException {
- return (previous == null || !previous.isFreeRecord()) && next != null
+ return (previous == null || !previous.isFreeRecord())
+ && next != null
&& next.isFreeRecord()
- && !intOverflow(heapRecord.getAreaSize(), next.getAreaSize());
+ && !intOverflow(heapRecord.getRecordSize(),
+ next.getRecordSize());
}
private boolean canMergeWithNextIgnorePrevious(final HeapRecord heapRecord,
final HeapRecord next) throws HeapException {
- return next != null && next.isFreeRecord()
- && !intOverflow(heapRecord.getAreaSize(), next.getAreaSize());
+ return next != null
+ && next.isFreeRecord()
+ && !intOverflow(heapRecord.getRecordSize(),
+ next.getRecordSize());
}
private boolean canMergeWithOnlyPrevious(final HeapRecord heapRecord,
@@ -439,16 +445,16 @@
return previous != null
&& previous.isFreeRecord()
&& (next == null || !next.isFreeRecord())
- && !intOverflow(heapRecord.getAreaSize(),
- previous.getAreaSize());
+ && !intOverflow(heapRecord.getRecordSize(),
+ previous.getRecordSize());
}
private boolean canMergeWithPreviousIgnoreNext(final HeapRecord heapRecord,
final HeapRecord previous) throws HeapException {
return previous != null
&& previous.isFreeRecord()
- && !intOverflow(heapRecord.getAreaSize(),
- previous.getAreaSize());
+ && !intOverflow(heapRecord.getRecordSize(),
+ previous.getRecordSize());
}
private void freeRecord(final long positionInFile, final int areaSize,
@@ -460,12 +466,11 @@
}
// ASSERTX
assert freeHeapRecord.isFreeRecord() : "must be a free heap record";
- freeHeapRecord.setAreaSize(areaSize);
+ freeHeapRecord.setRecordSize(areaSize);
freeNodeTreeAppend(freeHeapRecord);
if (nextPositionInFile != -1) {
- final HeapRecord nextOfFreeHeapRecord = heapElementManager
- .readHeapFileDataRecord(nextPositionInFile);
+ final HeapRecord nextOfFreeHeapRecord = readHeapFileDataRecord(nextPositionInFile);
nextOfFreeHeapRecord
.setPreviousRecordPositionInFile(positionInFile);
}
@@ -473,7 +478,7 @@
private void noMerge(final HeapRecord heapRecord) throws HeapException {
heapRecord.freeRecord();
- freeRecord(heapRecord.getPositionInFile(), heapRecord.getAreaSize(),
+ freeRecord(heapRecord.getPositionInFile(), heapRecord.getRecordSize(),
heapRecord.getNextRecordFilePosition(), heapRecord);
}
@@ -481,8 +486,8 @@
final HeapRecord next, final HeapRecord heapRecord,
final HeapHeader heapHeader) throws HeapException {
final long positionInFile = previous.getPositionInFile();
- final int areaSize = previous.getAreaSize() + next.getAreaSize()
- + heapRecord.getAreaSize();
+ final int areaSize = previous.getRecordSize() + next.getRecordSize()
+ + heapRecord.getRecordSize();
/* first delete from free node tree */
freeNodeTreeDelete(previous);
freeNodeTreeDelete(next);
@@ -504,7 +509,7 @@
throws HeapException {
heapRecord.freeRecord();
final long positionInFile = heapRecord.getPositionInFile();
- final int areaSize = next.getAreaSize() + heapRecord.getAreaSize();
+ final int areaSize = next.getRecordSize() + heapRecord.getRecordSize();
/* first delete from free node tree */
final HeapRecord nextHeapRecord = next;
freeNodeTreeDelete(nextHeapRecord);
@@ -523,7 +528,8 @@
throws HeapException {
heapElementManager.delete(heapRecord.getPositionInFile());
final long positionInFile = previous.getPositionInFile();
- final int areaSize = previous.getAreaSize() + heapRecord.getAreaSize();
+ final int areaSize = previous.getRecordSize()
+ + heapRecord.getRecordSize();
/* first delete from free node tree */
freeNodeTreeDelete(previous);
/* then delete record */
@@ -682,6 +688,13 @@
return dataAssociated;
}
+ @Override
+ public IFileStorable createHeapRecord(
+ final HeapElementManager heapElementManager,
+ final long positionInFile) throws HeapException {
+ return new HeapRecord(heapElementManager, positionInFile);
+ }
+
/**
* @param neededDataSize
* the needed data associated size
@@ -697,13 +710,13 @@
*/
private HeapRecord createRecordForDataFromFree(final int neededDataSize,
final DataRecordIdentifier nodeIdentifier,
- final int neededAreaSize, final HeapFreeNode freeNode)
+ final int neededRecordSize, final HeapFreeNode freeNode)
throws HeapException {
final HeapRecord heapRecord;
/* usable free area size */
- final int freeAreaSize = freeNode.getAreaSize();
+ final int freeRecordSize = freeNode.getRecordSize();
/* unused area size is usable minus needed area size */
- final int newFreeAreaSize = freeAreaSize - neededAreaSize;
+ final int newFreeRecordSize = freeRecordSize - neededRecordSize;
heapRecord = freeNode.getHeapRecord();
/* no more free */
freeNodeTreeDelete(heapRecord);
@@ -713,24 +726,35 @@
final HeapHeader heapHeader = (HeapHeader) heapElementManager
.getHeapHeader();
- if (newFreeAreaSize >= HeapRecord.MAX_RECORD_HEADER_SIZE
+ if (newFreeRecordSize >= HeapRecord.MAX_RECORD_HEADER_SIZE
+ MINIMUM_RECORD_DATA_SIZE + 4) {
/*
* split free record : one part used for data the other keep free
*/
final long nextRecordFilePosition = heapRecord
.getNextRecordFilePosition();
- heapRecord.unfreeRecord(neededDataSize, neededAreaSize,
+ heapRecord.unfreeRecord(neededDataSize, neededRecordSize,
nodeIdentifier, false);
final long positionInFile = heapRecord.getPositionInFile();
final long newFreeRecordPositionInFile = positionInFile
- + neededAreaSize;
- final HeapRecord newFreeHeapRecord = heapElementManager
- .newHeapFileRecord(
- newFreeRecordPositionInFile/* positionInFile */,
- positionInFile/* previousPositionInFile */,
- null/* nodeIdentifier */, true/* freeRecord */,
- null/* newFreeDataSize */, newFreeAreaSize);
+ + neededRecordSize;
+
+ final HeapRecord newFreeHeapRecord = new HeapRecord(
+ heapElementManager,
+ newFreeRecordPositionInFile/* positionInFile */,
+ positionInFile/* previousPositionInFile */,
+ null/* nodeIdentifier */, true/* freeRecord */,
+ 0/* dataAssociatedSize */, newFreeRecordSize/* recordSize */);
+
+ // final HeapRecord newFreeHeapRecord = heapElementManager
+ // .newHeapFileRecord(
+ // newFreeRecordPositionInFile/* positionInFile */,
+ // positionInFile/* previousPositionInFile */,
+ // null/* nodeIdentifier */, true/* freeRecord */,
+ // null/* dataAssociatedSize */, newFreeRecordSize/*recordSize*/);
+
+ heapElementManager.appendHeapFileRecord(newFreeHeapRecord);
+
if (heapRecord.getPositionInFile() == heapHeader
.getLastRecordPositionInFile()) {
heapHeader
@@ -738,15 +762,14 @@
}
freeNodeTreeAppend(newFreeHeapRecord);
if (nextRecordFilePosition != -1) {
- final HeapRecord nextHeapRecord = heapElementManager
- .readHeapFileDataRecord(nextRecordFilePosition);
+ final HeapRecord nextHeapRecord = readHeapFileDataRecord(nextRecordFilePosition);
nextHeapRecord
.setPreviousRecordPositionInFile(newFreeRecordPositionInFile);
}
} else {
heapRecord.unfreeRecord(neededDataSize, nodeIdentifier);
}
- heapHeader.unfreeSize(heapRecord.getAreaSize());
+ heapHeader.unfreeSize(heapRecord.getRecordSize());
return heapRecord;
}
@@ -786,9 +809,13 @@
heapHeader.setLastRecordPositionInFile(positionInFile);
heapHeader.addUsedSize(needAreaSize);
/* new at end of file with a new record identification */
- final HeapRecord heapRecord = heapElementManager.newHeapFileRecord(
+ final HeapRecord heapRecord = new HeapRecord(heapElementManager,
positionInFile, previousRecordPositionInFile, nodeIdentifier,
false, needDataSize, needAreaSize);
+ // final HeapRecord heapRecord = heapElementManager.newHeapFileRecord(
+ // positionInFile, previousRecordPositionInFile, nodeIdentifier,
+ // false, needDataSize, needAreaSize);
+ heapElementManager.appendHeapFileRecord(heapRecord);
return heapRecord;
}
@@ -817,12 +844,17 @@
heapRecord = null;
} else {
final long positionInFile = heapIdNode.getPositionInFile();
- heapRecord = heapElementManager
- .readHeapFileDataRecord(positionInFile);
+ heapRecord = readHeapFileDataRecord(positionInFile);
}
return heapRecord;
}
+ private HeapRecord readHeapFileDataRecord(final long recordFilePosition)
+ throws HeapException {
+ return (HeapRecord) heapElementManager
+ .readHeapFileDataRecord(recordFilePosition);
+ }
+
@Override
protected boolean hasDataRecordImpl(
final DataRecordIdentifier dataRecordIdentifier)
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-18 00:29:55 UTC (rev 3015)
+++ trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/TestIdNode.java 2012-04-18 03:29:16 UTC (rev 3016)
@@ -27,13 +27,17 @@
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.record.service.IHeapRecordFactory;
+import net.sf.joafip.kvstore.entity.IFileStorable;
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.service.RBTException;
@NotStorableClass
@DoNotTransform
-public class TestIdNode extends AbstractCommonDeleteFileTestCase {// NOPMD
+public class TestIdNode extends AbstractCommonDeleteFileTestCase implements
+ IHeapRecordFactory {// NOPMD
private HeapIdNode heapIdNode; // NOPMD
@@ -78,7 +82,7 @@
fileForStorable = new FileForStorable(dataFile, 1, 0);
backupFileForStorable = new FileForStorable(backupFile, 1, 0);
- heapElementManager = new HeapElementManager(fileForStorable,
+ heapElementManager = new HeapElementManager(this, fileForStorable,
stateDataFile, backupFileForStorable, stateDataBackupFile,
globalStateFlag, false, false, 1, 0, openFileTraceFile);
heapElementManager.setHeapHeader(new HeapHeader(heapElementManager));
@@ -87,12 +91,21 @@
// final HeapRecord heapRecord = new HeapRecord(heapElementManager,
// HeapHeader.HEAP_HEADER_SIZE);
- final HeapRecord heapRecord = heapElementManager.newHeapFileRecord(
+ final HeapRecord heapRecord = new HeapRecord(heapElementManager,
HeapHeader.HEAP_HEADER_SIZE,
- -1/* previousRecordPositionInFile */,
+ -1L/* previousRecordPositionInFile */,
DataRecordIdentifier.ZERO/* nodeIdentification */,
false/* freeRecord */, 0/* dataAssociatedSize */,
HeapRecord.DATA_RECORD_HEADER_SIZE + 4/* areaSize */);
+
+ // final HeapRecord heapRecord = heapElementManager.newHeapFileRecord(
+ // HeapHeader.HEAP_HEADER_SIZE,
+ // -1/* previousRecordPositionInFile */,
+ // DataRecordIdentifier.ZERO/* nodeIdentification */,
+ // false/* freeRecord */, 0/* dataAssociatedSize */,
+ // HeapRecord.DATA_RECORD_HEADER_SIZE + 4/* areaSize */);
+ heapElementManager.appendHeapFileRecord(heapRecord);
+
heapIdNode = new HeapIdNode(heapElementManager,
heapRecord.getPositionInFile(), -1, -1, -1, false, false, 0,
heapRecord.getNodeIdentifier());
@@ -130,4 +143,10 @@
super.tearDown();
}
+ @Override
+ public IFileStorable createHeapRecord(
+ final HeapElementManager heapElementManager,
+ final long positionInFile) throws HeapException {
+ return new HeapRecord(heapElementManager, positionInFile);
+ }
}
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-18 00:29:55 UTC (rev 3015)
+++ trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestHeapElementManager.java 2012-04-18 03:29:16 UTC (rev 3016)
@@ -25,6 +25,7 @@
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.kvstore.entity.IFileStorable;
import net.sf.joafip.kvstore.record.entity.DataRecordIdentifier;
import net.sf.joafip.kvstore.service.FileForStorable;
import net.sf.joafip.kvstore.service.HeapException;
@@ -33,7 +34,8 @@
@NotStorableClass
@DoNotTransform
-public class TestHeapElementManager extends AbstractCommonDeleteFileTestCase {// NOPMD
+public class TestHeapElementManager extends AbstractCommonDeleteFileTestCase
+ implements IHeapRecordFactory {// NOPMD
private FileForStorable fileForStorable; // NOPMD
@@ -84,7 +86,7 @@
globalStateFile = new File(runtimePath + File.separator + "global.flag");
final File openFileTraceFile = new File(runtimePath + File.separator
+ "trace.txt");
- heapElementManager = new HeapElementManager(fileForStorable,
+ heapElementManager = new HeapElementManager(this, fileForStorable,
stateDataFile, backupFileForStorable, stateDataBackupFile,
globalStateFile, false, false, 1, 0, openFileTraceFile);
heapElementManager.setHeapHeader(new HeapHeader(heapElementManager));
@@ -188,8 +190,7 @@
heapElementManager.openTransaction();
final int dataSize = 100;
- final HeapRecord heapRecord1 = heapElementManager
- .readHeapFileDataRecord(record1pos);
+ final HeapRecord heapRecord1 = readHeapFileDataRecord(record1pos);
assertEquals("bad record position", record1pos,
heapRecord1.getPositionInFile());
assertEquals("bad record data size", dataSize, heapRecord1
@@ -202,8 +203,7 @@
assertEquals("read must be equals appened", heapRecordAppened1,
heapRecord1);
- final HeapRecord heapRecord2 = heapElementManager
- .readHeapFileDataRecord(record2pos);
+ final HeapRecord heapRecord2 = readHeapFileDataRecord(record2pos);
assertEquals("bad record position", record2pos,
heapRecord2.getPositionInFile());
assertEquals("bad record data size", dataSize, heapRecord2
@@ -228,7 +228,7 @@
heapElementManager.startService();
heapElementManager.openTransaction();
- HeapRecord heapRecord1 = heapElementManager.readHeapFileDataRecord(pos);
+ HeapRecord heapRecord1 = readHeapFileDataRecord(pos);
byte[] data1 = heapRecord1.getDataAssociated();
for (int index = 0; index < data1.length; index++) {
data1[index] = (byte) (data1.length - index);
@@ -237,7 +237,7 @@
heapElementManager.closeTransaction();
heapElementManager.openTransaction();
- heapRecord1 = heapElementManager.readHeapFileDataRecord(pos);
+ heapRecord1 = readHeapFileDataRecord(pos);
data1 = heapRecord1.getDataAssociated();
for (int index = 0; index < data1.length; index++) {
assertEquals("bad record data", data1.length - index, data1[index]);
@@ -254,7 +254,7 @@
heapElementManager.startService();
heapElementManager.openTransaction();
- HeapRecord heapRecord1 = heapElementManager.readHeapFileDataRecord(pos);
+ HeapRecord heapRecord1 = readHeapFileDataRecord(pos);
try {
assert false;
@@ -280,14 +280,14 @@
heapElementManager.closeTransaction();
heapElementManager.openTransaction();
- heapRecord1 = heapElementManager.readHeapFileDataRecord(pos);
+ heapRecord1 = readHeapFileDataRecord(pos);
node = heapRecord1.getIdNode();
assertEquals("color must have changed", !color, node.getColor());
assertFalse("not modified record", heapRecord1.isValueChangedToSave());
heapElementManager.closeTransaction();
heapElementManager.openTransaction();
- heapRecord1 = heapElementManager.readHeapFileDataRecord(pos);
+ heapRecord1 = readHeapFileDataRecord(pos);
heapRecord1.freeRecord();
try {
assert false;
@@ -310,7 +310,7 @@
heapElementManager.closeTransaction();
heapElementManager.openTransaction();
- heapRecord1 = heapElementManager.readHeapFileDataRecord(pos);
+ heapRecord1 = readHeapFileDataRecord(pos);
node = heapRecord1.getFreeNode();
assertEquals("color must have changed", !color, node.getColor());
heapElementManager.closeTransaction();
@@ -325,7 +325,7 @@
/*
* read record 1 whithout data managing and check state
*/
- HeapRecord heapRecord1 = heapElementManager.readHeapFileDataRecord(
+ HeapRecord heapRecord1 = readHeapFileDataRecord(
/**/pos/* position in file */);
assertFalse("record1 must be data record", heapRecord1.isFreeRecord());
assertFalse("record1 must be in state just created",
@@ -349,7 +349,7 @@
/*
* change to manage data mode
*/
- heapRecord1 = heapElementManager.readHeapFileDataRecord(
+ heapRecord1 = readHeapFileDataRecord(
/**/pos/* position in file */);
assertFalse("record1 must be data record", heapRecord1.isFreeRecord());
assertFalse("must not be just created", heapRecord1.isJustCreated());
@@ -402,13 +402,21 @@
record1pos = HeapHeader.HEAP_HEADER_SIZE;
final int dataSize = 100;
final DataRecordIdentifier dataRecordIdentifier = DataRecordIdentifier.ZERO;
- heapRecordAppened1 = heapElementManager.newHeapFileRecord(record1pos,
+ heapRecordAppened1 = new HeapRecord(heapElementManager, record1pos,
-1L/* prev pos */, dataRecordIdentifier/* id */,
false/* free */, dataSize, dataSize
+ HeapRecord.MAX_RECORD_HEADER_SIZE + 4/*
* area size: +
* 4 for crc32
*/);
+ // heapRecordAppened1 = heapElementManager.newHeapFileRecord(record1pos,
+ // -1L/* prev pos */, dataRecordIdentifier/* id */,
+ // false/* free */, dataSize, dataSize
+ // + HeapRecord.MAX_RECORD_HEADER_SIZE + 4/*
+ // * area size: +
+ // * 4 for crc32
+ // */);
+ heapElementManager.appendHeapFileRecord(heapRecordAppened1);
try {
assert false;
} catch (AssertionError error) {
@@ -431,13 +439,20 @@
heapElementManager.openTransaction();
- record2pos = record1pos + heapRecordAppened1.getAreaSize();
- heapRecordAppened2 = heapElementManager.newHeapFileRecord(record2pos,
+ record2pos = record1pos + heapRecordAppened1.getRecordSize();
+ heapRecordAppened2 = new HeapRecord(heapElementManager, record2pos,
record1pos, dataRecordIdentifier/* id */, false/* free */,
dataSize, dataSize + HeapRecord.MAX_RECORD_HEADER_SIZE + 4/*
* 4 for
* crc32
*/);
+ // heapRecordAppened2 = heapElementManager.newHeapFileRecord(record2pos,
+ // record1pos, dataRecordIdentifier/* id */, false/* free */,
+ // dataSize, dataSize + HeapRecord.MAX_RECORD_HEADER_SIZE + 4/*
+ // * 4 for
+ // * crc32
+ // */);
+ heapElementManager.appendHeapFileRecord(heapRecordAppened2);
heapRecordAppened2.setDataAssociated(testData);
heapElementManager.closeTransaction();
heapElementManager.stopService();
@@ -467,4 +482,17 @@
}
return heapHeader;
}
+
+ private HeapRecord readHeapFileDataRecord(final long positionInFile)
+ throws HeapException {
+ return (HeapRecord) heapElementManager
+ .readHeapFileDataRecord(positionInFile);
+ }
+
+ @Override
+ public IFileStorable createHeapRecord(
+ final HeapElementManager heapElementManager,
+ final long positionInFile) throws HeapException {
+ return new HeapRecord(heapElementManager, positionInFile);
+ }
}
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-18 00:29:55 UTC (rev 3015)
+++ trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestHeapRecordManage.java 2012-04-18 03:29:16 UTC (rev 3016)
@@ -26,6 +26,7 @@
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.kvstore.entity.IFileStorable;
import net.sf.joafip.kvstore.record.entity.DataRecordIdentifier;
import net.sf.joafip.kvstore.service.FileForStorable;
import net.sf.joafip.kvstore.service.HeapException;
@@ -35,7 +36,8 @@
@NotStorableClass
@DoNotTransform
-public class TestHeapRecordManage extends AbstractCommonDeleteFileTestCase {// NOPMD
+public class TestHeapRecordManage extends AbstractCommonDeleteFileTestCase
+ implements IHeapRecordFactory {// NOPMD
/** the heap element manager, data record are accessible by position in file */
private IHeapElementManager heapElementManager; // NOPMD
@@ -80,7 +82,7 @@
fileForStorable = new FileForStorable(dataFile, 1, 0);
final FileForStorable backupFileForStorage;
backupFileForStorage = new FileForStorable(backupFile, 1, 0);
- heapElementManager = new HeapElementManager(fileForStorable,
+ heapElementManager = new HeapElementManager(this, fileForStorable,
stateDataFile, backupFileForStorage, stateDataBackupFile,
globalStateFile, false, false, 1, 0, openFileTraceFile);
heapElementManager.setHeapHeader(new HeapHeader(heapElementManager));
@@ -122,11 +124,15 @@
for (int index = 0; index < 10; index++) {
final long position = (long) HeapHeader.HEAP_HEADER_SIZE
+ (long) index * 100;
- final HeapRecord heapRecord = heapElementManager
- .newHeapFileRecord(position, previousPosition,
- newDataRecordIdentifier(index),
- false/* freeRecord */, 10/* dataAssociatedSize */,
- 100/* areaSize */);
+ final HeapRecord heapRecord = new HeapRecord(heapElementManager,
+ position, previousPosition, newDataRecordIdentifier(index),
+ false/* freeRecord...
[truncated message content] |