Example #1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testStickyGenerator()
        public virtual void TestStickyGenerator()
        {
            try
            {
                IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
                IdGenerator idGen = new IdGeneratorImpl(_fs, IdGeneratorFile(), 3, 1000, false, IdType.NODE, () => 0L);
                try
                {
                    new IdGeneratorImpl(_fs, IdGeneratorFile(), 3, 1000, false, IdType.NODE, () => 0L);
                    fail("Opening sticky id generator should throw exception");
                }
                catch (StoreFailureException)
                {                         // good
                }
                CloseIdGenerator(idGen);
            }
            finally
            {
                File file = IdGeneratorFile();
                if (file.exists())
                {
                    assertTrue(file.delete());
                }
            }
        }
Example #2
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());
                }
            }
        }
Example #3
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: void clearIdFiles(java.nio.file.Path backupLocation) throws java.io.IOException
        internal virtual void ClearIdFiles(Path backupLocation)
        {
            IOException exception       = null;
            File        targetDirectory = backupLocation.toFile();

            File[] files = _fs.listFiles(targetDirectory);
            foreach (File file in files)
            {
                if (!_fs.isDirectory(file) && file.Name.EndsWith(".id"))
                {
                    try
                    {
                        long highId = IdGeneratorImpl.readHighId(_fs, file);
                        _fs.deleteFile(file);
                        IdGeneratorImpl.createGenerator(_fs, file, highId, true);
                    }
                    catch (IOException e)
                    {
                        exception = Exceptions.chain(exception, e);
                    }
                }
            }
            if (exception != null)
            {
                throw exception;
            }
        }
Example #4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testClose()
        public virtual void TestClose()
        {
            try
            {
                IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
                IdGenerator idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 2, 1000, false, IdType.NODE, () => 0L);
                CloseIdGenerator(idGenerator);
                try
                {
                    idGenerator.NextId();
                    fail("nextId after close should throw exception");
                }
                catch (System.InvalidOperationException)
                {                         // good
                }
                try
                {
                    idGenerator.FreeId(0);
                    fail("freeId after close should throw exception");
                }
                catch (System.InvalidOperationException)
                {                         // good
                }
                idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 2, 1000, false, IdType.NODE, () => 0L);
                assertEquals(0L, idGenerator.NextId());
                assertEquals(1L, idGenerator.NextId());
                assertEquals(2L, idGenerator.NextId());
                CloseIdGenerator(idGenerator);
                try
                {
                    idGenerator.NextId();
                    fail("nextId after close should throw exception");
                }
                catch (System.InvalidOperationException)
                {                         // good
                }
                try
                {
                    idGenerator.FreeId(0);
                    fail("freeId after close should throw exception");
                }
                catch (System.InvalidOperationException)
                {                         // good
                }
            }
            finally
            {
                File file = IdGeneratorFile();
                if (file.exists())
                {
                    assertTrue(file.delete());
                }
            }
        }
Example #5
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testNextId()
        public virtual void TestNextId()
        {
            try
            {
                IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
                IdGenerator idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 3, 1000, false, IdType.NODE, () => 0L);
                for (long i = 0; i < 7; i++)
                {
                    assertEquals(i, idGenerator.NextId());
                }
                idGenerator.FreeId(1);
                idGenerator.FreeId(3);
                idGenerator.FreeId(5);
                assertEquals(7L, idGenerator.NextId());
                idGenerator.FreeId(6);
                CloseIdGenerator(idGenerator);
                idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 5, 1000, false, IdType.NODE, () => 0L);
                idGenerator.FreeId(2);
                idGenerator.FreeId(4);
                assertEquals(1L, idGenerator.NextId());
                idGenerator.FreeId(1);
                assertEquals(3L, idGenerator.NextId());
                idGenerator.FreeId(3);
                assertEquals(5L, idGenerator.NextId());
                idGenerator.FreeId(5);
                assertEquals(6L, idGenerator.NextId());
                idGenerator.FreeId(6);
                assertEquals(8L, idGenerator.NextId());
                idGenerator.FreeId(8);
                assertEquals(9L, idGenerator.NextId());
                idGenerator.FreeId(9);
                CloseIdGenerator(idGenerator);
                idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 3, 1000, false, IdType.NODE, () => 0L);
                assertEquals(6L, idGenerator.NextId());
                assertEquals(8L, idGenerator.NextId());
                assertEquals(9L, idGenerator.NextId());
                assertEquals(1L, idGenerator.NextId());
                assertEquals(3L, idGenerator.NextId());
                assertEquals(5L, idGenerator.NextId());
                assertEquals(2L, idGenerator.NextId());
                assertEquals(4L, idGenerator.NextId());
                assertEquals(10L, idGenerator.NextId());
                assertEquals(11L, idGenerator.NextId());
                CloseIdGenerator(idGenerator);
            }
            finally
            {
                File file = IdGeneratorFile();
                if (file.exists())
                {
                    assertTrue(file.delete());
                }
            }
        }
Example #6
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testFreeId()
        public virtual void TestFreeId()
        {
            try
            {
                IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
                IdGenerator idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 3, 1000, false, IdType.NODE, () => 0L);
                for (long i = 0; i < 7; i++)
                {
                    assertEquals(i, idGenerator.NextId());
                }
                try
                {
                    idGenerator.FreeId(-1);
                    fail("Negative id should throw exception");
                }
                catch (System.ArgumentException)
                {                         // good
                }
                try
                {
                    idGenerator.FreeId(7);
                    fail("Greater id than ever returned should throw exception");
                }
                catch (System.ArgumentException)
                {                         // good
                }
                for (int i = 0; i < 7; i++)
                {
                    idGenerator.FreeId(i);
                }
                CloseIdGenerator(idGenerator);
                idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 2, 1000, false, IdType.NODE, () => 0L);
                assertEquals(5L, idGenerator.NextId());
                assertEquals(6L, idGenerator.NextId());
                assertEquals(3L, idGenerator.NextId());
                CloseIdGenerator(idGenerator);
                idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 30, 1000, false, IdType.NODE, () => 0L);

                assertEquals(0L, idGenerator.NextId());
                assertEquals(1L, idGenerator.NextId());
                assertEquals(2L, idGenerator.NextId());
                assertEquals(4L, idGenerator.NextId());
                CloseIdGenerator(idGenerator);
            }
            finally
            {
                File file = IdGeneratorFile();
                if (file.exists())
                {
                    assertTrue(file.delete());
                }
            }
        }
Example #7
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void delete()
        public virtual void Delete()
        {
            IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
            IdGeneratorImpl idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 10, 1000, false, IdType.NODE, () => 0L);
            long            id          = idGenerator.NextId();

            idGenerator.NextId();
            idGenerator.FreeId(id);
            idGenerator.Dispose();
            idGenerator.Delete();
            assertFalse(IdGeneratorFile().exists());
            IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
            idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 10, 1000, false, IdType.NODE, () => 0L);
            assertEquals(id, idGenerator.NextId());
            idGenerator.Dispose();
        }
Example #8
0
        private void MakeSureMagicMinusOneIsSkipped(RecordFormat format)
        {
            DeleteIdGeneratorFile();
            IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
            IdGenerator idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 1, format.MaxId, false, IdType.NODE, () => 0L);
            long        id          = ( long )Math.Pow(2, 32) - 3;

            idGenerator.HighId = id;
            assertEquals(id, idGenerator.NextId());
            assertEquals(id + 1, idGenerator.NextId());
            // Here we make sure that id+2 (integer -1) is skipped
            assertEquals(id + 3, idGenerator.NextId());
            assertEquals(id + 4, idGenerator.NextId());
            assertEquals(id + 5, idGenerator.NextId());
            CloseIdGenerator(idGenerator);
        }
Example #9
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void mustOverwriteExistingFileIfRequested()
        public virtual void MustOverwriteExistingFileIfRequested()
        {
            IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
            IdGenerator idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 1008, 1000, false, IdType.NODE, () => 0L);

            long[] firstFirstIds = new long[] { idGenerator.NextId(), idGenerator.NextId(), idGenerator.NextId() };
            idGenerator.Dispose();

            IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
            idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 1008, 1000, false, IdType.NODE, () => 0L);
            long[] secondFirstIds = new long[] { idGenerator.NextId(), idGenerator.NextId(), idGenerator.NextId() };
            idGenerator.Dispose();

            // Basically, recreating the id file should be the same as start over with the ids.
            assertThat(secondFirstIds, @is(firstFirstIds));
        }
Example #10
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void makeSureMagicMinusOneCannotBeReturnedEvenIfFreed()
        public virtual void MakeSureMagicMinusOneCannotBeReturnedEvenIfFreed()
        {
            IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
            IdGenerator idGenerator   = new IdGeneratorImpl(_fs, IdGeneratorFile(), 1, (new NodeRecordFormat()).MaxId, false, IdType.NODE, () => 0L);
            long        magicMinusOne = ( long )Math.Pow(2, 32) - 1;

            idGenerator.HighId = magicMinusOne;
            assertEquals(magicMinusOne + 1, idGenerator.NextId());
            idGenerator.FreeId(magicMinusOne - 1);
            idGenerator.FreeId(magicMinusOne);
            CloseIdGenerator(idGenerator);

            idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 1, (new NodeRecordFormat()).MaxId, false, IdType.NODE, () => 0L);
            assertEquals(magicMinusOne - 1, idGenerator.NextId());
            assertEquals(magicMinusOne + 2, idGenerator.NextId());
            CloseIdGenerator(idGenerator);
        }
Example #11
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testRandomTest()
        public virtual void TestRandomTest()
        {
            Random random   = new Random(DateTimeHelper.CurrentUnixTimeMillis());
            int    capacity = random.Next(1024) + 1024;
            int    grabSize = random.Next(128) + 128;

            IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
            IdGenerator  idGenerator    = new IdGeneratorImpl(_fs, IdGeneratorFile(), grabSize, capacity * 2, false, IdType.NODE, () => 0L);
            IList <long> idsTaken       = new List <long>();
            float        releaseIndex   = 0.25f;
            float        closeIndex     = 0.05f;
            int          currentIdCount = 0;

            try
            {
                while (currentIdCount < capacity)
                {
                    float rIndex = random.nextFloat();
                    if (rIndex < releaseIndex && currentIdCount > 0)
                    {
                        idGenerator.FreeId(idsTaken.Remove(random.Next(currentIdCount)).intValue());
                        currentIdCount--;
                    }
                    else
                    {
                        idsTaken.Add(idGenerator.NextId());
                        currentIdCount++;
                    }
                    if (rIndex > (1.0f - closeIndex) || rIndex < closeIndex)
                    {
                        CloseIdGenerator(idGenerator);
                        grabSize    = random.Next(128) + 128;
                        idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), grabSize, capacity * 2, false, IdType.NODE, () => 0L);
                    }
                }
                CloseIdGenerator(idGenerator);
            }
            finally
            {
                File file = IdGeneratorFile();
                if (file.exists())
                {
                    assertTrue(file.delete());
                }
            }
        }
Example #12
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testUnsignedId()
        public virtual void TestUnsignedId()
        {
            try
            {
                PropertyKeyTokenRecordFormat recordFormat = new PropertyKeyTokenRecordFormat();
                IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
                IdGenerator idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 1, recordFormat.MaxId, false, IdType.NODE, () => 0L);
                idGenerator.HighId = recordFormat.MaxId;
                long id = idGenerator.NextId();
                assertEquals(recordFormat.MaxId, id);
                idGenerator.FreeId(id);
                try
                {
                    idGenerator.NextId();
                    fail("Shouldn't be able to get next ID");
                }
                catch (StoreFailureException)
                {                         // good, capacity exceeded
                }
                CloseIdGenerator(idGenerator);
                idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 1, recordFormat.MaxId, false, IdType.NODE, () => 0L);
                assertEquals(recordFormat.MaxId + 1, idGenerator.HighId);
                id = idGenerator.NextId();
                assertEquals(recordFormat.MaxId, id);
                try
                {
                    idGenerator.NextId();
                }
                catch (StoreFailureException)
                {                         // good, capacity exceeded
                }
                CloseIdGenerator(idGenerator);
            }
            finally
            {
                File file = IdGeneratorFile();
                if (file.exists())
                {
                    assertTrue(file.delete());
                }
            }
        }
Example #13
0
        private void MakeSureIdCapacityCannotBeExceeded(RecordFormat format)
        {
            DeleteIdGeneratorFile();
            IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
            long        maxValue    = format.MaxId;
            IdGenerator idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 1, maxValue - 1, false, IdType.NODE, () => 0L);
            long        id          = maxValue - 2;

            idGenerator.HighId = id;
            assertEquals(id, idGenerator.NextId());
            assertEquals(id + 1, idGenerator.NextId());
            try
            {
                idGenerator.NextId();
                fail("Id capacity shouldn't be able to be exceeded for " + format);
            }
            catch (StoreFailureException)
            {               // Good
            }
            CloseIdGenerator(idGenerator);
        }
Example #14
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testChurnIdBatchAtGrabSize()
        public virtual void TestChurnIdBatchAtGrabSize()
        {
            IdGenerator idGenerator = null;

            try
            {
                IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
                const int grabSize = 10;
                int       rounds   = 10;
                idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), grabSize, 1000, true, IdType.NODE, () => 0L);

                for (int i = 0; i < rounds; i++)
                {
                    ISet <long> ids = new HashSet <long>();
                    for (int j = 0; j < grabSize; j++)
                    {
                        ids.Add(idGenerator.NextId());
                    }
                    foreach (long?id in ids)
                    {
                        idGenerator.FreeId(id.Value);
                    }
                }
                long newId = idGenerator.NextId();
                assertTrue("Expected IDs to be reused (" + grabSize + " at a time). high ID was: " + newId, newId < grabSize * rounds);
            }
            finally
            {
                if (idGenerator != null)
                {
                    CloseIdGenerator(idGenerator);
                }
                File file = IdGeneratorFile();
                if (file.exists())
                {
                    assertTrue(file.delete());
                }
            }
        }
Example #15
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expected = IllegalArgumentException.class) public void cannotCreateIdGeneratorWithNullFile()
        public virtual void CannotCreateIdGeneratorWithNullFile()
        {
            IdGeneratorImpl.createGenerator(_fs, null, 0, false);
        }
Example #16
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expected = IllegalArgumentException.class) public void grabSizeCannotBeNegative()
        public virtual void GrabSizeCannotBeNegative()
        {
            IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
            (new IdGeneratorImpl(_fs, IdGeneratorFile(), -1, 100, false, IdType.NODE, () => 0L)).Dispose();
        }
Example #17
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testOddAndEvenWorstCase()
        public virtual void TestOddAndEvenWorstCase()
        {
            int capacity = 1024 * 8 + 1;

            try
            {
                IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
                IdGenerator idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 128, capacity * 2, false, IdType.NODE, () => 0L);
                for (int i = 0; i < capacity; i++)
                {
                    idGenerator.NextId();
                }
                IDictionary <long, object> freedIds = new Dictionary <long, object>();
                for (long i = 1; i < capacity; i += 2)
                {
                    idGenerator.FreeId(i);
                    freedIds[i] = this;
                }
                CloseIdGenerator(idGenerator);
                idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 2000, capacity * 2, false, IdType.NODE, () => 0L);
                long oldId = -1;
                for (int i = 0; i < capacity - 1; i += 2)
                {
                    long id = idGenerator.NextId();
                    if (freedIds.Remove(id) == null)
                    {
                        throw new Exception("Id=" + id + " prevId=" + oldId + " list.size()=" + freedIds.Count);
                    }
                    oldId = id;
                }
                assertEquals(0, freedIds.Values.Count);
                CloseIdGenerator(idGenerator);
            }
            finally
            {
                File file = IdGeneratorFile();
                if (_fs.fileExists(file))
                {
                    assertTrue(_fs.deleteFile(file));
                }
            }
            try
            {
                IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
                IdGenerator idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 128, capacity * 2, false, IdType.NODE, () => 0L);
                for (int i = 0; i < capacity; i++)
                {
                    idGenerator.NextId();
                }
                IDictionary <long, object> freedIds = new Dictionary <long, object>();
                for (long i = 0; i < capacity; i += 2)
                {
                    idGenerator.FreeId(i);
                    freedIds[i] = this;
                }
                CloseIdGenerator(idGenerator);
                idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 2000, capacity * 2, false, IdType.NODE, () => 0L);
                for (int i = 0; i < capacity; i += 2)
                {
                    assertEquals(this, freedIds.Remove(idGenerator.NextId()));
                }
                assertEquals(0, freedIds.Values.Count);
                CloseIdGenerator(idGenerator);
            }
            finally
            {
                File file = IdGeneratorFile();
                if (file.exists())
                {
                    assertTrue(file.delete());
                }
            }
        }
Example #18
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expected = IllegalArgumentException.class) public void cannotCreateIdGeneratorWithNullFileSystem()
        public virtual void CannotCreateIdGeneratorWithNullFileSystem()
        {
            IdGeneratorImpl.createGenerator(null, IdGeneratorFile(), 0, false);
        }