Author: bpoussin Date: 2013-11-25 16:25:58 +0100 (Mon, 25 Nov 2013) New Revision: 463 Url: http://nuiton.org/projects/nuiton-matrix/repository/revisions/463 Log: if reading data with mapped file fail, load directly data in memory and use it in readonly mode Modified: trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/DoubleBigMappedVector.java Modified: trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/DoubleBigMappedVector.java =================================================================== --- trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/DoubleBigMappedVector.java 2012-12-29 11:29:21 UTC (rev 462) +++ trunk/nuiton-matrix/src/main/java/org/nuiton/math/matrix/DoubleBigMappedVector.java 2013-11-25 15:25:58 UTC (rev 463) @@ -28,10 +28,12 @@ import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; +import java.nio.ByteBuffer; import java.nio.DoubleBuffer; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; -import java.util.Arrays; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; /** * DoubleBigVector. @@ -46,12 +48,15 @@ */ public class DoubleBigMappedVector implements Vector { // DoubleBigMappedVector + static private Log log = LogFactory.getLog(DoubleBigMappedVector.class); + static final public int DOUBLE_SIZE = 8; /** le fichier temporaire creer pour la matrice, a effacer lorsque la matrice n'est plus utiliser */ protected File file; protected int capacity; protected DoubleBuffer data = null; + protected boolean readonly = false; @Override protected void finalize() throws Throwable { @@ -85,8 +90,22 @@ * @throws IOException */ public DoubleBigMappedVector(RandomAccessFile raf, long offset, int capacity) throws IOException { - this(raf.getChannel().map( - FileChannel.MapMode.READ_WRITE, offset, capacity*DOUBLE_SIZE).asDoubleBuffer(), capacity); + this.capacity = capacity; + try { + this.data = raf.getChannel().map( + FileChannel.MapMode.READ_WRITE, offset, capacity*DOUBLE_SIZE).asDoubleBuffer(); + } catch (Exception eee) { + // can't use mapped version, read array and create not mapped Buffer + log.error("Can't use mapped file, only read available"); + this.readonly = true; + byte[] tmp = new byte[capacity*DOUBLE_SIZE]; + long currentOffset = raf.getFilePointer(); + raf.seek(offset); + raf.readFully(tmp); + raf.seek(currentOffset); + ByteBuffer buf = ByteBuffer.wrap(tmp); + this.data = buf.asDoubleBuffer(); + } } public DoubleBigMappedVector(MappedByteBuffer bytes, int capacity) { @@ -126,6 +145,9 @@ @Override public void setValue(int pos, double value) { + if (readonly) { + throw new MatrixException("This object is Read only, perhaps because your system (Windows?) doesn't support large mapped file"); + } data.put(pos, value); } @@ -173,6 +195,9 @@ @Override public void paste(Vector src) { + if (readonly) { + throw new MatrixException("This object is Read only, perhaps because your system (Windows?) doesn't support large mapped file"); + } DoubleBigMappedVector dbmSrc = (DoubleBigMappedVector) src; // on se place au debut avant de faire la copie dbmSrc.data.position(0); @@ -183,6 +208,9 @@ @Override public void add(Vector v) { + if (readonly) { + throw new MatrixException("This object is Read only, perhaps because your system (Windows?) doesn't support large mapped file"); + } DoubleBigMappedVector fbv = (DoubleBigMappedVector) v; for (int i = 0; i < capacity; i++) { double value = data.get(i); @@ -193,6 +221,9 @@ @Override public void minus(Vector v) { + if (readonly) { + throw new MatrixException("This object is Read only, perhaps because your system (Windows?) doesn't support large mapped file"); + } DoubleBigMappedVector fbv = (DoubleBigMappedVector) v; for (int i = 0; i < capacity; i++) { double value = data.get(i); @@ -203,6 +234,9 @@ @Override public void map(MapFunction f) { + if (readonly) { + throw new MatrixException("This object is Read only, perhaps because your system (Windows?) doesn't support large mapped file"); + } for (int i = 0; i < capacity; i++) { double v = data.get(i); v = f.apply(v);