private DataContainerDescriptor GetDescriptorFromStore() { if (string.IsNullOrEmpty(m_settings.StorageRoot)) { throw new Exception("Storage root is not set"); } var path = Path.Combine(m_settings.StorageRoot, "descriptor.json"); if (!File.Exists(path)) { throw new Exception("Descriptor file does not exist in storage root: " + m_settings.StorageRoot); } try { using (var reader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read))) { var serializer = JsonSerializer.Create(new JsonSerializerSettings { ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor }); var file = (DataContainerDescriptorFile) serializer.Deserialize(reader, typeof(DataContainerDescriptorFile)); var storedVersion = new Version(file.DriverVersion); var minCompatible = RamDriverFactory.MinCompatibleStoreVersion(); if (minCompatible.CompareTo(storedVersion) > 0) { throw new Exception(string.Format( "Version of storage descriptor is too old. Found: {0}, minimum supported: {1}", file.DriverVersion, minCompatible)); } if (file.DataContainerDescriptor == null) { throw new Exception("Descriptor file is empty"); } return(file.DataContainerDescriptor); } } catch (Exception e) { throw new InvalidOperationException("Could not read descriptor from store: " + m_settings.StorageRoot, e); } }
private static DataContainerStats ReadStatsFromStore(string storageRoot) { var path = Path.Combine(storageRoot, "stats.json"); if (!File.Exists(path)) { return(new DataContainerStats()); } using (var reader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))) { var serializer = JsonSerializer.Create( new JsonSerializerSettings { ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor }); var file = (DataContainerStatsFile)serializer.Deserialize(reader, typeof(DataContainerStatsFile)); var storedVersion = new Version(file.DriverVersion); var minCompatible = RamDriverFactory.MinCompatibleStoreVersion(); if (minCompatible.CompareTo(storedVersion) > 0) { throw new Exception( string.Format( "Version of storage stats is too old. Found: {0}, minimum supported: {1}", file.DriverVersion, minCompatible)); } if (file.DataContainerStats == null) { throw new Exception("Stats file is empty"); } return(file.DataContainerStats); } }
public static void WriteDescriptorToStore(DataContainerDescriptor descriptor, string storageRoot) { if (string.IsNullOrEmpty(storageRoot)) { throw new ArgumentNullException(storageRoot); } if (descriptor == null) { throw new ArgumentNullException("descriptor"); } Directory.CreateDirectory(storageRoot); var file = new DataContainerDescriptorFile(RamDriverFactory.CurrentStoreVersion(), descriptor); using (var writer = new StreamWriter( new FileStream(Path.Combine(storageRoot, "descriptor.json"), FileMode.Create, FileAccess.ReadWrite))) { var serializer = new JsonSerializer(); serializer.Serialize(writer, file); writer.Flush(); } }