1 added + 6 modified, total 7 files
lcio/src/java/hep/lcio/implementation/sio
diff -N SIOLCRandomAccessReader.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ SIOLCRandomAccessReader.java 31 Oct 2009 00:26:19 -0000 1.1.2.1
@@ -0,0 +1,212 @@
+package hep.lcio.implementation.sio;
+
+import hep.io.sio.SIOBlock;
+import hep.io.sio.SIOReader;
+import hep.io.sio.SIORecord;
+import hep.lcio.event.LCEvent;
+import hep.lcio.event.LCIO;
+import hep.lcio.event.LCRunHeader;
+import java.io.IOException;
+import java.util.AbstractList;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ *
+ * @author tonyj
+ */
+class SIOLCRandomAccessReader extends SIOLCReader {
+
+ private RandomAccessSupport randomAccess;
+
+ public void open(String filename) throws IOException {
+ super.open(filename);
+ // 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);
+ } else {
+ reader.seek(0);
+ }
+ }
+
+ public LCRunHeader readNextRunHeader(int accessMode) throws IOException {
+ if (randomAccess != null) {
+ long position = randomAccess.findNextRunHeader();
+ if (position < 0) {
+ return null;
+ } else {
+ SIORecord record = reader.readRecord(position);
+ SIOBlock block = record.getBlock();
+ int major = block.getMajorVersion();
+ int minor = block.getMinorVersion();
+ if ((major < 1) && (minor < 8)) {
+ throw new IOException("Sorry: files created with versions older than v00-08" + " are no longer supported !");
+ }
+
+ // FIX ME: need to set access mode here....
+ return new SIORunHeader(block.getData(), major, minor);
+ }
+ } else {
+ return super.readNextRunHeader(accessMode);
+ }
+ }
+
+ //FIXME: What about accessMode?
+ 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));
+ SIORecord record = reader.readRecord(position);
+ SIOEvent event = new SIOEvent(record,LCIO.READ_ONLY);
+ event.readData(reader.readRecord());
+ return event;
+ } else {
+ return super.readEvent(runNumber, evtNumber);
+ }
+ }
+
+ @Override
+ public void skipNEvents(int n) throws IOException {
+ if (randomAccess != null) {
+ randomAccess.skipNEvents(n);
+ } else {
+ super.skipNEvents(n);
+ }
+ }
+
+ private static class RandomAccessSupport {
+
+ private RandomAccessBlock fileRandomAccessBlock;
+ private List<RandomAccessBlock> indexRandomAccessBlocks = new ArrayList<RandomAccessBlock>();
+ private boolean indexBlocksRead;
+ private SIOReader reader;
+ //FIXME: This should be a softHashMap
+ private Map<RandomAccessBlock, IndexBlock> indexHash = new HashMap<RandomAccessBlock, IndexBlock>();
+
+ RandomAccessSupport(SIOReader reader, SIORecord record) throws IOException {
+ this.reader = reader;
+ fileRandomAccessBlock = new RandomAccessBlock(record);
+ }
+
+// private void addRandomAccessRecord(SIORecord record) throws IOException {
+// RandomAccessBlock ra = new RandomAccessBlock(record);
+// System.out.println("Found ra=" + ra);
+// if (ra.getIndexLocation() == 0) {
+// fileRandomAccessBlock = ra;
+// } else {
+// indexRandomAccessBlocks.add(ra);
+// }
+// }
+
+ private long findNextRunHeader() throws IOException {
+ List<RandomAccessBlock> iab = findIndexRandomAccessBlocks();
+ int iabIndex = findIndexOfRandomAccessBlockContaining(reader.getNextRecordPosition());
+ for (RandomAccessBlock rab : iab.subList(iabIndex, iab.size())) {
+ if (rab.getRunHeaderCount() > 0) {
+ IndexBlock ib = findIndexBlock(rab);
+ long position = ib.findRecordHeader(reader.getNextRecordPosition());
+ if (position >= 0) {
+ return position;
+ }
+ }
+ }
+ return -1;
+ }
+
+ private void skipNEvents(int n) throws IOException {
+// int iabIndex = findIndexOfRandomAccessBlockContaining(lastRecordPosition);
+// List<RandomAccessBlock> iab = findIndexRandomAccessBlocks();
+// IndexBlock ib = findIndexBlock(iab.get(iabIndex));
+// int recordIndex = ib.findIndexOfRecord(lastRecordPosition);
+// for (int i = recordIndex; i<ib.getRecordCount(); i++) {
+// }
+//
+// for (RandomAccessBlock rab : iab.subList(iabIndex, iab.size())) {
+// IndexBlock ib = rab.get(iabIndex);
+//
+// }
+ }
+
+
+ private long findEvent(int run, int event) throws IOException {
+ RunEvent re = new RunEvent(run, event);
+
+ RandomAccessBlock fab = findFileRandomAccessBlock();
+ if (!fab.contains(re)) {
+ return -1;
+ }
+
+ List<RandomAccessBlock> iab = findIndexRandomAccessBlocks();
+ int location = Collections.binarySearch(iab, re);
+ IndexBlock ib = findIndexBlock(iab.get(location));
+ return ib.getLocation(re);
+ }
+
+ private RandomAccessBlock findFileRandomAccessBlock() throws IOException {
+ return fileRandomAccessBlock;
+ }
+
+ private List<RandomAccessBlock> findIndexRandomAccessBlocks() throws IOException {
+ if (!indexBlocksRead) {
+ long originalPosition = reader.getNextRecordPosition();
+ RandomAccessBlock fab = findFileRandomAccessBlock();
+ if (indexRandomAccessBlocks.isEmpty()) {
+ SIORecord record = reader.readRecord(fab.getPreviousLocation());
+ RandomAccessBlock rab = new RandomAccessBlock(record);
+ indexRandomAccessBlocks.add(rab);
+ indexBlocksRead = rab.getNextLocation() == 0;
+ }
+ while (!indexBlocksRead) {
+ long nextLocation = indexRandomAccessBlocks.get(indexRandomAccessBlocks.size() - 1).getNextLocation();
+ SIORecord record = reader.readRecord(nextLocation);
+ RandomAccessBlock rab = new RandomAccessBlock(record);
+ indexRandomAccessBlocks.add(rab);
+ indexBlocksRead = rab.getNextLocation() == 0;
+ }
+ reader.seek(originalPosition);
+ }
+ return indexRandomAccessBlocks;
+ }
+
+ private IndexBlock findIndexBlock(RandomAccessBlock rab) throws IOException {
+ IndexBlock result = indexHash.get(rab);
+ if (result == null) {
+ long originalPosition = reader.getNextRecordPosition();
+ result = new IndexBlock(reader.readRecord(rab.getIndexLocation()));
+ indexHash.put(rab, result);
+ reader.seek(originalPosition);
+ }
+ return result;
+ }
+
+ private int findIndexOfRandomAccessBlockContaining(long recordLocation) throws IOException {
+ int position = Collections.binarySearch(new RecordLocationList(),recordLocation);
+ if (position<0) position = Math.max(0, -position - 2);
+ return position;
+ }
+
+ private class RecordLocationList extends AbstractList<Long> {
+
+ List<RandomAccessBlock> iab;
+
+ RecordLocationList() throws IOException {
+ iab = findIndexRandomAccessBlocks();
+ }
+
+ @Override
+ public Long get(int index) {
+ return iab.get(index).getFirstRecordLocation();
+ }
+
+ @Override
+ public int size() {
+ return iab.size();
+ }
+
+ }
+ }
+}
lcio/src/java/hep/lcio/implementation/sio
diff -u -r1.1.2.2 -r1.1.2.3
--- RandomAccessBlock.java 27 Oct 2009 23:39:06 -0000 1.1.2.2
+++ RandomAccessBlock.java 31 Oct 2009 00:26:19 -0000 1.1.2.3
@@ -26,6 +26,7 @@
private long previousAccessBlockLocation;
private long nextAccessBlockLocation;
private long myLocation;
+ private long firstRecordLocation;
private SIOWriter writer;
RandomAccessBlock() {
@@ -58,6 +59,7 @@
sio.writeLong(indexLocation);
sio.writeLong(previousAccessBlockLocation);
sio.writeLong(nextAccessBlockLocation);
+ sio.writeLong(firstRecordLocation);
sio.close();
}
@@ -78,11 +80,12 @@
indexLocation = sio.readLong();
previousAccessBlockLocation = sio.readLong();
nextAccessBlockLocation = sio.readLong();
+ firstRecordLocation = sio.readLong();
sio.close();
}
int getRecordCount() {
- return nRunHeaders+nEvents;
+ return nRunHeaders + nEvents;
}
int getRunHeaderCount() {
@@ -104,6 +107,7 @@
nEvents = indexBlock.getEventCount();
nRunHeaders = indexBlock.getRunHeaderCount();
indexLocation = indexBlock.getLocation();
+ firstRecordLocation = indexBlock.getFirstRecordLocation();
}
/**
@@ -151,12 +155,11 @@
return compareTo(re) == 0;
}
- public int compareTo(RunEvent o) {
- RunEvent re = (RunEvent) o;
- if (re.compareTo(minRunEvent) > 0) {
- return -1;
- } else if (re.compareTo(maxRunEvent) < 0) {
+ public int compareTo(RunEvent re) {
+ if (minRunEvent.compareTo(re) > 0) {
return 1;
+ } else if (maxRunEvent.compareTo(re) < 0) {
+ return -1;
} else {
return 0;
}
@@ -169,4 +172,8 @@
long getNextLocation() {
return nextAccessBlockLocation;
}
+
+ long getFirstRecordLocation() {
+ return firstRecordLocation;
+ }
}
lcio/src/java/hep/lcio/implementation/sio
diff -u -r1.1.2.2 -r1.1.2.3
--- IndexBlock.java 27 Oct 2009 23:39:06 -0000 1.1.2.2
+++ IndexBlock.java 31 Oct 2009 00:26:19 -0000 1.1.2.3
@@ -8,6 +8,7 @@
import hep.lcio.event.LCEvent;
import hep.lcio.event.LCRunHeader;
import java.io.IOException;
+import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -86,31 +87,41 @@
}
void write(SIOWriter writer) throws IOException {
- myLocation = writer.createRecord(LCIOINDEX,true);
- SIOOutputStream sio = writer.createBlock(LCIOINDEX,majorVersion,minorVersion);
+ myLocation = writer.createRecord(LCIOINDEX, true);
+ SIOOutputStream sio = writer.createBlock(LCIOINDEX, majorVersion, minorVersion);
boolean oneRun = minEntry.getRun() == maxEntry.getRun();
long firstLocation = index.get(0).recordLocation;
boolean longOffset = lastEntry.recordLocation - firstLocation > Integer.MAX_VALUE;
int controlWord = 0;
- if (oneRun) controlWord |= 1;
- if (longOffset) controlWord |= 2;
+ if (oneRun) {
+ controlWord |= 1;
+ }
+ if (longOffset) {
+ controlWord |= 2;
+ }
sio.writeInt(controlWord);
sio.writeInt(minEntry.getRun());
sio.writeLong(firstLocation);
sio.writeInt(index.size());
for (IndexEntry entry : index) {
- if (!oneRun) sio.writeInt(entry.getRun()-minEntry.getRun());
+ if (!oneRun) {
+ sio.writeInt(entry.getRun() - minEntry.getRun());
+ }
sio.writeInt(entry.getEvent());
- if (longOffset) sio.writeLong(entry.recordLocation-firstLocation);
- else sio.writeInt((int) (entry.recordLocation-firstLocation));
+ if (longOffset) {
+ sio.writeLong(entry.recordLocation - firstLocation);
+ } else {
+ sio.writeInt((int) (entry.recordLocation - firstLocation));
+ }
}
sio.close();
}
private void read(SIORecord record) throws IOException {
SIOBlock block = record.getBlock();
- if (!block.getBlockName().equals(LCIOINDEX) || block.getMajorVersion()!=1 || block.getMinorVersion()!=0)
+ if (!block.getBlockName().equals(LCIOINDEX) || block.getMajorVersion() != 1 || block.getMinorVersion() != 0) {
throw new IOException("Unexpected block in LCIOIndex record");
+ }
SIOInputStream sio = block.getData();
int controlWord = sio.readInt();
boolean oneRun = (controlWord & 1) == 1;
@@ -120,11 +131,11 @@
int size = sio.readInt();
index = new ArrayList<IndexEntry>(size);
maxEntries = size;
- for (int i=0; i<size; i++) {
+ for (int i = 0; i < size; i++) {
int run = oneRun ? minRun : minRun + sio.readInt();
int event = sio.readInt();
long location = firstLocation + (longOffset ? sio.readLong() : sio.readInt());
- index.add(new IndexEntry(run,event,location));
+ index.add(new IndexEntry(run, event, location));
}
sio.close();
}
@@ -155,34 +166,62 @@
long getLocation(RunEvent re) {
int position = Collections.binarySearch(index, re);
- if (position < 0) return -1;
- else return index.get(position).recordLocation;
+ if (position < 0) {
+ return -1;
+ } else {
+ return index.get(position).recordLocation;
+ }
}
-
+
long findRecordHeader(long startPosition) {
- // FIXME: Do something more efficient
- for (IndexEntry entry : index) {
- if (entry.recordLocation>startPosition && entry.getEvent() == -1) return entry.recordLocation;
+ int startIndex = findIndexOfRecordLocation(startPosition);
+ for (IndexEntry entry : index.subList(startIndex, index.size())) {
+ if (entry.recordLocation > startPosition && entry.getEvent() == -1) {
+ return entry.recordLocation;
+ }
}
return -1;
}
+ long getFirstRecordLocation() {
+ return index.isEmpty() ? 0 : index.get(0).recordLocation;
+ }
+
+ private int findIndexOfRecordLocation(long recordLocation) {
+ int position = Collections.binarySearch(new RecordLocationList(), recordLocation);
+ if (position < 0) {
+ position = Math.max(0, -position - 2);
+ }
+ return position;
+ }
+
+ private class RecordLocationList extends AbstractList<Long> {
+
+ public Long get(int i) {
+ return index.get(i).recordLocation;
+ }
+
+ public int size() {
+ return index.size();
+ }
+ }
+
private static class IndexEntry extends RunEvent {
private long recordLocation;
private IndexEntry(long recordLocation, LCRunHeader runHeader) {
- super(runHeader.getRunNumber(),-1);
+ super(runHeader.getRunNumber(), -1);
this.recordLocation = recordLocation;
}
private IndexEntry(long recordLocation, LCEvent eventHeader) {
- super(eventHeader.getRunNumber(),eventHeader.getEventNumber());
+ super(eventHeader.getRunNumber(), eventHeader.getEventNumber());
this.recordLocation = recordLocation;
}
private IndexEntry(int run, int event, long location) {
- super(run,event);
+ super(run, event);
this.recordLocation = location;
}
}
lcio/src/java/hep/lcio/implementation/sio
diff -u -r1.6 -r1.6.4.1
--- SIOFactory.java 10 Dec 2008 08:10:59 -0000 1.6
+++ SIOFactory.java 31 Oct 2009 00:26:19 -0000 1.6.4.1
@@ -9,7 +9,7 @@
/**
*
* @author Tony Johnson
- * @version $Id: SIOFactory.java,v 1.6 2008/12/10 08:10:59 gaede Exp $
+ * @version $Id: SIOFactory.java,v 1.6.4.1 2009/10/31 00:26:19 tonyj Exp $
*/
public class SIOFactory extends LCFactory
{
@@ -23,11 +23,11 @@
public LCReader createLCReader()
{
- return new SIOLCReader();
+ return new SIOLCRandomAccessReader();
}
public LCReader createLCReader(int lcReaderFlag)
- { //fg: dumy implementation - could use readerFlag in the future (now only C++)
- return new SIOLCReader();
+ {
+ return new SIOLCRandomAccessReader();
}
lcio/src/java/hep/lcio/implementation/sio
diff -u -r1.1.2.1 -r1.1.2.2
--- RunEvent.java 20 Oct 2009 23:18:21 -0000 1.1.2.1
+++ RunEvent.java 31 Oct 2009 00:26:19 -0000 1.1.2.2
@@ -30,4 +30,9 @@
return run;
}
+ @Override
+ public String toString() {
+ return String.format("Run: %d Event: %d",run,event);
+ }
+
}
lcio/src/java/hep/lcio/implementation/sio
diff -u -r1.16.10.2 -r1.16.10.3
--- SIOLCReader.java 27 Oct 2009 23:39:06 -0000 1.16.10.2
+++ SIOLCReader.java 31 Oct 2009 00:26:19 -0000 1.16.10.3
@@ -15,24 +15,19 @@
import java.io.EOFException;
import java.io.IOException;
import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
/**
*
* @author Tony Johnson
- * @version $Id: SIOLCReader.java,v 1.16.10.2 2009/10/27 23:39:06 tonyj Exp $
+ * @version $Id: SIOLCReader.java,v 1.16.10.3 2009/10/31 00:26:19 tonyj Exp $
*/
class SIOLCReader implements LCReader
{
private List eventListeners = new ArrayList();
private List runListeners = new ArrayList();
- private SIOReader reader;
- private RandomAccessSupport randomAccess;
- private long lastRecordPosition;
+ protected SIOReader reader;
private String[] _filenames ;
private int _currentIndex ;
@@ -45,7 +40,6 @@
public void open(String filename) throws IOException
{
reader = new SIOReader(filename);
- randomAccess = new RandomAccessSupport(reader);
}
/** Opens a list of files for reading (read-only). All subsequent
@@ -100,7 +94,6 @@
{
SIORecord record = reader.readRecord();
String name = record.getRecordName();
- if (name.equals("LCIORandomAccess")) randomAccess.addRandomAccessRecord(record);
if (!name.equals(SIOFactory.eventHeaderRecordName))
continue;
@@ -122,21 +115,14 @@
public void skipNEvents(int n) throws IOException
{
- if (randomAccess != null)
+ int nEvents = 0;
+ while( nEvents < n )
{
-
- }
- else
- {
- int nEvents = 0;
- while( nEvents < n )
- {
- SIORecord record = reader.readRecord();
- String name = record.getRecordName();
- if ( ! name.equals( SIOFactory.eventHeaderRecordName) )
- continue;
- ++nEvents ;
- }
+ SIORecord record = reader.readRecord();
+ String name = record.getRecordName();
+ if ( ! name.equals( SIOFactory.eventHeaderRecordName) )
+ continue;
+ ++nEvents ;
}
}
public LCEvent readNextEvent() throws IOException
@@ -151,14 +137,15 @@
public LCRunHeader readNextRunHeader(int accessMode) throws IOException
{
- if (randomAccess != null)
+ try
{
- long position = randomAccess.findNextRunHeader(lastRecordPosition);
- if (position < 0) return null;
- else
+ for (;;)
{
- SIORecord record = reader.readRecord(position);
- lastRecordPosition = position;
+ SIORecord record = reader.readRecord();
+ String name = record.getRecordName();
+ if (!name.equals(SIOFactory.runRecordName))
+ continue;
+
SIOBlock block = record.getBlock();
int major = block.getMajorVersion() ;
int minor = block.getMinorVersion() ;
@@ -169,36 +156,14 @@
return new SIORunHeader(block.getData(),major,minor);
}
}
- else
+ catch (EOFException x)
{
- try
- {
- for (;;)
- {
- SIORecord record = reader.readRecord();
- String name = record.getRecordName();
- if (!name.equals(SIOFactory.runRecordName))
- continue;
-
- SIOBlock block = record.getBlock();
- int major = block.getMajorVersion() ;
- int minor = block.getMinorVersion() ;
- if (( major < 1) && ( minor < 8))
- throw new IOException("Sorry: files created with versions older than v00-08" + " are no longer supported !");
-
- // FIX ME: need to set access mode here....
- return new SIORunHeader(block.getData(),major,minor);
- }
- }
- catch (EOFException x)
- {
- if( _filenames != null && ++_currentIndex < _filenames.length ){
- close() ;
- open( _filenames[ _currentIndex ] ) ;
- return readNextRunHeader( accessMode ) ;
- }
- return null;
- }
+ if( _filenames != null && ++_currentIndex < _filenames.length ){
+ close() ;
+ open( _filenames[ _currentIndex ] ) ;
+ return readNextRunHeader( accessMode ) ;
+ }
+ return null;
}
}
@@ -284,90 +249,4 @@
{
runListeners.remove(ls);
}
-
- private static class RandomAccessSupport {
-
- private RandomAccessBlock fileRandomAccessBlock;
- private List<RandomAccessBlock> indexRandomAccessBlocks = new ArrayList<RandomAccessBlock>();
- private boolean indexBlocksRead;
- private SIOReader reader;
- private Map<RandomAccessBlock,IndexBlock> indexHash = new HashMap<RandomAccessBlock,IndexBlock>();
-
- RandomAccessSupport(SIOReader reader) {
- this.reader = reader;
- }
-
- private void addRandomAccessRecord(SIORecord record) throws IOException {
- RandomAccessBlock ra = new RandomAccessBlock(record);
- System.out.println("Found ra=" + ra);
- if (ra.getIndexLocation() == 0) {
- fileRandomAccessBlock = ra;
- } else {
- indexRandomAccessBlocks.add(ra);
- }
- }
-
- private long findNextRunHeader(long currentPosition) throws IOException {
- List<RandomAccessBlock> iab = findIndexRandomAccessBlocks();
- for (RandomAccessBlock rab : iab) {
- // FIXME: Do something more efficient
- if (rab.getRunHeaderCount() > 0)
- {
- IndexBlock ib = findIndexBlock(rab);
- long position = ib.findRecordHeader(currentPosition);
- if (position >= 0) return position;
- }
- }
- return -1;
- }
-
- private long findEvent(int run, int event) throws IOException {
- RunEvent re = new RunEvent(run, event);
-
- RandomAccessBlock fab = findFileRandomAccessBlock();
- if (!fab.contains(re)) {
- return -1;
- }
-
- List<RandomAccessBlock> iab = findIndexRandomAccessBlocks();
- int location = Collections.binarySearch(iab, re);
- IndexBlock ib = findIndexBlock(iab.get(location));
- return ib.getLocation(re);
- }
-
- private RandomAccessBlock findFileRandomAccessBlock() throws IOException {
- if (fileRandomAccessBlock == null) {
- SIORecord record = reader.readRecord(0);
- fileRandomAccessBlock = new RandomAccessBlock(record);
- }
- return fileRandomAccessBlock;
- }
-
- private List<RandomAccessBlock> findIndexRandomAccessBlocks() throws IOException {
- RandomAccessBlock fab = findFileRandomAccessBlock();
- if (indexRandomAccessBlocks.isEmpty()) {
- SIORecord record = reader.readRecord(fab.getPreviousLocation());
- RandomAccessBlock rab = new RandomAccessBlock(record);
- indexRandomAccessBlocks.add(rab);
- indexBlocksRead = rab.getNextLocation() == 0;
- }
- while (!indexBlocksRead) {
- long nextLocation = indexRandomAccessBlocks.get(indexRandomAccessBlocks.size() - 1).getNextLocation();
- SIORecord record = reader.readRecord(nextLocation);
- RandomAccessBlock rab = new RandomAccessBlock(record);
- indexRandomAccessBlocks.add(rab);
- indexBlocksRead = rab.getNextLocation() == 0;
- }
- return indexRandomAccessBlocks;
- }
-
- private IndexBlock findIndexBlock(RandomAccessBlock rab) throws IOException {
- IndexBlock result = indexHash.get(rab);
- if (result == null) {
- result = new IndexBlock(reader.readRecord(rab.getIndexLocation()));
- indexHash.put(rab,result);
- }
- return result;
- }
- }
}
CVSspam 0.2.8