Exemple #1
0
        public void Dispose()
        {
            State = IncomingEdgeSorterState.Closed;
            _cancel.Cancel();
            _chunks.Clear();

            foreach (var file in _chunkFiles)
            {
                File.Delete(file);
            }
        }
Exemple #2
0
        public IEnumerable <IncomingEdge> GetSorted()
        {
            if (State == IncomingEdgeSorterState.Closed)
            {
                throw new InvalidOperationException("Sorter is closed.");
            }

            if (State == IncomingEdgeSorterState.Writing)
            {
                State = IncomingEdgeSorterState.Reading;
            }

            var pos      = (Count - 1);
            var posChunk = (int)((pos / ChunkLength) % ChunkCount);
            var posSlot  = (int)(pos % ChunkLength);



            //sort the chunk of the last inserted edge.
            Array.Sort <IncomingEdge>(_current, 0, posSlot + 1, IncomingEdgeComparer.Instance);



            //we read the chunks from memory
            var inMemoryChunks = _chunks                                                   //take all chunks
                                 .Where(_ => _sortTasks.ContainsKey(_.Key))                //filter all unused
                                 .Select(_ => _.Value)                                     //take the values
                                 .Union(Enumerable.Repeat(_current.Take(posSlot + 1), 1)); //union them with the current one

            //we wait for the sort task to finish
            Task.WaitAll(_sortTasks.Values.ToArray());

            //We cancel the store process of the current locations
            foreach (var location in _storeTasks.Keys)
            {
                location.Cancel();
            }

            //now we wait until all store tasks are canceled.
            Task.WaitAll(_storeTasks.Values.ToArray());

            var filesToRead = _chunkFiles.Except(_storeTasks.Keys.Select(_ => _.CurrentFile));

            Console.WriteLine(string.Join(", ", _chunkFiles));

            return(ReadFromSortedChunks(inMemoryChunks, filesToRead));
        }