You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2017/09/28 16:06:01 UTC
[049/100] [abbrv] [partial] jena git commit: JENA-1397: Rename
modules and set versions
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/buffer/RecordBuffer.java
----------------------------------------------------------------------
diff --git a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/buffer/RecordBuffer.java b/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/buffer/RecordBuffer.java
deleted file mode 100644
index 8999f07..0000000
--- a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/buffer/RecordBuffer.java
+++ /dev/null
@@ -1,236 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * 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 org.seaborne.dboe.base.buffer ;
-
-import static java.lang.String.format ;
-import static org.apache.jena.atlas.lib.Alg.encodeIndex ;
-
-import java.nio.ByteBuffer ;
-import java.util.Iterator ;
-
-import org.seaborne.dboe.base.record.Record ;
-import org.seaborne.dboe.base.record.RecordFactory ;
-import org.seaborne.dboe.base.record.RecordMapper ;
-
-final public class RecordBuffer extends BufferBase {
- private RecordFactory factory ;
-
- // Need own specialized binary search :-(
-
- public RecordBuffer(RecordFactory recFactory, int maxRec) {
- this(ByteBuffer.allocate(recFactory.recordLength() * maxRec), recFactory, 0) ;
- }
-
- public RecordBuffer(ByteBuffer bb, RecordFactory recFactory, int num) {
- super(bb, recFactory.recordLength(), num) ;
- this.factory = recFactory ;
- }
-
- public <X> X access(int idx, byte[] key, RecordMapper<X> mapper) {
- checkBounds(idx, numSlot) ;
- return _access(idx, key, mapper) ;
- }
-
- public Record get(int idx) {
- checkBounds(idx, numSlot) ;
- return _get(idx) ;
- }
-
- public Record getLow() {
- if ( numSlot == 0 )
- throw new IllegalArgumentException("getLow: Empty RecordBuffer") ;
- return _get(0) ;
- }
-
- public Record getHigh() {
- if ( numSlot == 0 )
- throw new IllegalArgumentException("getHigh: Empty RecordBuffer") ;
- return _get(numSlot - 1) ;
- }
-
- // Inserts at top.
- public void add(Record record) {
- add(numSlot, record) ;
- }
-
- // Inserts at slot idx
- public void add(int idx, Record record) {
- if ( idx != numSlot ) {
- checkBounds(idx, numSlot) ;
- shiftUp(idx) ; // Changes count.
- } else {
- if ( numSlot >= maxSlot )
- throw new BufferException(format("Out of bounds: idx=%d, ptrs=%d", idx, maxSlot)) ;
- numSlot++ ;
- }
- _set(idx, record) ;
- }
-
- // Overwrites the contents of slot idx
- public void set(int idx, Record record) {
- if ( idx == numSlot ) {
- add(idx, record) ;
- return ;
- } else
- checkBounds(idx, numSlot) ;
- _set(idx, record) ;
- }
-
- // No checking bound : careful use only!
-
- public <X> X _access(int idx, byte[] key, RecordMapper<X> mapper) {
- return factory.access(bb, idx, key, mapper) ;
- }
-
- public Record _get(int idx) {
- return factory.buildFrom(bb, idx) ;
- }
-
- // No bounds checking : careful use only!
- void _set(int idx, Record rec) {
- factory.insertInto(rec, bb, idx) ;
- }
-
- // Linear search for testing.
- int find1(byte[] data) {
- for ( int i = 0 ; i < numSlot ; i++ ) {
- int x = compare(i, data) ;
- if ( x == 0 )
- return i ;
- if ( x > 0 )
- return encodeIndex(i) ;
- }
- return encodeIndex(numSlot) ;
- }
-
- // Binary search
- public int find(Record k) {
- return find(k, 0, numSlot) ;
- }
-
- public Iterator<Record> iterator() {
- return new RecordBufferIterator(this) ;
- }
-
- /** Iterator over a range from min (inclusive) to max(exclusive) */
- public Iterator<Record> iterator(Record min, Record max) {
- return new RecordBufferIterator(this, min, max) ;
- }
-
- public Record findGet(Record k) {
- int x = find(k) ;
- if ( x >= 0 )
- return get(x) ;
- return null ;
- }
-
- /** return true is removed anything */
- public boolean removeByKey(Record k) {
- int x = find(k) ;
- if ( x < 0 )
- return false ;
- super.remove(x) ;
- return true ;
- }
-
- /** Search for key in range fromIndex (inclusive) to toIndex (exclusive) */
- public int find(Record rec, int fromIndex, int toIndex) {
- int low = fromIndex ;
- int high = toIndex - 1 ;
-
- byte[] key = rec.getKey() ;
- // http://en.wikipedia.org/wiki/Binary_search
-
- while (low <= high) {
- int mid = (low + high) >>> 1 ; // int divide by 2
-
- int x = compare(mid, key) ;
- // System.out.printf("Compare: %d(%s) %s ==> %d\n", mid,
- // Record.str(get(mid)), Record.str(data), x) ;
-
- if ( x < 0 )
- low = mid + 1 ;
- else if ( x > 0 )
- high = mid - 1 ;
- else
- return mid ;
- }
- // On exit, when not finding, low is the least value
- // above, including off the end of the array.
- return encodeIndex(low) ;
- }
-
- // Record compareByKey except we avoid touching bytes by exiting as soon as
- // possible.
- // No record created as would be by using
- // compareByKey(RecordBuffer.get(idx), record)
- // Compare the slot at idx with value.
- private int compare(int idx, byte[] value) {
- idx = idx * slotLen ;
-
- for ( int i = 0 ; i < value.length ; i++ ) {
- byte b1 = bb.get(idx + i) ;
- byte b2 = value[i] ;
- if ( b1 == b2 )
- continue ;
- return (b1 & 0xFF) - (b2 & 0xFF) ;
- }
- return 0 ;
- }
-
- @Override
- public String toString() {
- StringBuilder str = new StringBuilder(40000) ;
- str.append(format("Len=%d Max=%d: ", numSlot, bb.limit() / slotLen)) ;
-
- // Print active slots as records.
- for ( int i = 0 ; i < numSlot ; i++ ) {
- if ( i != 0 )
- str.append(" ") ;
- Record r = _get(i) ;
- str.append(r.toString()) ;
- }
-
- // // Print empty slots
- // for ( int i = numSlot*slotLen ; i < maxSlot*slotLen ; i++ )
- // {
- // if ( i != 0 && i%slotLen == 0 )
- // str.append(" ") ;
- // byte b = bb.get(i) ;
- // str.append(format("%02x", b)) ;
- // }
- String s = str.toString() ;
- return s ;
- }
-
- private static void checkBounds(int idx, int len) {
- if ( idx < 0 || idx >= len )
- throw new IllegalArgumentException(format("Out of bounds: idx=%d, size=%d", idx, len)) ;
- }
-
- /**
- * A duplicate which does not share anything with the original - for testing
- */
- public RecordBuffer duplicate() {
- RecordBuffer n = new RecordBuffer(factory, maxSlot) ;
- copy(0, n, 0, maxSlot) ; // numSlot
- n.numSlot = numSlot ; // reset the allocated length
- return n ;
- }
-}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/buffer/RecordBufferIterator.java
----------------------------------------------------------------------
diff --git a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/buffer/RecordBufferIterator.java b/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/buffer/RecordBufferIterator.java
deleted file mode 100644
index f2b8944..0000000
--- a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/buffer/RecordBufferIterator.java
+++ /dev/null
@@ -1,98 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * 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 org.seaborne.dboe.base.buffer;
-
-import static org.apache.jena.atlas.lib.Alg.decodeIndex ;
-
-import java.util.Iterator;
-import java.util.NoSuchElementException;
-
-import org.seaborne.dboe.base.record.Record ;
-
-public class RecordBufferIterator implements Iterator<Record>
-{
- private RecordBuffer rBuff ;
- private int nextIdx ;
- private Record slot = null ;
- private final Record maxRec ;
- private final Record minRec ;
-
- RecordBufferIterator(RecordBuffer rBuff)
- { this(rBuff, null, null); }
-
- RecordBufferIterator(RecordBuffer rBuff, Record minRecord, Record maxRecord)
- {
- this.rBuff = rBuff ;
- nextIdx = 0 ;
- minRec = minRecord ;
- if ( minRec != null )
- {
- nextIdx = rBuff.find(minRec) ;
- if ( nextIdx < 0 )
- nextIdx = decodeIndex(nextIdx) ;
- }
-
- maxRec = maxRecord ;
- }
-
- private void finish()
- {
- rBuff = null ;
- nextIdx = -99 ;
- slot = null ;
- }
-
- @Override
- public boolean hasNext()
- {
- if ( slot != null )
- return true ;
- if ( nextIdx < 0 )
- return false ;
- if ( nextIdx >= rBuff.size() )
- {
- finish() ;
- return false ;
- }
-
- slot = rBuff.get(nextIdx) ;
- if ( maxRec != null && Record.keyGE(slot, maxRec) )
- {
- // Finished - now to large
- finish() ;
- return false ;
- }
- nextIdx ++ ;
- return true ;
- }
-
- @Override
- public Record next()
- {
- if ( ! hasNext() )
- throw new NoSuchElementException("RecordBufferIterator") ;
- Record r = slot ;
- slot = null ;
- return r ;
- }
-
- @Override
- public void remove()
- { throw new UnsupportedOperationException("RecordBufferIterator.remove") ; }
-}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/AlreadyLocked.java
----------------------------------------------------------------------
diff --git a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/AlreadyLocked.java b/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/AlreadyLocked.java
deleted file mode 100644
index dfe4125..0000000
--- a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/AlreadyLocked.java
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * 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 org.seaborne.dboe.base.file;
-
-import org.seaborne.dboe.DBOpEnvException;
-
-/** Exception throws when an attempt to take a {@link ProcessFileLock}
- * with a call of {@link ProcessFileLock#lockEx}
- * is made and the lock is already held.
- */
-public class AlreadyLocked extends DBOpEnvException {
- public AlreadyLocked(String msg) { super(msg); }
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFile.java
----------------------------------------------------------------------
diff --git a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFile.java b/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFile.java
deleted file mode 100644
index 1afa89f..0000000
--- a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFile.java
+++ /dev/null
@@ -1,100 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * 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 org.seaborne.dboe.base.file;
-
-import java.io.RandomAccessFile ;
-
-import org.apache.jena.atlas.lib.Closeable ;
-import org.apache.jena.atlas.lib.Sync ;
-
-/** An append-only, read-anywhere, binary file.
- * A {@code BinaryDataFile} does not record the length and assumes the
- * entries are self-defining.
- *
- * @see RandomAccessFile
- */
-public interface BinaryDataFile extends Closeable, Sync {
- // What about java.nio.channels.FileChannel?
- // On OpenJDK, RandomAccessFile and FileChannelImpl both dive into native code.
- //
- // The choice seems to come down to ByteBuffers vs byte[]
- // which in turn is a small/large data (scattered data)
- // issue. We are currently expecting small(er) I/O so byte[]
- // and being like Thrift is better.
-
- // byte[] vs ByteBuffer
-
- /** Open the file */
- public void open() ;
-
- /** Is it open? */
- public boolean isOpen() ;
-
- /** Read into a byte array, returning the number of bytes read.
- * Reads are at an absolute position and a read is atomic/thread-safe.
- *
- * @param posn Location of the read operation.
- * @param b byte array
- *
- * @return The number of bytes read
- */
- public default int read(long posn, byte b[]) {
- return read(posn, b, 0, b.length);
- }
-
- /** Read into a byte array, returning the number of bytes read.
- * Reads are at an absolute position and a read is atomic/thread-safe.
- *
- * @param posn Location of the read operation.
- * @param b
- * @param start of bytesarray to read into
- * @param length Maximum number of bytes to read.
- * @return The number of bytes read
- */
-
- public int read(long posn, byte b[], int start, int length) ;
-
- /** Write bytes - bytes are always written to the end of the file.
- * Return the location where the write started.
- */
- public default long write(byte b[]) {
- return write(b, 0, b.length) ;
- }
-
- /** Write bytes - bytes are always written to the end of the file.
- * Return the location where the write started.
- */
- public long write(byte b[], int start, int length) ;
-
- /** Return the length of the file (including any buffered writes) */
- public long length() ;
-
- /** Truncate the file */
- public void truncate(long length) ;
-
- /** Return whether this is an empty file or not */
- public default boolean isEmpty() { return length() == 0 ; }
-
- @Override
- public void sync() ;
-
- @Override
- public void close() ;
-}
-
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFileMem.java
----------------------------------------------------------------------
diff --git a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFileMem.java b/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFileMem.java
deleted file mode 100644
index 677a523..0000000
--- a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFileMem.java
+++ /dev/null
@@ -1,115 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * 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 org.seaborne.dboe.base.file;
-
-import org.apache.jena.atlas.RuntimeIOException ;
-import org.apache.jena.atlas.io.IO ;
-
-/** Implementation of {@link BinaryDataFile} in memory for testing
- * and development use. Raw performance is not an objective.
- *
- * <li>This implementation is thread-safe.
- */
-public class BinaryDataFileMem implements BinaryDataFile {
-
- private boolean readMode ;
- private SegmentedMemBuffer storage ;
-
- public BinaryDataFileMem() {
- }
-
- @Override
- synchronized
- public void open() {
- if ( storage != null )
- throw new RuntimeIOException("Already open") ;
- storage = new SegmentedMemBuffer() ;
- readMode = true ;
- }
-
- @Override
- synchronized
- public boolean isOpen() {
- return storage != null ;
- }
-
- @Override
- synchronized
- public int read(long posn, byte[] b, int start, int length) {
- checkOpen() ;
- switchToReadMode() ;
- return storage.read(posn, b, start, length) ;
- }
-
- @Override
- synchronized
- public long write(byte[] b, int start, int length) {
- checkOpen() ;
- switchToWriteMode() ;
- long x = storage.length() ;
- storage.write(x, b, start, length) ;
- return x ;
- }
-
- @Override
- synchronized
- public void truncate(long length) {
- if ( length < 0 )
- IO.exception(String.format("truncate: bad length : %d", length)) ;
- checkOpen() ;
- switchToWriteMode() ;
- storage.truncate(length);
- }
-
- @Override
- synchronized
- public void sync() {
- checkOpen() ;
- storage.sync();
- }
-
- @Override
- synchronized
- public void close() {
- if ( ! isOpen() )
- return ;
- storage.close();
- storage = null ;
- }
-
- @Override
- synchronized
- public long length() {
- return storage.length() ;
- }
-
- private void switchToReadMode() {
- readMode = true ;
- }
-
- private void switchToWriteMode() {
- readMode = false ;
- }
-
- private void checkOpen() {
- if ( ! isOpen() )
- throw new RuntimeIOException("Not open") ;
- }
-}
-
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFileRandomAccess.java
----------------------------------------------------------------------
diff --git a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFileRandomAccess.java b/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFileRandomAccess.java
deleted file mode 100644
index 2266e25..0000000
--- a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFileRandomAccess.java
+++ /dev/null
@@ -1,149 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * 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 org.seaborne.dboe.base.file;
-
-import java.io.IOException ;
-import java.io.RandomAccessFile ;
-
-import org.apache.jena.atlas.RuntimeIOException ;
-import org.apache.jena.atlas.io.IO ;
-
-/** Implementation of {@link BinaryDataFile} using {@link RandomAccessFile}.
- *
- * <li>No buffering of reads or writes provided.
- * <li>Not thread-safe.
- *
- * @see BinaryDataFileWriteBuffered
- */
-public class BinaryDataFileRandomAccess implements BinaryDataFile {
- // On OpenJDK, RandomAccessFile and FileChannelImpl both
- // dive into native code.
- protected RandomAccessFile file ;
- protected boolean readMode ;
- protected long readPosition ;
- protected long writePosition ;
- private final String filename ;
-
- public BinaryDataFileRandomAccess(String filename) {
- this.filename = filename ;
- }
-
- @Override
- public void open() {
- if ( file != null )
- throw new RuntimeIOException("Already open") ;
- try {
- file = new RandomAccessFile(filename, "rw") ;
- writePosition = file.length() ;
- readPosition = 0 ;
- readMode = true ;
- }
- catch (IOException e) { IO.exception(e); }
- }
-
- @Override
- public boolean isOpen() {
- return file != null ;
- }
-
- @Override
- public int read(long posn, byte[] b, int start, int length) {
- checkOpen() ;
- switchToReadMode() ;
- seek(posn) ;
- try {
- int x = file.read(b, start, length) ;
- readPosition += x ;
- return x ;
- }
- catch (IOException ex) { IO.exception(ex); return -1 ; }
- }
-
- @Override
- public long write(byte[] b, int start, int length) {
- checkOpen() ;
- switchToWriteMode() ;
- long x = writePosition ;
- try {
- file.write(b, start, length) ;
- writePosition += length ;
- }
- catch (IOException ex) { IO.exception(ex) ; }
- return x ;
- }
-
-
- // Move the RandomAccess file pointer.
- private void seek(long posn) {
- try { file.seek(posn) ; }
- catch (IOException ex) { IO.exception(ex) ; }
- }
-
- @Override
- public void truncate(long length) {
- checkOpen() ;
- switchToWriteMode() ;
- try { file.setLength(length); }
- catch (IOException ex) { IO.exception(ex) ; }
- }
-
- @Override
- public void sync() {
- checkOpen() ;
- flush$() ;
- }
-
- protected void flush$() {
- try { file.getFD().sync() ; }
- catch (IOException ex) { IO.exception(ex) ; }
- }
-
- @Override
- public void close() {
- if ( ! isOpen() )
- return ;
- try { file.close(); }
- catch (IOException ex) { IO.exception(ex) ; }
- file = null ;
- }
-
- @Override
- public long length() {
- try { return file.length() ;}
- catch (IOException ex) { IO.exception(ex) ; return -1 ;}
- }
-
- protected void switchToReadMode() {
- if ( ! readMode )
- readMode = true ;
- }
-
- protected void switchToWriteMode() {
- if ( readMode ) {
- readMode = false ;
- seek(writePosition);
- }
- }
-
- protected void checkOpen() {
- if ( ! isOpen() )
- throw new RuntimeIOException("Not open") ;
- }
-}
-
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFileSync.java
----------------------------------------------------------------------
diff --git a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFileSync.java b/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFileSync.java
deleted file mode 100644
index d5f0aba..0000000
--- a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFileSync.java
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * 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 org.seaborne.dboe.base.file;
-
-
-/** Add synchronization to all BinaryDataFile operations.
- * This gives the correct thread-safe operation
- * but isn'tnecessarily the best way to do it.
- */
-public class BinaryDataFileSync implements BinaryDataFile {
- private final BinaryDataFile other ;
-
- public BinaryDataFileSync(BinaryDataFile other) {
- this.other = other ;
- }
-
- @Override
- synchronized
- public void open() { other.open() ; }
-
- @Override
- synchronized
- public boolean isOpen() { return other.isOpen() ; }
-
- @Override
- synchronized
- public int read(long posn, byte[] b) {
- return other.read(posn, b) ;
- }
-
- @Override
- synchronized
- public int read(long posn, byte[] b, int start, int length) {
- return other.read(posn, b, start, length) ;
- }
-
- @Override
- synchronized
- public long write(byte[] b) {
- return other.write(b) ;
- }
-
- @Override
- synchronized
- public long write(byte[] b, int start, int length) {
- return other.write(b, start, length) ;
- }
-
- @Override
- synchronized
- public long length() {
- return other.length() ;
- }
-
- @Override
- synchronized
- public void truncate(long length) { other.truncate(length); }
-
- @Override
- synchronized
- public void sync() { other.sync(); }
-
- @Override
- synchronized
- public void close() { other.close(); }
-}
-
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFileWrapper.java
----------------------------------------------------------------------
diff --git a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFileWrapper.java b/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFileWrapper.java
deleted file mode 100644
index 9788e19..0000000
--- a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFileWrapper.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * 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 org.seaborne.dboe.base.file;
-
-public class BinaryDataFileWrapper implements BinaryDataFile {
- private final BinaryDataFile other ;
-
- public BinaryDataFileWrapper(BinaryDataFile other) {
- this.other = other ;
- }
-
- @Override
- public void open() { other.open() ; }
-
- @Override
- public boolean isOpen() { return other.isOpen() ; }
-
- @Override
- public int read(long posn, byte[] b) {
- return other.read(posn, b) ;
- }
-
- @Override
- public int read(long posn, byte[] b, int start, int length) {
- return other.read(posn, b, start, length) ;
- }
-
- @Override
- public long write(byte[] b) {
- return other.write(b) ;
- }
-
- @Override
- public long write(byte[] b, int start, int length) {
- return other.write(b, start, length) ;
- }
-
- @Override
- public long length() {
- return other.length() ;
- }
-
- @Override
- public void truncate(long length) { other.truncate(length); }
-
- @Override
- public void sync() { other.sync(); }
-
- @Override
- public void close() { other.close(); }
-}
-
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFileWriteBuffered.java
----------------------------------------------------------------------
diff --git a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFileWriteBuffered.java b/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFileWriteBuffered.java
deleted file mode 100644
index a9701e7..0000000
--- a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BinaryDataFileWriteBuffered.java
+++ /dev/null
@@ -1,163 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * 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 org.seaborne.dboe.base.file;
-
-import org.apache.jena.atlas.RuntimeIOException ;
-
-/** Implementation of {@link BinaryDataFile} adding write buffering to another
- * {@link BinaryDataFile} file such as a {@link BinaryDataFileRandomAccess}.
- * <li>Thread-safe.
- * <li>No read buffering provided.
- * <li>The write buffer is flushed when switching to read.
- */
-
-public class BinaryDataFileWriteBuffered implements BinaryDataFile {
- private static final int SIZE = 128*1024 ;
- private final Object sync = new Object() ;
- private byte[] buffer ;
- private int bufferLength ;
- private boolean pendingOutput ;
- private final BinaryDataFile other ;
-
- public BinaryDataFileWriteBuffered(BinaryDataFile other) {
- this(other, SIZE) ;
- }
-
- public BinaryDataFileWriteBuffered(BinaryDataFile other, int bufferSize) {
- this.other = other ;
- buffer = new byte[bufferSize] ;
- }
-
- @Override
- public void open() {
- synchronized(sync) {
- other.open() ;
- bufferLength = 0 ;
- pendingOutput = false ;
- }
- }
-
- @Override
- public void close() {
- synchronized(sync) {
- if ( ! isOpen() )
- return ;
- writeBuffer();
- other.close() ;
- }
- }
-
- @Override
- public boolean isOpen() {
- synchronized(sync) {
- return other.isOpen() ;
- }
- }
-
- @Override
- public long length() {
- synchronized(sync) {
- return other.length()+bufferLength ;
- }
- }
-
- @Override
- public void truncate(long posn) {
- synchronized(sync) {
- checkOpen() ;
- if ( pendingOutput && posn >= other.length() )
- writeBuffer() ;
- other.truncate(posn) ;
- }
- }
-
- private void checkOpen() {
- if ( ! other.isOpen() )
- throw new RuntimeIOException("Not open") ;
- }
-
- @Override
- public int read(long posn, byte[] b, int start, int length) {
- synchronized(sync) {
- // Overlap with buffered area
- // We flush the write buffer for a read so no need to check.
- checkOpen() ;
- switchToReadMode() ;
- return other.read(posn, b, start, length) ;
- }
- }
-
- @Override
- public long write(byte[] buf, int off, int len) {
- synchronized(sync) {
- checkOpen() ;
- switchToWriteMode() ;
- long x = length() ;
-
-// if ( false ) {
-// // No buffering
-// try { file.write(buf, off, len) ; }
-// catch (IOException e) { IO.exception(e); }
-// bufferLength = 0 ;
-// return ;
-// }
-
- // No room.
- if ( bufferLength + len >= SIZE )
- writeBuffer() ;
-
- if ( bufferLength + len < SIZE ) {
- // Room to buffer
- System.arraycopy(buf, off, buffer, bufferLength, len);
- bufferLength += len ;
- pendingOutput = true ;
- return x ;
- }
- // Larger than the buffer space. Write directly.
- other.write(buf, off, len) ;
- return x ;
- }
- }
-
- @Override
- public void sync() {
- synchronized(sync) {
- writeBuffer() ;
- other.sync();
- }
- }
-
- private void writeBuffer() {
- if ( pendingOutput ) {
- pendingOutput = false ;
- other.write(buffer, 0, bufferLength) ;
- bufferLength = 0 ;
- }
- }
-
- // Inside synchronization
- protected void switchToWriteMode() {
- }
-
- // Inside synchronization
- protected void switchToReadMode() {
- writeBuffer() ;
- }
-}
-
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccess.java
----------------------------------------------------------------------
diff --git a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccess.java b/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccess.java
deleted file mode 100644
index 6b8d942..0000000
--- a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccess.java
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * 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 org.seaborne.dboe.base.file;
-
-import org.apache.jena.atlas.lib.Closeable ;
-import org.apache.jena.atlas.lib.Sync ;
-import org.seaborne.dboe.base.block.Block ;
-
-/**
- * Interface to concrete storage - read and write Blocks, addressed by id.
- * Suitable for memory mapped I/O (returns
- * internally allocated space for read, not provided from outside; write() can
- * insist the block written comes from allocate()).
- * This interfce can also be backed by an in-memory implemntation
- * ({@link BlockAccessMem}, {@link BlockAccessByteArray}).
- *
- * This is wrapped in a BlockMgr to provide a higher level abstraction.
- *
- * @see BufferChannel
- */
-public interface BlockAccess extends Sync, Closeable
-{
- public Block allocate(int size) ;
-
- public Block read(long id) ;
-
- public void write(Block block) ;
-
- public void overwrite(Block block) ;
-
- public boolean isEmpty() ;
-
- /* The limit of the current allocation space.
- * Allocated blocks have ids in [0, allocBoundary).
- * Allocation units need not be in +1 increments.
- */
- public long allocBoundary() ;
-
- /** Reset the allocation space (i.e. truncate).
- * The boundary should be a number obtained from a previous
- * call of allocLimit. It can not be greater than the current
- * allocation boundary.
- */
- public void resetAllocBoundary(long boundary) ;
-
- public boolean valid(long id) ;
-
- public String getLabel() ;
-}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccessBase.java
----------------------------------------------------------------------
diff --git a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccessBase.java b/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccessBase.java
deleted file mode 100644
index cd66c79..0000000
--- a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccessBase.java
+++ /dev/null
@@ -1,176 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * 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 org.seaborne.dboe.base.file;
-
-import static java.lang.String.format ;
-
-import java.nio.ByteBuffer ;
-import java.nio.channels.FileChannel ;
-import java.util.concurrent.atomic.AtomicLong ;
-
-import org.seaborne.dboe.base.block.Block ;
-import org.seaborne.dboe.base.block.BlockException ;
-import org.seaborne.dboe.sys.FileLib ;
-import org.seaborne.dboe.sys.Sys;
-import org.slf4j.Logger ;
-
-/** Support for a disk file backed FileAccess */
-public abstract class BlockAccessBase implements BlockAccess {
- protected final int blockSize ;
- protected FileChannel file ;
- protected final String filename ;
-
- protected final String label ;
- // Does this need to be tread safe?
- // Only changes in a write transaction
-
- // Don't overload use of this!
- protected final AtomicLong seq ;
- protected long numFileBlocks = -1 ;
-
- public BlockAccessBase(String filename, int blockSize) {
- this.filename = filename ;
- this.file = FileLib.openManaged(filename) ;
- this.blockSize = blockSize ;
- this.label = label(filename) ;
- // This is not related to used file length in mapped mode.
- long filesize = FileLib.size(file) ;
- long longBlockSize = blockSize ;
-
- numFileBlocks = filesize / longBlockSize ; // This is not related to
- // used file length in mapped
- // mode.
- seq = new AtomicLong(numFileBlocks) ;
-
- if ( numFileBlocks > Integer.MAX_VALUE )
- getLog().warn(format("File size (%d) exceeds tested block number limits (%d)", filesize, blockSize)) ;
-
- if ( filesize % longBlockSize != 0 )
- throw new BlockException(format("File size (%d) not a multiple of blocksize (%d)", filesize, blockSize)) ;
- }
-
- /** Find path compoent, with extension */
- private static String label(String filename) {
- int j = filename.lastIndexOf('/') ;
- if ( j < 0 )
- j = filename.lastIndexOf('\\') ;
- String fn = (j >= 0) ? filename.substring(j + 1) : filename ;
- return fn ;
- }
-
- protected abstract Logger getLog() ;
-
- @Override
- final public boolean isEmpty() {
- return numFileBlocks <= 0 ;
- }
-
- final protected void writeNotification(Block block) {}
-
- final protected void overwriteNotification(Block block) {
- // Write at end => extend
- if ( block.getId() >= numFileBlocks ) {
- numFileBlocks = block.getId() + 1 ;
- seq.set(numFileBlocks) ;
- }
- }
-
- final protected int allocateId() {
- checkIfClosed() ;
- int id = (int)seq.getAndIncrement() ;
- numFileBlocks++ ; // TODO Fix this when proper freeblock management is
- // introduced.
- return id ;
- }
-
- @Override
- final public long allocBoundary() {
- checkIfClosed() ;
- return seq.get() ;
- // Underlying area is untouched.
- }
-
- @Override
- final public void resetAllocBoundary(long boundary) {
- checkIfClosed() ;
- seq.set(boundary) ;
- _resetAllocBoundary(boundary) ;
- }
-
- protected abstract void _resetAllocBoundary(long boundary) ;
-
- @Override
- final synchronized public boolean valid(long id) {
- if ( id >= numFileBlocks )
- return false ;
- if ( id < 0 )
- return false ;
- return true ;
- }
-
- final protected void check(long id) {
- if ( id > Integer.MAX_VALUE )
- throw new BlockException(format("BlockAccessBase: Id (%d) too large", id)) ;
-
- // Access to numFileBlocks not synchronized - it's only a check
- if ( id < 0 || id >= numFileBlocks ) {
- // Do it properly!
- synchronized (this) {
- if ( id < 0 || id >= numFileBlocks )
- throw new BlockException(format("BlockAccessBase: Bounds exception: %s: (%d,%d)", filename, id, numFileBlocks)) ;
- }
- }
- }
-
- final protected void check(Block block) {
- check(block.getId()) ;
- ByteBuffer bb = block.getByteBuffer() ;
- if ( bb.capacity() != blockSize )
- throw new BlockException(format("BlockMgrFile: Wrong size block. Expected=%d : actual=%d", blockSize, bb.capacity())) ;
- if ( bb.order() != Sys.NetworkOrder )
- throw new BlockException("BlockMgrFile: Wrong byte order") ;
- }
-
- protected void force() {
- FileLib.sync(file) ;
- }
-
- // @Override
- final public boolean isClosed() {
- return file == null ;
- }
-
- protected final void checkIfClosed() {
- if ( isClosed() )
- getLog().error("File has been closed") ;
- }
-
- protected abstract void _close() ;
-
- @Override
- final public void close() {
- _close() ;
- file = null ;
- }
-
- @Override
- public String getLabel() {
- return label ;
- }
-}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccessByteArray.java
----------------------------------------------------------------------
diff --git a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccessByteArray.java b/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccessByteArray.java
deleted file mode 100644
index 1f8e2b3..0000000
--- a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccessByteArray.java
+++ /dev/null
@@ -1,130 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * 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 org.seaborne.dboe.base.file ;
-
-import java.nio.ByteBuffer ;
-
-import org.apache.jena.atlas.lib.ByteBufferLib ;
-import org.seaborne.dboe.base.block.Block ;
-import static org.seaborne.dboe.sys.Sys.SizeOfInt ;
-
-/**
- * FileAccess interface backed by a byte array.
- */
-public class BlockAccessByteArray implements BlockAccess {
- private ByteBuffer bytes ;
- private long length ; // Bytes in use: 0 to length-1
- private long alloc ; // Bytes allocated - next allocation number.
- private final String label ;
-
- public BlockAccessByteArray(String label) {
- bytes = ByteBuffer.allocate(1024) ;
- length = 0 ;
- alloc = 0 ;
- this.label = label ;
- }
-
- @Override
- public String getLabel() {
- return label ;
- }
-
- @Override
- public Block allocate(int size) {
- long addr = alloc ;
- ByteBuffer bb = ByteBuffer.allocate(size) ;
- alloc += (size + SizeOfInt) ;
- return new Block((int)addr, bb) ;
- }
-
- @Override
- public long allocBoundary() {
- return alloc ;
- }
-
- @Override
- public void resetAllocBoundary(long boundary) {
- ByteBufferLib.fill(bytes, (int)boundary, (int)length, (byte)0);
- length = boundary ;
- alloc = boundary ;
- bytes.limit((int)alloc) ;
-
- }
-
- @Override
- public Block read(long id) {
- // Variable length blocks.
- if ( id < 0 || id >= length || id >= bytes.capacity() )
- throw new FileException("Bad id (read): " + id) ;
- bytes.position((int)id) ;
- int len = bytes.getInt() ;
- ByteBuffer bb = ByteBuffer.allocate(len) ;
- // Copy out the bytes - copy for safety.
- bytes.get(bb.array(), 0, len) ;
- return new Block(id, bb) ;
- }
-
- @Override
- public void write(Block block) {
- // Variable length blocks.
- long loc = block.getId() ;
- if ( loc < 0 || loc > length ) // Can be equal => append.
- throw new FileException("Bad id (write): " + loc + " (" + alloc + "," + length + ")") ;
- ByteBuffer bb = block.getByteBuffer() ;
- int len = bb.capacity() ;
-
- if ( loc == length ) {
- if ( bytes.capacity() - length < len ) {
- int cap2 = bytes.capacity() + 1024 ;
- while (bytes.capacity() - length < len)
- cap2 += 1024 ;
-
- ByteBuffer bytes2 = ByteBuffer.allocate(cap2) ;
- bytes2.position(0) ;
- bytes2.put(bytes) ;
- bytes = bytes2 ;
- }
- length += len + SizeOfInt ;
- }
- bytes.position((int)loc) ;
- bytes.putInt(len) ;
- bytes.put(bb.array(), 0, bb.capacity()) ;
- }
-
- @Override
- public void overwrite(Block block) {
- write(block) ;
- }
-
- @Override
- public boolean isEmpty() {
- return length == 0 ;
- }
-
- @Override
- public boolean valid(long id) {
- return (id >= 0 && id < length) ;
- }
-
- @Override
- public void sync() {}
-
- @Override
- public void close() {}
-}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccessDirect.java
----------------------------------------------------------------------
diff --git a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccessDirect.java b/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccessDirect.java
deleted file mode 100644
index 1cc808d..0000000
--- a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccessDirect.java
+++ /dev/null
@@ -1,131 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * 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 org.seaborne.dboe.base.file;
-
-import static java.lang.String.format ;
-
-import java.io.IOException ;
-import java.nio.ByteBuffer ;
-
-import org.apache.jena.atlas.lib.FileOps ;
-import org.seaborne.dboe.base.block.Block ;
-import org.seaborne.dboe.sys.FileLib ;
-import org.slf4j.Logger ;
-import org.slf4j.LoggerFactory ;
-
-public class BlockAccessDirect extends BlockAccessBase
-{
- // Maybe layer BlockAccess on BufferChannel - retrofitting.
- // but need separate for memory mapped files anyway.
-
- private static Logger log = LoggerFactory.getLogger(BlockAccessDirect.class) ;
-
- public BlockAccessDirect(String filename, int blockSize)
- {
- super(filename, blockSize) ;
- }
-
- @Override
- public Block allocate(int blkSize)
- {
- if ( blkSize > 0 && blkSize != this.blockSize )
- throw new FileException("Fixed blocksize only: request= "+blkSize+"fixed size="+this.blockSize) ;
- int x = allocateId() ;
- ByteBuffer bb = ByteBuffer.allocate(blkSize) ;
- Block block = new Block(x, bb) ;
- return block;
- }
-
- @Override
- public Block read(long id)
- {
- check(id) ;
- checkIfClosed() ;
- ByteBuffer bb = ByteBuffer.allocate(blockSize) ;
- readByteBuffer(id, bb) ;
- bb.rewind() ;
- Block block = new Block(id, bb) ;
- return block ;
- }
-
- private void readByteBuffer(long id, ByteBuffer dst)
- {
- try {
- int len = file.read(dst, filePosition(id)) ;
- if ( len != blockSize )
- throw new FileException(format("get: short read (%d, not %d)", len, blockSize)) ;
- } catch (IOException ex)
- { throw new FileException("FileAccessDirect", ex) ; }
- }
-
- private final long filePosition(long id)
- {
- return id*blockSize ;
- }
-
- @Override
- public void write(Block block)
- {
- check(block) ;
- checkIfClosed() ;
- ByteBuffer bb = block.getByteBuffer() ;
- // This .clear() except the javadoc suggests this is not the correct use of .clear()
- // and the name does
- bb.limit(bb.capacity()) ; // It shouldn't have been changed.
- bb.rewind() ;
- try {
- int len = file.write(bb, filePosition(block.getId())) ;
- if ( len != blockSize )
- throw new FileException(format("write: short write (%d, not %d)", len, blockSize)) ;
- } catch (IOException ex)
- { throw new FileException("FileAccessDirect", ex) ; }
- writeNotification(block) ;
- }
-
- @Override
- public void overwrite(Block block)
- {
- overwriteNotification(block) ;
- write(block) ;
- }
-
- @Override
- protected void _resetAllocBoundary(long boundary) {
- FileLib.truncate(file, filePosition(boundary)) ;
- }
-
- @Override
- public void sync()
- {
- force() ;
- }
-
- @Override
- protected void _close()
- { super.force() ; }
-
- @Override
- protected Logger getLog()
- {
- return log ;
- }
-
- @Override
- public String toString() { return "Direct:"+FileOps.basename(filename) ; }
-}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccessMapped.java
----------------------------------------------------------------------
diff --git a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccessMapped.java b/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccessMapped.java
deleted file mode 100644
index aa9a484..0000000
--- a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccessMapped.java
+++ /dev/null
@@ -1,268 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * 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 org.seaborne.dboe.base.file;
-
-import static java.lang.String.format ;
-
-import java.io.IOException ;
-import java.nio.ByteBuffer ;
-import java.nio.MappedByteBuffer ;
-import java.nio.channels.FileChannel.MapMode ;
-import java.util.Arrays ;
-
-import org.seaborne.dboe.base.block.Block ;
-import org.seaborne.dboe.sys.SystemIndex ;
-import org.slf4j.Logger ;
-import org.slf4j.LoggerFactory ;
-
-/** FileAccess for a file, using memory mapped I/O */
-final
-public class BlockAccessMapped extends BlockAccessBase
-{
- /* Blocks are addressed by positive ints -
- * Is that a limit?
- * One billion is 2^30
- * If a block is 8K, the 2^31*2^13 = 2^44 bits or 2^14 billion = 16K Billion. = 16 trillion bytes.
- * No limit at the moment - later performance tuning will see what the cost of 48 or 63 bit addresses would be.
- */
-
- private static Logger log = LoggerFactory.getLogger(BlockAccessMapped.class) ;
- private enum CopyContents { Overwrite, NoCopy }
-
- // Segmentation avoids over-mapping; allows file to grow (in chunks)
- private final int GrowthFactor = 2 ;
- private final int SegmentSize = SystemIndex.SegmentSize ;
- private final int blocksPerSegment ;
-
- private int initialNumSegements = 1 ;
- private MappedByteBuffer[] segments = new MappedByteBuffer[initialNumSegements] ;
-
- // Unflushed segments.
- private int segmentDirtyCount = 0 ;
- private boolean[] segmentDirty = new boolean[initialNumSegements] ;
-
- public BlockAccessMapped(String filename, int blockSize) {
- super(filename, blockSize) ;
- blocksPerSegment = SegmentSize/blockSize ;
- if ( SegmentSize%blockSize != 0 )
- getLog().warn(format("%s: Segment size(%d) not a multiple of blocksize (%d)", filename, SegmentSize, blockSize)) ;
-
- for ( int i = 0 ; i < initialNumSegements ; i++ )
- // Not strictly necessary - default value is false.
- segmentDirty[i] = false ;
- segmentDirtyCount = 0 ;
-
- if ( getLog().isDebugEnabled() )
- getLog().debug(format("Segment:%d BlockSize=%d blocksPerSegment=%d", SegmentSize, blockSize, blocksPerSegment)) ;
- }
-
-
- @Override
- public Block allocate(int blkSize) {
- if ( blkSize > 0 && blkSize != this.blockSize )
- throw new FileException("Fixed blocksize only: request= "+blkSize+"fixed size="+this.blockSize) ;
- int id = allocateId() ;
- ByteBuffer bb = getByteBuffer(id) ;
- bb.position(0) ;
- Block block = new Block(id, bb) ;
- return block ;
- }
-
- @Override
- public Block read(long id) {
- check(id) ;
- checkIfClosed() ;
- ByteBuffer bb = getByteBuffer(id) ;
- bb.position(0) ;
- Block block = new Block(id, bb) ;
- return block ;
- }
-
- @Override
- public void write(Block block) {
- write(block, CopyContents.NoCopy) ;
- }
-
- @Override
- public void overwrite(Block block) {
- overwriteNotification(block) ;
- write(block, CopyContents.Overwrite) ;
- }
-
- private void write(Block block, CopyContents copyContents) {
- check(block) ;
- checkIfClosed() ;
- int id = block.getId().intValue() ;
-
- if ( copyContents == CopyContents.Overwrite ) {
- ByteBuffer bbDst = getByteBuffer(id) ;
- bbDst.position(0) ;
- ByteBuffer bbSrc = block.getByteBuffer() ;
- bbSrc.rewind() ;
- bbDst.put(bbSrc) ;
- }
-
- // Assumed MRSW - no need to sync as we are the only Writer
- segmentDirty[segment(id)] = true ;
- writeNotification(block) ;
- }
-
- @Override
- public void sync() {
- checkIfClosed() ;
- force() ;
- }
-
- private ByteBuffer getByteBuffer(long _id) {
- // Limitation: ids must be integers.
- // ids are used to index into []-arrays.
- int id = (int)_id ;
-
- int seg = segment(id) ; // Segment.
- int segOff = byteOffset(id) ; // Byte offset in segment
-
- if ( getLog().isTraceEnabled() )
- getLog().trace(format("%d => [%d, %d]", id, seg, segOff)) ;
-
- synchronized (this) {
- try {
- // Need to put the alloc AND the slice/reset inside a sync.
- ByteBuffer segBuffer = allocSegment(seg) ;
- // Now slice the buffer to get the ByteBuffer to return
- segBuffer.position(segOff) ;
- segBuffer.limit(segOff+blockSize) ;
- ByteBuffer dst = segBuffer.slice() ;
-
- // And then reset limit to max for segment.
- segBuffer.limit(segBuffer.capacity()) ;
- // Extend block count when we allocate above end.
- numFileBlocks = Math.max(numFileBlocks, id+1) ;
- return dst ;
- } catch (IllegalArgumentException ex) {
- // Shouldn't (ha!) happen because the second "limit" resets
- log.error("Id: "+id) ;
- log.error("Seg="+seg) ;
- log.error("Segoff="+segOff) ;
- log.error(ex.getMessage(), ex) ;
- throw ex ;
- }
- }
- }
-
- private final int segment(int id) { return id/blocksPerSegment ; }
- private final int byteOffset(int id) { return (id%blocksPerSegment)*blockSize ; }
- private final long fileLocationForSegment(long segmentNumber) { return segmentNumber*SegmentSize ; }
-
- // Even for MultipleReader this needs to be sync'ed.??
- private MappedByteBuffer allocSegment(int seg)
- {
- // Auxiliary function for get - which holds the lock needed here.
- // The MappedByteBuffer must be sliced and reset once found/allocated
- // so as not to mess up the underlying MappedByteBuffer in segments[].
-
- // Only allocSegment(seg) and flushDirtySegements() and close()
- // directly access segments[]
-
- if ( seg < 0 ) {
- getLog().error("Segment negative: "+seg) ;
- throw new FileException("Negative segment: "+seg) ;
- }
-
- while ( seg >= segments.length ) {
- // More space needed.
- MappedByteBuffer[] segments2 = new MappedByteBuffer[GrowthFactor*segments.length] ;
- System.arraycopy(segments, 0, segments2, 0, segments.length) ;
- boolean[] segmentDirty2 = new boolean[GrowthFactor*segmentDirty.length] ;
- System.arraycopy(segmentDirty, 0, segmentDirty2, 0, segmentDirty.length) ;
-
- segmentDirty = segmentDirty2 ;
- segments = segments2 ;
- }
-
- long offset = fileLocationForSegment(seg) ;
-
- if ( offset < 0 ) {
- getLog().error("Segment offset gone negative: "+seg) ;
- throw new FileException("Negative segment offset: "+seg) ;
- }
-
- MappedByteBuffer segBuffer = segments[seg] ;
- if ( segBuffer == null ) {
- try {
- segBuffer = file.map(MapMode.READ_WRITE, offset, SegmentSize) ;
- if ( getLog().isDebugEnabled() )
- getLog().debug(format("Segment: %d", seg)) ;
- segments[seg] = segBuffer ;
- }
- catch (IOException ex) {
- if ( ex.getCause() instanceof java.lang.OutOfMemoryError )
- throw new FileException("BlockMgrMapped.segmentAllocate: Segment = " + seg + " : Offset = " + offset) ;
- throw new FileException("BlockMgrMapped.segmentAllocate: Segment = " + seg, ex) ;
- }
- }
- return segBuffer ;
- }
-
- private synchronized void flushDirtySegments() {
-
- // A linked list (with uniqueness) of dirty segments may be better.
- for ( int i = 0 ; i < segments.length ; i++ ) {
- if ( segments[i] != null && segmentDirty[i] ) {
- // Can we "flush" them all at once?
- segments[i].force() ;
- segmentDirty[i] = false ;
- segmentDirtyCount-- ;
- }
- }
- // This on its own does not force dirty segments to disk.
- super.force() ;
- }
-
- @Override
- protected void _resetAllocBoundary(long boundary) {
-
- }
-
-
- @Override
- protected void _close() {
- force() ;
- // There is no unmap operation for MappedByteBuffers.
- // Sun Bug id bug_id=4724038
- // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4724038
- Arrays.fill(segments, null) ;
- Arrays.fill(segmentDirty, false) ;
- segmentDirtyCount = 0 ;
- }
-
- @Override
- protected void force() {
- flushDirtySegments() ;
- }
-
- @Override
- protected Logger getLog() {
- return log ;
- }
-
- @Override
- public String toString() {
- return super.getLabel() ;
- }
-}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccessMem.java
----------------------------------------------------------------------
diff --git a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccessMem.java b/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccessMem.java
deleted file mode 100644
index 74a9433..0000000
--- a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BlockAccessMem.java
+++ /dev/null
@@ -1,189 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * 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 org.seaborne.dboe.base.file;
-
-import static java.lang.String.format ;
-
-import java.nio.ByteBuffer ;
-import java.util.ArrayList ;
-import java.util.List ;
-
-import org.apache.jena.atlas.RuntimeIOException ;
-import org.seaborne.dboe.base.block.Block ;
-import org.seaborne.dboe.sys.Sys;
-
-/**
- * File access layer that simulates a disk in-memory - for testing, not written for efficiency.
- * There is a safe mode, whereby blocks are copied in and out to guarantee no writing to an unallocated block.
- * This is very inefficient but a better simulation of a disk.
- *
- * @see BlockAccessByteArray
- */
-
-public class BlockAccessMem implements BlockAccess
-{
- // "SafeMode" - duplicate the block data much like a disk.
- // Mildly expensive for large block sizes.
-
- public static boolean SafeMode = true ;
- static final boolean Checking = true ;
- boolean fileClosed = false ;
- private List<Block> blocks = new ArrayList<>() ;
- private final boolean safeModeThisMgr ;
- protected final int blockSize ;
- private final String label ;
-
- public BlockAccessMem(String label, int blockSize) {
- this(label, blockSize, SafeMode) ;
- }
-
- private BlockAccessMem(String label, int blockSize, boolean b) {
- this.blockSize = blockSize ;
- this.label = label ;
- safeModeThisMgr = b ;
- }
-
- @Override
- public Block allocate(int blkSize) {
- checkNotClosed() ;
- if ( blkSize > 0 && blkSize != this.blockSize )
- throw new FileException("Fixed blocksize only: request= " + blkSize + " / fixed size=" + this.blockSize) ;
-
- int x = blocks.size() ;
- ByteBuffer bb = ByteBuffer.allocate(blkSize) ;
- Block block = new Block(x, bb) ;
- blocks.add(block) ;
- return block ;
- }
-
- @Override
- public Block read(long id) {
- checkNotClosed() ;
- check(id) ;
- Block blk = blocks.get((int)id) ;
- blk = replicateBlock(blk) ;
- blk.setModified(false) ;
- return blk ;
- }
-
- @Override
- public void write(Block block) {
- checkNotClosed() ;
- check(block) ;
- _write(block) ;
- }
-
- @Override
- public void overwrite(Block block) {
- checkNotClosed() ;
- write(block) ;
- }
-
- private void _write(Block block) {
- block = replicateBlock(block) ;
- block.setModified(false) ;
- // Memory isn't scaling to multi gigabytes.
- blocks.set(block.getId().intValue(), block) ;
- }
-
- private Block replicateBlock(Block blk) {
- if ( safeModeThisMgr )
- // Deep replicate.
- return blk.replicate() ;
- // Just the block wrapper.
- return new Block(blk.getId(), blk.getByteBuffer()) ;
- }
-
- @Override
- public boolean isEmpty() {
- checkNotClosed() ;
- return blocks.isEmpty() ;
- }
-
- @Override
- public long allocBoundary() {
- checkNotClosed() ;
- return blocks.size() ;
- }
-
- @Override
- public void resetAllocBoundary(long boundary) {
- checkNotClosed() ;
- // Clear the list from boundary onwards.
- blocks.subList((int)boundary, blocks.size()).clear() ;
- }
-
- @Override
- public boolean valid(long id) {
- checkNotClosed() ;
- return id >= 0 && id < blocks.size() ;
- }
-
- private void checkNotClosed() {
- if ( fileClosed )
- throw new RuntimeIOException("Already closed") ;
- }
-
- @Override
- public void close() {
- if ( fileClosed )
- return ;
- fileClosed = true ;
- blocks = null ;
- }
-
- @Override
- public void sync() {
- checkNotClosed() ;
- }
-
-
- private void check(Block block) {
- check(block.getId()) ;
- check(block.getByteBuffer()) ;
- }
-
- private void check(long id) {
- if ( id > Integer.MAX_VALUE )
- throw new FileException("BlockAccessMem: Bounds exception (id large than an int): " + id) ;
- if ( !Checking )
- return ;
- if ( id < 0 || id >= blocks.size() )
- throw new FileException("BlockAccessMem: "+label+": Bounds exception: " + id + " in [0, "+blocks.size()+")") ;
- }
-
- private void check(ByteBuffer bb) {
- if ( !Checking )
- return ;
- if ( bb.capacity() != blockSize )
- throw new FileException(format("FileAccessMem: Wrong size block. Expected=%d : actual=%d", blockSize, bb.capacity())) ;
- if ( bb.order() != Sys.NetworkOrder )
- throw new FileException("BlockMgrMem: Wrong byte order") ;
- }
-
- @Override
- public String getLabel() {
- return label ;
- }
-
- @Override
- public String toString() {
- return "Mem:" + label ;
- }
-}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BufferChannel.java
----------------------------------------------------------------------
diff --git a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BufferChannel.java b/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BufferChannel.java
deleted file mode 100644
index 2629e86f..0000000
--- a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BufferChannel.java
+++ /dev/null
@@ -1,96 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * 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 org.seaborne.dboe.base.file;
-
-import java.nio.ByteBuffer ;
-import java.nio.channels.FileChannel ;
-
-import org.apache.jena.atlas.lib.Closeable ;
-import org.apache.jena.atlas.lib.Sync ;
-
-
-/**
- * Interface to storage : a simplified version of FileChannel. Read and write
- * bytes, passed via ByteBuffers, addressed by file location. This interface is
- * not suitable for memory mapped I/O - there is no ability to use slices of a
- * memort mapped file. This interface does not insert size of ByteBuffer - size
- * of ByteBuffer passed to read controls the number of bytes read. Having our
- * own abstraction enables us to implement memory-backed versions.
- *
- * @see BlockAccess
- * @see java.nio.channels.FileChannel
- */
-public interface BufferChannel extends Sync, Closeable
-{
- // Like java.nio.channels.FileChannel except
- // Allows implementation by non-files e.g. a memory backed storage.
- // Runtime exceptions.
- // This is a simple, low level "file = array of bytes" interface"
- // This interface does not support slicing - so it's not suitable for memory mapped I/O
- //
- // TODO Consider use of allocateDirect
-
- /** Return another channel to the same storage but with independent position.
- * Chaos may result due to concurrent use.
- */
- public BufferChannel duplicate() ;
-
- /** return the position */
- public long position() ;
-
- /** set the position */
- public void position(long pos) ;
-
- /** Read into a ByteBuffer. Returns the number of bytes read. -1 for end of file.
- */
- public int read(ByteBuffer buffer) ;
-
- /** Read into a ByteBuffer, starting at position loc. Return the number of bytes read.
- * loc must be within the file.
- */
- public int read(ByteBuffer buffer, long loc) ;
-
- /** Write from ByteBuffer, starting at the current position.
- * Return the number of bytes written
- */
- public int write(ByteBuffer buffer) ;
-
- /** Write from ByteBuffer, starting at position loc.
- * Return the number of bytes written.
- * loc must be within 0 to length - writing at length is append */
- public int write(ByteBuffer buffer, long loc) ;
-
- /** Truncate the file.
- * @see FileChannel#truncate(long)
- */
- public void truncate(long size) ;
-
- /** Length of storage, in bytes.*/
- public long size() ;
-
- /** Is it empty? */
- public boolean isEmpty() ;
-
- /** useful display string */
- public String getLabel() ;
-
- /** Filename for this BufferChannel (maybe null) */
- public String getFilename() ;
-
-}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BufferChannelFile.java
----------------------------------------------------------------------
diff --git a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BufferChannelFile.java b/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BufferChannelFile.java
deleted file mode 100644
index 73ff170..0000000
--- a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BufferChannelFile.java
+++ /dev/null
@@ -1,172 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * 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 org.seaborne.dboe.base.file ;
-
-import java.io.IOException ;
-import java.nio.ByteBuffer ;
-import java.nio.channels.FileChannel ;
-
-import org.apache.jena.atlas.io.IO ;
-import org.seaborne.dboe.sys.FileLib ;
-
-public class BufferChannelFile implements BufferChannel {
- private String filename ;
- private FileChannel file ;
-
- /** Create a BufferChannelFile */
- public static BufferChannelFile create(String filename) {
- return create(filename, "rw") ;
- }
-
- /** Create a BufferChannelFile */
- public static BufferChannelFile create(String filename, String mode) {
- FileChannel base = ChannelManager.acquire(filename, mode) ;
- return new BufferChannelFile(filename, base) ;
- }
-
- /** Create a BufferChannelFile with unmangaged file resources - use with care */
- public static BufferChannelFile createUnmanaged(String filename, String mode) {
- FileChannel channel = FileLib.openUnmanaged(filename, mode) ;
- return new BufferChannelFile(filename, channel) ;
- }
-
- private BufferChannelFile(String filename, FileChannel channel) {
- this.filename = filename ;
- this.file = channel ;
- }
-
- @Override
- public BufferChannel duplicate() {
- return new BufferChannelFile(filename, file) ;
- }
-
- @Override
- public long position() {
- try {
- return file.position() ;
- }
- catch (IOException e) {
- IO.exception(e) ;
- return -1 ;
- }
- }
-
- @Override
- public void position(long pos) {
- try {
- file.position(pos) ;
- }
- catch (IOException e) {
- IO.exception(e) ;
- }
- }
-
- @Override
- public void truncate(long length) {
- try {
- // http://bugs.sun.com/view_bug.do?bug_id=6191269
- if ( length < file.position() )
- file.position(length) ;
- file.truncate(length) ;
- }
- catch (IOException e) {
- IO.exception(e) ;
- }
- }
-
- @Override
- public int read(ByteBuffer buffer) {
- try {
- return file.read(buffer) ;
- }
- catch (IOException e) {
- IO.exception(e) ;
- return -1 ;
- }
- }
-
- @Override
- public int read(ByteBuffer buffer, long loc) {
- try {
- return file.read(buffer, loc) ;
- }
- catch (IOException e) {
- IO.exception(e) ;
- return -1 ;
- }
- }
-
- @Override
- public int write(ByteBuffer buffer) {
- try {
- return file.write(buffer) ;
- }
- catch (IOException e) {
- IO.exception(e) ;
- return -1 ;
- }
- }
-
- @Override
- public int write(ByteBuffer buffer, long loc) {
- try {
- return file.write(buffer, loc) ;
- }
- catch (IOException e) {
- IO.exception(e) ;
- return -1 ;
- }
- }
-
- @Override
- public long size() {
- return FileLib.size(file) ;
- }
-
- @Override
- public boolean isEmpty() {
- return size() == 0 ;
- }
-
- @Override
- public void sync() {
- FileLib.sync(file) ;
- }
-
- @Override
- public void close() {
- FileLib.close(file) ;
- }
-
- @Override
- public String getLabel() {
- return filename ;
- }
-
- @Override
- public String toString() {
- return filename ;
- }
-
- @Override
- public String getFilename() {
- return filename ;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BufferChannelMem.java
----------------------------------------------------------------------
diff --git a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BufferChannelMem.java b/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BufferChannelMem.java
deleted file mode 100644
index f683741..0000000
--- a/jena-db/dboe-base/src/main/java/org/seaborne/dboe/base/file/BufferChannelMem.java
+++ /dev/null
@@ -1,241 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
- *
- * 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 org.seaborne.dboe.base.file ;
-
-import java.nio.ByteBuffer ;
-
-import org.apache.jena.atlas.lib.ByteBufferLib ;
-import org.apache.jena.atlas.logging.FmtLog ;
-import org.seaborne.dboe.base.StorageException ;
-import org.slf4j.Logger ;
-import org.slf4j.LoggerFactory ;
-
-// XXX Merge/replace with SegmentedByteBuffer which has more predicable performance.
-public class BufferChannelMem implements BufferChannel {
- private static Logger log = LoggerFactory.getLogger(BufferChannelMem.class) ;
- // The "file pointer" is the position of this buffer.
- // The "size" is the limit of this buffer.
- private ByteBuffer bytes ;
- private String name ;
- private static int INC_SIZE = 1024 ;
-
- private final boolean TRACKING ;
-
- static public BufferChannel create() {
- return new BufferChannelMem("unnamed") ;
- }
-
- static public BufferChannel create(String name) {
- return new BufferChannelMem(name) ;
- }
-
- private BufferChannelMem() {
- // Unitialized blank.
- TRACKING = false ;
- }
-
- private BufferChannelMem(String name) {
- bytes = ByteBuffer.allocate(1024) ;
- bytes.limit(0) ;
- this.name = name ;
- TRACKING = false ;
- // Debugging : pick a filename.
- // TRACKING = name.endsWith("prefixes.dat") ;
- }
-
- @Override
- synchronized public BufferChannel duplicate() {
- BufferChannelMem chan = new BufferChannelMem() ;
- int x = bytes.position() ;
- bytes.rewind() ;
- chan.bytes = bytes.slice() ;
- chan.bytes.position(0) ;
- bytes.position(x) ;
- return chan ;
- }
-
- @Override
- synchronized public long position() {
- checkIfClosed() ;
- return bytes.position() ;
- }
-
- @Override
- synchronized public void position(long pos) {
- checkIfClosed() ;
- if ( pos < 0 || pos > bytes.capacity() )
- throw new StorageException("Out of range: " + pos) ;
- bytes.position((int)pos) ;
- }
-
- @Override
- synchronized public int read(ByteBuffer buffer) {
- checkIfClosed() ;
- if ( TRACKING )
- log("read<<%s", ByteBufferLib.details(buffer));
-
- int x = bytes.position() ;
-
- int len = buffer.limit() - buffer.position() ;
- if ( len > bytes.remaining() )
- len = bytes.remaining() ;
- // Copy out, moving the position of the bytes of stroage.
- for (int i = 0; i < len; i++) {
- byte b = bytes.get() ;
- buffer.put(b) ;
- }
- if ( TRACKING )
- log("read>>") ;
- return len ;
- }
-
- @Override
- synchronized public int read(ByteBuffer buffer, long loc) {
- checkIfClosed() ;
- if ( TRACKING )
- log("read<<@%d", loc) ;
- if ( loc < 0 || loc > bytes.limit() )
- throw new StorageException("Out of range(" + name + "[read]): " + loc + " [0," + bytes.limit() + ")") ;
- if ( loc == bytes.limit() )
- log.warn("At the limit(" + name + "[read]): " + loc) ;
- int x = bytes.position() ;
- bytes.position((int)loc) ;
- int len = read(buffer) ;
- bytes.position(x) ;
- if ( TRACKING )
- log("read>>@%d",loc) ;
- return len ;
- }
-
- @Override
- synchronized public int write(ByteBuffer buffer) {
- checkIfClosed() ;
- if ( TRACKING )
- log("write<<%s", ByteBufferLib.details(buffer));
- int len = buffer.limit() - buffer.position() ;
- int posn = bytes.position() ;
-
- int freespace = bytes.capacity() - bytes.position() ;
-
- if ( len > freespace ) {
- int inc = len - freespace ;
- inc += INC_SIZE ;
- ByteBuffer bb2 = ByteBuffer.allocate(bytes.capacity() + inc) ;
- bytes.position(0) ;
- // Copy contents; make written bytes area the same as before.
- bb2.put(bytes) ;
- bb2.limit(bytes.limit()) ; // limit is used as the end of active
- // bytes.
- bb2.position(posn) ;
- bytes = bb2 ;
- }
-
- if ( bytes.limit() < posn + len )
- bytes.limit(posn + len) ;
-
- bytes.put(buffer) ;
-
- if ( TRACKING )
- log("write>>") ;
- return len ;
- }
-
- // Invert : write(ByteBuffer) = write(ByteBuffer,posn)
- @Override
- synchronized public int write(ByteBuffer buffer, long loc) {
- checkIfClosed() ;
- if ( TRACKING )
- log("write<<@%d", loc) ;
- if ( loc < 0 || loc > bytes.limit() )
- // Can write at loc = bytes()
- throw new StorageException("Out of range(" + name + "[write]): " + loc + " [0," + bytes.limit() + ")") ;
- int x = bytes.position() ;
- bytes.position((int)loc) ;
- int len = write(buffer) ;
- bytes.position(x) ;
- if ( TRACKING )
- log("write>>@%d", loc) ;
- return len ;
- }
-
- @Override
- synchronized public void truncate(long size) {
- checkIfClosed() ;
- if ( TRACKING )
- log("truncate(%d)", size) ;
- int x = (int)size ;
- if ( x < 0 )
- throw new StorageException("Out of range: " + size) ;
- if ( x > bytes.limit() )
- return ;
-
- if ( bytes.position() > x )
- bytes.position(x) ;
- bytes.limit(x) ;
- }
-
- @Override
- synchronized public long size() {
- checkIfClosed() ;
- return bytes.limit() ;
- }
-
- @Override
- synchronized public boolean isEmpty() {
- checkIfClosed() ;
- return size() == 0 ;
- }
-
- @Override
- synchronized public void sync() {
- checkIfClosed() ;
- }
-
- @Override
- synchronized public void close() {
- if ( bytes == null )
- return ;
- bytes = null ;
- }
-
- private void checkIfClosed() {
- if ( bytes == null )
- throw new StorageException("Closed: " + name) ;
- }
-
- @Override
- synchronized public String getLabel() {
- return name ;
- }
-
- @Override
- synchronized public String toString() {
- return name ;
- }
-
- @Override
- public String getFilename() {
- return null ;
- }
-
- private void log(String fmt, Object... args) {
- if ( TRACKING )
- FmtLog.debug(log, fmt, args);
- }
-}