Exemple #1
0
        public void GDID_TryParse()
        {
            GDID parsed;

            Assert.IsTrue(GDID.TryParse("1:2:3", out parsed));
            Assert.AreEqual(1, parsed.Era);
            Assert.AreEqual(2, parsed.Authority);
            Assert.AreEqual(3, parsed.Counter);

            Assert.IsTrue(GDID.TryParse("231:2:3123", out parsed));
            Assert.AreEqual(231, parsed.Era);
            Assert.AreEqual(2, parsed.Authority);
            Assert.AreEqual(3123, parsed.Counter);

            Assert.IsTrue(GDID.TryParse("31 : 2:  3123", out parsed));
            Assert.AreEqual(31, parsed.Era);
            Assert.AreEqual(2, parsed.Authority);
            Assert.AreEqual(3123, parsed.Counter);

            Assert.IsFalse(GDID.TryParse("-1:2:3123", out parsed));
            Assert.IsFalse(GDID.TryParse("1:18:3123", out parsed));
            Assert.IsFalse(GDID.TryParse(":18:3123", out parsed));
            Assert.IsFalse(GDID.TryParse("::3123", out parsed));
            Assert.IsFalse(GDID.TryParse("1::3", out parsed));
            Assert.IsFalse(GDID.TryParse("1::", out parsed));
            Assert.IsFalse(GDID.TryParse("1:-:-", out parsed));
            Assert.IsFalse(GDID.TryParse("1: : ", out parsed));

            Assert.IsFalse(GDID.TryParse("FTGEJK-IR", out parsed));
        }
Exemple #2
0
        public static string ParseGDID(string str)
        {
            if (str.IsNullOrWhiteSpace())
            {
                return(string.Empty);
            }

            GDID?gdid = null;

            ELink link = null;

            try
            {
                link = new ELink(str);
                gdid = link.GDID;
            }
            catch
            {
            }

            if (!gdid.HasValue && !GDID.TryParse(str, out gdid))
            {
                try
                {
                    byte[] raw = getGDIDBytes(str);
                    gdid = new GDID(raw);
                }
                catch (Exception)
                {
                    throw new FormatException("Invalid GDID format");
                }
            }

            return(getAllGDIDFormats(gdid.Value));
        }
Exemple #3
0
        public void GDID_TryParse()
        {
            GDID parsed;

            Assert.IsTrue(GDID.TryParse("1:2:3", out parsed));
            Assert.AreEqual(1, parsed.Era);
            Assert.AreEqual(2, parsed.Authority);
            Assert.AreEqual(3, parsed.Counter);

            Assert.IsTrue(GDID.TryParse("231:2:3123", out parsed));
            Assert.AreEqual(231, parsed.Era);
            Assert.AreEqual(2, parsed.Authority);
            Assert.AreEqual(3123, parsed.Counter);

            Assert.IsTrue(GDID.TryParse("   231:2:3123   ", out parsed));
            Assert.AreEqual(231, parsed.Era);
            Assert.AreEqual(2, parsed.Authority);
            Assert.AreEqual(3123, parsed.Counter);

            Assert.IsTrue(GDID.TryParse("31 : 2:  3123", out parsed));
            Assert.AreEqual(31, parsed.Era);
            Assert.AreEqual(2, parsed.Authority);
            Assert.AreEqual(3123, parsed.Counter);

            Assert.IsFalse(GDID.TryParse("-1:2:3123", out parsed));
            Assert.IsFalse(GDID.TryParse("1:18:3123", out parsed));
            Assert.IsFalse(GDID.TryParse(":18:3123", out parsed));
            Assert.IsFalse(GDID.TryParse("::3123", out parsed));
            Assert.IsFalse(GDID.TryParse("1::3", out parsed));
            Assert.IsFalse(GDID.TryParse("1::", out parsed));
            Assert.IsFalse(GDID.TryParse("1:-:-", out parsed));
            Assert.IsFalse(GDID.TryParse("1: : ", out parsed));

            Assert.IsFalse(GDID.TryParse("FTGEJK-IR", out parsed));


            //0x 00 00 00 00 10 00 00 00 00 00 00 4B
            //   ----era---- ------ulong -----------
            Assert.IsTrue(GDID.TryParse("0x00000000100000000000004B", out parsed));
            Assert.AreEqual(new GDID(0, 1, 0x4b), parsed);

            GDID?nullable;

            Assert.IsTrue(GDID.TryParse("0x00000000100000000000004B", out nullable));
            Assert.IsTrue(nullable.HasValue);
            Assert.AreEqual(new GDID(0, 1, 0x4b), nullable.Value);

            Assert.IsFalse(GDID.TryParse("0x0001000000000000", out nullable));//too short
            Assert.IsFalse(nullable.HasValue);

            Assert.IsFalse(GDID.TryParse("0x00000303030303003031000000000000", out nullable));//too long
            Assert.IsFalse(nullable.HasValue);
        }
Exemple #4
0
        public void GDID_BinBufferAndTryParseBin()
        {
            var gdid = new GDID(347827, 15, 0xaedb3434b);
            var buf  = gdid.Bytes;
            var hex  = "0x" + buf.ToDumpString(DumpFormat.Hex).Replace(" ", "");

            Console.WriteLine(hex);

            GDID gdid2;

            Assert.IsTrue(GDID.TryParse(hex, out gdid2));
            Assert.AreEqual(gdid, gdid2);
        }
Exemple #5
0
        //20160622 DKh
        public static GDIDSymbol AsGDIDSymbol(this object val)
        {
            if (val == null)
            {
                return(new GDIDSymbol());
            }

            if (val is GDIDSymbol)
            {
                return((GDIDSymbol)val);
            }
            if (val is GDID)
            {
                return(new GDIDSymbol((GDID)val, val.ToString()));
            }

            if (val is ELink)
            {
                return(((ELink)val).AsGDIDSymbol);
            }

            if (val is string)
            {
                var sval = ((string)val).Trim();

                GDID gdid;
                if (GDID.TryParse(sval, out gdid))
                {
                    return(new GDIDSymbol(gdid, sval));
                }

                try
                {
                    var elink = new ELink(sval);
                    return(elink.AsGDIDSymbol);
                }
                catch {}
            }

            if (val is ulong)
            {
                return(new GDIDSymbol(new GDID(0, (ulong)val), val.ToString()));
            }
            if (val is byte[])
            {
                var buf = (byte[])val;
                return(new GDIDSymbol(new GDID(buf), buf.ToDumpString(DumpFormat.Hex)));
            }
            return(new GDIDSymbol(new GDID(0, Convert.ToUInt64(val)), val.ToString()));
        }
Exemple #6
0
        public static GDID AsGDID(this object val)
        {
            if (val == null)
            {
                return(GDID.Zero);
            }

            if (val is GDID)
            {
                return((GDID)val);
            }

            if (val is ELink)
            {
                return(((ELink)val).GDID);
            }

            if (val is string)
            {
                var sval = ((string)val).Trim();

                GDID gdid;
                if (GDID.TryParse(sval, out gdid))
                {
                    return(gdid);
                }

                //20160109 DKH also interpret string via ELink
                try
                {
                    var elink = new ELink(sval);
                    return(elink.GDID);
                }
                catch {}
            }

            if (val is ulong)
            {
                return(new GDID(0, (ulong)val));
            }
            if (val is byte[])
            {
                return(new GDID((byte[])val));
            }
            return(new GDID(0, Convert.ToUInt64(val)));
        }
Exemple #7
0
            internal Partition(MdbArea area, IConfigSectionNode config) : base(area)
            {
                m_Area = area;

                if (area is MDBCentralArea)
                {
                    m_StartGDID = new GDID(0, 0, 0);
                }
                else
                {
                    var sgdid = config.AttrByName(CONFIG_START_GDID_ATTR).ValueAsString();

                    if (!GDID.TryParse(sgdid, out m_StartGDID))
                    {
                        throw new MdbException(StringConsts.MDB_AREA_CONFIG_PARTITION_GDID_ERROR.Args(area.Name, sgdid, "unparsable, expected 'era:0:counter'"));
                    }

                    if (m_StartGDID.Authority != 0)
                    {
                        throw new MdbException(StringConsts.MDB_AREA_CONFIG_PARTITION_GDID_ERROR.Args(area.Name, sgdid, "authority segment must be 0"));
                    }
                }
                //Shards
                var shards = new List <Shard>();

                foreach (var snode in config.Children.Where(cn => cn.IsSameName(CONFIG_SHARD_SECTION)))
                {
                    var shard = new Shard(this, snode);
                    shards.Add(shard);
                }

                if (shards.Count == 0)
                {
                    throw new MdbException(StringConsts.MDB_AREA_CONFIG_NO_PARTITION_SHARDS_ERROR.Args(area.Name, this.m_StartGDID));
                }

                if (shards.Count != shards.Distinct().Count())
                {
                    throw new MdbException(StringConsts.MDB_AREA_CONFIG_DUPLICATE_PARTITION_SHARDS_ERROR.Args(area.Name, this.m_StartGDID));
                }

                shards.Sort();
                m_Shards = shards.ToArray();
            }
Exemple #8
0
        public static GDID AsGDID(this object val)
        {
            if (val is GDID)
            {
                return((GDID)val);
            }

            if (val is string)
            {
                var sval = ((string)val).Trim();

                GDID gdid;
                if (GDID.TryParse(sval, out gdid))
                {
                    return(gdid);
                }
            }

            if (val is ulong)
            {
                return(new GDID(0, (ulong)val));
            }
            return(new GDID(0, Convert.ToUInt64(val)));
        }
Exemple #9
0
        public void GDID_TryParse()
        {
            GDID parsed;

            Aver.IsTrue(GDID.TryParse("1:2:3", out parsed));
            Aver.AreEqual((uint)1, parsed.Era);
            Aver.AreEqual(2, parsed.Authority);
            Aver.AreEqual((ulong)3, parsed.Counter);

            Aver.IsTrue(GDID.TryParse("231:2:3123", out parsed));
            Aver.AreEqual((uint)231, parsed.Era);
            Aver.AreEqual(2, parsed.Authority);
            Aver.AreEqual((ulong)3123, parsed.Counter);

            Aver.IsTrue(GDID.TryParse("   231:2:3123   ", out parsed));
            Aver.AreEqual((uint)231, parsed.Era);
            Aver.AreEqual(2, parsed.Authority);
            Aver.AreEqual((ulong)3123, parsed.Counter);

            Aver.IsTrue(GDID.TryParse("31 : 2:  3123", out parsed));
            Aver.AreEqual((uint)31, parsed.Era);
            Aver.AreEqual(2, parsed.Authority);
            Aver.AreEqual((ulong)3123, parsed.Counter);

            Aver.IsFalse(GDID.TryParse("-1:2:3123", out parsed));
            Aver.IsFalse(GDID.TryParse("1:18:3123", out parsed));
            Aver.IsFalse(GDID.TryParse(":18:3123", out parsed));
            Aver.IsFalse(GDID.TryParse("::3123", out parsed));
            Aver.IsFalse(GDID.TryParse("1::3", out parsed));
            Aver.IsFalse(GDID.TryParse("1::", out parsed));
            Aver.IsFalse(GDID.TryParse("1:-:-", out parsed));
            Aver.IsFalse(GDID.TryParse("1: : ", out parsed));

            Aver.IsFalse(GDID.TryParse("FTGEJK-IR", out parsed));


            //0x 00 00 00 00 10 00 00 00 00 00 00 4B
            //   ----era---- ------ulong -----------
            Aver.IsTrue(GDID.TryParse("0x00000000100000000000004B", out parsed));
            Aver.AreEqual(new GDID(0, 1, 0x4b), parsed);
            Aver.IsTrue(GDID.TryParse("00000000100000000000004B", out parsed));
            Aver.AreEqual(new GDID(0, 1, 0x4b), parsed);
            Aver.IsTrue(GDID.TryParse(new GDID(0, 1, 0x4b).ToHexString(), out parsed));
            Aver.AreEqual(new GDID(0, 1, 0x4b), parsed);

            GDID?nullable;

            Aver.IsTrue(GDID.TryParse("0X00000000100000000000004b", out nullable));
            Aver.IsTrue(nullable.HasValue);
            Aver.AreEqual(new GDID(0, 1, 0x4b), nullable.Value);

            Aver.IsTrue(GDID.TryParse("00000000100000000000004b", out nullable));
            Aver.IsTrue(nullable.HasValue);
            Aver.AreEqual(new GDID(0, 1, 0x4b), nullable.Value);

            Aver.IsTrue(GDID.TryParse(new GDID(0, 1, 0x4b).ToHexString(), out nullable));
            Aver.IsTrue(nullable.HasValue);
            Aver.AreEqual(new GDID(0, 1, 0x4b), nullable.Value);

            Aver.IsFalse(GDID.TryParse("0x0001000000000000", out nullable));//too short
            Aver.IsFalse(nullable.HasValue);

            Aver.IsFalse(GDID.TryParse("0x00000303030303003031000000000000", out nullable));//too long
            Aver.IsFalse(nullable.HasValue);
        }