[Joafip-svn] SF.net SVN: joafip:[3167] trunk/joafip-kvstore/src
Brought to you by:
luc_peuvrier
From: <luc...@us...> - 2012-11-28 12:16:25
|
Revision: 3167 http://joafip.svn.sourceforge.net/joafip/?rev=3167&view=rev Author: luc_peuvrier Date: 2012-11-28 12:16:14 +0000 (Wed, 28 Nov 2012) Log Message: ----------- red black tree management changed. foreground garbage sweep changed Modified Paths: -------------- trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/record/service/HeapElementManager.java trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/service/AbstractHeapDataManager.java trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/service/IHeapDataManager.java trunk/joafip-kvstore/src/test/java/net/sf/joafip/kvstore/service/DualWrapDataManager.java Added Paths: ----------- trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/entity/NullFileStorable.java Added: trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/entity/NullFileStorable.java =================================================================== --- trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/entity/NullFileStorable.java (rev 0) +++ trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/entity/NullFileStorable.java 2012-11-28 12:16:14 UTC (rev 3167) @@ -0,0 +1,111 @@ +/* + * 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.kvstore.entity; + +import net.sf.joafip.kvstore.service.HeapException; + +/** + * + * @author luc peuvrier + * + */ +public final class NullFileStorable implements IFileStorable { + + private static final String NULL_FILE_STORABLE_CAN_NOT_BE_IN_FILE = "null file storable can not be in file"; + + private static final NullFileStorable INSTANCE = new NullFileStorable(); + + public static NullFileStorable getInstance() { + return INSTANCE; + } + + private NullFileStorable() { + super(); + } + + @Override + public long getPositionInFile() { + return -1L; + } + + @Override + public long getPreviousRecordPositionInFile() throws HeapException { + return -1L; + } + + @Override + public void writeToFile() throws HeapException { + throw new HeapException(NULL_FILE_STORABLE_CAN_NOT_BE_IN_FILE); + } + + @Override + public byte[] writeToFileGetWrited() throws HeapException { + throw new HeapException(NULL_FILE_STORABLE_CAN_NOT_BE_IN_FILE); + } + + @Override + public void readFromFile() throws HeapException { + throw new HeapException(NULL_FILE_STORABLE_CAN_NOT_BE_IN_FILE); + } + + @Override + public void clear() { + // no implementation + } + + @Override + public void setValueIsChangedValueToSave() throws HeapException { + throw new HeapException(NULL_FILE_STORABLE_CAN_NOT_BE_IN_FILE); + } + + @Override + public void setValueIsNotChanged() { + // no implementation + } + + @Override + public boolean isValueChangedToSave() { + return false; + } + + @Override + public boolean isJustCreated() { + return true; + } + + @Override + public int getRecordSize() throws HeapException { + throw new HeapException(NULL_FILE_STORABLE_CAN_NOT_BE_IN_FILE); + } + + @Override + public int hashCode() { + return 0; + } + + @Override + public boolean equals(final Object obj) { + return this == obj; + } +} Property changes on: trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/entity/NullFileStorable.java ___________________________________________________________________ Added: svn:mime-type + text/plain Modified: trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/record/service/HeapElementManager.java =================================================================== --- trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/record/service/HeapElementManager.java 2012-11-28 12:15:54 UTC (rev 3166) +++ trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/record/service/HeapElementManager.java 2012-11-28 12:16:14 UTC (rev 3167) @@ -31,6 +31,7 @@ import net.sf.joafip.file.service.HelperFileUtil; import net.sf.joafip.kvstore.entity.EnumFileState; import net.sf.joafip.kvstore.entity.IFileStorable; +import net.sf.joafip.kvstore.entity.NullFileStorable; import net.sf.joafip.kvstore.entity.ToBackupRecord; import net.sf.joafip.kvstore.service.FileForStorable; import net.sf.joafip.kvstore.service.HeapException; @@ -158,6 +159,9 @@ private static final HelperFileUtil HELPER_FILE_UTIL = HelperFileUtil .getInstance(); + private static final NullFileStorable NULL_FILE_STORABLE = NullFileStorable + .getInstance(); + /** true if crash safe mode enabled */ private final boolean crashSafeMode; @@ -802,45 +806,49 @@ for (Map.Entry<Long, IFileStorable> entry : heapRecordToWriteMap .entrySet()) { final IFileStorable heapRecord = entry.getValue(); - try { - assert false; - } catch (AssertionError error) { - // ASSERTX - final long recordPositionInFile = heapRecord - .getPositionInFile(); - if (recordPositionInFile != entry.getKey()) { - throw new HeapException(// NOPMD - "integrity error record position is " - + recordPositionInFile + " and map key is " - + entry.getKey()); - } - if (recordPositionInFile <= lastWrotePositionInFile) { - throw new HeapException("last wrote position is "// NOPMD - + lastWrotePositionInFile - + " and record position in file is " - + recordPositionInFile - + " for \n" - + heapRecord.toString()); - } - final long previousRecordPositionInFile = heapRecord - .getPreviousRecordPositionInFile(); - if (previousRecordPositionInFile == -1) { - if (recordPositionInFile != header.getRecordSize()) { - throw new HeapException("bad position of first record "// NOPMD + if (heapRecord != NULL_FILE_STORABLE) { + try { + assert false; + } catch (AssertionError error) { + // ASSERTX + final long recordPositionInFile = heapRecord + .getPositionInFile(); + if (recordPositionInFile != entry.getKey()) { + throw new HeapException(// NOPMD + "integrity error record position is " + + recordPositionInFile + + " and map key is " + entry.getKey()); + } + if (recordPositionInFile <= lastWrotePositionInFile) { + throw new HeapException("last wrote position is "// NOPMD + + lastWrotePositionInFile + + " and record position in file is " + recordPositionInFile - + " for " - + header.getRecordSize() + " expected"); + + " for \n" + + heapRecord.toString()); } - } else if (previousRecordPositionInFile < lastRecordPositionInFile) { - badPreviousRecordPositionInFileError( - lastRecordPositionInFile, recordPositionInFile, - previousRecordPositionInFile); + final long previousRecordPositionInFile = heapRecord + .getPreviousRecordPositionInFile(); + if (previousRecordPositionInFile == -1) { + if (recordPositionInFile != header.getRecordSize()) { + throw new HeapException( + "bad position of first record "// NOPMD + + recordPositionInFile + + " for " + + header.getRecordSize() + + " expected"); + } + } else if (previousRecordPositionInFile < lastRecordPositionInFile) { + badPreviousRecordPositionInFileError( + lastRecordPositionInFile, recordPositionInFile, + previousRecordPositionInFile); + } + lastWrotePositionInFile = recordPositionInFile + + heapRecord.getRecordSize() - 1; + lastRecordPositionInFile = recordPositionInFile; } - lastWrotePositionInFile = recordPositionInFile - + heapRecord.getRecordSize() - 1; - lastRecordPositionInFile = recordPositionInFile; + saveHeapRecord(heapRecord, toBackupList); } - saveHeapRecord(heapRecord, toBackupList); } } @@ -1030,6 +1038,12 @@ IFileStorable heapRecord; heapRecord = heapRecordToWriteMap.get(positionInFile); + if (heapRecord == NULL_FILE_STORABLE) { + final String message = READ_HEAP_FILE_NODE_FAILED + + "read record at poistion " + positionInFile + " failed"; + LOGGER.fatal(message); + throw new HeapException(message); + } if (heapRecord == null) { heapRecord = readHeapRecordMap.get(positionInFile); } @@ -1040,7 +1054,8 @@ heapRecord.readFromFile(); } catch (HeapException exception) { final String message = READ_HEAP_FILE_NODE_FAILED - + "read record failed"; + + "read record at poistion " + positionInFile + + " failed"; LOGGER.fatal(message, exception); throw new HeapException(message, exception); } @@ -1072,6 +1087,9 @@ assert assertPositionInFile(positionInFile); IFileStorable previous = heapRecordToWriteMap.put(positionInFile, heapRecord); + if( previous==NULL_FILE_STORABLE){ + previous=null; + } // ASSERTX assert assertNoPreviousOrPreviousSameAsCurrent(previous, heapRecord); if (previous == null) { @@ -1108,7 +1126,8 @@ public void delete(final long positionInFile) throws HeapException { // delete case : can be deleted even if not read nor write readHeapRecordMap.remove(positionInFile); - heapRecordToWriteMap.remove(positionInFile); + // heapRecordToWriteMap.remove(positionInFile); + heapRecordToWriteMap.put(positionInFile, NULL_FILE_STORABLE); } @Override @@ -1154,7 +1173,8 @@ * * @param positionInFile * heap record position in file - * @return heap record in write cache for position in file, null if none + * @return heap record in write cache for position in file, null if none. + * can be {@link #NULL_FILE_STORABLE} */ @Fortest public IFileStorable getHeapFileRecordInWriteCache(final long positionInFile) { Modified: trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/service/AbstractHeapDataManager.java =================================================================== --- trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/service/AbstractHeapDataManager.java 2012-11-28 12:15:54 UTC (rev 3166) +++ trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/service/AbstractHeapDataManager.java 2012-11-28 12:16:14 UTC (rev 3167) @@ -202,7 +202,8 @@ * * @throws HeapException */ - protected abstract void stopServiceImpl(boolean removeFiles) throws HeapException; + protected abstract void stopServiceImpl(boolean removeFiles) + throws HeapException; @Override public boolean isServiceStarted() { @@ -489,6 +490,30 @@ } @Override + public DataRecordIdentifier nextDataRecordIdentifier( + final DataRecordIdentifier dataRecordIdentifier) + throws HeapException { + try { + // ASSERTX + assert assertStarted(); + return nextDataRecordIdentifierImpl(dataRecordIdentifier); + } catch (HeapException exception) { + logger.fatal(GET_FIRST_DATA_RECORD_FAILED, exception); + closeHeapManagerAfterException(); + throw exception; + } catch (RuntimeException exception) { + logger.fatal(GET_FIRST_DATA_RECORD_FAILED, exception); + closeHeapManagerAfterException(); + throw exception; + } + } + + protected DataRecordIdentifier nextDataRecordIdentifierImpl( + final DataRecordIdentifier dataRecordIdentifier) throws HeapException { + throw new HeapException(UNSUPPORTED_OPERATION); + } + + @Override public DataRecordIdentifier lastDataRecordIdentifier() throws HeapException { try { // ASSERTX Modified: trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/service/IHeapDataManager.java =================================================================== --- trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/service/IHeapDataManager.java 2012-11-28 12:15:54 UTC (rev 3166) +++ trunk/joafip-kvstore/src/main/java/net/sf/joafip/kvstore/service/IHeapDataManager.java 2012-11-28 12:16:14 UTC (rev 3167) @@ -180,6 +180,14 @@ /** * + * @param dataRecordIdentifier wich for next is requested + * @return next data record identifier, null if none + * @throws HeapException + */ + DataRecordIdentifier nextDataRecordIdentifier(DataRecordIdentifier dataRecordIdentifier) throws HeapException; + + /** + * * @return last data record identifier, null if none (heap empty) * @throws HeapException */ Modified: trunk/joafip-kvstore/src/test/java/net/sf/joafip/kvstore/service/DualWrapDataManager.java =================================================================== --- trunk/joafip-kvstore/src/test/java/net/sf/joafip/kvstore/service/DualWrapDataManager.java 2012-11-28 12:15:54 UTC (rev 3166) +++ trunk/joafip-kvstore/src/test/java/net/sf/joafip/kvstore/service/DualWrapDataManager.java 2012-11-28 12:16:14 UTC (rev 3167) @@ -223,6 +223,21 @@ } @Override + public DataRecordIdentifier nextDataRecordIdentifier( + final DataRecordIdentifier dataRecordIdentifier) + throws HeapException { + final DataRecordIdentifier dr1 = firstDataManager + .nextDataRecordIdentifier(dataRecordIdentifier); + final DataRecordIdentifier dr2 = secondDataManager + .nextDataRecordIdentifier(dataRecordIdentifier); + if (dr1.value != dr2.value) { + throw new HeapException(DATA_RECORD_MISMATCH + dr1.value + " " + + dr2.value); + } + return dr1; + } + + @Override public DataRecordIdentifier lastDataRecordIdentifier() throws HeapException { final DataRecordIdentifier dr1 = firstDataManager .lastDataRecordIdentifier(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |