コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JsonStoreWriter"/> 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="createSubdirectory">If true, a numbered subdirectory is created for this store.</param>
        /// <param name="extension">The extension for the underlying file.</param>
        public JsonStoreWriter(string name, string path, bool createSubdirectory = true, string extension = DefaultExtension)
            : base(extension)
        {
            ushort id = 0;

            this.Name = name;
            this.Path = System.IO.Path.GetFullPath(path);
            if (createSubdirectory)
            {
                // if the root directory already exists, look for the next available id
                if (Directory.Exists(this.Path))
                {
                    var existingIds = Directory.EnumerateDirectories(this.Path, this.Name + ".????")
                                      .Select(d => d.Split('.').Last())
                                      .Where(n => ushort.TryParse(n, out ushort i))
                                      .Select(n => ushort.Parse(n));
                    id = (ushort)(existingIds.Count() == 0 ? 0 : existingIds.Max() + 1);
                }

                this.Path = System.IO.Path.Combine(this.Path, $"{this.Name}.{id:0000}");
            }

            if (!Directory.Exists(this.Path))
            {
                Directory.CreateDirectory(this.Path);
            }

            string dataPath = System.IO.Path.Combine(this.Path, StoreCommon.GetDataFileName(this.Name) + this.Extension);

            this.streamWriter = File.CreateText(dataPath);
            this.jsonWriter   = new JsonTextWriter(this.streamWriter);
            this.jsonWriter.WriteStartArray();
        }
コード例 #2
0
ファイル: JsonStoreReader.cs プロジェクト: maria-chang/psi
        /// <summary>
        /// Seek to envelope in stream according to specified replay descriptor.
        /// </summary>
        /// <param name="descriptor">The replay descriptor.</param>
        public void Seek(ReplayDescriptor descriptor)
        {
            this.descriptor = descriptor;

            // load data
            string dataPath = System.IO.Path.Combine(this.Path, StoreCommon.GetDataFileName(this.Name) + this.Extension);

            this.streamReader?.Dispose();
            this.streamReader     = File.OpenText(dataPath);
            this.jsonReader       = new JsonTextReader(this.streamReader);
            this.validatingReader = new JSchemaValidatingReader(this.jsonReader)
            {
                Schema = this.DataSchema
            };
            this.validatingReader.ValidationEventHandler += (s, e) => throw new InvalidDataException(e.Message);

            // iterate through data store until we either reach the end or we find the start of the replay descriptor
            while (this.validatingReader.Read())
            {
                // data stores are arrays of messages, messages start as objects
                if (this.validatingReader.TokenType == JsonToken.StartObject)
                {
                    // read envelope
                    if (!this.validatingReader.Read() || !this.ReadEnvelope(out this.envelope))
                    {
                        throw new InvalidDataException("Messages must be an ordered object: {\"Envelope\": <Envelope>, \"Data\": <Data>}. Deserialization needs to read the envelope before the data to know what type of data to deserialize.");
                    }

                    if (this.descriptor.Interval.Left < (descriptor.UseOriginatingTime ? this.envelope.OriginatingTime : this.envelope.Time))
                    {
                        // found start of interval
                        break;
                    }

                    // skip data
                    if (!this.ReadData(out this.data))
                    {
                        throw new InvalidDataException("Messages must be an ordered object: {\"Envelope\": <Envelope>, \"Data\": <Data>}. Deserialization needs to read the envelope before the data to know what type of data to deserialize.");
                    }
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JsonStoreWriter"/> 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="createSubdirectory">If true, a numbered subdirectory is created for this store</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 JsonStoreWriter(string name, string path, string dataSchemaString, bool createSubdirectory = true, string extension = DefaultExtension, IDictionary <Uri, string> preloadSchemas = null)
            : base(dataSchemaString, extension, preloadSchemas)
        {
            ushort id = 0;

            this.Name = name;
            this.Path = System.IO.Path.GetFullPath(path);
            if (createSubdirectory)
            {
                // if the root directory already exists, look for the next available id
                if (Directory.Exists(this.Path))
                {
                    var existingIds = Directory.EnumerateDirectories(this.Path, this.Name + ".????")
                                      .Select(d => d.Split('.').Last())
                                      .Where(n => ushort.TryParse(n, out ushort i))
                                      .Select(n => ushort.Parse(n));
                    id = (ushort)(existingIds.Count() == 0 ? 0 : existingIds.Max() + 1);
                }

                this.Path = System.IO.Path.Combine(this.Path, $"{this.Name}.{id:0000}");
            }

            if (!Directory.Exists(this.Path))
            {
                Directory.CreateDirectory(this.Path);
            }

            string dataPath = System.IO.Path.Combine(this.Path, StoreCommon.GetDataFileName(this.Name) + this.Extension);

            this.streamWriter            = File.CreateText(dataPath);
            this.jsonWriter              = new JsonTextWriter(this.streamWriter);
            this.validatingWriter        = new JSchemaValidatingWriter(this.jsonWriter);
            this.validatingWriter.Schema = this.DataSchema;
            this.validatingWriter.ValidationEventHandler += (s, e) => throw new InvalidDataException(e.Message);
            this.validatingWriter.WriteStartArray();
        }