Beispiel #1
0
        internal StreamMetadata(int?maxCount, TimeSpan?maxAge, int?truncateBefore, TimeSpan?cacheControl,
                                StreamAcl acl, IDictionary <string, JToken> customMetadata = null)
        {
            if (maxCount <= 0)
            {
                throw new ArgumentOutOfRangeException("maxCount", string.Format("{0} should be positive value.", SystemMetadata.MaxCount));
            }
            if (maxAge <= TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException("maxAge", string.Format("{0} should be positive time span.", SystemMetadata.MaxAge));
            }
            if (truncateBefore < 0)
            {
                throw new ArgumentOutOfRangeException("truncateBefore", string.Format("{0} should be non negative value.", SystemMetadata.TruncateBefore));
            }

            if (cacheControl <= TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException("cacheControl", string.Format("{0} should be positive time span.", SystemMetadata.CacheControl));
            }

            MaxCount        = maxCount;
            MaxAge          = maxAge;
            TruncateBefore  = truncateBefore;
            CacheControl    = cacheControl;
            Acl             = acl;
            _customMetadata = customMetadata ?? Empty.CustomStreamMetadata;
        }
Beispiel #2
0
        internal StreamMetadata(long?maxCount, TimeSpan?maxAge, long?truncateBefore, TimeSpan?cacheControl,
                                StreamAcl acl, IDictionary <string, JToken> customMetadata = null)
        {
            if (maxCount <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(maxCount),
                                                      $"{SystemMetadata.MaxCount} should be positive value."
                                                      );
            }
            if (maxAge <= TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(maxAge),
                                                      $"{SystemMetadata.MaxAge} should be positive time span."
                                                      );
            }
            if (truncateBefore < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(truncateBefore),
                                                      $"{SystemMetadata.TruncateBefore} should be non-negative value."
                                                      );
            }
            if (cacheControl <= TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(cacheControl),
                                                      $"{SystemMetadata.CacheControl} should be positive time span."
                                                      );
            }

            MaxCount        = maxCount;
            MaxAge          = maxAge;
            TruncateBefore  = truncateBefore;
            CacheControl    = cacheControl;
            Acl             = acl;
            _customMetadata = customMetadata ?? Empty.CustomStreamMetadata;
        }
Beispiel #3
0
 internal static void WriteAcl(JsonTextWriter jsonWriter, StreamAcl acl)
 {
     jsonWriter.WriteStartObject();
     WriteAclRoles(jsonWriter, SystemMetadata.AclRead, acl.ReadRoles);
     WriteAclRoles(jsonWriter, SystemMetadata.AclWrite, acl.WriteRoles);
     WriteAclRoles(jsonWriter, SystemMetadata.AclDelete, acl.DeleteRoles);
     WriteAclRoles(jsonWriter, SystemMetadata.AclMetaRead, acl.MetaReadRoles);
     WriteAclRoles(jsonWriter, SystemMetadata.AclMetaWrite, acl.MetaWriteRoles);
     jsonWriter.WriteEndObject();
 }
Beispiel #4
0
        /// <summary>
        /// Creates a <see cref="SystemSettings"/> object from a JSON string
        /// in a byte array.
        /// </summary>
        /// <param name="json">Byte array containing a JSON string.</param>
        /// <returns>A <see cref="SystemSettings"/> object.</returns>
        public static SystemSettings FromJsonBytes(byte[] json)
        {
            using (var reader = new JsonTextReader(new StreamReader(new MemoryStream(json)))) {
                Check(reader.Read(), reader);
                Check(JsonToken.StartObject, reader);

                StreamAcl userStreamAcl   = null;
                StreamAcl systemStreamAcl = null;

                while (true)
                {
                    Check(reader.Read(), reader);
                    if (reader.TokenType == JsonToken.EndObject)
                    {
                        break;
                    }
                    Check(JsonToken.PropertyName, reader);
                    var name = (string)reader.Value;
                    switch (name)
                    {
                    case SystemMetadata.UserStreamAcl:
                        userStreamAcl = StreamMetadata.ReadAcl(reader);
                        break;

                    case SystemMetadata.SystemStreamAcl:
                        systemStreamAcl = StreamMetadata.ReadAcl(reader);
                        break;

                    default: {
                        Check(reader.Read(), reader);
                        // skip
                        JToken.ReadFrom(reader);
                        break;
                    }
                    }
                }

                return(new SystemSettings(userStreamAcl, systemStreamAcl));
            }
        }
 /// <summary>
 /// Constructs a new <see cref="SystemSettings"/>.
 /// </summary>
 /// <param name="userStreamAcl"></param>
 /// <param name="systemStreamAcl"></param>
 public SystemSettings(StreamAcl userStreamAcl, StreamAcl systemStreamAcl)
 {
     UserStreamAcl   = userStreamAcl;
     SystemStreamAcl = systemStreamAcl;
 }
Beispiel #6
0
 /// <summary>
 /// Creates a <see cref="StreamMetadata" /> with the specified parameters.
 /// </summary>
 /// <param name="maxCount">The maximum number of events allowed in the stream.</param>
 /// <param name="maxAge">The maximum age of events allowed in the stream.</param>
 /// <param name="truncateBefore">The event number from which previous events can be scavenged.</param>
 /// <param name="cacheControl">The amount of time for which the stream head is cachable.</param>
 /// <param name="acl">The access control list for the stream.</param>
 /// <returns></returns>
 public static StreamMetadata Create(int?maxCount = null, TimeSpan?maxAge = null, int?truncateBefore = null, TimeSpan?cacheControl = null, StreamAcl acl = null)
 {
     return(new StreamMetadata(maxCount, maxAge, truncateBefore, cacheControl, acl));
 }
Beispiel #7
0
        /// <summary>
        /// Builds a <see cref="StreamMetadata" /> object from a byte array
        /// containing stream metadata.
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static StreamMetadata FromJsonBytes(byte[] json)
        {
            using (var reader = new JsonTextReader(new StreamReader(new MemoryStream(json))))
            {
                Check(reader.Read(), reader);
                Check(JsonToken.StartObject, reader);

                int?      maxCount       = null;
                TimeSpan? maxAge         = null;
                int?      truncateBefore = null;
                TimeSpan? cacheControl   = null;
                StreamAcl acl            = null;
                Dictionary <string, JToken> customMetadata = null;

                while (true)
                {
                    Check(reader.Read(), reader);
                    if (reader.TokenType == JsonToken.EndObject)
                    {
                        break;
                    }
                    Check(JsonToken.PropertyName, reader);
                    var name = (string)reader.Value;
                    switch (name)
                    {
                    case SystemMetadata.MaxCount:
                    {
                        Check(reader.Read(), reader);
                        Check(JsonToken.Integer, reader);
                        maxCount = (int)(long)reader.Value;
                        break;
                    }

                    case SystemMetadata.MaxAge:
                    {
                        Check(reader.Read(), reader);
                        Check(JsonToken.Integer, reader);
                        maxAge = TimeSpan.FromSeconds((long)reader.Value);
                        break;
                    }

                    case SystemMetadata.TruncateBefore:
                    {
                        Check(reader.Read(), reader);
                        Check(JsonToken.Integer, reader);
                        truncateBefore = (int)(long)reader.Value;
                        break;
                    }

                    case SystemMetadata.CacheControl:
                    {
                        Check(reader.Read(), reader);
                        Check(JsonToken.Integer, reader);
                        cacheControl = TimeSpan.FromSeconds((long)reader.Value);
                        break;
                    }

                    case SystemMetadata.Acl:
                    {
                        acl = ReadAcl(reader);
                        break;
                    }

                    default:
                    {
                        if (customMetadata == null)
                        {
                            customMetadata = new Dictionary <string, JToken>();
                        }
                        Check(reader.Read(), reader);
                        var jToken = JToken.ReadFrom(reader);
                        customMetadata.Add(name, jToken);
                        break;
                    }
                    }
                }
                return(new StreamMetadata(maxCount, maxAge, truncateBefore, cacheControl, acl, customMetadata));
            }
        }
 /// <summary>
 /// Constructs a new <see cref="SystemSettings"/>.
 /// </summary>
 /// <param name="userStreamAcl"></param>
 /// <param name="systemStreamAcl"></param>
 public SystemSettings(StreamAcl userStreamAcl, StreamAcl systemStreamAcl)
 {
     UserStreamAcl = userStreamAcl;
     SystemStreamAcl = systemStreamAcl;
 }