コード例 #1
0
        /// <summary>
        /// Presorts the provided enumeration in batches and then performs an optimized insert on the resulting set(s).
        /// </summary>
        /// <param name="unorderedItems">The items to insert</param>
        /// <param name="allowUpdates">True to overwrite any existing records</param>
        /// <returns>The total number of records inserted or updated</returns>
        public int AddRange(IEnumerable <KeyValuePair <TKey, TValue> > unorderedItems, bool allowUpdates)
        {
            int total = 0;
            int count = 0;

            KeyValuePair <TKey, TValue>[]   items      = new KeyValuePair <TKey, TValue> [2048];
            KeyValueComparer <TKey, TValue> kvcomparer = new KeyValueComparer <TKey, TValue>(_keyComparer);

            foreach (KeyValuePair <TKey, TValue> item in unorderedItems)
            {
                if (count == 0x010000)
                {
                    MergeSort.Sort(items, kvcomparer);
                    total += AddRangeSorted(items, allowUpdates);
                    Array.Clear(items, 0, items.Length);
                    count = 0;
                }

                if (count == items.Length)
                {
                    Array.Resize(ref items, items.Length * 2);
                }
                items[count++] = item;
            }

            if (count > 0)
            {
                if (count != items.Length)
                {
                    Array.Resize(ref items, count);
                }

                MergeSort.Sort(items, kvcomparer);
                total += AddRangeSorted(items, allowUpdates);
            }
            return(total);
        }
        private IEnumerable <T> PagedAndOrdered()
        {
            T[] items = new T[Math.Min(InMemoryLimit, 2048)];
            using (DisposingList resources = new DisposingList())
            {
                List <IEnumerable <T> > orderedSet = new List <IEnumerable <T> >();
                int count = 0;

                foreach (T item in _unordered)
                {
                    if (_memoryLimit > 0 && count == _memoryLimit)
                    {
                        if (_serializer != null)
                        {
                            TempFile temp = new TempFile();
                            resources.Add(temp);
                            Stream io;
                            resources.Add(io = new FileStream(temp.TempPath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read));

                            MergeSort.Sort(items, _comparer);
                            foreach (T i in items)
                            {
                                _serializer.WriteTo(i, io);
                            }
                            io.Position = 0;
                            orderedSet.Add(Read(temp, io));
                        }
                        else
                        {
                            T[] copy;
                            MergeSort.Sort(items, out copy, 0, items.Length, _comparer);
                            orderedSet.Add(items);
                            items = copy;
                        }
                        Array.Clear(items, 0, items.Length);
                        count = 0;
                    }

                    if (count == items.Length)
                    {
                        Array.Resize(ref items, Math.Min(InMemoryLimit, items.Length * 2));
                    }
                    items[count++] = item;
                }

                if (count != items.Length)
                {
                    Array.Resize(ref items, count);
                }

                MergeSort.Sort(items, _comparer);

                IEnumerable <T> result;
                if (orderedSet.Count == 0)
                {
                    result = items;
                }
                else
                {
                    orderedSet.Add(items);
                    result = Merge(_comparer, orderedSet.ToArray());
                }

                foreach (T item in result)
                {
                    yield return(item);
                }
            }
        }