Пример #1
0
        public void Write(BinaryWriter w)
        {
            if (w is null)
            {
                throw new ArgumentNullException(nameof(w));
            }
            if (IsNull)
            {
                throw new HierarchyIdException("24002: SqlHierarchyId.Write failed because 'this' was a NULL instance.");
            }
            BitWriter bw = new BitWriter(w);

            var nodes = this._imp.GetNodes();

            for (int i = 0; i < nodes.Length; i++)
            {
                var subNodes = nodes[i];
                for (int j = 0; j < subNodes.Length; j++)
                {
                    long val = subNodes[j];

                    BitPattern p = KnownPatterns.GetPatternByValue(val);

                    bool isLast = j == (subNodes.Length - 1);

                    ulong value = p.EncodeValue(val, isLast);

                    bw.Write(value, p.BitLength);
                }
            }

            bw.Finish();
        }
Пример #2
0
        public void Read(BinaryReader r)
        {
            if (r is null)
            {
                throw new ArgumentNullException(nameof(r));
            }
            var bitR = new BitReader(r);
            List <List <long> > result = new List <List <long> >();

            while (true)
            {
                List <long> step = new List <long>();

                while (true)
                {
                    var p = KnownPatterns.GetPatternByPrefix(bitR);

                    if (p == null)
                    {
                        goto finish;
                    }

                    ulong encodedValue = bitR.Read(p.BitLength);

                    int value = p.Decode(encodedValue, out bool isLast);

                    step.Add(value);

                    if (isLast)
                    {
                        break;
                    }
                }

                result.Add(step);
            }

finish:

            this._imp       = new HierarchyId(result.Select(a => a.ToArray()).ToArray());
            this._isNotNull = !_imp.IsNull;
        }
Пример #3
0
        public static string RandomHierarhyId(Random random)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("/");
            var levels = random.Next(4);

            for (int i = 0; i < levels; i++)
            {
                var subLevels = random.Next(1, 4);
                for (int j = 0; j < subLevels; j++)
                {
                    var pattern = KnownPatterns.RandomPattern(random);
                    sb.Append(random.NextLong(pattern.MinValue, pattern.MaxValue + 1).ToString());
                    if (j < subLevels - 1)
                    {
                        sb.Append(".");
                    }
                }
                sb.Append("/");
            }

            return(sb.ToString());
        }