/// <summary> /// Gets the next set of bindings from the buffer stream. /// </summary> /// <returns> /// A set of bindings. /// </returns> public BindingSet Next() { if (!HasNext) { throw new InvalidOperationException("No new bindings are available!"); } // // see if we need to look in the main-memory array, or in the secondary-memory file if (m_posCurrent >= m_memRangeMin) { return(m_memBuffer[m_posCurrent++ % m_memSize]); } else { #if DEBUG m_planOperator.StartIOWork(); #endif // // very important to flush the file buffer here: not everything from the writer // might be flushed to disk, so the reader potentially does not see the full file m_fileWriter.Flush(); m_posCurrent++; var b = BindingSerializer.Read(m_fileReader); #if DEBUG m_planOperator.StopIOWork(); #endif return(b); } }
/// <summary> /// Appends a given binding set to the end of the buffer. /// </summary> /// <param name="value">The binding set.</param> public void Add(BindingSet value) { #if DEBUG m_planOperator.StartCPUWork(); #endif // // the position in the memory array where the new binding set will be inserted var memPos = m_count % m_memSize; if (m_memRangeMax - m_memRangeMin + 1 < m_memSize) { // // if the entire buffer still fits in main memory, we can simply insert the new // binding set and be done with it m_memBuffer[memPos] = value; m_memRangeMax++; } else { // // if the buffer does not fit in memory (anymore), we need to write the tail of // the memory array to a secondary-memory file, and insert the new binding set on // the position of the tail #if DEBUG m_planOperator.StartIOWork(); #endif if (!m_fileCreated) { m_fileName = String.Format("~{0}.tmp", Generator.GetRandomFilename(12)); m_fileOutStream = File.Open(m_fileName, FileMode.Create, FileAccess.Write, FileShare.Read); m_fileInStream = File.Open(m_fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); m_fileWriter = new BinaryWriter(m_fileOutStream); m_fileReader = new BinaryReader(m_fileInStream); m_fileCreated = true; m_fileRangeMin = m_memRangeMin; m_fileRangeMax = m_fileRangeMin - 1; } BindingSerializer.Write(m_fileWriter, m_memBuffer[memPos]); m_memBuffer[memPos] = value; m_memRangeMin++; m_memRangeMax++; m_fileRangeMax++; #if DEBUG m_planOperator.StopIOWork(); #endif } m_count++; #if DEBUG m_planOperator.StopCPUWork(); #endif }