[Mulgara-svn] r871 - in projects/xa2/object-pool/src: . scheduler
andrae at mulgara.org
andrae at mulgara.org
Wed Apr 30 08:59:08 UTC 2008
Author: andrae
Date: 2008-04-30 01:59:06 -0700 (Wed, 30 Apr 2008)
New Revision: 871
Added:
projects/xa2/object-pool/src/scheduler/
projects/xa2/object-pool/src/scheduler/Block.java
projects/xa2/object-pool/src/scheduler/FileCache.java
projects/xa2/object-pool/src/scheduler/FileHandle.java
projects/xa2/object-pool/src/scheduler/FileHandleFactory.java
projects/xa2/object-pool/src/scheduler/IOScheduler.java
Log:
Incomplete initial cut at an io-scheduler sufficient to start work on ISAM-Tree.
Added: projects/xa2/object-pool/src/scheduler/Block.java
===================================================================
--- projects/xa2/object-pool/src/scheduler/Block.java (rev 0)
+++ projects/xa2/object-pool/src/scheduler/Block.java 2008-04-30 08:59:06 UTC (rev 871)
@@ -0,0 +1,28 @@
+/*
+ * Copyright Topaz Foundation 2008
+ * Author Andrae Muys
+ * Date 30th April 2008
+ */
+package scheduler;
+
+import java.lang.ref.SoftReference;
+import java.util.HashMap;
+import java.util.Map;
+
+public class Block {
+ private FileHandle handle;
+ private Long position;
+
+ public Block(FileHandle handle, Long position) {
+ this.handle = handle;
+ this.position = position;
+ }
+
+ public FileHandle getHandle() {
+ return handle;
+ }
+
+ public Long getPosition() {
+ return position;
+ }
+}
Added: projects/xa2/object-pool/src/scheduler/FileCache.java
===================================================================
--- projects/xa2/object-pool/src/scheduler/FileCache.java (rev 0)
+++ projects/xa2/object-pool/src/scheduler/FileCache.java 2008-04-30 08:59:06 UTC (rev 871)
@@ -0,0 +1,51 @@
+/*
+ * Copyright Topaz Foundation 2008
+ * Author Andrae Muys
+ * Date 30th April 2008
+ */
+package scheduler;
+
+import java.io.File;
+import java.ref.SoftReference;
+import java.util.WeakHashMap;
+
+public class FileCache {
+ private FileHandle handle;
+ // The Block object is required to maintain a strong reference to the Long key obj.
+ private WeakHashMap<Long, SoftReference<Block>> blockCache;
+
+ private static final int LOG2_BLOCK_SIZE = 21;
+
+ FileCache(FileHandle handle) {
+ this.handle = handle;
+ this.blockCache = new WeakHashMap
+ }
+
+ public File getFile() {
+ return file;
+ }
+
+ public Block getBlock(long position) {
+ Long pos = new Long(position >> handle.log2BlockSize());
+ SoftReference<Block> blockRef = blockCache.get(pos);
+ if (blockRef != null) {
+ Block block = blockRef.get();
+ if (block != null) {
+
+ return block;
+ } else {
+ // Note that the combination of soft and weak references means that we need this double check.
+ // Because soft-references prevent weak-references from being reaped it is legal for the gc to reap
+ // the block and the key in subsequent passes.
+ // Here we remove the stale entry so it can be reentered.
+ blockCache.remove(pos);
+ }
+ }
+ Block newBlock = handle.getBlock(pos);
+ // Note we use the newBlock's position, not pos - this ensures the map's entry will be retained until
+ // memory pressure forces the SoftReference to be reaped.
+ blockCache.put(newBlock.getPosition(), new SoftReference(newBlock));
+
+ return newBlock;
+ }
+}
Added: projects/xa2/object-pool/src/scheduler/FileHandle.java
===================================================================
--- projects/xa2/object-pool/src/scheduler/FileHandle.java (rev 0)
+++ projects/xa2/object-pool/src/scheduler/FileHandle.java 2008-04-30 08:59:06 UTC (rev 871)
@@ -0,0 +1,40 @@
+/*
+ * Copyright Topaz Foundation 2008
+ * Author Andrae Muys
+ * Date 30th April 2008
+ */
+package scheduler;
+
+import java.io.File;
+
+public class FileHandle {
+ private File file;
+ private FileHandleCMap fhmap;
+ private FileChannel channel;
+
+ private long seeks;
+
+ FileHandle(File file, FileHandleCMap fhmap) {
+ this.file = file;
+ this.fhmap = fhmap;
+ this.seeks = 0;
+
+ this.channel = file.exists() ?
+ new FileInputStream(file).getChannel() :
+ new FileOutputStream(file).getChannel();
+ }
+
+ public File getFile() {
+ return file;
+ }
+
+ Block getBlock(Long blockId) {
+ long position = blockId.longValue();
+ if (channel.position() != position) {
+ seeks++;
+ channel.position(position);
+ }
+
+
+ }
+}
Added: projects/xa2/object-pool/src/scheduler/FileHandleFactory.java
===================================================================
--- projects/xa2/object-pool/src/scheduler/FileHandleFactory.java (rev 0)
+++ projects/xa2/object-pool/src/scheduler/FileHandleFactory.java 2008-04-30 08:59:06 UTC (rev 871)
@@ -0,0 +1,30 @@
+/*
+ * Copyright Topaz Foundation 2008
+ * Author Andrae Muys
+ * Date 30th April 2008
+ */
+package scheduler;
+
+import java.io.File;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * A canonical mapping of File objects to FileHandles.
+ */
+public class FileHandleCMap {
+ private Map<File, FileHandle> handles;
+
+ public FileHandleCMap() {
+ this.handles = new HashMap<File, FileHandle>();
+ }
+
+ public FileHandle getHandle(File file) {
+ FileHandle result = handles.get(file);
+ if (result == null) {
+ result = new FileHandle(file, this);
+ handles.put(result);
+ }
+ return result;
+ }
+}
Added: projects/xa2/object-pool/src/scheduler/IOScheduler.java
===================================================================
--- projects/xa2/object-pool/src/scheduler/IOScheduler.java (rev 0)
+++ projects/xa2/object-pool/src/scheduler/IOScheduler.java 2008-04-30 08:59:06 UTC (rev 871)
@@ -0,0 +1,18 @@
+/*
+ * Copyright Topaz Foundation 2008
+ * Author Andrae Muys
+ * Date 30th April 2008
+ */
+package scheduler;
+
+import java.lang.ref.SoftReference;
+import java.util.HashMap;
+import java.util.Map;
+
+public class IOScheduler {
+ private Map<FileHandle, FileCache> fileCache;
+
+ public IOScheduler() {
+ fileCache = new HashMap<FileHandle, FileCache>();
+ }
+}
More information about the Mulgara-svn
mailing list