// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System.IO;
namespace FASTER.core
{
///
/// Implementation of checkpoint interface for local file storage
///
public class LocalLogCommitManager : ILogCommitManager
{
private string CommitFile;
///
/// Create new instance of local checkpoint manager at given base directory
///
///
public LocalLogCommitManager(string CommitFile)
{
this.CommitFile = CommitFile;
}
///
/// Perform (synchronous) commit with specified metadata
///
/// Committed begin address (for information only, not necessary to persist)
/// Address committed until (for information only, not necessary to persist)
/// Commit metadata
public void Commit(long beginAddress, long untilAddress, byte[] commitMetadata)
{
// Two phase to ensure we write metadata in single Write operation
using (var ms = new MemoryStream())
{
using (var writer = new BinaryWriter(ms))
{
writer.Write(commitMetadata.Length);
writer.Write(commitMetadata);
}
using (var writer = new BinaryWriter(new FileStream(CommitFile, FileMode.OpenOrCreate)))
{
writer.Write(ms.ToArray());
writer.Flush();
}
}
}
///
/// Retrieve commit metadata
///
/// Metadata, or null if invalid
public byte[] GetCommitMetadata()
{
if (!File.Exists(CommitFile))
return null;
using (var reader = new BinaryReader(new FileStream(CommitFile, FileMode.Open)))
{
var len = reader.ReadInt32();
return reader.ReadBytes(len);
}
}
}
}