public LiteFileInfo(string id, string filename)
        {
            if (!IdPattern.IsMatch(id))
            {
                throw LiteException.InvalidFormat("FileId", id);
            }

            this.Id         = id;
            this.Filename   = Path.GetFileName(filename);
            this.MimeType   = MimeTypeConverter.GetMimeType(this.Filename);
            this.Length     = 0;
            this.Chunks     = 0;
            this.UploadDate = DateTime.Now;
            this.Metadata   = new BsonDocument();
        }
示例#2
0
        /// <summary>
        /// Create a new permanent index in all documents inside this collections if index not exists already. Returns true if index was created or false if already exits
        /// </summary>
        /// <param name="field">Document field name (case sensitive)</param>
        /// <param name="options">All index options</param>
        public bool EnsureIndex(string field, IndexOptions options)
        {
            if (string.IsNullOrEmpty(field))
            {
                throw new ArgumentNullException("field");
            }
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
            if (field == "_id")
            {
                return(false);                // always exists
            }
            if (!CollectionIndex.IndexPattern.IsMatch(field))
            {
                throw LiteException.InvalidFormat("IndexField", field);
            }

            return(_engine.EnsureIndex(_name, field, options));
        }
示例#3
0
        /// <summary>
        /// Add a new collection. Check if name the not exists
        /// </summary>
        public CollectionPage Add(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            if (!CollectionPage.NamePattern.IsMatch(name))
            {
                throw LiteException.InvalidFormat("CollectionName", name);
            }

            // get header marked as dirty because I will use header after (and NewPage can get another header instance)
            var header = _pager.GetPage <HeaderPage>(0, true);

            // check limit count (8 bytes per collection = 4 to string length, 4 for uint pageID)
            if (header.CollectionPages.Sum(x => x.Key.Length + 8) + name.Length + 8 >= CollectionPage.MAX_COLLECTIONS_SIZE)
            {
                throw LiteException.CollectionLimitExceeded(CollectionPage.MAX_COLLECTIONS_SIZE);
            }

            // get new collection page (marked as dirty)
            var col = _pager.NewPage <CollectionPage>();

            // add this page to header page collection
            header.CollectionPages.Add(name, col.PageID);

            col.CollectionName = name;

            // create PK index
            var pk = _indexer.CreateIndex(col);

            pk.Field   = "_id";
            pk.Options = new IndexOptions {
                Unique = true
            };

            return(col);
        }
示例#4
0
        /// <summary>
        /// Read all properties from a type - store in a static cache - exclude: Id and [BsonIgnore]
        /// </summary>
        public static Dictionary <string, PropertyMapper> GetProperties(Type type, Func <string, string> resolvePropertyName)
        {
            var dict      = new Dictionary <string, PropertyMapper>(StringComparer.OrdinalIgnoreCase);
            var id        = GetIdProperty(type);
            var ignore    = typeof(BsonIgnoreAttribute);
            var idAttr    = typeof(BsonIdAttribute);
            var fieldAttr = typeof(BsonFieldAttribute);
            var indexAttr = typeof(BsonIndexAttribute);
            var props     = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);

            foreach (var prop in props)
            {
                // ignore indexer property
                if (prop.GetIndexParameters().Length > 0)
                {
                    continue;
                }

                // ignore not read/write
                if (!prop.CanRead || !prop.CanWrite)
                {
                    continue;
                }

                // [BsonIgnore]
                if (prop.IsDefined(ignore, false))
                {
                    continue;
                }

                // check if property has [BsonField]
                var bsonField = prop.IsDefined(fieldAttr, false);

                // create getter/setter IL function
                var getter = CreateGetMethod(type, prop, bsonField);
                var setter = CreateSetMethod(type, prop, bsonField);

                // if not getter or setter - no mapping
                if (getter == null)
                {
                    continue;
                }

                var name = id != null && id.Equals(prop) ? "_id" : resolvePropertyName(prop.Name);

                // check if property has [BsonField] with a custom field name
                if (bsonField)
                {
                    var field = (BsonFieldAttribute)prop.GetCustomAttributes(fieldAttr, false).FirstOrDefault();
                    if (field != null && field.Name != null)
                    {
                        name = field.Name;
                    }
                }

                // check if property has [BsonId] to get with was setted AutoId = true
                var autoId = (BsonIdAttribute)prop.GetCustomAttributes(idAttr, false).FirstOrDefault();

                // checks if this proerty has [BsonIndex]
                var index = (BsonIndexAttribute)prop.GetCustomAttributes(indexAttr, false).FirstOrDefault();

                // if is _id field, do not accept index definition
                if (name == "_id")
                {
                    index = null;
                }

                // test if field name is OK (avoid to check in all instances) - do not test internal classes, like DbRef
                if (BsonDocument.IsValidFieldName(name) == false)
                {
                    throw LiteException.InvalidFormat(prop.Name, name);
                }

                // create a property mapper
                var p = new PropertyMapper
                {
                    AutoId       = autoId == null ? true : autoId.AutoId,
                    FieldName    = name,
                    PropertyName = prop.Name,
                    PropertyType = prop.PropertyType,
                    IndexOptions = index == null ? null : index.Options,
                    Getter       = getter,
                    Setter       = setter
                };

                dict.Add(prop.Name, p);
            }

            return(dict);
        }