Commit in lcio/src/java/hep/lcio/implementation/sio on random_access_io_branch
SIOLCWriter.java+2-21.14.10.1 -> 1.14.10.2
SIOLCReader.java+9-91.16.10.4 -> 1.16.10.5
SIOLCRandomAccessReader.java+96-251.1.2.2 -> 1.1.2.3
+107-36
3 modified files
Commit latest random access code

lcio/src/java/hep/lcio/implementation/sio
SIOLCWriter.java 1.14.10.1 -> 1.14.10.2
diff -u -r1.14.10.1 -r1.14.10.2
--- SIOLCWriter.java	20 Oct 2009 23:18:20 -0000	1.14.10.1
+++ SIOLCWriter.java	15 Dec 2009 02:17:46 -0000	1.14.10.2
@@ -11,7 +11,7 @@
 /**
  *
  * @author Tony Johnson
- * @version $Id: SIOLCWriter.java,v 1.14.10.1 2009/10/20 23:18:20 tonyj Exp $
+ * @version $Id: SIOLCWriter.java,v 1.14.10.2 2009/12/15 02:17:46 tonyj Exp $
  */
 class SIOLCWriter implements LCWriter
 {
@@ -44,7 +44,7 @@
          fileRandomAccessBlock = new RandomAccessBlock();
          fileRandomAccessBlock.write(writer);
          //FIXME: Set more sensible limit
-         indexBlock = new IndexBlock(50);
+         indexBlock = new IndexBlock(5000);
       }
    }
 

lcio/src/java/hep/lcio/implementation/sio
SIOLCReader.java 1.16.10.4 -> 1.16.10.5
diff -u -r1.16.10.4 -r1.16.10.5
--- SIOLCReader.java	19 Nov 2009 01:15:54 -0000	1.16.10.4
+++ SIOLCReader.java	15 Dec 2009 02:17:46 -0000	1.16.10.5
@@ -23,7 +23,7 @@
 /**
  *
  * @author Tony Johnson
- * @version $Id: SIOLCReader.java,v 1.16.10.4 2009/11/19 01:15:54 tonyj Exp $
+ * @version $Id: SIOLCReader.java,v 1.16.10.5 2009/12/15 02:17:46 tonyj Exp $
  */
 class SIOLCReader implements LCReader
 {
@@ -31,7 +31,7 @@
    private List runListeners = new ArrayList();
    protected SIOReader reader;
    private List<String> filenames;
-   private int currentFile ;
+   private int currentFile;
 
    public void close() throws IOException
    {
@@ -52,12 +52,12 @@
      *
      * @throws IOException
      */
-    public void open(String[] files) throws IOException{
-	filenames = Arrays.asList(files);
-	currentFile=0;
-	reader = new SIOReader(filenames.get(currentFile));
-    }
-
+   public void open(String[] files) throws IOException
+   {
+      filenames = Arrays.asList(files);
+      currentFile=0;
+      reader = new SIOReader(filenames.get(currentFile));
+   }
 
    public LCEvent readEvent(int runNumber, int evtNumber) throws IOException
    {
@@ -67,7 +67,7 @@
          {            
             SIORecord record = reader.readRecord();
             String name = record.getRecordName();
-            if (SIOFactory.eventHeaderRecordName.equals(name)) continue;
+            if (!SIOFactory.eventHeaderRecordName.equals(name)) continue;
 
             SIOEvent event = new SIOEvent(record,LCIO.READ_ONLY);
             if (event.getRunNumber() == runNumber && event.getEventNumber() == evtNumber)

lcio/src/java/hep/lcio/implementation/sio
SIOLCRandomAccessReader.java 1.1.2.2 -> 1.1.2.3
diff -u -r1.1.2.2 -r1.1.2.3
--- SIOLCRandomAccessReader.java	3 Nov 2009 22:26:01 -0000	1.1.2.2
+++ SIOLCRandomAccessReader.java	15 Dec 2009 02:17:46 -0000	1.1.2.3
@@ -28,12 +28,35 @@
         // Peek at the first record to see if this file supports random access
         SIORecord record = reader.readRecord();
         if ("LCIORandomAccess".equals(record.getRecordName())) {
-            randomAccess = new RandomAccessSupport(reader,record);
+            randomAccess = new FileRandomAccessSupport(reader, record);
         } else {
             reader.seek(0);
         }
     }
 
+    @Override
+    public void open(String[] filenames) throws IOException {
+        super.open(filenames);
+        List<FileRandomAccessSupport> fileBlocks = new ArrayList<FileRandomAccessSupport>(filenames.length);
+        for (String filename : filenames) {
+            SIOReader reader = new SIOReader(filename);
+            try {
+                SIORecord record = reader.readRecord();
+                if (!"LCIORandomAccess".equals(record.getRecordName())) {
+                    fileBlocks = null;
+                    break;
+                } else {
+                    fileBlocks.add(new FileRandomAccessSupport(reader,record));
+                }
+            } finally {
+                //reader.close();
+            }
+        }
+        if (fileBlocks != null) {
+            randomAccess = new ChainRandomAccessSupport(fileBlocks);
+        }
+    }
+
     public LCRunHeader readNextRunHeader(int accessMode) throws IOException {
         if (randomAccess != null) {
             long position = randomAccess.findNextRunHeader();
@@ -60,9 +83,11 @@
     public LCEvent readEvent(int runNumber, int evtNumber) throws IOException {
         if (randomAccess != null) {
             long position = randomAccess.findEvent(runNumber, evtNumber);
-            if (position < 0) throw new IOException(String.format("Run: %d Event: %d not found",runNumber,evtNumber));
+            if (position < 0) {
+                throw new IOException(String.format("Run: %d Event: %d not found", runNumber, evtNumber));
+            }
             SIORecord record = reader.readRecord(position);
-            SIOEvent event = new SIOEvent(record,LCIO.READ_ONLY);
+            SIOEvent event = new SIOEvent(record, LCIO.READ_ONLY);
             event.readData(reader.readRecord());
             return event;
         } else {
@@ -79,7 +104,46 @@
         }
     }
 
-    private static class RandomAccessSupport {
+    private interface RandomAccessSupport {
+        long findNextRunHeader() throws IOException;
+        void skipNEvents(int n) throws IOException;
+        long findEvent(int run, int event) throws IOException;
+    }
+
+    private class ChainRandomAccessSupport implements RandomAccessSupport {
+        List<RandomAccessBlock> fileRandomAccessBlocks;
+        List<FileRandomAccessSupport> fileBlocks;
+        ChainRandomAccessSupport(List<FileRandomAccessSupport> fileBlocks) {
+            this.fileBlocks = fileBlocks;
+            fileRandomAccessBlocks = new ArrayList<RandomAccessBlock>(fileBlocks.size());
+            for (FileRandomAccessSupport fra : fileBlocks) {
+                fileRandomAccessBlocks.add(fra.fileRandomAccessBlock);
+            }
+        }
+
+        public long findNextRunHeader() throws IOException {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        public void skipNEvents(int n) throws IOException {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        public long findEvent(int run, int event) throws IOException {
+            RunEvent re = new RunEvent(run, event);
+
+//            RandomAccessBlock fab = findFileRandomAccessBlock();
+//            if (!fab.contains(re)) {
+//                return -1;
+//            }
+
+            int location = Collections.binarySearch(fileRandomAccessBlocks, re);
+            reader = fileBlocks.get(location).reader;
+            return fileBlocks.get(location).findEvent(run, event);
+        }
+    }
+
+    private static class FileRandomAccessSupport implements RandomAccessSupport {
 
         private RandomAccessBlock fileRandomAccessBlock;
         private List<RandomAccessBlock> indexRandomAccessBlocks = new ArrayList<RandomAccessBlock>();
@@ -88,12 +152,12 @@
         //FIXME: This should be a softHashMap
         private Map<RandomAccessBlock, IndexBlock> indexHash = new HashMap<RandomAccessBlock, IndexBlock>();
 
-        RandomAccessSupport(SIOReader reader, SIORecord record) throws IOException {
+        FileRandomAccessSupport(SIOReader reader, SIORecord record) throws IOException {
             this.reader = reader;
             fileRandomAccessBlock = new RandomAccessBlock(record);
         }
 
-        private long findNextRunHeader() throws IOException {
+        public long findNextRunHeader() throws IOException {
             List<RandomAccessBlock> iab = findIndexRandomAccessBlocks();
             int iabIndex = findIndexOfRandomAccessBlockContaining(reader.getNextRecordPosition());
             for (RandomAccessBlock rab : iab.subList(iabIndex, iab.size())) {
@@ -108,29 +172,36 @@
             return -1;
         }
 
-        private void skipNEvents(int n) throws IOException {
-            if (n==0) return;
+        public void skipNEvents(int n) throws IOException {
+            if (n == 0) {
+                return;
+            }
             int iabIndex = findIndexOfRandomAccessBlockContaining(reader.getNextRecordPosition());
             List<RandomAccessBlock> iab = findIndexRandomAccessBlocks();
             IndexBlock ib = findIndexBlock(iab.get(iabIndex));
             // Find how many events are left in current block.
             int recordIndex = ib.findIndexOfRecordLocation(reader.getNextRecordPosition());
-            for (int i = recordIndex; i<ib.getRecordCount(); i++) {
-                if (ib.isEvent(i)) if (n-- == 0) {
-                    reader.seek(ib.getLocation(i));
-                    return;
+            for (int i = recordIndex; i < ib.getRecordCount(); i++) {
+                if (ib.isEvent(i)) {
+                    if (n-- == 0) {
+                        reader.seek(ib.getLocation(i));
+                        return;
+                    }
                 }
             }
 
-            for (RandomAccessBlock rab : iab.subList(iabIndex+1, iab.size())) {
+            for (RandomAccessBlock rab : iab.subList(iabIndex + 1, iab.size())) {
                 ib = findIndexBlock(rab);
                 int nEvents = ib.getEventCount();
-                if (nEvents <= n) n -= nEvents;
-                else {
-                    for (int i=0; i<ib.getRecordCount(); i++) {
-                        if (ib.isEvent(i)) if (n-- == 0) {
-                            reader.seek(ib.getLocation(i));
-                            return;
+                if (nEvents <= n) {
+                    n -= nEvents;
+                } else {
+                    for (int i = 0; i < ib.getRecordCount(); i++) {
+                        if (ib.isEvent(i)) {
+                            if (n-- == 0) {
+                                reader.seek(ib.getLocation(i));
+                                return;
+                            }
                         }
                     }
                 }
@@ -138,8 +209,7 @@
             throw new EOFException();
         }
 
-
-        private long findEvent(int run, int event) throws IOException {
+        public long findEvent(int run, int event) throws IOException {
             RunEvent re = new RunEvent(run, event);
 
             RandomAccessBlock fab = findFileRandomAccessBlock();
@@ -191,8 +261,10 @@
         }
 
         private int findIndexOfRandomAccessBlockContaining(long recordLocation) throws IOException {
-            int position = Collections.binarySearch(new RecordLocationList(),recordLocation);
-            if (position<0) position = Math.max(0, -position - 2);
+            int position = Collections.binarySearch(new RecordLocationList(), recordLocation);
+            if (position < 0) {
+                position = Math.max(0, -position - 2);
+            }
             return position;
         }
 
@@ -201,7 +273,7 @@
             List<RandomAccessBlock> iab;
 
             RecordLocationList() throws IOException {
-               iab = findIndexRandomAccessBlocks();
+                iab = findIndexRandomAccessBlocks();
             }
 
             @Override
@@ -213,7 +285,6 @@
             public int size() {
                 return iab.size();
             }
-
         }
     }
 }
CVSspam 0.2.8