[Joafip-svn] SF.net SVN: joafip:[2988] trunk
Brought to you by:
luc_peuvrier
|
From: <luc...@us...> - 2012-03-03 01:35:58
|
Revision: 2988
http://joafip.svn.sourceforge.net/joafip/?rev=2988&view=rev
Author: luc_peuvrier
Date: 2012-03-03 01:35:51 +0000 (Sat, 03 Mar 2012)
Log Message:
-----------
heap file can be use with a comparable key, map in file
Modified Paths:
--------------
trunk/joafip-heapfile/pom.xml
trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/DataRecordIdentifier.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/main/java/net/sf/joafip/heapfile/service/IHeapDataManager.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/TestStoreRestore.java
trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/BlockDataManager.java
trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/DualWrapDataManager.java
trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/HeapFileCheckerDataManager.java
trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/HeapMemoryDataManagerMock.java
trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/HeapMultiFileDataManager.java
trunk/joafip-testsuite/src/main/java/net/sf/joafip/heapfile/service/HeapFileServiceTests.java
Added Paths:
-----------
trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/DataRecordKey.java
trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/IDataRecordKey.java
trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/NullDataRecordKey.java
trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/AbstractDataRecordKeyComparator.java
trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/DataRecordKeyManager.java
trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/IDataRecordKeyComparator.java
trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/TestHeapFileDataMgrWithKey.java
Modified: trunk/joafip-heapfile/pom.xml
===================================================================
--- trunk/joafip-heapfile/pom.xml 2012-01-06 04:58:24 UTC (rev 2987)
+++ trunk/joafip-heapfile/pom.xml 2012-03-03 01:35:51 UTC (rev 2988)
@@ -27,6 +27,14 @@
<dependency>
<groupId>net.sf.joafip</groupId>
+ <artifactId>joafip-common</artifactId>
+ <version>4.0.0b8</version>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>net.sf.joafip</groupId>
<artifactId>joafip-file</artifactId>
<version>4.0.0b8</version>
</dependency>
Modified: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/DataRecordIdentifier.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/DataRecordIdentifier.java 2012-01-06 04:58:24 UTC (rev 2987)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/DataRecordIdentifier.java 2012-03-03 01:35:51 UTC (rev 2988)
@@ -19,6 +19,8 @@
import java.io.Serializable;
import net.sf.joafip.NotStorableClass;
+import net.sf.joafip.heapfile.service.HeapException;
+import net.sf.joafip.heapfile.service.HeapRuntimeException;
/**
*
@@ -37,10 +39,12 @@
public static final DataRecordIdentifier ZERO = new DataRecordIdentifier();
public static final DataRecordIdentifier LAST = new DataRecordIdentifier(
- Long.MIN_VALUE);
+ Long.MAX_VALUE);
public final long value;
+ private final IDataRecordKey dataRecordKey;
+
/**
* create the first data record identifier: #0
*
@@ -48,6 +52,7 @@
public DataRecordIdentifier() {
super();
this.value = 0;
+ this.dataRecordKey = NullDataRecordKey.getInstance();
}
/**
@@ -60,6 +65,7 @@
public DataRecordIdentifier(final int value) {
super();
this.value = value;
+ this.dataRecordKey = NullDataRecordKey.getInstance();
}
/**
@@ -71,6 +77,7 @@
public DataRecordIdentifier(final long long1) {
super();
this.value = long1;
+ this.dataRecordKey = NullDataRecordKey.getInstance();
}
/**
@@ -82,12 +89,31 @@
public DataRecordIdentifier(final DataRecordIdentifier dataRecordIdentifier) {
super();
this.value = dataRecordIdentifier.value + 1;
+ this.dataRecordKey = NullDataRecordKey.getInstance();
}
// public long getValue() {
// return value;
// }
+ public DataRecordIdentifier(final IDataRecordKey dataRecordKey) {
+ super();
+ this.value = -1;
+ this.dataRecordKey = dataRecordKey;
+ }
+
+ public IDataRecordKey getDataRecordKey() {
+ return dataRecordKey;
+ }
+
+ public int getKeyDataSize() {
+ return dataRecordKey.getKeyDataSize();
+ }
+
+ public byte[] getKeyData() {
+ return dataRecordKey.getKeyData();
+ }
+
public int compareTo(final DataRecordIdentifier dataRecordIdentifier) {
final int compareTo;
final long otherLong1 = dataRecordIdentifier.value;
@@ -96,14 +122,19 @@
} else if (value > otherLong1) {
compareTo = 1;
} else {
- compareTo = 0;
+ try {
+ compareTo = dataRecordKey
+ .compareTo(dataRecordIdentifier.dataRecordKey);
+ } catch (HeapException exception) {
+ throw new HeapRuntimeException(exception);
+ }
}
return compareTo;
}
@Override
public int hashCode() {
- return (int) (value ^ (value >>> 32));
+ return (int) (value ^ (value >>> 32)) + dataRecordKey.getKeyHashcode();
}
@Override
@@ -117,7 +148,12 @@
* if (!(obj instanceof DataRecordIdentifier)) return false;
*/
final DataRecordIdentifier other = (DataRecordIdentifier) obj;
- return value == other.value;
+ try {
+ return value == other.value
+ && dataRecordKey.compareTo(other.dataRecordKey) == 0;
+ } catch (HeapException exception) {
+ throw new HeapRuntimeException(exception);
+ }
// if (value != other.value)
// return false;
// return true;
Added: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/DataRecordKey.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/DataRecordKey.java (rev 0)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/DataRecordKey.java 2012-03-03 01:35:51 UTC (rev 2988)
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2012 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.record.entity;
+
+import net.sf.joafip.NotStorableClass;
+import net.sf.joafip.heapfile.record.service.IDataRecordKeyComparator;
+import net.sf.joafip.heapfile.service.HeapException;
+
+/**
+ *
+ * @author luc peuvrier
+ *
+ */
+@NotStorableClass
+public class DataRecordKey implements IDataRecordKey {
+
+ private final IDataRecordKeyComparator comparator;
+
+ private final byte[] data;
+
+ private transient Object key;
+
+ public DataRecordKey(final IDataRecordKeyComparator comparator,
+ final byte[] data) {// NOPMD
+ super();
+ this.comparator = comparator;
+ this.data = data;
+ }
+
+ @Override
+ public int getKeyHashcode() {
+ return 0;
+ }
+
+ @Override
+ public int compareTo(final IDataRecordKey dataRecordKey)
+ throws HeapException {
+ return comparator.compareDataRecordKey(this, dataRecordKey);
+ }
+
+ @Override
+ public int getKeyDataSize() {
+ return data.length;
+ }
+
+ @Override
+ public byte[] getKeyData() {
+ return data; // NOPMD
+ }
+
+ @Override
+ public Object getKey() {
+ return key;
+ }
+
+ @Override
+ public void setKey(final Object key) {
+ this.key = key;
+ }
+}
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-01-06 04:58:24 UTC (rev 2987)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/HeapRecord.java 2012-03-03 01:35:51 UTC (rev 2988)
@@ -102,12 +102,6 @@
*/
private boolean freeRecord;// NOPMD
- /** node identification tree root node, may be null for free record */
- // private HeapIdNode idNode;
-
- /** free area tree root node, may be null for data record */
- // private HeapFreeNode freeNode;
-
private DataRecordIdentifier nodeIdentifier;
/** position in heap file of this node parent */
@@ -157,7 +151,7 @@
/**
* construction for node creation, it is a data record, not free should be
- * only used bye {@link HeapElementManager}<br>
+ * only used by {@link HeapElementManager}<br>
*
* @param heapElementManager
* heap header and record manager in heap file
@@ -192,7 +186,8 @@
* dataAssociatedSize + 4 : +4 for crc32
*/
// ASSERTX
- assert assertAreaSizeForData(freeRecord, dataAssociatedSize, areaSize);
+ assert assertAreaSizeForData(freeRecord, nodeIdentifier,
+ dataAssociatedSize, areaSize);
this.areaSize = areaSize;
// if (freeRecord) {
// freeNode = new HeapFreeNode(heapElementManager, positionInFile,
@@ -208,10 +203,14 @@
}
private boolean assertAreaSizeForData(final boolean freeRecord,
+ final DataRecordIdentifier nodeIdentifier,
final Integer dataAssociatedSize, final Integer areaSize)
throws HeapException {
- if (!freeRecord && areaSize != null
- && areaSize < MAX_RECORD_HEADER_SIZE + dataAssociatedSize + 4) {
+ if (!freeRecord
+ && areaSize != null
+ && areaSize < MAX_RECORD_HEADER_SIZE
+ + nodeIdentifier.getKeyDataSize() + dataAssociatedSize
+ + 4) {
logger.fatal(AREA_SIZE_TO_SMALL_FOR_DATA);
throw new HeapException(new HeapException(
AREA_SIZE_TO_SMALL_FOR_DATA));
@@ -410,15 +409,10 @@
private void marshallDataRecord() throws HeapException {
updateIdNodeData();
writeInteger(dataAssociatedSize);
- // writeLong(idNode.getParentPositionInFile());
writeLong(parentPositionInFile);
- // writeLong(idNode.getLeftPositionInFile());
writeLong(leftPositionInFile);
- // writeLong(idNode.getRightPositionInFile());
writeLong(rightPositionInFile);
- // if (idNode.isColorSetted()) {
if (colorSetted) {
- // if (idNode.getColor()) {
if (color) {
writeByte((byte) 0x03);
} else {
@@ -427,10 +421,13 @@
} else {
writeByte((byte) 0x00);
}
- // writeInteger(idNode.getNumberOfChild());
writeInteger(numberOfChild);
- // writeLong(idNode.getElement().value);// write node identifier
writeLong(nodeIdentifier.value);
+ final byte[] keyData = nodeIdentifier.getKeyData();
+ if (keyData != null) {
+ writeInteger(keyData.length);
+ writeBytes(keyData);
+ }
writeCrc32();
if (dataValueChanged) {
clearCrc32();
@@ -535,30 +532,30 @@
dataAssociatedSize = readInteger();
// ASSERTX
assert dataAssociatedSize >= 0 : ASSOCIATED_DATA_SIZE_MUST_BE_DEFINED;
- // idNode.setParentPositionInFile(readLong());
parentPositionInFile = readLong();
- // idNode.setLeftPositionInFile(readLong());
leftPositionInFile = readLong();
- // idNode.setRightPositionInFile(readLong());
rightPositionInFile = readLong();
final byte colorCode = readByte();
if (colorCode == 0x02) {
- // idNode.setColorUnmarshalling(false);
colorSetted = true;
color = false;
} else if (colorCode == 0x03) {
- // idNode.setColorUnmarshalling(true);
colorSetted = true;
color = true;
} else {
colorSetted = false;
}
- // idNode.setNumberOfChildUnmarshalling(readInteger());
numberOfChild = readInteger();
final long long1 = readLong();
- // idNode.setNodeIdentifier(new DataRecordIdentifier(long1));
- nodeIdentifier = new DataRecordIdentifier(long1);
- // idNode.valueSetted();
+ if (long1 == -1) {
+ final int keySize = readInteger();
+ final byte[] keyData = readBytes(keySize);
+ final IDataRecordKey dataRecordKey = new DataRecordKey(
+ heapElementManager.getDataRecordKeyComparator(), keyData);
+ nodeIdentifier = new DataRecordIdentifier(dataRecordKey);
+ } else {
+ nodeIdentifier = new DataRecordIdentifier(long1);
+ }
readAndCheckCrc32();
}
Added: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/IDataRecordKey.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/IDataRecordKey.java (rev 0)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/IDataRecordKey.java 2012-03-03 01:35:51 UTC (rev 2988)
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2012 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.record.entity;
+
+import net.sf.joafip.heapfile.service.HeapException;
+
+/**
+ *
+ * @author luc peuvrier
+ *
+ */
+public interface IDataRecordKey {
+
+ int getKeyHashcode();
+
+ int compareTo(IDataRecordKey dataRecordKey) throws HeapException;
+
+ int getKeyDataSize();
+
+ byte[] getKeyData();
+
+ Object getKey();
+
+ void setKey(Object key);
+}
Added: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/NullDataRecordKey.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/NullDataRecordKey.java (rev 0)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/entity/NullDataRecordKey.java 2012-03-03 01:35:51 UTC (rev 2988)
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2012 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.record.entity;
+
+import java.io.Serializable;
+
+import net.sf.joafip.NotStorableClass;
+
+/**
+ *
+ * @author luc peuvrier
+ *
+ */
+@NotStorableClass
+public final class NullDataRecordKey implements IDataRecordKey, Serializable {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = -6437312314493430699L;
+
+ private final static NullDataRecordKey INSTANCE = new NullDataRecordKey();
+
+ public static NullDataRecordKey getInstance() {
+ return INSTANCE;
+ }
+
+ private NullDataRecordKey() {
+ super();
+ }
+
+ @Override
+ public int getKeyHashcode() {
+ return 0;
+ }
+
+ @Override
+ public int compareTo(final IDataRecordKey dataRecordKey) {
+ return 0;
+ }
+
+ @Override
+ public int getKeyDataSize() {
+ return 0;
+ }
+
+ @Override
+ public byte[] getKeyData() { // NOPMD
+ return null;
+ }
+
+ @Override
+ public Object getKey() {
+ // no implementation
+ return null;
+ }
+
+ @Override
+ public void setKey(final Object key) {
+ // no implementation
+ }
+}
Added: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/AbstractDataRecordKeyComparator.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/AbstractDataRecordKeyComparator.java (rev 0)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/AbstractDataRecordKeyComparator.java 2012-03-03 01:35:51 UTC (rev 2988)
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2012 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.record.service;
+
+import net.sf.joafip.NotStorableClass;
+import net.sf.joafip.heapfile.record.entity.IDataRecordKey;
+import net.sf.joafip.heapfile.service.HeapException;
+
+/**
+ *
+ * @author luc peuvrier
+ *
+ * @param <K>
+ */
+@NotStorableClass
+public abstract class AbstractDataRecordKeyComparator<K extends Comparable<K>>
+ implements IDataRecordKeyComparator {
+
+ @Override
+ public int compareDataRecordKey(final IDataRecordKey dataRecordKey1,
+ final IDataRecordKey dataRecordKey2) throws HeapException {
+ final K key1 = getObject(dataRecordKey1);
+ final K key2 = getObject(dataRecordKey2);
+ return key1.compareTo(key2);
+ }
+
+ @SuppressWarnings("unchecked")
+ private K getObject(final IDataRecordKey dataRecordKey)
+ throws HeapException {
+ K key = (K) dataRecordKey.getKey();
+ if (key == null) {
+ key = unmarshall(dataRecordKey.getKeyData());
+ dataRecordKey.setKey(key);
+ }
+ return key;
+ }
+
+ protected abstract K unmarshall(byte[] keyData) throws HeapException;
+
+}
Added: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/DataRecordKeyManager.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/DataRecordKeyManager.java (rev 0)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/DataRecordKeyManager.java 2012-03-03 01:35:51 UTC (rev 2988)
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2012 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.record.service;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+import net.sf.joafip.NotStorableClass;
+import net.sf.joafip.heapfile.record.entity.DataRecordIdentifier;
+import net.sf.joafip.heapfile.record.entity.DataRecordKey;
+import net.sf.joafip.heapfile.service.HeapException;
+
+/**
+ *
+ * @author luc peuvrier
+ *
+ */
+@NotStorableClass
+@SuppressWarnings("rawtypes")
+public final class DataRecordKeyManager extends AbstractDataRecordKeyComparator {
+
+ private static final DataRecordKeyManager INSTANCE = new DataRecordKeyManager();
+
+ public static DataRecordKeyManager getInstance() {
+ return INSTANCE;
+ }
+
+ private DataRecordKeyManager() {
+ super();
+ }
+
+ @Override
+ protected Comparable<?> unmarshall(final byte[] keyData)
+ throws HeapException {
+ try {
+ final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
+ keyData);
+ final ObjectInputStream objectInputStream = new ObjectInputStream(
+ byteArrayInputStream);
+ final Object result = objectInputStream.readObject();
+ objectInputStream.close();
+ return (Comparable<?>) result;
+ } catch (Exception exception) {
+ throw new HeapException("unmarshalling key", exception);
+ }
+ }
+
+ public DataRecordIdentifier createKey(final Object key)
+ throws HeapException {
+ try {
+ final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+ final ObjectOutputStream outputStream = new ObjectOutputStream(
+ byteArrayOutputStream);
+ outputStream.writeObject(key);
+ outputStream.close();
+ final byte[] keyData = byteArrayOutputStream.toByteArray();
+ final DataRecordKey dataRecordKey = new DataRecordKey(this, keyData);
+ dataRecordKey.setKey(key);
+ return new DataRecordIdentifier(dataRecordKey);
+ } catch (Exception exception) {
+ throw new HeapException("creating key", exception);
+ }
+ }
+}
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-01-06 04:58:24 UTC (rev 2987)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/HeapElementManager.java 2012-03-03 01:35:51 UTC (rev 2988)
@@ -219,6 +219,8 @@
private final int fileOperationRetryMsDelay;
+ private IDataRecordKeyComparator dataRecordKeyComparator;
+
/**
* construction for crash safe mode disabled<br>
*
@@ -1186,4 +1188,15 @@
public HeapRecord getHeapFileRecordInWriteCache(final long positionInFile) {
return heapRecordToWriteMap.get(positionInFile);
}
+
+ @Override
+ public void setDataRecordKeyComparator(
+ final IDataRecordKeyComparator dataRecordKeyComparator) {
+ this.dataRecordKeyComparator = dataRecordKeyComparator;
+ }
+
+ @Override
+ public IDataRecordKeyComparator getDataRecordKeyComparator() {
+ return dataRecordKeyComparator;
+ }
}
Added: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/IDataRecordKeyComparator.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/IDataRecordKeyComparator.java (rev 0)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/IDataRecordKeyComparator.java 2012-03-03 01:35:51 UTC (rev 2988)
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2012 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.record.service;
+
+import net.sf.joafip.heapfile.record.entity.IDataRecordKey;
+import net.sf.joafip.heapfile.service.HeapException;
+
+/**
+ *
+ * @author luc peuvrier
+ *
+ */
+public interface IDataRecordKeyComparator {
+
+ int compareDataRecordKey(IDataRecordKey dataRecordKey1,
+ IDataRecordKey dataRecordKey2) throws HeapException;
+}
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-01-06 04:58:24 UTC (rev 2987)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/record/service/IHeapElementManager.java 2012-03-03 01:35:51 UTC (rev 2988)
@@ -207,4 +207,9 @@
long getFileSize() throws HeapException;
int getNumberOfHeaprecordInMemory();
+
+ void setDataRecordKeyComparator(
+ IDataRecordKeyComparator dataRecordKeyComparator);
+
+ IDataRecordKeyComparator getDataRecordKeyComparator();
}
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-01-06 04:58:24 UTC (rev 2987)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/service/HeapFileDataManager.java 2012-03-03 01:35:51 UTC (rev 2988)
@@ -37,6 +37,7 @@
import net.sf.joafip.heapfile.record.service.HeapElementManager;
import net.sf.joafip.heapfile.record.service.HeapFreeNodeManager;
import net.sf.joafip.heapfile.record.service.HeapIdNodeManager;
+import net.sf.joafip.heapfile.record.service.IDataRecordKeyComparator;
import net.sf.joafip.heapfile.record.service.IHeapElementManager;
import net.sf.joafip.redblacktree.entity.IRBTNode;
import net.sf.joafip.redblacktree.service.IRBTVisitor;
@@ -283,10 +284,12 @@
/* 4 byte added for crc32 on data added at end of data */
if (neededDataSize < MINIMUM_RECORD_DATA_SIZE) {
neededAreaSize = 4 + MINIMUM_RECORD_DATA_SIZE
- + HeapRecord.MAX_RECORD_HEADER_SIZE;
+ + HeapRecord.MAX_RECORD_HEADER_SIZE
+ + dataRecordKeyDataSize(nodeIdentifier);
} else {
neededAreaSize = 4 + neededDataSize
- + HeapRecord.MAX_RECORD_HEADER_SIZE;
+ + HeapRecord.MAX_RECORD_HEADER_SIZE
+ + dataRecordKeyDataSize(nodeIdentifier);
}
final HeapRecord heapRecord;
@@ -311,6 +314,18 @@
return heapRecord;
}
+ private int dataRecordKeyDataSize(final DataRecordIdentifier nodeIdentifier) {
+ final int keyDataSize = nodeIdentifier.getKeyDataSize();
+ final int dataRecordKeyDataSize;
+ if (keyDataSize == 0) {
+ dataRecordKeyDataSize = 0;
+ } else {
+ // add 4 for integer containing key data size
+ dataRecordKeyDataSize = keyDataSize + 4;
+ }
+ return dataRecordKeyDataSize;
+ }
+
@Override
public boolean deleteDataRecordImpl(
final DataRecordIdentifier dataRecordIdentifier)
@@ -1109,6 +1124,12 @@
return new DataRecordIterator();
}
+ @Override
+ public void setDataRecordKeyComparator(
+ final IDataRecordKeyComparator dataRecordKeyComparator) {
+ heapElementManager.setDataRecordKeyComparator(dataRecordKeyComparator);
+ }
+
/**
*
* @return last record in file position
Modified: trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/service/IHeapDataManager.java
===================================================================
--- trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/service/IHeapDataManager.java 2012-01-06 04:58:24 UTC (rev 2987)
+++ trunk/joafip-heapfile/src/main/java/net/sf/joafip/heapfile/service/IHeapDataManager.java 2012-03-03 01:35:51 UTC (rev 2988)
@@ -21,6 +21,7 @@
import net.sf.joafip.Fortest;
import net.sf.joafip.heapfile.record.entity.DataRecordIdentifier;
+import net.sf.joafip.heapfile.record.service.IDataRecordKeyComparator;
/**
* data management on heap file<br>
@@ -292,4 +293,7 @@
@Fortest
long getLastRecordPositionInFile() throws HeapException;
+
+ void setDataRecordKeyComparator(
+ IDataRecordKeyComparator dataRecordKeyComparator);
}
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-01-06 04:58:24 UTC (rev 2987)
+++ trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/TestIdNode.java 2012-03-03 01:35:51 UTC (rev 2988)
@@ -23,6 +23,7 @@
import net.sf.joafip.NotStorableClass;
import net.sf.joafip.TestConstant;
import net.sf.joafip.TestException;
+import net.sf.joafip.heapfile.record.entity.DataRecordIdentifier;
import net.sf.joafip.heapfile.record.entity.HeapHeader;
import net.sf.joafip.heapfile.record.entity.HeapIdNode;
import net.sf.joafip.heapfile.record.entity.HeapRecord;
@@ -88,8 +89,8 @@
final HeapRecord heapRecord = heapElementManager.newHeapFileRecord(
HeapHeader.HEAP_HEADER_SIZE,
-1/* previousRecordPositionInFile */,
- null/* nodeIdentification */, false/* freeRecord */,
- 0/* dataAssociatedSize */,
+ DataRecordIdentifier.ZERO/* nodeIdentification */,
+ false/* freeRecord */, 0/* dataAssociatedSize */,
HeapRecord.DATA_RECORD_HEADER_SIZE + 4/* areaSize */);
heapIdNode = new HeapIdNode(heapElementManager,
heapRecord.getPositionInFile(), -1, -1, -1, false, false, 0,
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-01-06 04:58:24 UTC (rev 2987)
+++ trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/record/service/TestStoreRestore.java 2012-03-03 01:35:51 UTC (rev 2988)
@@ -330,4 +330,15 @@
public int getNumberOfHeaprecordInMemory() {
throw new UnsupportedOperationException("not implemented");
}
+
+ @Override
+ public void setDataRecordKeyComparator(
+ final IDataRecordKeyComparator dataRecordKeyComparator) {
+ throw new UnsupportedOperationException("not implemented");
+ }
+
+ @Override
+ public IDataRecordKeyComparator getDataRecordKeyComparator() {
+ throw new UnsupportedOperationException("not implemented");
+ }
}
Modified: trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/BlockDataManager.java
===================================================================
--- trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/BlockDataManager.java 2012-01-06 04:58:24 UTC (rev 2987)
+++ trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/BlockDataManager.java 2012-03-03 01:35:51 UTC (rev 2988)
@@ -36,6 +36,7 @@
import net.sf.joafip.heapfile.entity.BlockDataManagerHeader;
import net.sf.joafip.heapfile.entity.DataBlock;
import net.sf.joafip.heapfile.record.entity.DataRecordIdentifier;
+import net.sf.joafip.heapfile.record.service.IDataRecordKeyComparator;
/**
*
@@ -490,4 +491,11 @@
header.setDataLength(length);
}
}
+
+ @Override
+ public void setDataRecordKeyComparator(
+ final IDataRecordKeyComparator dataRecordKeyComparator) {
+ throw new UnsupportedOperationException(
+ "works only with long data record identifier");
+ }
}
Modified: trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/DualWrapDataManager.java
===================================================================
--- trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/DualWrapDataManager.java 2012-01-06 04:58:24 UTC (rev 2987)
+++ trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/DualWrapDataManager.java 2012-03-03 01:35:51 UTC (rev 2988)
@@ -27,6 +27,7 @@
import java.util.Set;
import net.sf.joafip.heapfile.record.entity.DataRecordIdentifier;
+import net.sf.joafip.heapfile.record.service.IDataRecordKeyComparator;
/**
*
@@ -277,4 +278,11 @@
public long getLastRecordPositionInFile() throws HeapException {
throw new HeapException("unsupported");
}
+
+ @Override
+ public void setDataRecordKeyComparator(
+ final IDataRecordKeyComparator dataRecordKeyComparator) {
+ firstDataManager.setDataRecordKeyComparator(dataRecordKeyComparator);
+ secondDataManager.setDataRecordKeyComparator(dataRecordKeyComparator);
+ }
}
Modified: trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/HeapFileCheckerDataManager.java
===================================================================
--- trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/HeapFileCheckerDataManager.java 2012-01-06 04:58:24 UTC (rev 2987)
+++ trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/HeapFileCheckerDataManager.java 2012-03-03 01:35:51 UTC (rev 2988)
@@ -23,6 +23,7 @@
import net.sf.joafip.heapfile.entity.HeapFileSetup;
import net.sf.joafip.heapfile.record.entity.DataRecordIdentifier;
+import net.sf.joafip.heapfile.record.service.IDataRecordKeyComparator;
/**
*
@@ -259,4 +260,11 @@
public long getLastRecordPositionInFile() throws HeapException {
throw new HeapException("unsupported");
}
+
+ @Override
+ public void setDataRecordKeyComparator(
+ final IDataRecordKeyComparator dataRecordKeyComparator) {
+ throw new UnsupportedOperationException(
+ "works only with long data record identifier");
+ }
}
Modified: trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/HeapMemoryDataManagerMock.java
===================================================================
--- trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/HeapMemoryDataManagerMock.java 2012-01-06 04:58:24 UTC (rev 2987)
+++ trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/HeapMemoryDataManagerMock.java 2012-03-03 01:35:51 UTC (rev 2988)
@@ -27,6 +27,7 @@
import net.sf.joafip.NotStorableClass;
import net.sf.joafip.heapfile.record.entity.DataRecordIdentifier;
+import net.sf.joafip.heapfile.record.service.IDataRecordKeyComparator;
/**
* mock of heap data manager in memory for tests
@@ -302,4 +303,11 @@
public long getLastRecordPositionInFile() throws HeapException {
throw new HeapException(UNSUPPORTED);
}
+
+ @Override
+ public void setDataRecordKeyComparator(
+ final IDataRecordKeyComparator dataRecordKeyComparator) {
+ throw new UnsupportedOperationException(
+ "works only with long data record identifier");
+ }
}
\ No newline at end of file
Modified: trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/HeapMultiFileDataManager.java
===================================================================
--- trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/HeapMultiFileDataManager.java 2012-01-06 04:58:24 UTC (rev 2987)
+++ trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/HeapMultiFileDataManager.java 2012-03-03 01:35:51 UTC (rev 2988)
@@ -31,6 +31,7 @@
import net.sf.joafip.NotStorableClass;
import net.sf.joafip.heapfile.record.entity.DataRecordIdentifier;
+import net.sf.joafip.heapfile.record.service.IDataRecordKeyComparator;
/**
* Data management on heap multiple file implementation<br>
@@ -473,4 +474,11 @@
public long getLastRecordPositionInFile() throws HeapException {
throw new HeapException("unsupported");
}
+
+ @Override
+ public void setDataRecordKeyComparator(
+ final IDataRecordKeyComparator dataRecordKeyComparator) {
+ throw new UnsupportedOperationException(
+ "works only with long data record identifier");
+ }
}
Added: trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/TestHeapFileDataMgrWithKey.java
===================================================================
--- trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/TestHeapFileDataMgrWithKey.java (rev 0)
+++ trunk/joafip-heapfile/src/test/java/net/sf/joafip/heapfile/service/TestHeapFileDataMgrWithKey.java 2012-03-03 01:35:51 UTC (rev 2988)
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2012 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 net.sf.joafip.NoStorableAccess;
+import net.sf.joafip.NotStorableClass;
+import net.sf.joafip.TestException;
+import net.sf.joafip.heapfile.record.entity.DataRecordIdentifier;
+import net.sf.joafip.heapfile.record.service.DataRecordKeyManager;
+
+/**
+ *
+ * @author luc peuvrier
+ *
+ */
+@NotStorableClass
+@NoStorableAccess
+public class TestHeapFileDataMgrWithKey extends AbstractTestHeapDataManager {
+
+ public TestHeapFileDataMgrWithKey() throws TestException {
+ super();
+ }
+
+ public TestHeapFileDataMgrWithKey(final String name) throws TestException {
+ super(name);
+ }
+
+ public void testUseAsAMap() throws HeapException {
+ final DataRecordKeyManager keyManager = DataRecordKeyManager
+ .getInstance();
+ createHeapFileDataManager(true);
+ heapDataManager.setDataRecordKeyComparator(keyManager);
+ final DataRecordIdentifier keyA = keyManager.createKey("A");
+ assertFalse("must not found 'A'", heapDataManager.hasDataRecord(keyA));
+ final byte[] dataA = "valueA".getBytes();
+ heapDataManager.writeDataRecord(keyA, dataA);
+ assertTrue("must found 'A'", heapDataManager.hasDataRecord(keyA));
+ final byte[] readDataA = heapDataManager.readDataRecord(keyA);
+ assertNotNull("must read 'A' data", readDataA);
+ assertEquals("valueA", new String(readDataA));
+ }
+
+ @Override
+ protected void createHeap(final boolean removeFile) throws HeapException {
+ // not implemented
+ }
+
+ @Override
+ protected boolean manageFreeRecord() {
+ // not implemented
+ return false;
+ }
+
+}
Modified: trunk/joafip-testsuite/src/main/java/net/sf/joafip/heapfile/service/HeapFileServiceTests.java
===================================================================
--- trunk/joafip-testsuite/src/main/java/net/sf/joafip/heapfile/service/HeapFileServiceTests.java 2012-01-06 04:58:24 UTC (rev 2987)
+++ trunk/joafip-testsuite/src/main/java/net/sf/joafip/heapfile/service/HeapFileServiceTests.java 2012-03-03 01:35:51 UTC (rev 2988)
@@ -36,6 +36,7 @@
suite.addTestSuite(TestHeapFileDataManagerBackup.class);
suite.addTestSuite(TestHeapFileDataMgrWithScenario.class);
suite.addTestSuite(TestBlockDataManager.class);
+ suite.addTestSuite(TestHeapFileDataMgrWithKey.class);
// $JUnit-END$
return suite;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|