Exemplo n.º 1
0
 /// <summary>
 /// Returns all documents inside collection order by _id index.
 /// </summary>
 public IEnumerable <T> FindAll()
 {
     return(this.Find(Query.All()));
 }
Exemplo n.º 2
0
 /// <summary>
 /// Returns all documents inside collection order by _id index.
 /// </summary>
 public IEnumerable <BsonDocument> FindAll(string collection)
 {
     return(this.Find(collection, Query.All()));
 }
Exemplo n.º 3
0
        private Query VisitExpression(Expression expr, string prefix = null)
        {
            try
            {
                // Single: x.Active
                if (expr is MemberExpression && expr.Type == typeof(bool))
                {
                    return(Query.EQ(this.GetField(expr, prefix), new BsonValue(true)));
                }
                // Not: !x.Active or !(x.Id == 1)
                else if (expr.NodeType == ExpressionType.Not)
                {
                    var unary = expr as UnaryExpression;
                    return(Query.Not(this.VisitExpression(unary.Operand, prefix)));
                }
                // Equals: x.Id == 1
                else if (expr.NodeType == ExpressionType.Equal)
                {
                    var bin = expr as BinaryExpression;
                    return(new QueryEquals(this.GetField(bin.Left, prefix), this.VisitValue(bin.Right, bin.Left)));
                }
                // NotEquals: x.Id != 1
                else if (expr.NodeType == ExpressionType.NotEqual)
                {
                    var bin = expr as BinaryExpression;
                    return(Query.Not(this.GetField(bin.Left, prefix), this.VisitValue(bin.Right, bin.Left)));
                }
                // LessThan: x.Id < 5
                else if (expr.NodeType == ExpressionType.LessThan)
                {
                    var bin = expr as BinaryExpression;
                    return(Query.LT(this.GetField(bin.Left, prefix), this.VisitValue(bin.Right, bin.Left)));
                }
                // LessThanOrEqual: x.Id <= 5
                else if (expr.NodeType == ExpressionType.LessThanOrEqual)
                {
                    var bin = expr as BinaryExpression;
                    return(Query.LTE(this.GetField(bin.Left, prefix), this.VisitValue(bin.Right, bin.Left)));
                }
                // GreaterThan: x.Id > 5
                else if (expr.NodeType == ExpressionType.GreaterThan)
                {
                    var bin = expr as BinaryExpression;
                    return(Query.GT(this.GetField(bin.Left, prefix), this.VisitValue(bin.Right, bin.Left)));
                }
                // GreaterThanOrEqual: x.Id >= 5
                else if (expr.NodeType == ExpressionType.GreaterThanOrEqual)
                {
                    var bin = expr as BinaryExpression;
                    return(Query.GTE(this.GetField(bin.Left, prefix), this.VisitValue(bin.Right, bin.Left)));
                }
                // And: x.Id > 1 && x.Name == "John"
                else if (expr.NodeType == ExpressionType.AndAlso)
                {
                    var bin   = expr as BinaryExpression;
                    var left  = this.VisitExpression(bin.Left, prefix);
                    var right = this.VisitExpression(bin.Right, prefix);

                    return(Query.And(left, right));
                }
                // Or: x.Id == 1 || x.Name == "John"
                else if (expr.NodeType == ExpressionType.OrElse)
                {
                    var bin   = expr as BinaryExpression;
                    var left  = this.VisitExpression(bin.Left);
                    var right = this.VisitExpression(bin.Right);

                    return(Query.Or(left, right));
                }
                // Constant: do nothing (at this point it's useful only to PredicateBuilder)
                else if (expr.NodeType == ExpressionType.Constant)
                {
                    var constant = expr as ConstantExpression;

                    if (constant.Value is bool)
                    {
                        var value = (bool)constant.Value;

                        return(value ? Query.All() : new QueryEmpty());
                    }
                }
                // Invoke: call inner Lambda expression (used in PredicateBuilder)
                else if (expr.NodeType == ExpressionType.Invoke)
                {
                    var invocation = expr as InvocationExpression;
                    var lambda     = invocation.Expression as LambdaExpression;
                    return(this.VisitExpression(lambda.Body));
                }
                // MethodCall: x.Name.StartsWith("John")
                else if (expr is MethodCallExpression)
                {
                    var met    = expr as MethodCallExpression;
                    var method = met.Method.Name;
// #if HAVE_TYPE_INFO
                    var type = met.Method.DeclaringType;
// #else
//                     var type = met.Method.ReflectedType;
// #endif
                    var paramType = met.Arguments[0] is MemberExpression ?
                                    (ExpressionType?)(met.Arguments[0] as MemberExpression).Expression.NodeType :
                                    null;

                    // StartsWith
                    if (method == "StartsWith")
                    {
                        var value = this.VisitValue(met.Arguments[0], null);

                        return(Query.StartsWith(this.GetField(met.Object, prefix), value));
                    }
                    // Equals
                    else if (method == "Equals")
                    {
                        var value = this.VisitValue(met.Arguments[0], null);

                        return(Query.EQ(this.GetField(met.Object, prefix), value));
                    }
                    // Contains (String): x.Name.Contains("auricio")
                    else if (method == "Contains" && type == typeof(string))
                    {
                        var value = this.VisitValue(met.Arguments[0], null);

                        return(Query.Contains(this.GetField(met.Object, prefix), value));
                    }
                    // Contains (Enumerable): x.ListNumber.Contains(2)
                    else if (method == "Contains" && type == typeof(Enumerable))
                    {
                        var field = this.GetField(met.Arguments[0], prefix);
                        var value = this.VisitValue(met.Arguments[1], null);

                        return(Query.EQ(field, value));
                    }
                    // Any (Enumerable): x.Customer.Any(z => z.Name.StartsWith("John"))
                    else if (method == "Any" && type == typeof(Enumerable) && paramType == ExpressionType.Parameter)
                    {
                        var field  = this.GetField(met.Arguments[0]);
                        var lambda = met.Arguments[1] as LambdaExpression;

                        return(this.VisitExpression(lambda.Body, field + "."));
                    }
                    // System.Linq.Enumerable methods (constant list items)
                    else if (type == typeof(Enumerable))
                    {
                        return(ParseEnumerableExpression(met));
                    }
                }

                throw new NotSupportedException("Not implemented Linq expression");
            }
            catch (NotSupportedException)
            {
                // when there is no linq implementation, use QueryLinq
                return(new QueryLinq <T>(expr, _param, _mapper));
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Returns true/false if filter returns any result
 /// </summary>
 public bool Exists()
 {
     return(_collection.Exists(_query ?? Query.All()));
 }
Exemplo n.º 5
0
        /// <summary>
        /// Reduce disk size re-arranging unused spaces. Can change password. If temporary disk was not provided, use MemoryStream temp disk
        /// </summary>
        public long Shrink(string password = null, IDiskService tempDisk = null)
        {
            var originalSize = _disk.FileLength;

            // if temp disk are not passed, use memory stream disk
            using (var temp = tempDisk ?? new StreamDiskService(new MemoryStream()))
                using (_locker.Write())
                    using (var engine = new LiteEngine(temp, password))
                    {
                        // read all collection
                        foreach (var collectionName in this.GetCollectionNames())
                        {
                            // first create all user indexes (exclude _id index)
                            foreach (var index in this.GetIndexes(collectionName).Where(x => x.Field != "_id"))
                            {
                                engine.EnsureIndex(collectionName, index.Field, index.Unique);
                            }

                            // now copy documents
                            var docs = this.Find(collectionName, Query.All());

                            engine.InsertBulk(collectionName, docs);

                            // fix collection sequence number
                            var seq = _collections.Get(collectionName).Sequence;

                            engine.Transaction(collectionName, true, (col) =>
                            {
                                col.Sequence = seq;
                                engine._pager.SetDirty(col);
                                return(true);
                            });
                        }

                        // copy user version
                        engine.UserVersion = this.UserVersion;

                        // set current disk size to exact new disk usage
                        _disk.SetLength(temp.FileLength);

                        // read new header page to start copy
                        var header = BasePage.ReadPage(temp.ReadPage(0)) as HeaderPage;

                        // copy (as is) all pages from temp disk to original disk
                        for (uint i = 0; i <= header.LastPageID; i++)
                        {
                            // skip lock page
                            if (i == 1)
                            {
                                continue;
                            }

                            var page = temp.ReadPage(i);

                            _disk.WritePage(i, page);
                        }

                        // create/destroy crypto class
                        _crypto = password == null ? null : new AesEncryption(password, header.Salt);

                        // initialize all services again (crypto can be changed)
                        this.InitializeServices();

                        // return how many bytes are reduced
                        return(originalSize - temp.FileLength);
                    }
        }
Exemplo n.º 6
0
 /// <summary>
 /// Execute query returning IEnumerable results.
 /// </summary>
 public IEnumerable <T> ToEnumerable()
 {
     return(_collection.Find(_query ?? Query.All(), _skip, _limit));
 }