Esempio n. 1
0
        /// <summary>
        /// Read an BsonArray from reader
        /// </summary>
        public BsonArray ReadArray(ByteReader reader)
        {
            var length = reader.ReadInt32();
            var end    = reader.Position + length - 5;
            var arr    = new BsonArray();

            while (reader.Position < end)
            {
                var value = this.ReadElement(reader, out string name);
                arr.Add(value);
            }

            reader.ReadByte(); // zero

            return(arr);
        }
Esempio n. 2
0
        public override BsonValue ToMongoQuery()
        {
            BsonArray array = new BsonArray();

            foreach (var value in _values.Distinct())
            {
                array.Add(value);
            }
            BsonDocument opt = new BsonDocument();

            opt.Add("$in", array);
            BsonDocument mq = new BsonDocument();

            mq.Add(this.Field, opt);
            return(mq);
        }
Esempio n. 3
0
        /// <summary>
        /// Register a property as a DbRefList - implement a custom Serialize/Deserialize actions to convert entity to $id, $ref only
        /// </summary>
        internal static void RegisterDbRefList(PropertyMapper p, string collectionName, Type listType, Type itemType, Dictionary <string, PropertyMapper> itemMapper)
        {
            p.Serialize = (list, m) =>
            {
                var result  = new BsonArray();
                var idField = itemMapper.Select(x => x.Value).FirstOrDefault(x => x.FieldName == "_id");

                foreach (var item in (IEnumerable)list)
                {
                    result.Add(new BsonDocument()
                               .Add("$id", new BsonValue(idField.Getter(item)))
                               .Add("$ref", collectionName));
                }

                return(result);
            };

            p.Deserialize = (bson, m) =>
            {
                var array = bson.AsArray;

                if (array.Count == 0)
                {
                    return(m.Deserialize(listType, array));
                }

                var hasIdRef = array[0].AsDocument["$id"].IsNull;

                if (hasIdRef)
                {
                    // if no $id, deserialize as full (was loaded via Include)
                    return(m.Deserialize(listType, array));
                }
                else
                {
                    // copy array changing $id to _id
                    var arr = new BsonArray();

                    foreach (var item in array)
                    {
                        arr.Add(new BsonDocument().Add("_id", item.AsDocument["$id"]));
                    }

                    return(m.Deserialize(listType, arr));
                }
            };
        }
Esempio n. 4
0
        public BsonArray ReadArray(BinaryReader reader)
        {
            var length = reader.ReadInt32();
            var end    = (int)reader.BaseStream.Position + length - 1;
            var arr    = new BsonArray();

            while (reader.BaseStream.Position < end)
            {
                string name;
                var    value = this.ReadElement(reader, out name);
                arr.Add(value);
            }

            reader.ReadByte(); // zero

            return(arr);
        }
Esempio n. 5
0
        /// <summary>
        /// Get internal information about database. Can filter collections
        /// </summary>
        public BsonDocument Info()
        {
            using (_locker.Read())
            {
                var header      = _pager.GetPage <HeaderPage>(0);
                var collections = new BsonArray();

                foreach (var colName in header.CollectionPages.Keys)
                {
                    var col = this.GetCollectionPage(colName, false);

                    var colDoc = new BsonDocument
                    {
                        { "name", col.CollectionName },
                        { "pageID", (double)col.PageID },
                        { "count", col.DocumentCount },
                        { "sequence", col.Sequence },
                        { "indexes", new BsonArray(
                              col.Indexes.Where(x => !x.IsEmpty).Select(i => new BsonDocument
                            {
                                { "slot", i.Slot },
                                { "field", i.Field },
                                { "expression", i.Expression },
                                { "unique", i.Unique }
                            })) }
                    };

                    collections.Add(colDoc);
                }

                return(new BsonDocument
                {
                    { "userVersion", (int)header.UserVersion },
                    { "encrypted", header.Password.Any(x => x > 0) },
                    { "changeID", (int)header.ChangeID },
                    { "lastPageID", (int)header.LastPageID },
                    { "fileSize", BasePage.GetSizeOfPages(header.LastPageID + 1) },
                    { "collections", collections }
                });
            }
        }
Esempio n. 6
0
        private BsonArray ReadArray()
        {
            var arr = new BsonArray();

            var token = _tokenizer.ReadToken();

            while (token.TokenType != JsonTokenType.EndArray)
            {
                var value = this.ReadValue(token);

                arr.Add(value);

                token = _tokenizer.ReadToken();

                if (token.TokenType == JsonTokenType.Comma)
                {
                    token = _tokenizer.ReadToken();
                }
            }

            return(arr);
        }
Esempio n. 7
0
        /// <summary>
        /// Register a property as a DbRefList - implement a custom Serialize/Deserialize actions to convert entity to $id, $ref only
        /// </summary>
        private static void RegisterDbRefList(BsonMapper mapper, MemberMapper member, string collection)
        {
            // get entity from list item type
            var entity = mapper.GetEntityMapper(member.UnderlyingType);

            member.Serialize = (list, m) =>
            {
                // supports null values when "SerializeNullValues = true"
                if (list == null)
                {
                    return(BsonValue.Null);
                }

                var result  = new BsonArray();
                var idField = entity.Id;

                foreach (var item in (IEnumerable)list)
                {
                    if (item == null)
                    {
                        continue;
                    }

                    var id = idField.Getter(item);

                    result.Add(new BsonDocument
                    {
                        { "$id", m.Serialize(id.GetType(), id, 0) },
                        { "$ref", collection }
                    });
                }

                return(result);
            };

            member.Deserialize = (bson, m) =>
            {
                var array = bson.AsArray;

                if (array.Count == 0)
                {
                    return(m.Deserialize(member.DataType, array));
                }

                var hasIdRef = array[0].AsDocument == null || array[0].AsDocument["$id"].IsNull;

                if (hasIdRef)
                {
                    // if no $id, deserialize as full (was loaded via Include)
                    return(m.Deserialize(member.DataType, array));
                }
                else
                {
                    // copy array changing $id to _id
                    var arr = new BsonArray();

                    foreach (var item in array)
                    {
                        arr.Add(new BsonDocument {
                            { "_id", item.AsDocument["$id"] }
                        });
                    }

                    return(m.Deserialize(member.DataType, arr));
                }
            };
        }
Esempio n. 8
0
        /// <summary>
        /// Register a property as a DbRefList - implement a custom Serialize/Deserialize actions to convert entity to $id, $ref only
        /// </summary>
        private static void RegisterDbRefList(BsonMapper mapper, MemberMapper member, string collection)
        {
            // get entity from list item type
            var entity = mapper.GetEntityMapper(member.UnderlyingType);

            member.Serialize = (list, m) =>
            {
                // supports null values when "SerializeNullValues = true"
                if (list == null)
                {
                    return(BsonValue.Null);
                }

                var result  = new BsonArray();
                var idField = entity.Id;

                foreach (var item in (IEnumerable)list)
                {
                    if (item == null)
                    {
                        continue;
                    }

                    var id = idField.Getter(item);

                    result.Add(new BsonDocument
                    {
                        { "$id", m.Serialize(id.GetType(), id, 0) },
                        { "$ref", collection }
                    });
                }

                return(result);
            };

            member.Deserialize = (bson, m) =>
            {
                var array = bson.AsArray;

                if (array.Count == 0)
                {
                    return(m.Deserialize(member.DataType, array));
                }

                // copy array changing $id to _id
                var result = new BsonArray();

                foreach (var item in array)
                {
                    var refId = item.AsDocument["$id"];

                    // if refId is null was included by "include" query, so "item" is full filled document
                    if (refId.IsNull)
                    {
                        result.Add(item);
                    }
                    else
                    {
                        result.Add(new BsonDocument {
                            { "_id", refId }
                        });
                    }
                }

                return(m.Deserialize(member.DataType, result));
            };
        }
Esempio n. 9
0
        /// <summary>
        /// Register a property as a DbRefList - implement a custom Serialize/Deserialize actions to convert entity to $id, $ref only
        /// </summary>
        private static void RegisterDbRefList(BsonMapper mapper, MemberMapper member, ITypeNameBinder typeNameBinder, string collection)
        {
            // get entity from list item type
            var entity = mapper.GetEntityMapper(member.UnderlyingType);

            member.Serialize = (list, m) =>
            {
                // supports null values when "SerializeNullValues = true"
                if (list == null)
                {
                    return(BsonValue.Null);
                }

                var result  = new BsonArray();
                var idField = entity.Id;

                foreach (var item in (IEnumerable)list)
                {
                    if (item == null)
                    {
                        continue;
                    }

                    var id = idField.Getter(item);

                    var bsonDocument = new BsonDocument
                    {
                        ["$id"]  = m.Serialize(id.GetType(), id, 0),
                        ["$ref"] = collection
                    };

                    if (member.UnderlyingType != item.GetType())
                    {
                        bsonDocument["$type"] = typeNameBinder.GetName(item.GetType());
                    }

                    result.Add(bsonDocument);
                }

                return(result);
            };

            member.Deserialize = (bson, m) =>
            {
                if (bson.IsArray == false)
                {
                    return(null);
                }

                var array = bson.AsArray;

                if (array.Count == 0)
                {
                    return(m.Deserialize(member.DataType, array));
                }

                // copy array changing $id to _id
                var result = new BsonArray();

                foreach (var item in array)
                {
                    if (item.IsDocument == false)
                    {
                        continue;
                    }

                    var doc      = item.AsDocument;
                    var idRef    = doc["$id"];
                    var missing  = doc["$missing"] == true;
                    var included = doc.ContainsKey("$ref") == false;

                    // if referece document are missing, do not inlcude on output list
                    if (missing)
                    {
                        continue;
                    }

                    // if refId is null was included by "include" query, so "item" is full filled document
                    if (included)
                    {
                        item["_id"] = idRef;
                        if (item.AsDocument.ContainsKey("$type"))
                        {
                            item["_type"] = item["$type"];
                        }

                        result.Add(item);
                    }
                    else
                    {
                        var bsonDocument = new BsonDocument {
                            ["_id"] = idRef
                        };

                        if (item.AsDocument.ContainsKey("$type"))
                        {
                            bsonDocument["_type"] = item["$type"];
                        }

                        result.Add(bsonDocument);
                    }
                }

                return(m.Deserialize(member.DataType, result));
            };
        }