Exemple #1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void shouldDeleteFileOnCloseWithDataBeforeDoneAdding() throws java.io.IOException
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void ShouldDeleteFileOnCloseWithDataBeforeDoneAdding()
        {
            FileSystemAbstraction fs = Directory.FileSystem;
            File makeSureImDeleted   = Directory.file("makeSureImDeleted");

            using (IndexKeyStorage <GenericKey> keyStorage = keyStorage(makeSureImDeleted))
            {
                keyStorage.add(RandomKey(1));
                assertTrue(fs.FileExists(makeSureImDeleted), "Expected this file to exist now so that we can assert deletion later.");
            }
            assertFalse(fs.FileExists(makeSureImDeleted), "Expected this file to be deleted on close.");
        }
Exemple #2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void shouldNotCreateFileIfNoData() throws java.io.IOException
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void ShouldNotCreateFileIfNoData()
        {
            FileSystemAbstraction fs = Directory.FileSystem;
            File makeSureImDeleted   = Directory.file("makeSureImDeleted");

            using (IndexKeyStorage <GenericKey> keyStorage = keyStorage(makeSureImDeleted))
            {
                assertFalse(fs.FileExists(makeSureImDeleted), "Expected this file to exist now so that we can assert deletion later.");
                keyStorage.doneAdding();
                assertFalse(fs.FileExists(makeSureImDeleted), "Expected this file to exist now so that we can assert deletion later.");
            }
            assertFalse(fs.FileExists(makeSureImDeleted), "Expected this file to be deleted on close.");
        }
Exemple #3
0
        /// <summary>
        /// Create zip archive for requested <code>sourceToCompress</code>.
        /// If <code>sourceToCompress</code> is a directory then content of that directory and all its sub-directories will be added to the archive.
        /// If <code>sourceToCompress</code> does not exist or is an empty directory then archive will not be created. </summary>
        /// <param name="fileSystem"> source file system </param>
        /// <param name="sourceToCompress"> source file to compress </param>
        /// <param name="destinationZip"> zip file compress source to </param>
        /// <exception cref="IOException"> when underlying file system access produce IOException </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public static void zip(org.neo4j.io.fs.FileSystemAbstraction fileSystem, java.io.File sourceToCompress, java.io.File destinationZip) throws java.io.IOException
        public static void Zip(FileSystemAbstraction fileSystem, File sourceToCompress, File destinationZip)
        {
            if (!fileSystem.FileExists(sourceToCompress))
            {
                return;
            }
            if (IsEmptyDirectory(fileSystem, sourceToCompress))
            {
                return;
            }
            IDictionary <string, string> env = MapUtil.stringMap("create", "true");
            Path rootPath           = sourceToCompress.toPath();
            URI  archiveAbsoluteURI = URI.create("jar:file:" + destinationZip.toURI().RawPath);

            using (FileSystem zipFs = FileSystems.newFileSystem(archiveAbsoluteURI, env))
            {
                IList <FileHandle> fileHandles = fileSystem.StreamFilesRecursive(sourceToCompress).collect(toList());
                foreach (FileHandle fileHandle in fileHandles)
                {
                    Path sourcePath = fileHandle.File.toPath();
                    Path zipFsPath  = fileSystem.IsDirectory(sourceToCompress) ? zipFs.getPath(rootPath.relativize(sourcePath).ToString()) : zipFs.getPath(sourcePath.FileName.ToString());
                    if (zipFsPath.Parent != null)
                    {
                        Files.createDirectories(zipFsPath.Parent);
                    }
                    Files.copy(sourcePath, zipFsPath);
                }
            }
        }
Exemple #4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void mustAllocateResourcesLazilyAndCleanUpOnClose() throws java.io.IOException
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void MustAllocateResourcesLazilyAndCleanUpOnClose()
        {
            FileSystemAbstraction fs = Directory.FileSystem;
            LocalMemoryTracker    allocationTracker = new LocalMemoryTracker();
            File file = Directory.file("file");

            using (UnsafeDirectByteBufferAllocator bufferFactory = new UnsafeDirectByteBufferAllocator(allocationTracker), IndexKeyStorage <GenericKey> keyStorage = keyStorage(file, bufferFactory))
            {
                assertEquals(0, allocationTracker.UsedDirectMemory(), "Expected to not have any buffers allocated yet");
                assertFalse(fs.FileExists(file), "Expected file to be created lazily");
                keyStorage.add(RandomKey(1));
                assertEquals(BLOCK_SIZE, allocationTracker.UsedDirectMemory(), "Expected to have exactly one buffer allocated by now");
                assertTrue(fs.FileExists(file), "Expected file to be created by now");
            }
            assertFalse(fs.FileExists(file), "Expected file to be deleted on close");
        }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void assertFormatSpecifierAndHeadersOnly(java.util.Map<String,byte[]> headers, org.neo4j.io.fs.FileSystemAbstraction fs, java.io.File file) throws java.io.IOException
        private void AssertFormatSpecifierAndHeadersOnly(IDictionary <string, sbyte[]> headers, FileSystemAbstraction fs, File file)
        {
            assertTrue(fs.FileExists(file));
            using (Stream stream = fs.OpenAsInputStream(file))
            {
                // format specifier
                int     read;
                int     size      = 16;
                sbyte[] readEntry = new sbyte[size];
                sbyte[] allZeros  = new sbyte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

                read = stream.Read(readEntry, 0, readEntry.Length);
                assertEquals(size, read);
                assertArrayEquals(allZeros, readEntry);

                read = stream.Read(readEntry, 0, readEntry.Length);
                assertEquals(size, read);
                assertArrayEquals(new sbyte[] { ( sbyte )-1, ( sbyte )-1, ( sbyte )-1, ( sbyte )-1, ( sbyte )-1, ( sbyte )-1, ( sbyte )-1, ( sbyte )-1, ( sbyte )-1, ( sbyte )-1, ( sbyte )-1, ( sbyte )-1, ( sbyte )-1, ( sbyte )-1, ( sbyte )-1, ( sbyte )-1 }, readEntry);

                for (int i = 0; i < headers.Count; i++)
                {
                    read = stream.Read(readEntry, 0, readEntry.Length);
                    assertEquals(size, read);
                    assertArrayEquals(allZeros, readEntry);

                    read = stream.Read(readEntry, 0, readEntry.Length);
                    assertEquals(size, read);
                    headers.ContainsValue(readEntry);
                }

                assertEquals(-1, stream.Read());
            }
        }
        /// <summary>
        /// Select record format for the given store directory.
        /// <para>
        /// <b>Note:</b> package private only for testing.
        ///
        /// </para>
        /// </summary>
        /// <param name="databaseLayout"> directory with the store </param>
        /// <param name="fs"> file system used to access store files </param>
        /// <param name="pageCache"> page cache to read store files </param>
        /// <returns> record format of the given store or <code>null</code> if <seealso cref="DatabaseLayout.metadataStore()"/> file not
        /// found or can't be read </returns>
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Nullable static RecordFormats selectForStore(org.neo4j.io.layout.DatabaseLayout databaseLayout, org.neo4j.io.fs.FileSystemAbstraction fs, org.neo4j.io.pagecache.PageCache pageCache, org.neo4j.logging.LogProvider logProvider)
        internal static RecordFormats SelectForStore(DatabaseLayout databaseLayout, FileSystemAbstraction fs, PageCache pageCache, LogProvider logProvider)
        {
            File neoStoreFile = databaseLayout.MetadataStore();

            if (fs.FileExists(neoStoreFile))
            {
                try
                {
                    long value = MetaDataStore.getRecord(pageCache, neoStoreFile, STORE_VERSION);
                    if (value != MetaDataRecordFormat.FIELD_NOT_PRESENT)
                    {
                        string storeVersion = MetaDataStore.versionLongToString(value);

                        foreach (RecordFormats format in AllFormats())
                        {
                            if (format.StoreVersion().Equals(storeVersion))
                            {
                                Info(logProvider, "Selected " + format + " record format from store " + databaseLayout.DatabaseDirectory());
                                return(format);
                            }
                        }
                    }
                }
                catch (IOException e)
                {
                    Info(logProvider, "Unable to read store format: " + e.Message);
                }
            }
            return(null);
        }
Exemple #7
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public static void dumpCountsStore(org.neo4j.io.fs.FileSystemAbstraction fs, java.io.File path, java.io.PrintStream out) throws Exception
//JAVA TO C# CONVERTER NOTE: Members cannot have the same name as their enclosing type:
        public static void DumpCountsStoreConflict(FileSystemAbstraction fs, File path, PrintStream @out)
        {
            using (JobScheduler jobScheduler = createInitialisedScheduler(), PageCache pages = createPageCache(fs, jobScheduler), Lifespan life = new Lifespan())
            {
                NullLogProvider logProvider = NullLogProvider.Instance;
                Config          config      = Config.defaults();
                if (fs.IsDirectory(path))
                {
                    DatabaseLayout databaseLayout = DatabaseLayout.of(path);
                    StoreFactory   factory        = new StoreFactory(databaseLayout, Config.defaults(), new DefaultIdGeneratorFactory(fs), pages, fs, logProvider, EmptyVersionContextSupplier.EMPTY);

                    NeoStores     neoStores     = factory.OpenAllNeoStores();
                    SchemaStorage schemaStorage = new SchemaStorage(neoStores.SchemaStore);
                    neoStores.Counts.accept(new DumpCountsStore(@out, neoStores, schemaStorage));
                }
                else
                {
                    VisitableCountsTracker tracker = new VisitableCountsTracker(logProvider, fs, pages, config, DatabaseLayout.of(path.ParentFile));
                    if (fs.FileExists(path))
                    {
                        tracker.VisitFile(path, new DumpCountsStore(@out));
                    }
                    else
                    {
                        life.Add(tracker).accept(new DumpCountsStore(@out));
                    }
                }
            }
        }
Exemple #8
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private static java.io.File clean(org.neo4j.io.fs.FileSystemAbstraction fs, java.io.File dir) throws java.io.IOException
        private static File Clean(FileSystemAbstraction fs, File dir)
        {
            if (fs.FileExists(dir))
            {
                fs.DeleteRecursively(dir);
            }
            fs.Mkdirs(dir);
            return(dir);
        }
Exemple #9
0
        private static int LastArchivedOutputFileNumber(FileSystemAbstraction fileSystem, File outputFile)
        {
            int i = 1;

            while (fileSystem.FileExists(ArchivedOutputFile(outputFile, i)))
            {
                i++;
            }
            return(i - 1);
        }
Exemple #10
0
        public IndexProviderStore(File file, FileSystemAbstraction fileSystem, long expectedVersion, bool allowUpgrade)
        {
            this._file   = file;
            this._random = new Random(DateTimeHelper.CurrentUnixTimeMillis());
            StoreChannel channel = null;
            bool         success = false;

            try
            {
                // Create it if it doesn't exist
                if (!fileSystem.FileExists(file) || fileSystem.GetFileSize(file) == 0)
                {
                    Create(file, fileSystem, expectedVersion);
                }

                // Read all the records in the file
                channel = fileSystem.Open(file, OpenMode.READ_WRITE);
                long?[] records = ReadRecordsWithNullDefaults(channel, RECORD_COUNT, allowUpgrade);
                _creationTime     = records[0].Value;
                _randomIdentifier = records[1].Value;
                _version          = records[2].Value;
                _lastCommittedTx  = records[3].Value;
                long?readIndexVersion = records[4];
                _fileChannel = channel;

                // Compare version and throw exception if there's a mismatch, also considering "allow upgrade"
                bool versionDiffers = CompareExpectedVersionWithStoreVersion(expectedVersion, allowUpgrade, readIndexVersion);

                // Here we know that either the version matches or we just upgraded to the expected version
                _indexVersion = expectedVersion;
                if (versionDiffers)
                {
                    // We have upgraded the version, let's write it
                    WriteOut();
                }
                success = true;
            }
            catch (IOException e)
            {
                throw new Exception(e);
            }
            finally
            {
                if (!success && channel != null)
                {
                    try
                    {
                        channel.close();
                    }
                    catch (IOException)
                    {                              // What to do?
                    }
                }
            }
        }
Exemple #11
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldRotateLog() throws java.io.IOException
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldRotateLog()
        {
            SecurityLog securityLog = new SecurityLog(_config, FileSystemRule.get(), ThreadStart.run);

            securityLog.Info("line 1");
            securityLog.Info("line 2");

            FileSystemAbstraction fs = FileSystemRule.get();

            File activeLogFile = _config.get(SecuritySettings.security_log_filename);

            assertThat(fs.FileExists(activeLogFile), equalTo(true));
            assertThat(fs.FileExists(Archive(1)), equalTo(true));
            assertThat(fs.FileExists(Archive(2)), equalTo(false));

            string[] activeLines = ReadLogFile(fs, activeLogFile);
            assertThat(activeLines, array(containsString("line 2")));

            string[] archiveLines = ReadLogFile(fs, Archive(1));
            assertThat(archiveLines, array(containsString("line 1")));
        }
Exemple #12
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void overrideDestination() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void OverrideDestination()
        {
            // because of https://bugs.openjdk.java.net/browse/JDK-8202127 and current surefire behaviour we need to have custom value for JRE >= 11
            string toArgument = JRE.JAVA_11.CurrentVersion ? "--to=" + System.getProperty("user.dir") + "/other/" : "--to=other/";

            string[] args = new string[] { toArgument, "all" };
            using (RealOutsideWorld outsideWorld = new RealOutsideWorld())
            {
                DiagnosticsReportCommand diagnosticsReportCommand = new DiagnosticsReportCommand(_homeDir, _configDir, outsideWorld);
                diagnosticsReportCommand.Execute(args);

                File other = _testDirectory.directory("other");
                FileSystemAbstraction fs = outsideWorld.FileSystem();
                assertThat(fs.FileExists(other), @is(true));
                assertThat(fs.ListFiles(other).Length, @is(1));

                // Default should be empty
                File reports = new File(_testDirectory.directory(), "reports");
                assertThat(fs.FileExists(reports), @is(false));
            }
        }
Exemple #13
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void create(java.io.File file, org.neo4j.io.fs.FileSystemAbstraction fileSystem, long indexVersion) throws java.io.IOException
        private void Create(File file, FileSystemAbstraction fileSystem, long indexVersion)
        {
            if (fileSystem.FileExists(file) && fileSystem.GetFileSize(file) > 0)
            {
                throw new System.ArgumentException(file + " already exist");
            }

            using (StoreChannel fileChannel = fileSystem.Open(file, OpenMode.READ_WRITE))
            {
                Write(fileChannel, DateTimeHelper.CurrentUnixTimeMillis(), _random.nextLong(), 0, 1, indexVersion);
            }
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldDeleteOnCloseIfOpenOptionsSaysSo()
        public virtual void ShouldDeleteOnCloseIfOpenOptionsSaysSo()
        {
            // GIVEN
            DatabaseLayout        databaseLayout = _dir.databaseLayout();
            File                  nodeStore      = databaseLayout.NodeStore();
            File                  idFile         = databaseLayout.IdFile(DatabaseFile.NODE_STORE).orElseThrow(() => new System.InvalidOperationException("Node store id file not found."));
            FileSystemAbstraction fs             = _fileSystemRule.get();
            PageCache             pageCache      = _pageCacheRule.getPageCache(fs, Config.defaults());
            TheStore              store          = new TheStore(nodeStore, databaseLayout.IdNodeStore(), _config, _idType, new DefaultIdGeneratorFactory(fs), pageCache, NullLogProvider.Instance, _recordFormat, DELETE_ON_CLOSE);

            store.Initialise(true);
            store.MakeStoreOk();
            assertTrue(fs.FileExists(nodeStore));
            assertTrue(fs.FileExists(idFile));

            // WHEN
            store.Close();

            // THEN
            assertFalse(fs.FileExists(nodeStore));
            assertFalse(fs.FileExists(idFile));
        }
Exemple #15
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: static SegmentFile create(org.neo4j.io.fs.FileSystemAbstraction fileSystem, java.io.File file, ReaderPool readerPool, long version, org.neo4j.causalclustering.messaging.marshalling.ChannelMarshal<org.neo4j.causalclustering.core.replication.ReplicatedContent> contentMarshal, org.neo4j.logging.LogProvider logProvider, SegmentHeader header) throws java.io.IOException
        internal static SegmentFile Create(FileSystemAbstraction fileSystem, File file, ReaderPool readerPool, long version, ChannelMarshal <ReplicatedContent> contentMarshal, LogProvider logProvider, SegmentHeader header)
        {
            if (fileSystem.FileExists(file))
            {
                throw new System.InvalidOperationException("File was not expected to exist");
            }

            SegmentFile segment = new SegmentFile(fileSystem, file, readerPool, version, contentMarshal, logProvider, header);

            _headerMarshal.marshal(header, segment.OrCreateWriter);
            segment.Flush();

            return(segment);
        }
Exemple #16
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: protected java.io.File getTempFile(org.neo4j.io.fs.FileSystemAbstraction fileSystem, java.io.File recordsFile) throws java.io.IOException
        protected internal virtual File GetTempFile(FileSystemAbstraction fileSystem, File recordsFile)
        {
            File directory = recordsFile.ParentFile;

            if (!fileSystem.FileExists(directory))
            {
                fileSystem.Mkdirs(directory);
            }

            long n = _random.nextLong();

            n = (n == long.MinValue) ? 0 : Math.Abs(n);
            return(new File(directory, Convert.ToString(n) + "_" + recordsFile.Name + ".tmp"));
        }
Exemple #17
0
 private static Stream <File> StreamFilesRecursiveInner(File directory, FileSystemAbstraction fs)
 {
     File[] files = fs.ListFiles(directory);
     if (files == null)
     {
         if (!fs.FileExists(directory))
         {
             return(Stream.empty());
         }
         return(Stream.of(directory));
     }
     else
     {
         return(Stream.of(files).flatMap(f => fs.IsDirectory(f) ? StreamFilesRecursiveInner(f, fs) : Stream.of(f)));
     }
 }
Exemple #18
0
        /// <summary>
        /// Exposes the algorithm for collecting existing rotated log files.
        /// </summary>
        public static IList <File> GetAllArchives(FileSystemAbstraction fileSystem, File outputFile)
        {
            List <File> ret = new List <File>();
            int         i   = 1;

            while (true)
            {
                File file = ArchivedOutputFile(outputFile, i);
                if (!fileSystem.FileExists(file))
                {
                    break;
                }
                ret.Add(file);
                i++;
            }
            return(ret);
        }
Exemple #19
0
        private string RealUsersExistErrorMsg(FileSystemAbstraction fileSystem, File authFile)
        {
            string files;
            File   parentFile = authFile.ParentFile;
            File   roles      = new File(parentFile, "roles");

            if (fileSystem.FileExists(roles))
            {
                files = "`auth` and `roles` files";
            }
            else
            {
                files = "`auth` file";
            }

            return("the provided initial password was not set because existing Neo4j users were detected at `" + authFile.AbsolutePath + "`. Please remove the existing " + files + " if you want to reset your database " +
                   "to only have a default user with the provided password.");
        }
Exemple #20
0
        private static void LoadPersistedSettings(Properties settings, File indexFolder, FileSystemAbstraction fs)
        {
            File settingsFile = new File(indexFolder, INDEX_CONFIG_FILE);

            if (fs.FileExists(settingsFile))
            {
                try
                {
                    using (Reader reader = fs.OpenAsReader(settingsFile, StandardCharsets.UTF_8))
                    {
                        settings.load(reader);
                    }
                }
                catch (IOException e)
                {
                    throw new UncheckedIOException("Failed to read persisted fulltext index properties: " + settingsFile, e);
                }
            }
        }
 internal ReadOnlyIdGenerator(System.Func <long> highId, FileSystemAbstraction fs, File filename)
 {
     if (fs != null && fs.FileExists(filename))
     {
         try
         {
             this.HighIdConflict = IdGeneratorImpl.ReadHighId(fs, filename);
             DefragCountConflict = IdGeneratorImpl.ReadDefragCount(fs, filename);
         }
         catch (IOException e)
         {
             throw new UnderlyingStorageException("Failed to read id counts of the id file: " + filename, e);
         }
     }
     else
     {
         this.HighIdConflict = highId();
         DefragCountConflict = 0;
     }
 }
Exemple #22
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: static java.util.List<String> readAllLines(org.neo4j.io.fs.FileSystemAbstraction fs, java.io.File logFilename) throws java.io.IOException
        internal static IList <string> ReadAllLines(FileSystemAbstraction fs, File logFilename)
        {
            IList <string> logLines = new List <string>();

            // this is needed as the EphemeralFSA is broken, and creates a new file when reading a non-existent file from
            // a valid directory
            if (!fs.FileExists(logFilename))
            {
                throw new FileNotFoundException("File does not exist.");
            }

            using (StreamReader reader = new StreamReader(fs.OpenAsReader(logFilename, StandardCharsets.UTF_8)))
            {
                for (string line; (line = reader.ReadLine()) != null;)
                {
                    logLines.Add(line);
                }
            }
            return(logLines);
        }
Exemple #23
0
 internal LabelScanWriteMonitor(FileSystemAbstraction fs, DatabaseLayout databaseLayout, long rotationThreshold, ByteUnit rotationThresholdUnit, long pruneThreshold, TimeUnit pruneThresholdUnit)
 {
     this._fs = fs;
     this._rotationThreshold = rotationThresholdUnit.toBytes(rotationThreshold);
     this._pruneThreshold    = pruneThresholdUnit.toMillis(pruneThreshold);
     this._storeDir          = databaseLayout.DatabaseDirectory();
     this._file = WriteLogBaseFile(databaseLayout);
     try
     {
         if (fs.FileExists(_file))
         {
             MoveAwayFile();
         }
         this._channel = InstantiateChannel();
     }
     catch (IOException e)
     {
         throw new UncheckedIOException(e);
     }
 }
Exemple #24
0
        /// <summary>
        /// Deletes index folder with the specific indexId, but has the option to first archive the index if it exists.
        /// The zip archive will be placed next to the root directory for that index with a timestamp included in its name.
        /// </summary>
        /// <param name="fs"> <seealso cref="FileSystemAbstraction"/> this index lives in. </param>
        /// <param name="directoryStructure"> <seealso cref="IndexDirectoryStructure"/> knowing the directory structure for the provider owning the index. </param>
        /// <param name="indexId"> id of the index. </param>
        /// <param name="archiveIfExists"> whether or not to archive the index before deleting it, if it exists. </param>
        /// <returns> whether or not an archive was created. </returns>
        /// <exception cref="IOException"> on I/O error. </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public static boolean deleteIndex(org.neo4j.io.fs.FileSystemAbstraction fs, org.neo4j.kernel.api.index.IndexDirectoryStructure directoryStructure, long indexId, boolean archiveIfExists) throws java.io.IOException
        public static bool DeleteIndex(FileSystemAbstraction fs, IndexDirectoryStructure directoryStructure, long indexId, bool archiveIfExists)
        {
            File rootIndexDirectory = directoryStructure.DirectoryForIndex(indexId);

            if (archiveIfExists && fs.IsDirectory(rootIndexDirectory) && fs.FileExists(rootIndexDirectory) && fs.ListFiles(rootIndexDirectory).Length > 0)
            {
                ZipUtils.zip(fs, rootIndexDirectory, new File(rootIndexDirectory.Parent, "archive-" + rootIndexDirectory.Name + "-" + DateTimeHelper.CurrentUnixTimeMillis() + ".zip"));
                return(true);
            }
            int attempt = 0;

            while (attempt < 5)
            {
                attempt++;
                try
                {
                    fs.DeleteRecursively(rootIndexDirectory);
                    break;
                }
                catch (Exception concurrentModificationException) when(concurrentModificationException is DirectoryNotEmptyException || concurrentModificationException is NoSuchFileException)
                {
                    // Looks like someone was poking around in our directory while we where deleting.
                    // Let's sleep for a bit and try again.
                    try
                    {
                        Thread.Sleep(100);
                    }
                    catch (InterruptedException)
                    {
                        // Let's abandon this attempt to clean up.
                        Thread.CurrentThread.Interrupt();
                        break;
                    }
                }
            }
            return(false);
        }
Exemple #25
0
 private void CompareSnapshotFiles(ISet <string> firstSnapshotFileNames, ISet <string> secondSnapshotFileNames, FileSystemAbstraction fileSystem)
 {
     assertThat(format("Should have %d modified index segment files. Snapshot segment files are: %s", NUMBER_OF_INDEXES, firstSnapshotFileNames), firstSnapshotFileNames, hasSize(NUMBER_OF_INDEXES));
     foreach (string fileName in firstSnapshotFileNames)
     {
         assertFalse("Snapshot segments fileset should not have files from another snapshot set." + DescribeFileSets(firstSnapshotFileNames, secondSnapshotFileNames), secondSnapshotFileNames.Contains(fileName));
         string path = FilenameUtils.getFullPath(fileName);
         assertTrue("Snapshot should contain files for index in path: " + path + "." + DescribeFileSets(firstSnapshotFileNames, secondSnapshotFileNames), secondSnapshotFileNames.Any(name => name.StartsWith(path)));
         assertTrue(format("Snapshot segment file '%s' should exist.", fileName), fileSystem.FileExists(new File(fileName)));
     }
 }
Exemple #26
0
        private long SizeOfFileIfExists(File file)
        {
            FileSystemAbstraction fileSystem = _outsideWorld.fileSystem();

            return(fileSystem.FileExists(file) ? fileSystem.GetFileSize(file) : 0);
        }
Exemple #27
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void whenHAModeSwitcherSwitchesToSlaveTheOtherModeSwitcherDoNotGetTheOldMasterClient() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void WhenHAModeSwitcherSwitchesToSlaveTheOtherModeSwitcherDoNotGetTheOldMasterClient()
        {
            InstanceId me      = new InstanceId(1);
            StoreId    storeId = newStoreIdForCurrentVersion();
            HighAvailabilityMemberContext context = mock(typeof(HighAvailabilityMemberContext));

            when(context.MyId).thenReturn(me);
            AvailabilityGuard      guard        = mock(typeof(DatabaseAvailabilityGuard));
            ObservedClusterMembers members      = mock(typeof(ObservedClusterMembers));
            ClusterMember          masterMember = mock(typeof(ClusterMember));

            when(masterMember.HARole).thenReturn("master");
            when(masterMember.HasRole("master")).thenReturn(true);
            when(masterMember.InstanceId).thenReturn(new InstanceId(2));
            when(masterMember.StoreId).thenReturn(storeId);
            ClusterMember self = new ClusterMember(me);

            when(members.Members).thenReturn(Arrays.asList(self, masterMember));
            when(members.CurrentMember).thenReturn(self);
            DependencyResolver    dependencyResolver = mock(typeof(DependencyResolver));
            FileSystemAbstraction fs = mock(typeof(FileSystemAbstraction));

            when(fs.FileExists(any(typeof(File)))).thenReturn(true);
            when(dependencyResolver.ResolveDependency(typeof(FileSystemAbstraction))).thenReturn(fs);
            when(dependencyResolver.ResolveDependency(typeof(Monitors))).thenReturn(new Monitors());
            NeoStoreDataSource dataSource = mock(typeof(NeoStoreDataSource));

            when(dataSource.DependencyResolver).thenReturn(dependencyResolver);
            when(dataSource.StoreId).thenReturn(storeId);
            when(dependencyResolver.ResolveDependency(typeof(NeoStoreDataSource))).thenReturn(dataSource);
            when(dependencyResolver.ResolveDependency(typeof(TransactionIdStore))).thenReturn(new SimpleTransactionIdStore());
            when(dependencyResolver.ResolveDependency(typeof(ObservedClusterMembers))).thenReturn(members);
            UpdatePuller updatePuller = mock(typeof(UpdatePuller));

            when(updatePuller.TryPullUpdates()).thenReturn(true);
            when(dependencyResolver.ResolveDependency(typeof(UpdatePuller))).thenReturn(updatePuller);

            ClusterMemberAvailability clusterMemberAvailability = mock(typeof(ClusterMemberAvailability));
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final TriggerableClusterMemberEvents events = new TriggerableClusterMemberEvents();
            TriggerableClusterMemberEvents events = new TriggerableClusterMemberEvents();

            Election election = mock(typeof(Election));
            HighAvailabilityMemberStateMachine stateMachine = new HighAvailabilityMemberStateMachine(context, guard, members, events, election, NullLogProvider.Instance);

            ClusterMembers clusterMembers = new ClusterMembers(members, stateMachine);

            when(dependencyResolver.ResolveDependency(typeof(ClusterMembers))).thenReturn(clusterMembers);

            stateMachine.Init();
            stateMachine.Start();

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.neo4j.kernel.ha.DelegateInvocationHandler<org.neo4j.kernel.ha.com.master.Master> handler = new org.neo4j.kernel.ha.DelegateInvocationHandler<>(org.neo4j.kernel.ha.com.master.Master.class);
            DelegateInvocationHandler <Master> handler = new DelegateInvocationHandler <Master>(typeof(Master));

            MasterClientResolver masterClientResolver = mock(typeof(MasterClientResolver));
            MasterClient         masterClient         = mock(typeof(MasterClient));

            when(masterClient.ProtocolVersion).thenReturn(MasterClient214.PROTOCOL_VERSION);
            when(masterClient.Handshake(anyLong(), any(typeof(StoreId)))).thenReturn(new ResponseAnonymousInnerClass(this, storeId, mock(typeof(ResourceReleaser)), handler));
            when(masterClient.ToString()).thenReturn("TheExpectedMasterClient!");
            when(masterClientResolver.Instantiate(anyString(), anyInt(), anyString(), any(typeof(Monitors)), any(typeof(StoreId)), any(typeof(LifeSupport)))).thenReturn(masterClient);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.CountDownLatch latch = new java.util.concurrent.CountDownLatch(2);
            System.Threading.CountdownEvent latch = new System.Threading.CountdownEvent(2);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.atomic.AtomicBoolean switchedSuccessfully = new java.util.concurrent.atomic.AtomicBoolean();
            AtomicBoolean switchedSuccessfully = new AtomicBoolean();

            SwitchToSlave.Monitor monitor = new MonitorAnonymousInnerClass(this, latch, switchedSuccessfully);

            Config config = Config.defaults(ClusterSettings.server_id, me.ToString());

            DatabaseTransactionStats transactionCounters = mock(typeof(DatabaseTransactionStats));

            when(transactionCounters.NumberOfActiveTransactions).thenReturn(0L);

            PageCache pageCacheMock = mock(typeof(PageCache));
            PagedFile pagedFileMock = mock(typeof(PagedFile));

            when(pagedFileMock.LastPageId).thenReturn(1L);
            when(pageCacheMock.Map(any(typeof(File)), anyInt())).thenReturn(pagedFileMock);

            TransactionIdStore transactionIdStoreMock = mock(typeof(TransactionIdStore));

            when(transactionIdStoreMock.LastCommittedTransaction).thenReturn(new TransactionId(0, 0, 0));
            SwitchToSlaveCopyThenBranch switchToSlave = new SwitchToSlaveCopyThenBranch(DatabaseLayout.of(new File("")), NullLogService.Instance, mock(typeof(FileSystemAbstraction)), config, mock(typeof(HaIdGeneratorFactory)), handler, mock(typeof(ClusterMemberAvailability)), mock(typeof(RequestContextFactory)), mock(typeof(PullerFactory), RETURNS_MOCKS), Iterables.empty(), masterClientResolver, monitor, new Org.Neo4j.com.storecopy.StoreCopyClientMonitor_Adapter(), Suppliers.singleton(dataSource), Suppliers.singleton(transactionIdStoreMock), slave =>
            {
                SlaveServer mock = mock(typeof(SlaveServer));
                when(mock.SocketAddress).thenReturn(new InetSocketAddress("localhost", 123));
                return(mock);
            }, updatePuller, pageCacheMock, mock(typeof(Monitors)), () => transactionCounters);

            ComponentSwitcherContainer   switcherContainer = new ComponentSwitcherContainer();
            HighAvailabilityModeSwitcher haModeSwitcher    = new HighAvailabilityModeSwitcher(switchToSlave, mock(typeof(SwitchToMaster)), election, clusterMemberAvailability, mock(typeof(ClusterClient)), storeSupplierMock(), me, switcherContainer, NeoStoreDataSourceSupplierMock(), NullLogService.Instance);

            haModeSwitcher.Init();
            haModeSwitcher.Start();
            haModeSwitcher.ListeningAt(URI.create("http://localhost:12345"));

            stateMachine.AddHighAvailabilityMemberListener(haModeSwitcher);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.concurrent.atomic.AtomicReference<org.neo4j.kernel.ha.com.master.Master> ref = new java.util.concurrent.atomic.AtomicReference<>(null);
            AtomicReference <Master> @ref = new AtomicReference <Master>(null);

            //noinspection unchecked
            AbstractComponentSwitcher <object> otherModeSwitcher = new AbstractComponentSwitcherAnonymousInnerClass(this, mock(typeof(DelegateInvocationHandler)), handler, latch, @ref);

            switcherContainer.Add(otherModeSwitcher);
            // When
            events.SwitchToSlave(me);

            // Then
            latch.await();
            assertTrue("mode switch failed", switchedSuccessfully.get());
            Master actual = @ref.get();

            // let's test the toString()s since there are too many wrappers of proxies
            assertEquals(masterClient.ToString(), actual.ToString());

            stateMachine.Stop();
            stateMachine.Shutdown();
            haModeSwitcher.Stop();
            haModeSwitcher.Shutdown();
        }