OpenInput() public method

Creates an IndexInput for the file with the given name.
public OpenInput ( string name, IOContext context ) : IndexInput
name string
context IOContext
return IndexInput
Exemplo n.º 1
0
        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();
        }
Exemplo n.º 2
0
 public virtual void TestSlicedSeeking()
 {
     for (int i = 0; i < 10; i++)
     {
         MMapDirectory mmapDir = new MMapDirectory(CreateTempDir("testSlicedSeeking"), null, 1 << i);
         IndexOutput   io      = mmapDir.CreateOutput("bytes", NewIOContext(Random()));
         var           bytes   = new byte[1 << (i + 1)]; // make sure we switch buffers
         Random().NextBytes(bytes);
         io.WriteBytes(bytes, bytes.Length);
         io.Dispose();
         IndexInput ii     = mmapDir.OpenInput("bytes", NewIOContext(Random()));
         var        actual = new byte[1 << (i + 1)]; // first read all bytes
         ii.ReadBytes(actual, 0, actual.Length);
         ii.Dispose();
         Assert.AreEqual(new BytesRef(bytes), new BytesRef(actual));
         IndexInputSlicer slicer = mmapDir.CreateSlicer("bytes", NewIOContext(Random()));
         for (int sliceStart = 0; sliceStart < bytes.Length; sliceStart++)
         {
             for (int sliceLength = 0; sliceLength < bytes.Length - sliceStart; sliceLength++)
             {
                 var        slice = new byte[sliceLength];
                 IndexInput input = slicer.OpenSlice("bytesSlice", sliceStart, slice.Length);
                 input.ReadBytes(slice, 0, slice.Length);
                 input.Dispose();
                 Assert.AreEqual(new BytesRef(bytes, sliceStart, sliceLength), new BytesRef(slice));
             }
         }
         slicer.Dispose();
         mmapDir.Dispose();
     }
 }
Exemplo n.º 3
0
 public virtual void TestCloneClose()
 {
     MMapDirectory mmapDir = new MMapDirectory(CreateTempDir("testCloneClose"));
     IndexOutput io = mmapDir.CreateOutput("bytes", NewIOContext(Random()));
     io.WriteVInt(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.ReadVInt());
     try
     {
         two.ReadVInt();
         Assert.Fail("Must throw AlreadyClosedException");
     }
     catch (AlreadyClosedException ignore)
     {
         // pass
     }
     Assert.AreEqual(5, three.ReadVInt());
     one.Dispose();
     three.Dispose();
     mmapDir.Dispose();
 }
Exemplo n.º 4
0
 public virtual void TestSeekZero()
 {
     for (int i = 0; i < 31; i++)
     {
         MMapDirectory mmapDir = new MMapDirectory(CreateTempDir("testSeekZero"), null, 1 << i);
         IndexOutput   io      = mmapDir.CreateOutput("zeroBytes", NewIOContext(Random()));
         io.Dispose();
         IndexInput ii = mmapDir.OpenInput("zeroBytes", NewIOContext(Random()));
         ii.Seek(0L);
         ii.Dispose();
         mmapDir.Dispose();
     }
 }
Exemplo n.º 5
0
        public virtual void TestCloneSafety()
        {
            MMapDirectory mmapDir = new MMapDirectory(CreateTempDir("testCloneSafety"));
            IndexOutput   io      = mmapDir.CreateOutput("bytes", NewIOContext(Random()));

            io.WriteVInt(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.ReadVInt();
                Assert.Fail("Must throw AlreadyClosedException");
            }
            catch (AlreadyClosedException ignore)
            {
                // pass
            }
            try
            {
                two.ReadVInt();
                Assert.Fail("Must throw AlreadyClosedException");
            }
            catch (AlreadyClosedException ignore)
            {
                // pass
            }
            try
            {
                three.ReadVInt();
                Assert.Fail("Must throw AlreadyClosedException");
            }
            catch (AlreadyClosedException ignore)
            {
                // pass
            }
            two.Dispose();
            three.Dispose();
            // test double close of master:
            one.Dispose();
            mmapDir.Dispose();
        }
Exemplo n.º 6
0
        public void TestDisposeIndexInput()
        {
            string name     = "foobar";
            var    dir      = CreateTempDir("testDisposeIndexInput");
            string fileName = Path.Combine(dir.FullName, name);

            // Create a zero byte file, and close it immediately
            File.WriteAllText(fileName, string.Empty, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false) /* No BOM */);

            MMapDirectory mmapDir = new MMapDirectory(dir);

            using (var indexInput = mmapDir.OpenInput(name, NewIOContext(Random())))
            {
            } // Dispose

            // Now it should be possible to delete the file. This is the condition we are testing for.
            File.Delete(fileName);
        }
Exemplo n.º 7
0
 public virtual void TestSeekEnd()
 {
     for (int i = 0; i < 17; i++)
     {
         MMapDirectory mmapDir = new MMapDirectory(CreateTempDir("testSeekEnd"), null, 1 << i);
         IndexOutput   io      = mmapDir.CreateOutput("bytes", NewIOContext(Random()));
         var           bytes   = new byte[1 << i];
         Random().NextBytes(bytes);
         io.WriteBytes(bytes, bytes.Length);
         io.Dispose();
         IndexInput ii     = mmapDir.OpenInput("bytes", NewIOContext(Random()));
         var        actual = new byte[1 << i];
         ii.ReadBytes(actual, 0, actual.Length);
         Assert.AreEqual(new BytesRef(bytes), new BytesRef(actual));
         ii.Seek(1 << i);
         ii.Dispose();
         mmapDir.Dispose();
     }
 }
Exemplo n.º 8
0
 public virtual void TestSlicedSeeking()
 {
     for (int i = 0; i < 10; i++)
     {
         MMapDirectory mmapDir = new MMapDirectory(CreateTempDir("testSlicedSeeking"), null, 1 << i);
         IndexOutput io = mmapDir.CreateOutput("bytes", NewIOContext(Random()));
         var bytes = new byte[1 << (i + 1)]; // make sure we switch buffers
         Random().NextBytes(bytes);
         io.WriteBytes(bytes, bytes.Length);
         io.Dispose();
         IndexInput ii = mmapDir.OpenInput("bytes", NewIOContext(Random()));
         var actual = new byte[1 << (i + 1)]; // first read all bytes
         ii.ReadBytes(actual, 0, actual.Length);
         ii.Dispose();
         Assert.AreEqual(new BytesRef(bytes), new BytesRef(actual));
         IndexInputSlicer slicer = mmapDir.CreateSlicer("bytes", NewIOContext(Random()));
         for (int sliceStart = 0; sliceStart < bytes.Length; sliceStart++)
         {
             for (int sliceLength = 0; sliceLength < bytes.Length - sliceStart; sliceLength++)
             {
                 var slice = new byte[sliceLength];
                 IndexInput input = slicer.OpenSlice("bytesSlice", sliceStart, slice.Length);
                 input.ReadBytes(slice, 0, slice.Length);
                 input.Dispose();
                 Assert.AreEqual(new BytesRef(bytes, sliceStart, sliceLength), new BytesRef(slice));
             }
         }
         slicer.Dispose();
         mmapDir.Dispose();
     }
 }
Exemplo n.º 9
0
 public virtual void TestSeekZero()
 {
     for (int i = 0; i < 31; i++)
     {
         MMapDirectory mmapDir = new MMapDirectory(CreateTempDir("testSeekZero"), null, 1 << i);
         IndexOutput io = mmapDir.CreateOutput("zeroBytes", NewIOContext(Random()));
         io.Dispose();
         IndexInput ii = mmapDir.OpenInput("zeroBytes", NewIOContext(Random()));
         ii.Seek(0L);
         ii.Dispose();
         mmapDir.Dispose();
     }
 }
Exemplo n.º 10
0
 public virtual void TestSeekEnd()
 {
     for (int i = 0; i < 17; i++)
     {
         MMapDirectory mmapDir = new MMapDirectory(CreateTempDir("testSeekEnd"), null, 1 << i);
         IndexOutput io = mmapDir.CreateOutput("bytes", NewIOContext(Random()));
         var bytes = new byte[1 << i];
         Random().NextBytes((byte[])(Array)bytes);
         io.WriteBytes(bytes, bytes.Length);
         io.Dispose();
         IndexInput ii = mmapDir.OpenInput("bytes", NewIOContext(Random()));
         var actual = new byte[1 << i];
         ii.ReadBytes(actual, 0, actual.Length);
         Assert.AreEqual(new BytesRef(bytes), new BytesRef(actual));
         ii.Seek(1 << i);
         ii.Dispose();
         mmapDir.Dispose();
     }
 }