Beispiel #1
0
        /// <summary>
        /// Creates the temporary file storing the input set, and sorts this file using the given
        /// sorting order.
        /// </summary>
        private void CreateTmpFile()
        {
            var memSize = 256 * 1024;

            using (var bw = new BinaryWriter(File.Open(m_tmpFile, FileMode.Create, FileAccess.Write, FileShare.None))) {
                if (m_input.HasNext)
                {
                    var pk     = m_input.Peek();
                    var pkSize = (uint)pk.Count * Database.MEMORY_SIZE_BINDING;
                    var m      = m_planOperator.MemorySize / pkSize;
                    if (m > Int32.MaxValue)
                    {
                        memSize = Int32.MaxValue;
                    }
                    else
                    {
                        memSize = Convert.ToInt32(m);
                    }
                }

                //
                // the first step involves simply consuming the input stream in full, and writing
                // it to the temporary file. note that it might be better in the future to first
                // attempt to fit the input stream in memory.

                while (m_input.HasNext)
                {
                    var next = m_input.Next();
                    for (int i = 0; i < m_sortOrder.Length; i++)
                    {
                        var b = next[m_sortOrder[i]];
                        bw.Write(b.Value.InternalValue);
                    }
                }
            }

            //
            // collect the sort order

            var order = new int[m_sortOrder.Length];

            for (int i = 0; i < order.Length; i++)
            {
                order[i] = i;
            }

            //
            // call the external sorting algorithm to sort the file for us

            GC.Collect();
            ExternalSort.SortFile(m_tmpFile, m_sortOrder.Length, memSize, order);
            GC.Collect();

            //
            // expose a binary reader on the sorted file. we are now ready to start returning
            // triples.

            m_reader         = new BinaryReader(File.Open(m_tmpFile, FileMode.Open, FileAccess.Read, FileShare.Read));
            m_tmpFileCreated = true;
        }
Beispiel #2
0
 /// <summary>
 /// Builds the indexes over the database. It is assumed that insertion of triples has
 /// finished.
 /// </summary>
 public void BuildIndex()
 {
     if (m_state == State.Open)
     {
         if (!m_isBatchInserting)
         {
             GC.Collect();
             ExternalSort.SortBucket(m_sBucket, MAX_TRIPLES_IN_MEMORY);
             GC.Collect();
             ExternalSort.SortBucket(m_pBucket, MAX_TRIPLES_IN_MEMORY);
             GC.Collect();
             ExternalSort.SortBucket(m_oBucket, MAX_TRIPLES_IN_MEMORY);
             GC.Collect();
             m_index.Build();
             GC.Collect();
             MinifyBuckets();
             GC.Collect();
             ComputeStatistics();
             GC.Collect();
         }
     }
     else
     {
         throw new InvalidOperationException("Database is not in open state!");
     }
 }
Beispiel #3
0
        public static void TestMergeKSortedList()
        {
            int[] sortedList1 = { 1, 4, 8, 12, 20 };
            int[] sortedList2 = { -1, 0, 1, 2 };
            int[] sortedList3 = { -5, -4, -3, -2 };
            int[] sortedList4 = { 0, 0, 0, 10, 21 };

            var result = ExternalSort.MergeKSortedList(new List <int[]>
            {
                sortedList1,
                sortedList2,
                sortedList3,
                sortedList4
            });
        }