Skip to content

Commit

Permalink
File intermediate buffers should be flushed on all flushes.
Browse files Browse the repository at this point in the history
  • Loading branch information
koculu committed Aug 3, 2022
1 parent cfaad22 commit b2ed7d1
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private void AppendBlock(DecompressedBlock nextBlock)
CompressedBlockPositions.Add(offset);
CompressedBlockLengths.Add(compressedBytes.Length);
FileStream.Write(compressedBytes);
FileStream.Flush();
FileStream.Flush(true);
DecompressedBlocks.TryRemove(nextBlock.BlockIndex, out _);
++NextBlockIndex;
LastBlockLength = 0;
Expand Down Expand Up @@ -219,7 +219,7 @@ public void Close()
DecompressedBlocks.Clear();
if (FileStream == null)
return;
FileStream.Flush();
FileStream.Flush(true);
FileStream.Dispose();
FileStream = null;
if (Writable)
Expand Down Expand Up @@ -274,7 +274,7 @@ void WriteCompressedBlockPositionsAndLengths()
}
bw.Write(positions.Count);
bw.Write(offset);
FileStream.Flush();
FileStream.Flush(true);
}

(List<long> positions, List<int> lengths) ReadCompressedBlockPositionsAndLengths()
Expand Down
4 changes: 2 additions & 2 deletions src/ZoneTree/Segments/Disk/FileRandomAccessDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public long AppendBytesReturnPosition(byte[] bytes)
{
var pos = FileStream.Position;
FileStream.Write(bytes);
FileStream.Flush();
FileStream.Flush(true);
return pos;
}

Expand Down Expand Up @@ -81,7 +81,7 @@ public void Close()
{
if (FileStream == null)
return;
FileStream.Flush();
FileStream.Flush(true);
FileStream.Dispose();
FileStream = null;
if (Writable)
Expand Down
2 changes: 1 addition & 1 deletion src/ZoneTree/WAL/CompressedFileStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void WriteTail()
BinaryChunkWriter.Write(tailBlock.Length);
var bytes = tailBlock.GetBytes(0, tailBlock.Length);
BinaryChunkWriter.Write(bytes);
TailStream.Flush();
TailStream.Flush(true);
LastWrittenTailIndex = tailBlock.BlockIndex;
LastWrittenTailLength = tailBlock.Length;
}
Expand Down
4 changes: 2 additions & 2 deletions src/ZoneTree/WAL/FileSystemWriteAheadLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void Dispose()
private void Flush()
{
if (FileStream.CanWrite)
FileStream.Flush();
FileStream.Flush(true);
}

public long ReplaceWriteAheadLog(TKey[] keys, TValue[] values, bool disableBackup)
Expand All @@ -147,7 +147,7 @@ public long ReplaceWriteAheadLog(TKey[] keys, TValue[] values, bool disableBacku
FilePath + ".full",
() =>
{
FileStream.Flush();
FileStream.Flush(true);
FileStream.Seek(0, SeekOrigin.Begin);
var existingLength = (int)FileStream.Length;
var bytes = new byte[existingLength];
Expand Down
10 changes: 5 additions & 5 deletions src/ZoneTree/WAL/IncrementalLogAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,25 @@ public static void AppendLogToTheBackupFile(string backupFile, Func<byte[]> getB
fs.Write(BitConverter.GetBytes(fs.Length));
fs.Write(BitConverter.GetBytes(fs.Length));
fs.Write(BitConverter.GetBytes(fs.Length));
fs.Flush();
fs.Flush(true);
}

// first append the additional data.
var bytes = getBytesDelegate();

fs.Seek(0, SeekOrigin.End);
fs.Write(bytes);
fs.Flush();
fs.Flush(true);

// now write the file length-stamps.
// what happens if a crash happens with partial write of the fs Length?
// To prevent that, we write and flush the length-stamp three times with separate flushes..
fs.Position = 0;
fs.Write(BitConverter.GetBytes(fs.Length));
fs.Flush();
fs.Flush(true);
fs.Write(BitConverter.GetBytes(fs.Length));
fs.Flush();
fs.Flush(true);
fs.Write(BitConverter.GetBytes(fs.Length));
fs.Flush();
fs.Flush(true);
}
}

0 comments on commit b2ed7d1

Please sign in to comment.