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:05:49 UTC
[037/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/jena-dboe-base/README.md
----------------------------------------------------------------------
diff --git a/jena-db/jena-dboe-base/README.md b/jena-db/jena-dboe-base/README.md
new file mode 100644
index 0000000..5969da0
--- /dev/null
+++ b/jena-db/jena-dboe-base/README.md
@@ -0,0 +1 @@
+Basic file-level abstractions.
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/jena-dboe-base/pom.xml
----------------------------------------------------------------------
diff --git a/jena-db/jena-dboe-base/pom.xml b/jena-db/jena-dboe-base/pom.xml
new file mode 100644
index 0000000..07d6ba1
--- /dev/null
+++ b/jena-db/jena-dboe-base/pom.xml
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ 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.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <artifactId>jena-dboe-base</artifactId>
+ <packaging>jar</packaging>
+ <name>Mantis (${project.artifactId})</name>
+
+ <parent>
+ <groupId>org.apache.jena</groupId>
+ <artifactId>jena-db</artifactId>
+ <version>3.5.0-SNAPSHOT</version>
+ </parent>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>org.apache.jena</groupId>
+ <artifactId>jena-arq</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.jena</groupId>
+ <artifactId>jena-arq</artifactId>
+ <classifier>tests</classifier>
+ <scope>test</scope>
+ </dependency>
+
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jar-plugin</artifactId>
+ <executions>
+ <execution>
+ <goals>
+ <goal>test-jar</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+
+</project>
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/DBOpEnvException.java
----------------------------------------------------------------------
diff --git a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/DBOpEnvException.java b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/DBOpEnvException.java
new file mode 100644
index 0000000..eeac779
--- /dev/null
+++ b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/DBOpEnvException.java
@@ -0,0 +1,29 @@
+/*
+ * 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;
+
+import org.apache.jena.shared.JenaException;
+
+public class DBOpEnvException extends JenaException
+{
+ public DBOpEnvException() { super() ; }
+ public DBOpEnvException(String msg) { super(msg) ; }
+ public DBOpEnvException(Throwable th) { super(th) ; }
+ public DBOpEnvException(String msg, Throwable th) { super(msg, th) ; }
+}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/StorageException.java
----------------------------------------------------------------------
diff --git a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/StorageException.java b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/StorageException.java
new file mode 100644
index 0000000..5f2d3b2
--- /dev/null
+++ b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/StorageException.java
@@ -0,0 +1,27 @@
+/*
+ * 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;
+
+public class StorageException extends RuntimeException
+{
+ public StorageException() { super() ; }
+ public StorageException(String msg) { super(msg) ; }
+ public StorageException(Throwable th) { super(th) ; }
+ public StorageException(String msg, Throwable th) { super(msg, th) ; }
+}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/Block.java
----------------------------------------------------------------------
diff --git a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/Block.java b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/Block.java
new file mode 100644
index 0000000..a5ad514
--- /dev/null
+++ b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/Block.java
@@ -0,0 +1,166 @@
+/*
+ * 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.block;
+
+import java.io.ByteArrayOutputStream ;
+import java.io.PrintStream ;
+import java.nio.ByteBuffer ;
+import org.apache.jena.atlas.lib.ByteBufferLib ;
+
+public final class Block
+{
+ // XXX Note development code added to enforce read-only use. Marked [RO]
+
+ // While the general mechanisms support long block id,
+ // some uses make restrictions.
+ // BlockMgrs:
+ // Blocks for indexes (B+Trees) are addressed by int - one int is 2G of 8K units = 16T
+ // Blocks for objects are addressed by long - this is file offset so 2G is not enough.
+
+ private final Long id ; // Keep as object. It's the cache key.
+ //private BlockRef blockRef ;
+
+
+ // These are information carrying flags -- not enforced
+ // although .write checks the readOnly flags.
+ private boolean readOnly = false ;
+ private boolean modified = false ;
+
+ private /*[[Dev-RO]] final*/ ByteBuffer byteBuffer ;
+ // If the byteBuffer is, say, a slice of another one,
+ // this can be used to carry a ref to the real ByteBuffer.
+ private ByteBuffer underlyingByteBuffer ;
+
+ public Block(long id, ByteBuffer byteBuffer) {
+ this(Long.valueOf(id), byteBuffer) ;
+ }
+
+ public Block(Long id, ByteBuffer byteBuffer) {
+ // ByteBuffer is whole disk space from byte 0 for this disk unit.
+ this.id = id ;
+ this.byteBuffer = byteBuffer ;
+ // this.blockRef = null ;
+ // Default - writeable, unmodified block.
+ this.readOnly = false ;
+ this.modified = false ;
+ this.underlyingByteBuffer = null ;
+ }
+
+ public final Long getId() {
+ return id ;
+ }
+
+ public final ByteBuffer getByteBuffer() {
+ return byteBuffer ;
+ }
+
+ public boolean isReadOnly() {
+ return readOnly ;
+ }
+
+ public void setReadOnly(boolean readonly) {
+ if ( readonly && modified )
+ throw new BlockException("Attempt to mark a modified block as read-only") ;
+ // Hard to force a read-only byte buffer because higher levels
+ // may slice this and have their own byte buffers or even
+ // intBuffers etc.
+ this.readOnly = readonly ;
+ }
+
+ public boolean isModified() {
+ return modified ;
+ }
+
+ public void setModified(boolean modified) {
+ if ( readOnly && modified )
+ throw new BlockException("Attempt to mark a readonly block as modified") ;
+ this.modified = modified ;
+ }
+
+ public ByteBuffer getUnderlyingByteBuffer()
+ { return underlyingByteBuffer ; }
+
+ public void setUnderlyingByteBuffer(ByteBuffer underlyingByteBuffer)
+ { this.underlyingByteBuffer = underlyingByteBuffer ; }
+
+ @Override
+ public String toString() {
+ ByteBuffer bb = getByteBuffer() ;
+ if ( true )
+ // Short form.
+ return String.format("Block: %d (posn=%d, limit=%d, cap=%d)", id, bb.position(), bb.limit(), bb.capacity()) ;
+ // Long form - with some bytes from the ByteBuffer.
+ ByteArrayOutputStream out = new ByteArrayOutputStream() ;
+ PrintStream x = new PrintStream(out) ;
+ ByteBufferLib.print(x, bb) ;
+ x.flush() ;
+ String str = out.toString() ;
+ return String.format("Block: %d %s", id, str) ;
+ }
+
+ /** Deep copy, including ByteBuffer contents into a HeapByteBuffer. */
+ public Block replicate() {
+ ByteBuffer dstBuffer = ByteBuffer.allocate(getByteBuffer().capacity()) ;
+ return replicate(dstBuffer) ;
+ }
+
+ /**
+ * Deep copy, including ByteBuffer contents, using the supplied ByteBuffer
+ * to hold the contents and to be used when constructing the new Block. The
+ * capacity of the supplied ByteBuffer must be equal to or greater than this
+ * block's capacity.
+ */
+ private Block replicate(ByteBuffer dstBuffer) {
+ replicateByteBuffer(getByteBuffer(), dstBuffer) ;
+ Block b = new Block(getId(), dstBuffer) ;
+ b.setModified(modified) ;
+ b.setReadOnly(readOnly) ;
+ // b.blockRef = null ;
+ return b ;
+ }
+
+ public static void replicate(Block srcBlock, Block dstBlock) {
+ if ( !srcBlock.getId().equals(dstBlock.getId()) )
+ throw new BlockException("FileAccessMem: Attempt to copy across blocks: " + srcBlock.getId() + " => " + dstBlock.getId()) ;
+ replicate(srcBlock.getByteBuffer(), dstBlock.getByteBuffer()) ;
+ }
+
+ private static ByteBuffer replicateByteBuffer(ByteBuffer srcBlk, ByteBuffer dstBlk) {
+ int x = srcBlk.position() ;
+ int y = srcBlk.limit() ;
+ srcBlk.clear() ;
+
+ if ( srcBlk.hasArray() && dstBlk.hasArray() )
+ System.arraycopy(srcBlk.array(), 0, dstBlk.array(), 0, srcBlk.capacity()) ;
+ else
+ dstBlk.put(srcBlk) ;
+
+ srcBlk.position(x) ;
+ dstBlk.position(x) ;
+ srcBlk.limit(y) ;
+ dstBlk.limit(y) ;
+ return dstBlk ;
+ }
+
+ private static void replicate(ByteBuffer srcBlk, ByteBuffer dstBlk) {
+ srcBlk.position(0) ;
+ dstBlk.position(0) ;
+ dstBlk.put(srcBlk) ;
+ }
+}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockException.java
----------------------------------------------------------------------
diff --git a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockException.java b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockException.java
new file mode 100644
index 0000000..8a950af
--- /dev/null
+++ b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockException.java
@@ -0,0 +1,29 @@
+/*
+ * 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.block;
+
+import org.seaborne.dboe.base.file.FileException ;
+
+public class BlockException extends FileException
+{
+ public BlockException() { super() ; }
+ public BlockException(String msg) { super(msg) ; }
+ public BlockException(Throwable th) { super(th) ; }
+ public BlockException(String msg, Throwable th) { super(msg, th) ; }
+}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgr.java
----------------------------------------------------------------------
diff --git a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgr.java b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgr.java
new file mode 100644
index 0000000..641dc58
--- /dev/null
+++ b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgr.java
@@ -0,0 +1,100 @@
+/*
+ * 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.block;
+
+import org.apache.jena.atlas.lib.Closeable ;
+import org.apache.jena.atlas.lib.Sync ;
+
+
+public interface BlockMgr extends Sync, Closeable /*UnitMgr<Block>*/
+{
+ /** Allocate an uninitialized block - writable - call only inside a update sequence.
+ * If blockSize is -1, means "default/fixed size" for this BlockMgr
+ */
+ public Block allocate(int blockSize) ;
+
+ /** Answer whether there are any blocks in the collection being managed */
+ public boolean isEmpty() ;
+
+ /** The fixed allocated blocks are in the range [0,allocLimit).
+ * Allocation units need not be +1 increments */
+ public long allocLimit() ;
+
+ /** Reset the allocation limit, should be a number previously obtained from allocLimit */
+ public void resetAlloc(long boundary) ;
+
+ /** Fetch a block, use for read only */
+ public Block getRead(long id);
+
+ /** Fetch a block, use for write and read - only inside "update" */
+ public Block getWrite(long id);
+
+ /** Release a block, unmodified or already written. */
+ public void release(Block block) ;
+
+ /** Promote to writeable : it's OK to promote an already writeable block */
+ public Block promote(Block block);
+
+ // Bad name? "endWrite", "put" -- for a mapped block, the changes are made directly, not on the write() */
+ /** Write a block back - it still needs releasing. */
+ public void write(Block block) ;
+
+ /** Replace the contents of a block slot with new contents. Block does not need releasing.
+ * The write() operation may not do real work if the block is mapped - this operation
+ * really does replace the contents with the new contents.
+ */
+ public void overwrite(Block blk) ;
+
+ /** Announce a block is no longer in use (i.e it's now freed) */
+ public void free(Block block);
+
+ /** Is this a valid block id? (may be a free block)*/
+ public boolean valid(int id) ;
+
+ /** Close the block manager */
+ @Override
+ public void close() ;
+
+ /** Is this block manager still usable? Closed block managers can not perform any operations except this one. */
+ public boolean isClosed() ;
+
+ /** Sync the block manager */
+ @Override
+ public void sync() ;
+
+ /** Sync the block manager : system operation to ensure sync() is passed down */
+ public void syncForce() ;
+
+ // begin/end markers.
+
+ /** Start of update */
+ public void beginUpdate() ;
+
+ /** Completion of update */
+ public void endUpdate() ;
+
+ /** Start of read */
+ public void beginRead() ;
+
+ /** Completion of read */
+ public void endRead() ;
+
+ /* Label for helping trace which BlockMgr is which */
+ public String getLabel() ;
+}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrBase.java
----------------------------------------------------------------------
diff --git a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrBase.java b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrBase.java
new file mode 100644
index 0000000..5e2581b
--- /dev/null
+++ b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrBase.java
@@ -0,0 +1,52 @@
+/*
+ * 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.block;
+
+import org.slf4j.Logger ;
+
+public abstract class BlockMgrBase implements BlockMgr
+{
+ protected final int blockSize ;
+ private String label ;
+ protected abstract Logger log() ;
+
+ // Fixed size, fixed block type.
+ protected BlockMgrBase(String label, int blockSize)
+ {
+ this.label = label ;
+ this.blockSize = blockSize ;
+ }
+
+ @Override
+ public final Block allocate(int blkSize)
+ {
+ if ( blkSize > 0 && blkSize != this.blockSize )
+ throw new BlockException("Fixed blocksize BlockMgr: request= "+blkSize+" fixed size="+this.blockSize) ;
+ return allocate() ;
+ }
+
+ protected abstract Block allocate() ;
+
+ @Override final public String getLabel() { return label ; }
+
+ @Override public void endUpdate() {}
+ @Override public void beginUpdate() {}
+ @Override public void beginRead() {}
+ @Override public void endRead() {}
+}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrBuilder.java
----------------------------------------------------------------------
diff --git a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrBuilder.java b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrBuilder.java
new file mode 100644
index 0000000..1c5ede7
--- /dev/null
+++ b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrBuilder.java
@@ -0,0 +1,25 @@
+/*
+ * 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.block;
+
+import org.seaborne.dboe.base.file.FileSet ;
+
+public interface BlockMgrBuilder {
+ BlockMgr buildBlockMgr(FileSet fileSet, String ext, BlockParams indexParams) ;
+}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrBuilderStd.java
----------------------------------------------------------------------
diff --git a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrBuilderStd.java b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrBuilderStd.java
new file mode 100644
index 0000000..47bb37e
--- /dev/null
+++ b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrBuilderStd.java
@@ -0,0 +1,29 @@
+/*
+ * 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.block;
+
+import org.seaborne.dboe.base.file.FileSet ;
+
+public class BlockMgrBuilderStd implements BlockMgrBuilder {
+ @Override
+ public BlockMgr buildBlockMgr(FileSet fileSet, String ext, BlockParams indexParams) {
+ return BlockMgrFactory.create(fileSet, ext, indexParams) ;
+ }
+}
+
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrCache.java
----------------------------------------------------------------------
diff --git a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrCache.java b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrCache.java
new file mode 100644
index 0000000..1f0e7c1
--- /dev/null
+++ b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrCache.java
@@ -0,0 +1,326 @@
+/*
+ * 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.block ;
+
+import java.util.ArrayList ;
+import java.util.Iterator ;
+import java.util.List ;
+
+import org.apache.jena.atlas.lib.Cache ;
+import org.apache.jena.atlas.lib.CacheFactory ;
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+
+/** Caching block manager - this is an LRU cache */
+public class BlockMgrCache extends BlockMgrSync {
+ // Actually, this is two cache one on the read blocks and one on the write
+ // blocks.
+ // The overridden public operations are sync'ed.
+ // As sync is on "this", it also covers all the other operations via
+ // BlockMgrSync
+
+ private static Logger log = LoggerFactory.getLogger(BlockMgrCache.class) ;
+ // Read cache : always present.
+ private final Cache<Long, Block> readCache ;
+
+ // Delayed dirty writes. May be present, may not.
+ private final Cache<Long, Block> writeCache ;
+
+ // Also enable the logging level.
+ public static boolean globalLogging = false ;
+ private boolean logging = false ;
+ // ---- stats
+ long cacheReadHits = 0 ;
+ long cacheMisses = 0 ;
+ long cacheWriteHits = 0 ;
+
+ static BlockMgr create(int readSlots, int writeSlots, final BlockMgr blockMgr) {
+ if ( readSlots < 0 && writeSlots < 0 )
+ return blockMgr ;
+ return new BlockMgrCache(readSlots, writeSlots, blockMgr) ;
+ }
+
+ private BlockMgrCache(int readSlots, int writeSlots, final BlockMgr blockMgr) {
+ super(blockMgr) ;
+ // Caches are related so we can't use a Getter for cache management.
+ if ( readSlots < -1 )
+ readCache = CacheFactory.createNullCache() ;
+ else
+ readCache = CacheFactory.createCache(readSlots) ;
+ if ( writeSlots <= 0 )
+ writeCache = null ;
+ else {
+ writeCache = CacheFactory.createCache(writeSlots) ;
+ writeCache.setDropHandler((id, block) -> {
+ // We're inside a synchronized operation at this point.
+ log("Cache spill: write block: %d", id) ;
+ if ( block == null ) {
+ log.warn("Write cache: " + id + " dropping an entry that isn't there") ;
+ return ;
+ }
+ // Force the block to be writtern
+ // by sending it to the wrapped BlockMgr
+ BlockMgrCache.super.write(block) ;
+ }) ;
+ }
+ }
+
+ @Override
+ synchronized public void resetAlloc(long boundary) {
+ // On abort, need to clear the caches of inaccesible blocks.
+ // An abort is rare (?). We do very carefully.
+ // Could (probably) delete in the loop or use Iteator.remove on keys().
+ // (Check the Cache contract)
+ List<Long> removals = new ArrayList<>() ;
+ readCache.keys().forEachRemaining((x)->{
+ if ( x >= boundary )
+ removals.add(x) ;
+ }) ;
+ removals.forEach(readCache::remove) ;
+ removals.clear() ;
+ writeCache.keys().forEachRemaining((x)->{
+ if ( x >= boundary )
+ removals.add(x) ;
+ }) ;
+ removals.forEach(writeCache::remove) ;
+ super.resetAlloc(boundary);
+ }
+
+ @Override
+ synchronized public Block getRead(long id) {
+ // A Block may be in the read cache or the write cache.
+ // It can be just in the write cache because the read cache is finite.
+ Block blk = readCache.getIfPresent(id) ;
+ if ( blk != null ) {
+ cacheReadHits++ ;
+ log("Hit(r->r) : %d", id) ;
+ return blk ;
+ }
+
+ // A requested block may be in the other cache.
+ // Writable blocks are readable.
+ // readable blocks are not writeable (see below).
+ if ( writeCache != null )
+ // Might still be in the dirty blocks.
+ // Leave in write cache
+ blk = writeCache.getIfPresent(id) ;
+ if ( blk != null ) {
+ cacheWriteHits++ ;
+ log("Hit(r->w) : %d", id) ;
+ return blk ;
+ }
+
+ cacheMisses++ ;
+ log("Miss/r: %d", id) ;
+ blk = super.getRead(id) ;
+ readCache.put(id, blk) ;
+ return blk ;
+ }
+
+ @Override
+ synchronized public Block getWrite(long _id) {
+ Long id = _id ;
+ Block blk = null ;
+ if ( writeCache != null )
+ blk = writeCache.getIfPresent(id) ;
+ if ( blk != null ) {
+ cacheWriteHits++ ;
+ log("Hit(w->w) : %d", id) ;
+ return blk ;
+ }
+
+ // blk is null.
+ // A requested block may be in the other cache. Promote it.
+
+ if ( readCache.containsKey(id) ) {
+ blk = readCache.getIfPresent(id) ;
+ cacheReadHits++ ;
+ log("Hit(w->r) : %d", id) ;
+ blk = promote(blk) ;
+ return blk ;
+ }
+
+ // Did not find.
+ cacheMisses++ ;
+ log("Miss/w: %d", id) ;
+ // Pass operation to wrapper.
+ blk = super.getWrite(id) ;
+ if ( writeCache != null )
+ writeCache.put(id, blk) ;
+ return blk ;
+ }
+
+ @Override
+ synchronized public Block promote(Block block) {
+ Long id = block.getId() ;
+ readCache.remove(id) ;
+ Block block2 = super.promote(block) ;
+ if ( writeCache != null )
+ writeCache.put(id, block2) ;
+ return block ;
+ }
+
+ @Override
+ synchronized public void write(Block block) {
+ writeCache(block) ;
+ super.write(block) ;
+ }
+
+ @Override
+ synchronized public void overwrite(Block block) {
+ Long id = block.getId() ;
+ // It can be a read block (by the transaction), now being written for
+ // real (enacting a transaction).
+ super.overwrite(block) ;
+ // Keep read cache up-to-date.
+ // Must at least expel the read block (which is not the overwrite
+ // block).
+ readCache.put(id, block) ;
+ }
+
+ private void writeCache(Block block) {
+ Long id = block.getId() ;
+ log("WriteCache : %d", id) ;
+ // Should not be in the read cache due to a getWrite earlier.
+ if ( readCache.containsKey(id) )
+ log.warn("write: Block in the read cache") ;
+ if ( writeCache != null ) {
+ writeCache.put(id, block) ;
+ return ;
+ }
+ }
+
+ @Override
+ synchronized public void free(Block block) {
+ Long id = block.getId() ;
+ log("Free : %d", id) ;
+ if ( readCache.containsKey(id) ) {
+ log.warn("Freeing block from read cache") ;
+ readCache.remove(id) ;
+ }
+ if ( writeCache != null )
+ writeCache.remove(id) ;
+ super.free(block) ;
+ }
+
+ @Override
+ synchronized public void sync() {
+ _sync(false) ;
+ }
+
+ @Override
+ synchronized public void syncForce() {
+ _sync(true) ;
+ }
+
+ @Override
+ synchronized public void close() {
+ if ( writeCache != null )
+ log("close (" + writeCache.size() + " blocks)") ;
+ syncFlush() ;
+ super.close() ;
+ }
+
+ @Override
+ public String toString() {
+ return "Cache:" + super.blockMgr.toString() ;
+ }
+
+ private void log(String fmt, Object... args) {
+ if ( !logging && !globalLogging )
+ return ;
+ String msg = String.format(fmt, args) ;
+ if ( getLabel() != null )
+ msg = getLabel() + " : " + msg ;
+ log.debug(msg) ;
+ }
+
+ private void _sync(boolean force) {
+ if ( true ) {
+ String x = "" ;
+ if ( getLabel() != null )
+ x = getLabel() + " : " ;
+ log("%sH=%d, M=%d, W=%d", x, cacheReadHits, cacheMisses, cacheWriteHits) ;
+ }
+
+ if ( writeCache != null )
+ log("sync (%d blocks)", writeCache.size()) ;
+ else
+ log("sync") ;
+ boolean somethingWritten = syncFlush() ;
+
+ if ( force ) {
+ log("syncForce underlying BlockMgr") ;
+ super.syncForce() ;
+ } else if ( somethingWritten ) {
+ log("sync underlying BlockMgr") ;
+ super.sync() ;
+ } else
+ log("Empty sync") ;
+ }
+
+ private boolean syncFlush() {
+ if ( writeCache == null )
+ return false ;
+
+ boolean didSync = false ;
+
+ log("Flush (write cache)") ;
+
+ long N = writeCache.size() ;
+ Long[] ids = new Long[(int)N] ;
+
+ // Single writer (sync is a write operation MRSW)
+ // Iterating is safe.
+
+ Iterator<Long> iter = writeCache.keys() ;
+ if ( iter.hasNext() )
+ didSync = true ;
+
+ // Need to get all then delete else concurrent modification exception.
+ for ( int i = 0 ; iter.hasNext() ; i++ )
+ ids[i] = iter.next() ;
+
+ for ( int i = 0 ; i < N ; i++ ) {
+ Long id = ids[i] ;
+ expelEntry(id) ;
+ }
+ if ( didSync )
+ super.sync() ;
+ return didSync ;
+ }
+
+ // Write out when flushed.
+ // Do not call from drop handler.
+ private void expelEntry(Long id) {
+ Block block = writeCache.getIfPresent(id) ;
+ if ( block == null ) {
+ log.warn("Write cache: " + id + " expelling entry that isn't there") ;
+ return ;
+ }
+ log("Expel (write cache): %d", id) ;
+ // This pushes the block to the BlockMgr being cached.
+ super.write(block) ;
+ writeCache.remove(id) ;
+
+ // Move it into the readCache because it's often read after writing
+ // and the read cache is often larger.
+ readCache.put(id, block) ;
+ }
+}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrFactory.java
----------------------------------------------------------------------
diff --git a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrFactory.java b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrFactory.java
new file mode 100644
index 0000000..8515cbe
--- /dev/null
+++ b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrFactory.java
@@ -0,0 +1,151 @@
+/*
+ * 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.block ;
+
+import org.apache.jena.atlas.logging.Log ;
+import org.seaborne.dboe.DBOpEnvException ;
+import org.seaborne.dboe.base.file.* ;
+import org.seaborne.dboe.sys.SystemIndex ;
+
+public class BlockMgrFactory {
+ // This isn't always helpful so be careful if setting the default to "true".
+ // Sometimes the tracking is too strict
+ // e.g. transactions keep blocks and not release them down the layers.
+ public/* final */static boolean AddTracker = false ;
+
+ public static BlockMgr tracker(BlockMgr blockMgr) {
+ if ( blockMgr instanceof BlockMgrTracker )
+ return blockMgr ;
+ return BlockMgrTracker.track(blockMgr) ;
+ }
+
+ /** Add a tracker if the system default is to do so */
+ private static BlockMgr track(BlockMgr blockMgr) {
+ if ( !AddTracker )
+ return blockMgr ;
+ return tracker(blockMgr) ;
+ }
+
+ public static BlockMgr create(FileSet fileSet, String ext, BlockParams params) {
+ return create(fileSet, ext,
+ params.getFileMode(),
+ params.getBlockSize(),
+ params.getBlockReadCacheSize(),
+ params.getBlockWriteCacheSize()) ;
+ }
+
+ public static BlockMgr create(FileSet fileSet, String ext, int blockSize, int readBlockCacheSize, int writeBlockCacheSize) {
+ return create(fileSet, ext, null, blockSize, readBlockCacheSize, writeBlockCacheSize) ;
+ }
+
+ // XXX Deprecate?
+ public static BlockMgr create(FileSet fileSet, String ext, FileMode fileMode, int blockSize, int readBlockCacheSize, int writeBlockCacheSize) {
+ if ( fileSet.isMem() )
+ return createMem(fileSet.filename(ext), blockSize) ;
+ else
+ return createFile(fileSet.filename(ext), fileMode, blockSize, readBlockCacheSize, writeBlockCacheSize) ;
+ }
+
+ /** Create an in-memory block manager */
+ public static BlockMgr createMem(String indexName, int blockSize) {
+ BlockAccess file = new BlockAccessMem(indexName, blockSize) ;
+ BlockMgr blockMgr = new BlockMgrFileAccess(file, blockSize) ;
+ blockMgr = new BlockMgrFreeChain(blockMgr) ;
+ // Small cache - testing.
+ blockMgr = BlockMgrCache.create(3, 3, blockMgr) ;
+ return track(blockMgr) ;
+ }
+
+ /** Create a BlockMgr backed by a real file */
+ public static BlockMgr createFile(String filename, BlockParams params) {
+ return createFile(filename,
+ params.getFileMode(), params.getBlockSize(),
+ params.getBlockReadCacheSize(), params.getBlockWriteCacheSize()) ;
+ }
+
+ /** Create a BlockMgr backed by a real file */
+ public static BlockMgr createFile(String filename, FileMode fileMode, int blockSize, int readBlockCacheSize, int writeBlockCacheSize) {
+ if ( fileMode == null )
+ fileMode = SystemIndex.fileMode() ;
+ switch (fileMode) {
+ case mapped :
+ return createMMapFile(filename, blockSize) ;
+ case direct :
+ return createStdFile(filename, blockSize, readBlockCacheSize, writeBlockCacheSize) ;
+ }
+ throw new DBOpEnvException("Unknown file mode: " + fileMode) ;
+ }
+
+ /** Create a NIO Block Manager */
+ public static BlockMgr createMMapFile(String filename, int blockSize) {
+ BlockAccess file = new BlockAccessMapped(filename, blockSize) ;
+ BlockMgr blockMgr = wrapFileAccess(file, blockSize) ;
+ return track(blockMgr) ;
+ }
+
+ /** Create a Block Manager using direct access (and a cache) */
+ public static BlockMgr createStdFile(String filename, int blockSize, int readBlockCacheSize, int writeBlockCacheSize) {
+ BlockAccess file = new BlockAccessDirect(filename, blockSize) ;
+ BlockMgr blockMgr = wrapFileAccess(file, blockSize) ;
+ blockMgr = addCache(blockMgr, readBlockCacheSize, writeBlockCacheSize) ;
+ return track(blockMgr) ;
+ }
+
+ /** Create a Block Manager using direct access, no caching, no nothing. */
+ public static BlockMgr createStdFileNoCache(String filename, int blockSize) {
+ BlockAccess blockAccess = new BlockAccessDirect(filename, blockSize) ;
+ BlockMgr blockMgr = new BlockMgrFileAccess(blockAccess, blockSize) ;
+ return blockMgr ;
+ }
+
+ /**
+ * Add a caching layer to a BlockMgr.
+ * <p>
+ * This does not make sense for memory BlockMgr or for memory mapper files.
+ * This function always add the cache.
+ *
+ * @see #addCache(BlockMgr, FileSet, FileMode, int, int)
+ */
+ public static BlockMgr addCache(BlockMgr blockMgr, int readBlockCacheSize, int writeBlockCacheSize) {
+ if ( blockMgr instanceof BlockMgrCache )
+ Log.warn(BlockMgrFactory.class, "BlockMgr already has a cache: " + blockMgr.getLabel()) ;
+ return BlockMgrCache.create(readBlockCacheSize, writeBlockCacheSize, blockMgr) ;
+ }
+
+ /**
+ * Add a caching layer to a BlockMgr if appropriate. This does not make
+ * sense for memory BlockMgr or for memory mapper files. These are skipped.
+ */
+ public static BlockMgr addCache(BlockMgr blockMgr, FileSet fileSet, FileMode fileMode, int readBlockCacheSize, int writeBlockCacheSize) {
+ if ( fileSet.isMem() )
+ return blockMgr ;
+ if ( fileMode == null )
+ fileMode = SystemIndex.fileMode() ;
+ if ( fileMode == FileMode.mapped )
+ return blockMgr ;
+ return addCache(blockMgr, readBlockCacheSize, writeBlockCacheSize) ;
+ }
+
+ private static BlockMgr wrapFileAccess(BlockAccess blockAccess, int blockSize) {
+ BlockMgr blockMgr = new BlockMgrFileAccess(blockAccess, blockSize) ;
+ // This is a temporary fix to the problem
+ blockMgr = new BlockMgrFreeChain(blockMgr) ;
+ return blockMgr ;
+ }
+}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrFileAccess.java
----------------------------------------------------------------------
diff --git a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrFileAccess.java b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrFileAccess.java
new file mode 100644
index 0000000..796c634
--- /dev/null
+++ b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrFileAccess.java
@@ -0,0 +1,169 @@
+/*
+ * 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.block ;
+
+import static java.lang.String.format ;
+
+import org.apache.jena.atlas.lib.InternalErrorException ;
+import org.apache.jena.atlas.lib.Lib ;
+import org.seaborne.dboe.base.file.BlockAccess ;
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+
+/**
+ * Block manager that maps from the FileAccess layer to a BlockMgr. Add free
+ * block management (but we should layer with BlockMgrFreeChain)
+ */
+
+final public class BlockMgrFileAccess extends BlockMgrBase {
+ private static Logger log = LoggerFactory.getLogger(BlockMgrFileAccess.class) ;
+
+ private final BlockAccess file ;
+ private boolean closed = false ;
+ // Set on any write operations.
+ private boolean syncNeeded = false ;
+
+ // Create via the BlockMgrFactory.
+ /* package */BlockMgrFileAccess(BlockAccess blockAccess, int blockSize) {
+ super(blockAccess.getLabel(), blockSize) ;
+ file = blockAccess ;
+ }
+
+ @Override
+ protected Block allocate() {
+ syncNeeded = true ;
+ return file.allocate(blockSize) ;
+ }
+
+ @Override
+ public Block promote(Block block) {
+ return block ;
+ }
+
+ @Override
+ public Block getRead(long id) {
+ return getBlock(id, true) ;
+ }
+
+ @Override
+ public Block getWrite(long id) {
+ return getBlock(id, false) ;
+ }
+
+ private Block getBlock(long id, boolean readOnly) {
+ checkNotClosed() ;
+ Block block = file.read(id) ;
+ block.setReadOnly(readOnly) ;
+ return block ;
+ }
+
+ private void checkNotClosed() {
+ if ( closed )
+ throw new InternalErrorException(Lib.className(this)+" : already closed") ;
+ }
+
+ @Override
+ public void release(Block block) {
+ checkNotClosed() ;
+ }
+
+ @Override
+ public void write(Block block) {
+ checkNotClosed() ;
+ if ( block.isReadOnly() )
+ throw new BlockException("Attempt to write a read-only block ("+block.getId()+")" ) ;
+ syncNeeded = true ;
+ file.write(block) ;
+ }
+
+ @Override
+ public void overwrite(Block block) {
+ checkNotClosed() ;
+ syncNeeded = true ;
+ file.overwrite(block) ;
+ }
+
+ @Override
+ public void free(Block block) {
+ checkNotClosed() ;
+ // syncNeeded = true ;
+ // We do nothing about free blocks currently.
+ }
+
+ @Override
+ public boolean valid(int id) {
+ checkNotClosed() ;
+ return file.valid(id) ;
+ }
+
+ @Override
+ public void sync() {
+ checkNotClosed() ;
+ if ( syncNeeded )
+ file.sync() ;
+ else
+ syncNeeded = true ;
+ syncNeeded = false ;
+ }
+
+ @Override
+ public void syncForce() {
+ checkNotClosed() ;
+ file.sync() ;
+ }
+
+ @Override
+ public boolean isClosed() {
+ return closed ;
+ }
+
+ @Override
+ public void close() {
+ closed = true ;
+ file.close() ;
+ }
+
+ @Override
+ public boolean isEmpty() {
+ checkNotClosed() ;
+ return file.isEmpty() ;
+ }
+
+ @Override
+ public long allocLimit() {
+ checkNotClosed() ;
+ return file.allocBoundary() ;
+ }
+
+ @Override
+ public void resetAlloc(long boundary) {
+ checkNotClosed() ;
+ file.resetAllocBoundary(boundary);
+ }
+
+ @Override
+ public String toString() {
+ return format("BlockMgrFileAccess[%d bytes]:%s", blockSize, file) ;
+ }
+
+ @Override
+ protected Logger log() {
+ return log ;
+ }
+}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrFreeChain.java
----------------------------------------------------------------------
diff --git a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrFreeChain.java b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrFreeChain.java
new file mode 100644
index 0000000..4b01693
--- /dev/null
+++ b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrFreeChain.java
@@ -0,0 +1,84 @@
+/*
+ * 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.block ;
+
+import java.util.ArrayDeque ;
+import java.util.Deque ;
+
+/**
+ * Recycle blocks - but only in-session. At the end of JVM run, the blocks are
+ * made "permanent" as no one finds them again on restart.
+ */
+final public class BlockMgrFreeChain extends BlockMgrWrapper {
+ // Could keep Pair<Integer, ByteBuffer>
+ // List<Block> freeBlocks = new ArrayList<Block>() ;
+ private final Deque<Block> freeBlocks = new ArrayDeque<>() ; // Keep as heap?
+
+ public BlockMgrFreeChain(BlockMgr blockMgr) {
+ super(blockMgr) ;
+ }
+
+ @Override
+ public Block allocate(int blockSize) {
+ if ( !freeBlocks.isEmpty() ) {
+ Block block = freeBlocks.removeFirst() ;
+ block.getByteBuffer().position(0) ;
+ return block ;
+ }
+ return super.allocate(blockSize) ;
+ }
+
+ @Override
+ public void free(Block block) {
+ if ( block.getId() >= blockMgr.allocLimit() )
+ freeBlocks.add(block) ;
+ }
+
+ @Override
+ public void resetAlloc(long boundary) {
+ super.resetAlloc(boundary);
+ // Just clear - assumes this is effectively a transaction boundary.
+ freeBlocks.clear();
+// Iterator<Block> iter = freeBlocks.iterator() ;
+// while(iter.hasNext()) {
+// Block blk = iter.next() ;
+// if ( blk.getId() < boundary )
+// iter.remove();
+// }
+ }
+ @Override
+ public boolean valid(int id) {
+ for ( Block blk : freeBlocks ) {
+ if ( blk.getId() == id )
+ return true ;
+ }
+ return super.valid(id) ;
+ }
+
+ @Override
+ public void sync() {
+ // Flush free blocks?
+ super.sync() ;
+ }
+
+ @Override
+ public String toString() {
+ return "Free:" + super.toString() ;
+ }
+}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrLogger.java
----------------------------------------------------------------------
diff --git a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrLogger.java b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrLogger.java
new file mode 100644
index 0000000..941a55e
--- /dev/null
+++ b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrLogger.java
@@ -0,0 +1,181 @@
+/*
+ * 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.block ;
+
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+
+public class BlockMgrLogger implements BlockMgr // extends BlockMgrWrapper
+{
+ private final BlockMgr blockMgr ;
+ protected final Logger log ;
+ protected final boolean logAllOperations ;
+ private final String label ;
+
+ public BlockMgrLogger(BlockMgr blockMgr, boolean logAllOperations) {
+ this(null, blockMgr.getLabel(), blockMgr, logAllOperations) ;
+ }
+
+ public BlockMgrLogger(String label, BlockMgr blockMgr, boolean logAllOperations) {
+ this(null, label, blockMgr, logAllOperations) ;
+ }
+
+ public BlockMgrLogger(Logger log, String label, BlockMgr blockMgr, boolean logAllOperations) {
+ this.blockMgr = blockMgr ;
+ if ( log == null )
+ log = LoggerFactory.getLogger(BlockMgr.class) ;
+ this.log = log ;
+ this.logAllOperations = logAllOperations ;
+ if ( label == null )
+ label = blockMgr.getLabel() ;
+ this.label = label ;
+ }
+
+ @Override
+ public String getLabel() {
+ return label ;
+ }
+
+ @Override
+ public Block allocate(int blockSize) {
+ Block x = blockMgr.allocate(blockSize) ;
+ info("Allocate(" + x.getId() + ")") ;
+ return x ;
+ }
+
+ @Override
+ public boolean isEmpty() {
+ info("isEmpty()") ;
+ return blockMgr.isEmpty() ;
+ }
+
+ @Override
+ public long allocLimit() {
+ info("limit()") ;
+ return blockMgr.allocLimit() ;
+ }
+
+ @Override
+ public void resetAlloc(long boundary) {
+ info("resetAlloc("+boundary+")") ;
+ blockMgr.resetAlloc(boundary) ;
+ }
+
+ @Override
+ public Block getRead(long id) {
+ info("getRead(" + id + ")") ;
+ return blockMgr.getRead(id) ;
+ }
+
+ @Override
+ public Block getWrite(long id) {
+ info("getWrite(" + id + ")") ;
+ return blockMgr.getWrite(id) ;
+ }
+
+ @Override
+ public Block promote(Block block) {
+ info("promote(" + block.getId() + ")") ;
+ return blockMgr.promote(block) ;
+ }
+
+ @Override
+ public void release(Block block) {
+ info("release(" + block.getId() + ")") ;
+ blockMgr.release(block) ;
+ }
+
+ @Override
+ public void write(Block block) {
+ info("write(" + block.getId() + ")") ;
+ blockMgr.write(block) ;
+ }
+
+ @Override
+ public void overwrite(Block block) {
+ info("overwrite(" + block.getId() + ")") ;
+ blockMgr.overwrite(block) ;
+ }
+
+ @Override
+ public void free(Block block) {
+ info("freeBlock(" + block.getId() + ")") ;
+ blockMgr.free(block) ;
+ }
+
+ @Override
+ public boolean valid(int id) {
+ info("valid(" + id + ")") ;
+ return blockMgr.valid(id) ;
+ }
+
+ @Override
+ public void close() {
+ info("close") ;
+ blockMgr.close() ;
+ }
+
+ @Override
+ public boolean isClosed() {
+ info("isClosed") ;
+ return blockMgr.isClosed() ;
+ }
+
+ @Override
+ public void sync() {
+ info("sync") ;
+ blockMgr.sync() ;
+ }
+
+ @Override
+ public void syncForce() {
+ info("syncForce") ;
+ blockMgr.syncForce() ;
+ }
+
+ @Override
+ public void beginRead() {
+ info("> start read") ;
+ blockMgr.beginRead() ;
+ }
+
+ @Override
+ public void endRead() {
+ info("< finish read") ;
+ blockMgr.endRead() ;
+ }
+
+ @Override
+ public void beginUpdate() {
+ info("> start update") ;
+ blockMgr.beginUpdate() ;
+ }
+
+ @Override
+ public void endUpdate() {
+ info("< finish update") ;
+ blockMgr.endUpdate() ;
+ }
+
+ private void info(String string) {
+ if ( label != null )
+ string = label + ": " + string ;
+ log.info(string) ;
+ }
+}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrReadonly.java
----------------------------------------------------------------------
diff --git a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrReadonly.java b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrReadonly.java
new file mode 100644
index 0000000..6842da6
--- /dev/null
+++ b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrReadonly.java
@@ -0,0 +1,45 @@
+/*
+ * 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.block;
+
+import org.seaborne.dboe.DBOpEnvException ;
+
+public class BlockMgrReadonly extends BlockMgrWrapper
+{
+ public BlockMgrReadonly(BlockMgr blockMgr) {
+ super(blockMgr) ;
+ }
+
+ @Override public void beginUpdate() { throw new DBOpEnvException("Read-only block manager") ; }
+ @Override public void endUpdate() { throw new DBOpEnvException("Read-only block manager") ; }
+ @Override public Block allocate(int blockSize) { throw new DBOpEnvException("Read-only block manager") ; }
+ @Override public Block getWrite(long id) { throw new DBOpEnvException("Read-only block manager") ; }
+ @Override public Block promote(Block block) { throw new DBOpEnvException("Read-only block manager") ; }
+ @Override public void write(Block block) { throw new DBOpEnvException("Read-only block manager") ; }
+ @Override public void overwrite(Block block) { throw new DBOpEnvException("Read-only block manager") ; }
+ @Override public void free(Block block) { throw new DBOpEnvException("Read-only block manager") ; }
+
+// @Override
+// public void sync()
+// {
+// blockMgr.sync() ;
+// }
+
+ @Override public String toString() { return "RO:"+super.toString() ; }
+}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrSwitcher.java
----------------------------------------------------------------------
diff --git a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrSwitcher.java b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrSwitcher.java
new file mode 100644
index 0000000..50fe4b3
--- /dev/null
+++ b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrSwitcher.java
@@ -0,0 +1,40 @@
+/*
+ * 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.block;
+
+public class BlockMgrSwitcher extends BlockMgrWrapper
+{
+ protected final BlockMgr blockMgr1 ;
+ protected final BlockMgr blockMgr2 ;
+
+ public BlockMgrSwitcher(BlockMgr blockMgr1, BlockMgr blockMgr2)
+ {
+ super(blockMgr1) ;
+ this.blockMgr1 = blockMgr1 ;
+ this.blockMgr2 = blockMgr2 ;
+ }
+
+ public void switchover()
+ {
+ if ( super.blockMgr == blockMgr1 )
+ setBlockMgr(blockMgr2) ;
+ else
+ setBlockMgr(blockMgr1) ;
+ }
+}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrSync.java
----------------------------------------------------------------------
diff --git a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrSync.java b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrSync.java
new file mode 100644
index 0000000..9620698
--- /dev/null
+++ b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrSync.java
@@ -0,0 +1,149 @@
+/*
+ * 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.block ;
+
+
+/**
+ * Add synchronized to a BlockMgr. This is the same as BlockMgrWrapper but with
+ * 'synchronized' added
+ */
+
+public class BlockMgrSync implements BlockMgr {
+ protected final BlockMgr blockMgr ;
+
+ public BlockMgrSync(BlockMgr blockMgr) {
+ this.blockMgr = blockMgr ;
+ }
+
+ public BlockMgr getWrapped() {
+ return blockMgr ;
+ }
+
+ @Override
+ synchronized public Block allocate(int blockSize) {
+ return blockMgr.allocate(blockSize) ;
+ }
+
+ @Override
+ synchronized public Block getRead(long id) {
+ return blockMgr.getRead(id) ;
+ }
+
+ @Override
+ synchronized public Block getWrite(long id) {
+ return blockMgr.getWrite(id) ;
+ }
+
+ @Override
+ synchronized public Block promote(Block block) {
+ return blockMgr.promote(block) ;
+ }
+
+ @Override
+ synchronized public void release(Block block) {
+ blockMgr.release(block) ;
+ }
+
+ @Override
+ synchronized public void write(Block block) {
+ blockMgr.write(block) ;
+ }
+
+ @Override
+ synchronized public void overwrite(Block block) {
+ blockMgr.overwrite(block) ;
+ }
+
+ @Override
+ synchronized public void free(Block block) {
+ blockMgr.free(block) ;
+ }
+
+ @Override
+ synchronized public void sync() {
+ blockMgr.sync() ;
+ }
+
+ @Override
+ synchronized public void syncForce() {
+ blockMgr.syncForce() ;
+ }
+
+ @Override
+ synchronized public void close() {
+ blockMgr.close() ;
+ }
+
+ @Override
+ synchronized public boolean isEmpty() {
+ return blockMgr.isEmpty() ;
+ }
+
+ @Override
+ synchronized public long allocLimit() {
+ return blockMgr.allocLimit() ;
+ }
+
+
+ @Override
+ synchronized public void resetAlloc(long boundary) {
+ blockMgr.resetAlloc(boundary) ;
+ }
+
+ @Override
+ synchronized public void beginRead() {
+ blockMgr.beginRead() ;
+ }
+
+ @Override
+ synchronized public void endRead() {
+ blockMgr.endRead() ;
+ }
+
+ @Override
+ synchronized public void beginUpdate() {
+ blockMgr.beginUpdate() ;
+ }
+
+ @Override
+ synchronized public void endUpdate() {
+ blockMgr.endUpdate() ;
+ }
+
+ @Override
+ synchronized public boolean valid(int id) {
+ return blockMgr.valid(id) ;
+ }
+
+ @Override
+ synchronized public boolean isClosed() {
+ return blockMgr.isClosed() ;
+ }
+
+ @Override
+ synchronized public String getLabel() {
+ return blockMgr.getLabel() ;
+ }
+
+ @Override
+ public String toString() {
+ return "Sync:" + blockMgr.toString() ;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrTracker.java
----------------------------------------------------------------------
diff --git a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrTracker.java b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrTracker.java
new file mode 100644
index 0000000..df93726
--- /dev/null
+++ b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrTracker.java
@@ -0,0 +1,414 @@
+/*
+ * 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.block ;
+
+import static org.seaborne.dboe.base.block.BlockMgrTracker.Action.Alloc ;
+import static org.seaborne.dboe.base.block.BlockMgrTracker.Action.BeginRead ;
+import static org.seaborne.dboe.base.block.BlockMgrTracker.Action.BeginUpdate ;
+import static org.seaborne.dboe.base.block.BlockMgrTracker.Action.EndRead ;
+import static org.seaborne.dboe.base.block.BlockMgrTracker.Action.EndUpdate ;
+import static org.seaborne.dboe.base.block.BlockMgrTracker.Action.Free ;
+import static org.seaborne.dboe.base.block.BlockMgrTracker.Action.GetRead ;
+import static org.seaborne.dboe.base.block.BlockMgrTracker.Action.GetWrite ;
+import static org.seaborne.dboe.base.block.BlockMgrTracker.Action.Promote ;
+import static org.seaborne.dboe.base.block.BlockMgrTracker.Action.Release ;
+import static org.seaborne.dboe.base.block.BlockMgrTracker.Action.Write ;
+
+import java.util.ArrayList ;
+import java.util.List ;
+
+import org.apache.jena.atlas.lib.Pair ;
+import org.apache.jena.ext.com.google.common.collect.HashMultiset ;
+import org.apache.jena.ext.com.google.common.collect.Multiset ;
+import org.seaborne.dboe.DBOpEnvException ;
+import org.slf4j.Logger ;
+import org.slf4j.LoggerFactory ;
+
+public class BlockMgrTracker /* extends BlockMgrWrapper */ implements BlockMgr {
+ private static Logger loggerDefault = LoggerFactory.getLogger(BlockMgrTracker.class) ;
+ public static boolean verbose = false ;
+
+ static enum Action {
+ Alloc, Promote, GetRead, GetWrite, Write, Release, Free,
+ BeginRead, EndRead,
+ BeginUpdate, EndUpdate
+ }
+ static final Long NoId = (long)-9 ;
+
+ // ---- State for tracking
+ // Track and count block references and releases
+ // No - the page is dirty.
+
+
+ protected final Multiset<Long> activeReadBlocks = HashMultiset.create() ;
+ protected final Multiset<Long> activeWriteBlocks = HashMultiset.create() ;
+ // Track the operations
+ protected final List<Pair<Action, Long>> actions = new ArrayList<>() ;
+ // ---- State for tracking
+
+ protected final BlockMgr blockMgr ;
+
+ private void clearInternalRW() {
+ activeReadBlocks.clear() ;
+ activeWriteBlocks.clear() ;
+ actions.clear() ;
+ }
+
+ private void clearInternalIter() {
+ clearInternalRW() ;
+ }
+
+ private int inRead = 0 ;
+ private boolean inUpdate = false ;
+ private boolean inBatch = false ;
+ private final Logger log ;
+ private final String label ;
+
+ public static BlockMgr track(BlockMgr blkMgr) {
+ return track(blkMgr.getLabel(), blkMgr) ;
+ }
+
+ private static BlockMgr track(String label, BlockMgr blkMgr) {
+ return new BlockMgrTracker(label, blkMgr) ;
+ }
+
+ private BlockMgrTracker(BlockMgr blockMgr) {
+ this(LoggerFactory.getLogger(BlockMgrTracker.class), blockMgr.getLabel(), blockMgr) ;
+ }
+
+ private BlockMgrTracker(String label, BlockMgr blockMgr) {
+ this(loggerDefault, label, blockMgr) ;
+ }
+
+ //
+ // public BlockMgrTracker(Class<?> cls, String label, BlockMgr blockMgr)
+ // {
+ // this(LoggerFactory.getLogger(cls), label, blockMgr) ;
+ // }
+
+ private BlockMgrTracker(Logger logger, String label, BlockMgr blockMgr) {
+ this.blockMgr = blockMgr ;
+ this.log = logger ;
+ this.label = blockMgr.getLabel() ;
+ }
+
+ private void add(Action action, Long id) {
+ actions.add(new Pair<>(action, id)) ;
+ }
+
+ @Override
+ public Block allocate(int blockSize) {
+ Block block ;
+ synchronized (this) {
+ checkUpdate(Alloc) ;
+ block = blockMgr.allocate(blockSize) ;
+ Long id = block.getId() ;
+ activeWriteBlocks.add(id) ;
+ add(Alloc, id) ;
+ }
+ return block ;
+ }
+
+ @Override
+ public Block getRead(long id) {
+ // What if this is a write block already?
+ synchronized (this) {
+ checkRead(GetRead) ;
+ Long x = id ;
+ add(GetRead, x) ;
+
+ if ( activeWriteBlocks.contains(x) )
+ activeWriteBlocks.add(x) ;
+ else
+ activeReadBlocks.add(x) ;
+ }
+ return blockMgr.getRead(id) ;
+ }
+
+ @Override
+ public Block getWrite(long id) {
+ synchronized (this) {
+ checkUpdate(GetWrite) ;
+ Long x = id ;
+ add(GetWrite, x) ;
+ activeWriteBlocks.add(x) ;
+ }
+ return blockMgr.getWrite(id) ;
+ }
+
+ @Override
+ public Block promote(Block block) {
+ synchronized (this) {
+ checkUpdate(Promote) ;
+ Long id = block.getId() ;
+ add(Promote, id) ;
+
+ if ( !activeWriteBlocks.contains(id) && !activeReadBlocks.contains(id) )
+ error(Promote, id + " is not an active block") ;
+
+ if ( activeReadBlocks.contains(id) )
+ // Remove one read count
+ // Really, do this if obtained via "getRead" and not "getWrite"
+ activeReadBlocks.remove(id) ;
+
+ // Double promotion results in only one entry.
+ if ( !activeWriteBlocks.contains(id) )
+ activeWriteBlocks.add(id) ;
+ }
+ return blockMgr.promote(block) ;
+ }
+
+ @Override
+ public void release(Block block) {
+ synchronized (this) {
+ checkRead(Release) ;
+ Long id = block.getId() ;
+ add(Release, id) ;
+
+ if ( !activeReadBlocks.contains(id) && !activeWriteBlocks.contains(id) )
+ error(Release, id + " is not an active block") ;
+
+ // May have been promoted.
+ if ( activeWriteBlocks.contains(id) )
+ activeWriteBlocks.remove(id) ;
+ else
+ activeReadBlocks.remove(block.getId()) ;
+ }
+ blockMgr.release(block) ;
+ }
+
+ @Override
+ public void write(Block block) {
+ writeTracker(block) ;
+ blockMgr.write(block) ;
+ }
+
+ @Override
+ synchronized public void overwrite(Block block) {
+ writeTracker(block) ;
+ blockMgr.overwrite(block) ;
+ }
+
+ private void writeTracker(Block block) {
+ synchronized (this) {
+ checkUpdate(Write) ;
+ Long id = block.getId() ;
+ add(Write, id) ;
+ if ( !activeWriteBlocks.contains(id) )
+ error(Write, id + " is not an active write block") ;
+ }
+ }
+
+ @Override
+ public void free(Block block) {
+ synchronized (this) {
+ checkUpdate(Free) ;
+ Long id = block.getId() ;
+ add(Free, id) ;
+ if ( activeReadBlocks.contains(id) )
+ error(Free, id + " is a read block") ;
+ else if ( !activeWriteBlocks.contains(id) )
+ error(Free, id + " is not a write block") ;
+ activeWriteBlocks.remove(id) ;
+ }
+ blockMgr.free(block) ;
+ }
+
+ @Override
+ public void sync() {
+ blockMgr.sync() ;
+ }
+
+ @Override
+ public void syncForce() {
+ blockMgr.syncForce() ;
+ }
+
+ @Override
+ public void close() {
+ blockMgr.close() ;
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return blockMgr.isEmpty() ;
+ }
+
+ @Override
+ public long allocLimit() {
+ return blockMgr.allocLimit() ;
+ }
+
+ @Override
+ public void resetAlloc(long boundary) {
+ blockMgr.resetAlloc(boundary) ;
+ }
+
+ @Override
+ public boolean valid(int id) {
+ return blockMgr.valid(id) ;
+ }
+
+ @Override
+ public boolean isClosed() {
+ return blockMgr.isClosed() ;
+ }
+
+ @Override
+ synchronized public void beginRead() {
+ synchronized (this) {
+ if ( inUpdate )
+ error(BeginRead, "beginRead when already in update") ;
+ inRead++ ;
+ inUpdate = false ;
+ }
+ blockMgr.beginRead() ;
+ }
+
+ @Override
+ synchronized public void endRead() {
+ synchronized (this) {
+ if ( inRead == 0 )
+ error(EndRead, "endRead but not in read") ;
+ if ( inUpdate )
+ error(EndRead, "endRead when in update") ;
+
+ checkEmpty("Outstanding write blocks at end of read operations!", activeWriteBlocks) ;
+
+ if ( inRead == 0 ) {
+ // Check at end of multiple reads or a write
+ checkEmpty("Outstanding read blocks at end of read operations", activeReadBlocks) ;
+ clearInternalRW() ;
+ }
+
+ inUpdate = false ;
+ inRead-- ;
+ }
+ blockMgr.endRead() ;
+ }
+
+ @Override
+ public void beginUpdate() {
+ synchronized (this) {
+ if ( inRead > 0 )
+ error(BeginUpdate, "beginUpdate when already in read") ;
+ if ( inUpdate )
+ error(BeginUpdate, "beginUpdate when already in update") ;
+ inUpdate = true ;
+ }
+ blockMgr.beginUpdate() ;
+ }
+
+ @Override
+ public void endUpdate() {
+ synchronized (this) {
+ if ( !inUpdate )
+ error(EndUpdate, "endUpdate but not in update") ;
+ if ( inRead > 0 )
+ error(EndUpdate, "endUpdate when in read") ;
+
+ checkEmpty("Outstanding read blocks at end of update operations", activeReadBlocks) ;
+
+ checkEmpty("Outstanding write blocks at end of update operations", activeWriteBlocks) ;
+
+ inUpdate = false ;
+ inRead = 0 ;
+ clearInternalRW() ;
+ }
+ blockMgr.endUpdate() ;
+ }
+
+ private void checkUpdate(Action action) {
+// if ( ! inBatch )
+// warn(action, "update called outside a batch") ;
+ if ( ! inUpdate )
+ error(action, "called outside update") ;
+ }
+
+ private void checkRead(Action action) {
+ if ( !inUpdate && inRead == 0 )
+ error(action, "Called outside update and read") ;
+ }
+
+ private void checkEmpty(String string, Multiset<Long> blocks) {
+ if ( !blocks.isEmpty() ) {
+ error(string) ;
+ for ( Long id : blocks )
+ info(" Block: " + id) ;
+ history() ;
+ throw new DBOpEnvException() ;
+ // debugPoint() ;
+ }
+ }
+
+ private String msg(String string) {
+ if ( label == null )
+ return string ;
+ return label + ": " + string ;
+ }
+
+ private void info(String string) {
+ log.info(msg(string)) ;
+ }
+
+ private void warn(String string) {
+ log.warn(msg(string)) ;
+ }
+
+ private void warn(Action action, String string) {
+ warn(action + ": " + string) ;
+ }
+
+ private void error(String string) {
+ log.error(msg(string)) ;
+ }
+
+ private void error(Action action, String string) {
+ if ( verbose ) {
+ error(action + ": " + string) ;
+ history() ;
+ }
+ throw new BlockException(msg(action + ": " + string)) ;
+ // debugPoint() ;
+ }
+
+ // Do nothing - but use a a breakpoint point.
+ private void debugPoint() {}
+
+ private void history() {
+ info("History") ;
+ for ( Pair<Action, Long> p : actions ) {
+ if ( p.getRight() != NoId )
+ log.info(String.format("%s: %-12s %d", label, p.getLeft(), p.getRight())) ;
+ else
+ log.info(String.format("%s: %-12s", label, p.getLeft())) ;
+
+ }
+ }
+
+ @Override
+ public String toString() {
+ return "BlockMgrTracker" + ((label == null) ? "" : (": " + label)) ;
+ }
+
+ @Override
+ public String getLabel() {
+ return label ;
+ }
+}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrWrapper.java
----------------------------------------------------------------------
diff --git a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrWrapper.java b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrWrapper.java
new file mode 100644
index 0000000..6a6b12f
--- /dev/null
+++ b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockMgrWrapper.java
@@ -0,0 +1,149 @@
+/*
+ * 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.block ;
+
+
+public class BlockMgrWrapper implements BlockMgr {
+ protected BlockMgr blockMgr ;
+
+ public BlockMgrWrapper(BlockMgr blockMgr) {
+ setBlockMgr(blockMgr) ;
+ }
+
+ /** Set another BlockMgr as the target of the wrapper - return the old one */
+ protected final BlockMgr setBlockMgr(BlockMgr blockMgr) {
+ BlockMgr old = this.blockMgr ;
+ this.blockMgr = blockMgr ;
+ return old ;
+ }
+
+ public BlockMgr getWrapped() {
+ return blockMgr ;
+ }
+
+ @Override
+ public Block allocate(int blockSize) {
+ return blockMgr.allocate(blockSize) ;
+ }
+
+ @Override
+ public Block getRead(long id) {
+ return blockMgr.getRead(id) ;
+ }
+
+ @Override
+ public Block getWrite(long id) {
+ return blockMgr.getWrite(id) ;
+ }
+
+ @Override
+ public Block promote(Block block) {
+ return blockMgr.promote(block) ;
+ }
+
+ @Override
+ public void release(Block block) {
+ blockMgr.release(block) ;
+ }
+
+ @Override
+ public void write(Block block) {
+ blockMgr.write(block) ;
+ }
+
+ @Override
+ public void overwrite(Block block) {
+ blockMgr.overwrite(block) ;
+ }
+
+ @Override
+ public void free(Block block) {
+ blockMgr.free(block) ;
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return blockMgr.isEmpty() ;
+ }
+
+ @Override
+ public long allocLimit() {
+ return blockMgr.allocLimit() ;
+ }
+
+ @Override
+ public void resetAlloc(long boundary) {
+ blockMgr.resetAlloc(boundary) ;
+ }
+
+ @Override
+ public void sync() {
+ blockMgr.sync() ;
+ }
+
+ @Override
+ public void syncForce() {
+ blockMgr.syncForce() ;
+ }
+
+ @Override
+ public boolean valid(int id) {
+ return blockMgr.valid(id) ;
+ }
+
+ @Override
+ public boolean isClosed() {
+ return blockMgr.isClosed() ;
+ }
+
+ @Override
+ public void close() {
+ blockMgr.close() ;
+ }
+
+ @Override
+ public void beginRead() {
+ blockMgr.beginRead() ;
+ }
+
+ @Override
+ public void endRead() {
+ blockMgr.endRead() ;
+ }
+
+ @Override
+ public void beginUpdate() {
+ blockMgr.beginUpdate() ;
+ }
+
+ @Override
+ public void endUpdate() {
+ blockMgr.endUpdate() ;
+ }
+
+ @Override
+ public String getLabel() {
+ return blockMgr.getLabel() ;
+ }
+
+ @Override
+ public String toString() {
+ return blockMgr.toString() ;
+ }
+}
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockParams.java
----------------------------------------------------------------------
diff --git a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockParams.java b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockParams.java
new file mode 100644
index 0000000..b1c6882
--- /dev/null
+++ b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockParams.java
@@ -0,0 +1,27 @@
+/*
+ * 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.block;
+
+public interface BlockParams {
+ public FileMode getFileMode() ;
+ public Integer getBlockSize() ;
+ public Integer getBlockReadCacheSize() ;
+ public Integer getBlockWriteCacheSize() ;
+}
+
http://git-wip-us.apache.org/repos/asf/jena/blob/1dabea3a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockType.java
----------------------------------------------------------------------
diff --git a/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockType.java b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockType.java
new file mode 100644
index 0000000..5c7e9e0
--- /dev/null
+++ b/jena-db/jena-dboe-base/src/main/java/org/seaborne/dboe/base/block/BlockType.java
@@ -0,0 +1,69 @@
+/*
+ * 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.block;
+
+import org.apache.jena.atlas.io.IndentedWriter ;
+import org.apache.jena.atlas.io.Printable ;
+import org.seaborne.dboe.DBOpEnvException ;
+
+import org.apache.jena.sparql.util.Named ;
+
+public enum BlockType implements Printable, Named
+{
+ // The id should fit into an unsigned byte.
+ FREE(-1, "Free"),
+ BTREE_NODE(5, "BTreeNode") ,
+ BPTREE_BRANCH(6, "BPlusTreeBranch") ,
+ BPTREE_LEAF(7, "BPlusTreeLeaf") ,
+ DICTIONARY(10, "Dictionary") ,
+ RECORD_BLOCK(99, "RecordBlock"),
+ UNDEF(255, "UndefinedBlockType")
+ ;
+
+ private final int id ;
+ private final String name ;
+
+ BlockType(int id, String name)
+ {
+ this.id = id ;
+ this.name = name ;
+
+ }
+
+ @Override
+ public void output(IndentedWriter out)
+ { out.print(getName()) ; }
+
+ final public int id() { return id ; }
+
+ @Override
+ final public String getName() { return name ; }
+
+ @Override public String toString() { return getName() ; }
+
+ public static BlockType extract(int x)
+ {
+ if ( x == BTREE_NODE.id() ) return BTREE_NODE ;
+ if ( x == BPTREE_BRANCH.id() ) return BPTREE_BRANCH ;
+ if ( x == BPTREE_LEAF.id() ) return BPTREE_LEAF ;
+ if ( x == RECORD_BLOCK.id() ) return RECORD_BLOCK ;
+ if ( x == DICTIONARY.id() ) return DICTIONARY ;
+ throw new DBOpEnvException("No known block type for "+x) ;
+ }
+}