//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void readAndWriteMustTakeBufferPositionIntoAccount() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void ReadAndWriteMustTakeBufferPositionIntoAccount()
        {
            sbyte[]    bytes = new sbyte[] { 1, 2, 3, 4, 5 };
            ByteBuffer buf   = ByteBuffer.wrap(bytes);

            buf.position(1);

            Fsa.mkdirs(Path);
            File file = new File(Path, "file");

            using (StoreChannel channel = Fsa.open(file, OpenMode.ReadWrite))
            {
                assertThat(channel.write(buf), @is(4));
            }
            using (Stream stream = Fsa.openAsInputStream(file))
            {
                assertThat(stream.Read(), @is(2));
                assertThat(stream.Read(), @is(3));
                assertThat(stream.Read(), @is(4));
                assertThat(stream.Read(), @is(5));
                assertThat(stream.Read(), @is(-1));
            }
            Arrays.fill(bytes, ( sbyte )0);
            buf.position(1);
            using (StoreChannel channel = Fsa.open(file, OpenMode.ReadWrite))
            {
                assertThat(channel.read(buf), @is(4));
                buf.clear();
                assertThat(buf.get(), @is((sbyte)0));
                assertThat(buf.get(), @is((sbyte)2));
                assertThat(buf.get(), @is((sbyte)3));
                assertThat(buf.get(), @is((sbyte)4));
                assertThat(buf.get(), @is((sbyte)5));
            }
        }
示例#2
0
        /// <summary>
        /// Compacts away the gap which will form in non-aggressive (regular) mode
        /// when batches are read in from disk.
        /// <para>
        /// The gap will contain already used IDs so it is important to remove it
        /// on a clean shutdown. The freed IDs will not be reused after an
        /// unclean shutdown, as guaranteed by the external user.
        /// <pre>
        /// Below diagram tries to explain the situation
        ///
        ///   S = old IDs which are still free (on the Stack)
        ///   G = the Gap which has formed, due to consuming old IDs
        ///   N = the New IDs which have been freed during this run (will be compacted to the left)
        ///
        ///     stackPosition
        ///          v
        /// [ S S S S G G G N N N N N N N N ]
        ///                ^
        ///          initialPosition
        /// </pre>
        /// After compaction the state will be:
        /// <pre>
        /// [ S S S S N N N N N N N N ]
        /// </pre>
        /// and the last part of the file is truncated.
        /// </para>
        /// </summary>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void compact(ByteBuffer writeBuffer) throws java.io.IOException
        private void Compact(ByteBuffer writeBuffer)
        {
            Debug.Assert(_stackPosition <= _initialPosition);                 // the stack can only be consumed in regular mode
            if (_initialPosition == _stackPosition)
            {
                // there is no compaction to be done
                return;
            }

            long writePosition = _stackPosition;
            long readPosition  = _initialPosition;              // readPosition to end of file contain new free IDs, to be compacted
            int  nBytes;

            do
            {
                writeBuffer.clear();
                _channel.position(readPosition);
                nBytes = _channel.read(writeBuffer);

                if (nBytes > 0)
                {
                    readPosition += nBytes;

                    writeBuffer.flip();
                    _channel.position(writePosition);
                    _channel.writeAll(writeBuffer);
                    writePosition += nBytes;
                }
            } while (nBytes > 0);

            _channel.truncate(writePosition);
        }
示例#3
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expected = IllegalStateException.class) public void createIdGeneratorMustRefuseOverwritingExistingFile() throws java.io.IOException
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void CreateIdGeneratorMustRefuseOverwritingExistingFile()
        {
            IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
            IdGenerator idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 1008, 1000, false, IdType.NODE, () => 0L);

            try
            {
                IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, true);
            }
            finally
            {
                CloseIdGenerator(idGenerator);
                // verify that id generator is ok
                StoreChannel fileChannel = _fs.open(IdGeneratorFile(), OpenMode.READ_WRITE);
                ByteBuffer   buffer      = ByteBuffer.allocate(9);
                fileChannel.ReadAll(buffer);
                buffer.flip();
                assertEquals(( sbyte )0, buffer.get());
                assertEquals(0L, buffer.Long);
                buffer.flip();
                int readCount = fileChannel.read(buffer);
                if (readCount != -1 && readCount != 0)
                {
                    fail("Id generator header not ok read 9 + " + readCount + " bytes from file");
                }
                fileChannel.close();

                File file = IdGeneratorFile();
                if (file.exists())
                {
                    assertTrue(file.delete());
                }
            }
        }
示例#4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldNotAppendToFileWhenRetryingWithNewFile() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldNotAppendToFileWhenRetryingWithNewFile()
        {
            // given
            string fileName               = "foo";
            string copyFileName           = "bar";
            string unfinishedContent      = "abcd";
            string finishedContent        = "abcdefgh";
            IEnumerator <string> contents = Iterators.iterator(unfinishedContent, finishedContent);

            // and
            TestCatchupServerHandler halfWayFailingServerhandler = new TestCatchupServerHandlerAnonymousInnerClass(this, _logProvider, TestDirectory, _fsa, fileName, copyFileName, contents);

            Server halfWayFailingServer = null;

            try
            {
                // when
                ListenSocketAddress listenAddress = new ListenSocketAddress("localhost", PortAuthority.allocatePort());
                halfWayFailingServer = (new CatchupServerBuilder(halfWayFailingServerhandler)).listenAddress(listenAddress).build();
                halfWayFailingServer.Start();

                CatchupAddressProvider addressProvider = CatchupAddressProvider.fromSingleAddress(new AdvertisedSocketAddress(listenAddress.Hostname, listenAddress.Port));

                StoreId storeId     = halfWayFailingServerhandler.StoreId;
                File    databaseDir = TestDirectory.databaseDir();
                StreamToDiskProvider streamToDiskProvider = new StreamToDiskProvider(databaseDir, _fsa, new Monitors());

                // and
                _subject.copyStoreFiles(addressProvider, storeId, streamToDiskProvider, () => _defaultTerminationCondition, _targetLocation);

                // then
                assertEquals(FileContent(new File(databaseDir, fileName)), finishedContent);

                // and
                File fileCopy = new File(databaseDir, copyFileName);

                ByteBuffer buffer = ByteBuffer.wrap(new sbyte[finishedContent.Length]);
                using (StoreChannel storeChannel = _fsa.create(fileCopy))
                {
                    storeChannel.read(buffer);
                }
                assertEquals(finishedContent, new string( buffer.array(), Charsets.UTF_8 ));
            }
            finally
            {
                halfWayFailingServer.Stop();
                halfWayFailingServer.Shutdown();
            }
        }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void verifyRecordsInFile(java.io.File file, int recordCount) throws java.io.IOException
        private void VerifyRecordsInFile(File file, int recordCount)
        {
            using (StoreChannel channel = Fsa.open(file, OpenMode.Read))
            {
                ByteBuffer buf         = ByteBuffer.allocate(_recordSize);
                ByteBuffer observation = ByteBuffer.allocate(_recordSize);
                for (int i = 0; i < recordCount; i++)
                {
                    GenerateRecordForId(i, buf);
                    observation.position(0);
                    channel.read(observation);
                    AssertRecord(i, observation, buf);
                }
            }
        }
示例#6
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private System.Nullable<long>[] readRecordsWithNullDefaults(org.neo4j.io.fs.StoreChannel fileChannel, int count, boolean allowUpgrade) throws java.io.IOException
        private long?[] ReadRecordsWithNullDefaults(StoreChannel fileChannel, int count, bool allowUpgrade)
        {
            _buf.clear();
            int bytesRead        = fileChannel.read(_buf);
            int wholeRecordsRead = bytesRead / RECORD_SIZE;

            if (wholeRecordsRead < RECORD_COUNT && !allowUpgrade)
            {
                throw new UpgradeNotAllowedByConfigurationException("Index version (managed by " + _file + ") has changed and needs to be upgraded");
            }

            _buf.flip();
            long?[] result = new long?[count];
            for (int i = 0; i < wholeRecordsRead; i++)
            {
                result[i] = _buf.Long;
            }
            return(result);
        }
示例#7
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public int read(ByteBuffer dst) throws java.io.IOException
        public override int Read(ByteBuffer dst)
        {
            return(@delegate.read(dst));
        }