/// <summary> /// Initializes a new instance of the <see cref="JsonStoreReader"/> class. /// </summary> /// <param name="name">The name of the application that generated the persisted files, or the root name of the files</param> /// <param name="path">The directory in which the main persisted file resides or will reside, or null to create a volatile data store</param> /// <param name="dataSchemaString">JSON schema used to validate data stream.</param> /// <param name="extension">The extension for the underlying file.</param> /// <param name="preloadSchemas">Dictionary of URis to JSON schemas to preload before validating any JSON. Would likely include schemas references by the catalog and data schemas.</param> public JsonStoreReader(string name, string path, string dataSchemaString, string extension = DefaultExtension, IDictionary <Uri, string> preloadSchemas = null) : base(dataSchemaString, extension, preloadSchemas) { this.Name = name; this.Path = StoreCommon.GetPathToLatestVersion(name, path); // load catalog string metadataPath = System.IO.Path.Combine(this.Path, StoreCommon.GetCatalogFileName(this.Name) + this.Extension); using (var file = File.OpenText(metadataPath)) using (var reader = new JsonTextReader(file)) using (var validatingReader = new JSchemaValidatingReader(reader)) { validatingReader.Schema = this.CatalogSchema; validatingReader.ValidationEventHandler += (s, e) => throw new InvalidDataException(e.Message); this.catalog = this.Serializer.Deserialize <List <JsonStreamMetadata> >(validatingReader); } // compute originating time interval this.originatingTimeInterval = TimeInterval.Empty; foreach (var metadata in this.catalog) { var metadataTimeInterval = new TimeInterval(metadata.FirstMessageOriginatingTime, metadata.LastMessageOriginatingTime); this.originatingTimeInterval = TimeInterval.Coverage(new TimeInterval[] { this.originatingTimeInterval, metadataTimeInterval }); } }
private void WriteCatalog() { string metadataPath = System.IO.Path.Combine(this.Path, StoreCommon.GetCatalogFileName(this.Name) + this.Extension); using (var file = File.CreateText(metadataPath)) using (var writer = new JsonTextWriter(file)) { this.Serializer.Serialize(writer, this.catalog.Values.ToList()); } }
private void WriteCatalog() { string metadataPath = System.IO.Path.Combine(this.Path, StoreCommon.GetCatalogFileName(this.Name) + this.Extension); using (var file = File.CreateText(metadataPath)) using (var writer = new JsonTextWriter(file)) using (var validatingWriter = new JSchemaValidatingWriter(writer)) { validatingWriter.Schema = this.CatalogSchema; validatingWriter.ValidationEventHandler += (s, e) => throw new InvalidDataException(e.Message); this.Serializer.Serialize(validatingWriter, this.catalog.Values.ToList()); } }
/// <summary> /// Initializes a new instance of the <see cref="JsonStoreReader"/> class. /// </summary> /// <param name="name">The name of the application that generated the persisted files, or the root name of the files.</param> /// <param name="path">The directory in which the main persisted file resides or will reside, or null to create a volatile data store.</param> /// <param name="extension">The extension for the underlying file.</param> public JsonStoreReader(string name, string path, string extension = DefaultExtension) : base(extension) { this.Name = name; this.Path = StoreCommon.GetPathToLatestVersion(name, path); // load catalog string metadataPath = System.IO.Path.Combine(this.Path, StoreCommon.GetCatalogFileName(this.Name) + this.Extension); using (var file = File.OpenText(metadataPath)) using (var reader = new JsonTextReader(file)) { this.catalog = this.Serializer.Deserialize <List <JsonStreamMetadata> >(reader); } // compute originating time interval this.originatingTimeInterval = TimeInterval.Empty; foreach (var metadata in this.catalog) { var metadataTimeInterval = new TimeInterval(metadata.FirstMessageOriginatingTime, metadata.LastMessageOriginatingTime); this.originatingTimeInterval = TimeInterval.Coverage(new TimeInterval[] { this.originatingTimeInterval, metadataTimeInterval }); } }
/// <summary> /// Indicates whether the specified store file exists. /// </summary> /// <param name="name">The name of the store to check.</param> /// <param name="path">The path of the store to check.</param> /// <returns>Returns true if the store exists.</returns> public static bool Exists(string name, string path) { return((path == null) ? InfiniteFileReader.IsActive(StoreCommon.GetCatalogFileName(name), path) : StoreCommon.TryGetPathToLatestVersion(name, path, out _)); }