Пример #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 ListSegment(DirtyTracker tracker, ArraySegment <byte> originalBytes, int length)
 {
     this.originalBytes = originalBytes;
     this.formatter     = Formatters.Formatter <T> .Default;
     this.length        = length;
     this.tracker       = tracker.CreateChild();
 }
Пример #3
0
        internal static GroupingSegment <TKey, TElement> Create(DirtyTracker tracker, byte[] bytes, int offset, out int byteSize)
        {
            var segment = new GroupingSegment <TKey, TElement>();

            tracker         = tracker.CreateChild();
            segment.tracker = tracker;

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

            int keySize;

            segment.key = keyFormatter.Deserialize(ref bytes, offset, tracker, out keySize);

            segment.hashCode = BinaryUtil.ReadInt32(ref bytes, offset + keySize);

            int listSize;

            segment.elements = listFormatter.Deserialize(ref bytes, offset + keySize + 4, tracker, out listSize);

            byteSize = keySize + 4 + listSize;
            segment.originalBytes = new ArraySegment <byte>(bytes, offset, byteSize);

            return(segment);
        }
Пример #4
0
        DictionarySegment(DirtyTracker tracker, ArraySegment <byte> originalBytes)
        {
            tracker            = tracker.CreateChild();
            this.tracker       = tracker;
            this.originalBytes = originalBytes;

            var bytes  = originalBytes.Array;
            var offset = originalBytes.Offset + 4;

            this.count = BinaryUtil.ReadInt32(ref bytes, offset);
            offset    += 4;

            var intListFormatter   = Formatter <IList <int> > .Default;
            var entryListFormatter = Formatter <IList <DictionaryEntry <TKey, TValue> > > .Default;

            int size;

            this.buckets = intListFormatter.Deserialize(ref bytes, offset, tracker, out size);
            offset      += size;

            this.entries = entryListFormatter.Deserialize(ref bytes, offset, tracker, out size);
            offset      += size;

            // new size
            if (buckets.Count == 0)
            {
                var capacity = HashHelpers.GetPrime(0);
                Resize(capacity);
            }

            this.comparer = ZeroFormatterEqualityComparer <TKey> .Default;

            this.freeList  = -1;
            this.freeCount = 0;
        }
Пример #5
0
        internal DictionarySegment(DirtyTracker tracker, int size)
        {
            tracker      = tracker.CreateChild();
            this.tracker = tracker;

            this.count = 0;

            if (size == 0)
            {
                size = HashHelpers.GetPrime(size);
            }

            var newBuckets = new int[size];

            for (int i = 0; i < newBuckets.Length; i++)
            {
                newBuckets[i] = -1;
            }
            this.buckets = newBuckets;

            this.entries  = new DictionaryEntry <TKey, TValue> [size];
            this.comparer = ZeroFormatterEqualityComparer <TKey> .Default;

            this.freeList  = -1;
            this.freeCount = 0;
        }
Пример #6
0
 internal ListSegment(DirtyTracker tracker, int length)
 {
     this.isAllCached = true;
     this.cache       = new T[length];
     this.length      = length;
     this.tracker     = tracker.CreateChild();
     this.formatter   = Formatters.Formatter <T> .Default;
 }
Пример #7
0
        public CacheSegment(DirtyTracker tracker, ArraySegment <byte> originalBytes)
        {
            this.tracker         = tracker.CreateChild();
            this.state           = SegmentState.Original;
            this.serializedBytes = originalBytes;
            this.cached          = default(T);

            if (originalBytes.Array == null)
            {
                this.state = SegmentState.Dirty;
            }
        }
Пример #8
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);
            }
        }