Пример #1
0
        public static LookupSegment <TKey, TElement> Create(DirtyTracker tracker, byte[] bytes, int offset, out int byteSize)
        {
            tracker = tracker.CreateChild();

            var byteSizeOrCount = BinaryUtil.ReadInt32(ref bytes, offset);

            if (byteSizeOrCount == -1)
            {
                byteSize = 4;
                return(null);
            }

            var segment = new LookupSegment <TKey, TElement>();

            byteSize      = byteSizeOrCount;
            segment.count = BinaryUtil.ReadInt32(ref bytes, offset + 4);

            var formatter = Formatter <IList <IList <GroupingSegment <TKey, TElement> > > > .Default;
            int _;

            segment.groupings = formatter.Deserialize(ref bytes, offset + 8, tracker, out _);

            segment.tracker       = tracker;
            segment.originalBytes = new ArraySegment <byte>(bytes, offset, byteSize);

            return(segment);
        }
Пример #2
0
        public static LookupSegment <TKey, TElement> Create(DirtyTracker tracker, byte[] bytes, int offset, LookupSegmentMode mode, out int byteSize)
        {
            tracker = tracker.CreateChild();

            var byteSizeOrCount = BinaryUtil.ReadInt32(ref bytes, offset);

            if (byteSizeOrCount == -1)
            {
                byteSize = 4;
                return(null);
            }

            var segment = new LookupSegment <TKey, TElement>();

            if (mode == LookupSegmentMode.LazyAll)
            {
                byteSize      = byteSizeOrCount;
                segment.count = BinaryUtil.ReadInt32(ref bytes, offset + 4);

                var formatter = Formatter <IList <IList <GroupingSegment <TKey, TElement> > > > .Default;
                int _;
                segment.groupings = formatter.Deserialize(ref bytes, offset + 8, tracker, out _);

                segment.tracker       = tracker;
                segment.originalBytes = new ArraySegment <byte>(bytes, offset, byteSize);
                segment.mode          = LookupSegmentMode.LazyAll;

                return(segment);
            }
            else
            {
                var originalOffset = offset;

                var keyFormatter    = Formatter <TKey> .Default;
                var valuesFormatter = Formatter <IList <TElement> > .Default;

                segment.tracker   = tracker;
                segment.groupings = new List <IList <GroupingSegment <TKey, TElement> > >(byteSizeOrCount);
                for (int i = 0; i < byteSizeOrCount; i++)
                {
                    segment.groupings.Add(null); // null clear
                }

                int size = 0;
                byteSize = 4;
                offset  += 4;
                for (int i = 0; i < byteSizeOrCount; i++)
                {
                    var key = keyFormatter.Deserialize(ref bytes, offset, tracker, out size);
                    byteSize += size;
                    offset   += size;

                    var values = valuesFormatter.Deserialize(ref bytes, offset, tracker, out size);
                    byteSize += size;
                    offset   += size;

                    var group = segment.GetGrouping(key, true);
                    foreach (var item in values)
                    {
                        group.Add(item);
                    }
                }

                segment.originalBytes = new ArraySegment <byte>(bytes, originalOffset, byteSize);
                segment.mode          = LookupSegmentMode.Immediate;

                return(segment);
            }
        }