public virtual void TestCloneClose() { MMapDirectory mmapDir = new MMapDirectory(CreateTempDir("testCloneClose")); IndexOutput io = mmapDir.CreateOutput("bytes", NewIOContext(Random())); io.WriteVInt32(5); io.Dispose(); IndexInput one = mmapDir.OpenInput("bytes", IOContext.DEFAULT); IndexInput two = (IndexInput)one.Clone(); IndexInput three = (IndexInput)two.Clone(); // clone of clone two.Dispose(); Assert.AreEqual(5, one.ReadVInt32()); try { two.ReadVInt32(); Assert.Fail("Must throw ObjectDisposedException"); } #pragma warning disable 168 catch (ObjectDisposedException ignore) #pragma warning restore 168 { // pass } Assert.AreEqual(5, three.ReadVInt32()); one.Dispose(); three.Dispose(); mmapDir.Dispose(); }
public virtual void TestCloneSafety() { MMapDirectory mmapDir = new MMapDirectory(CreateTempDir("testCloneSafety")); IndexOutput io = mmapDir.CreateOutput("bytes", NewIOContext(Random)); io.WriteVInt32(5); io.Dispose(); IndexInput one = mmapDir.OpenInput("bytes", IOContext.DEFAULT); IndexInput two = (IndexInput)one.Clone(); IndexInput three = (IndexInput)two.Clone(); // clone of clone one.Dispose(); try { one.ReadVInt32(); Assert.Fail("Must throw ObjectDisposedException"); } catch (Exception ignore) when(ignore.IsAlreadyClosedException()) { // pass } try { two.ReadVInt32(); Assert.Fail("Must throw ObjectDisposedException"); } catch (Exception ignore) when(ignore.IsAlreadyClosedException()) { // pass } try { three.ReadVInt32(); Assert.Fail("Must throw ObjectDisposedException"); } catch (Exception ignore) when(ignore.IsAlreadyClosedException()) { // pass } two.Dispose(); three.Dispose(); // test double close of master: one.Dispose(); mmapDir.Dispose(); }
private static IDictionary <string, FileEntry> ReadLegacyEntries(IndexInput stream, int firstInt) { IDictionary <string, FileEntry> entries = new Dictionary <string, FileEntry>(); int count; bool stripSegmentName; if (firstInt < CompoundFileWriter.FORMAT_PRE_VERSION) { if (firstInt < CompoundFileWriter.FORMAT_NO_SEGMENT_PREFIX) { throw new CorruptIndexException("Incompatible format version: " + firstInt + " expected >= " + CompoundFileWriter.FORMAT_NO_SEGMENT_PREFIX + " (resource: " + stream + ")"); } // It's a post-3.1 index, read the count. count = stream.ReadVInt32(); stripSegmentName = false; } else { count = firstInt; stripSegmentName = true; } // read the directory and init files long streamLength = stream.Length; FileEntry entry = null; for (int i = 0; i < count; i++) { long offset = stream.ReadInt64(); if (offset < 0 || offset > streamLength) { throw new CorruptIndexException("Invalid CFS entry offset: " + offset + " (resource: " + stream + ")"); } string id = stream.ReadString(); if (stripSegmentName) { // Fix the id to not include the segment names. this is relevant for // pre-3.1 indexes. id = IndexFileNames.StripSegmentName(id); } if (entry != null) { // set length of the previous entry entry.Length = offset - entry.Offset; } entry = new FileEntry(); entry.Offset = offset; FileEntry previous = entries.Put(id, entry); if (previous != null) { throw new CorruptIndexException("Duplicate cfs entry id=" + id + " in CFS: " + stream); } } // set the length of the final entry if (entry != null) { entry.Length = streamLength - entry.Offset; } return(entries); }
/// <summary> /// Helper method that reads CFS entries from an input stream </summary> private static IDictionary <string, FileEntry> ReadEntries(IndexInputSlicer handle, Directory dir, string name) { IOException priorE = null; IndexInput stream = null; ChecksumIndexInput entriesStream = null; // read the first VInt. If it is negative, it's the version number // otherwise it's the count (pre-3.1 indexes) try { IDictionary <string, FileEntry> mapping; #pragma warning disable 612, 618 stream = handle.OpenFullSlice(); #pragma warning restore 612, 618 int firstInt = stream.ReadVInt32(); // impossible for 3.0 to have 63 files in a .cfs, CFS writer was not visible // and separate norms/etc are outside of cfs. if (firstInt == CODEC_MAGIC_BYTE1) { sbyte secondByte = (sbyte)stream.ReadByte(); sbyte thirdByte = (sbyte)stream.ReadByte(); sbyte fourthByte = (sbyte)stream.ReadByte(); if (secondByte != CODEC_MAGIC_BYTE2 || thirdByte != CODEC_MAGIC_BYTE3 || fourthByte != CODEC_MAGIC_BYTE4) { throw new CorruptIndexException("Illegal/impossible header for CFS file: " + secondByte + "," + thirdByte + "," + fourthByte); } int version = CodecUtil.CheckHeaderNoMagic(stream, CompoundFileWriter.DATA_CODEC, CompoundFileWriter.VERSION_START, CompoundFileWriter.VERSION_CURRENT); string entriesFileName = IndexFileNames.SegmentFileName( IndexFileNames.StripExtension(name), "", IndexFileNames.COMPOUND_FILE_ENTRIES_EXTENSION); entriesStream = dir.OpenChecksumInput(entriesFileName, IOContext.READ_ONCE); CodecUtil.CheckHeader(entriesStream, CompoundFileWriter.ENTRY_CODEC, CompoundFileWriter.VERSION_START, CompoundFileWriter.VERSION_CURRENT); int numEntries = entriesStream.ReadVInt32(); mapping = new Dictionary <string, FileEntry>(numEntries); for (int i = 0; i < numEntries; i++) { FileEntry fileEntry = new FileEntry(); string id = entriesStream.ReadString(); FileEntry previous = mapping.Put(id, fileEntry); if (previous != null) { throw new CorruptIndexException("Duplicate cfs entry id=" + id + " in CFS: " + entriesStream); } fileEntry.Offset = entriesStream.ReadInt64(); fileEntry.Length = entriesStream.ReadInt64(); } if (version >= CompoundFileWriter.VERSION_CHECKSUM) { CodecUtil.CheckFooter(entriesStream); } else { #pragma warning disable 612, 618 CodecUtil.CheckEOF(entriesStream); #pragma warning restore 612, 618 } } else { // TODO remove once 3.x is not supported anymore mapping = ReadLegacyEntries(stream, firstInt); } return(mapping); } catch (IOException ioe) { priorE = ioe; } finally { IOUtils.DisposeWhileHandlingException(priorE, stream, entriesStream); } // this is needed until Java 7's real try-with-resources: throw new InvalidOperationException("impossible to get here"); }
/// <summary> /// NOTE: this was readVInt() in Lucene /// </summary> public override int ReadVInt32() { EnsureOpen(); return(@delegate.ReadVInt32()); }