Esempio n. 1
1
        /// <summary>
        /// Count all nodes from a query execution - do not deserialize documents to count
        /// </summary>
        public int Count(string colName, Query query)
        {
            lock (_locker)
            {
                // get collection page (no col, returns 0)
                var col = this.GetCollectionPage(colName, false);

                if (col == null) return 0;

                if (query == null) return (int)col.DocumentCount;

                // run query in this collection
                var nodes = query.Run(col, _indexer);

                // count all nodes
                return nodes.Count();
            }
        }
Esempio n. 2
0
File: Find.cs Progetto: apkd/LiteDB
        /// <summary>
        /// Find index keys from collection. Do not retorn document, only key value
        /// </summary>
        public IEnumerable<BsonValue> FindIndex(string colName, Query query, int skip = 0, int limit = int.MaxValue)
        {
            using (_locker.Read())
            {
                // get my collection page
                var col = this.GetCollectionPage(colName, false);

                // no collection, no values
                if (col == null) yield break;

                // get nodes from query executor to get all IndexNodes
                var nodes = query.Run(col, _indexer);

                // skip first N nodes
                if (skip > 0) nodes = nodes.Skip(skip);

                // limit in M nodes
                if (limit != int.MaxValue) nodes = nodes.Take(limit);

                // for each document, read data and deserialize as document
                foreach (var node in nodes)
                {
                    _log.Write(Logger.QUERY, "read index key on '{0}' :: key = {1}", colName, node.Key);

                    _trans.CheckPoint();

                    yield return node.Key;
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Find for documents in a collection using Query definition
        /// </summary>
        public IEnumerable<BsonDocument> Find(string colName, Query query, int skip = 0, int limit = int.MaxValue)
        {
            // get my collection page
            var col = this.GetCollectionPage(colName, false);

            // no collection, no documents
            if (col == null) yield break;

            // get nodes from query executor to get all IndexNodes
            var nodes = query.Run(col, _indexer);

            // skip first N nodes
            if (skip > 0) nodes = nodes.Skip(skip);

            // limit in M nodes
            if (limit != int.MaxValue) nodes = nodes.Take(limit);

            // for each document, read data and deserialize as document
            foreach (var node in nodes)
            {
                _log.Write(Logger.QUERY, "read document on '{0}' :: _id = {1}", colName, node.Key);

                var dataBlock = _data.Read(node.DataBlock, true);

                var doc = BsonSerializer.Deserialize(dataBlock.Buffer).AsDocument;

                yield return doc;

                _cache.CheckPoint();
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Implements delete based on a query result
        /// </summary>
        public int Delete(string colName, Query query)
        {
            return this.Transaction<int>(colName, false, (col) =>
            {
                if (col == null) return 0;

                var nodes = query.Run(col, _indexer);
                var count = 0;

                foreach (var node in nodes)
                {
                    _log.Write(Logger.COMMAND, "delete document on '{0}' :: _id = {1}", colName, node.Key);

                    // read dataBlock (do not read all extend pages, i will not use)
                    var dataBlock = _data.Read(node.DataBlock, false);

                    // lets remove all indexes that point to this in dataBlock
                    foreach (var index in col.GetIndexes(true))
                    {
                        _indexer.Delete(index, dataBlock.IndexRef[index.Slot]);
                    }

                    // remove object data
                    _data.Delete(col, node.DataBlock);

                    _cache.CheckPoint();

                    count++;
                }

                return count;
            });
        }
Esempio n. 5
0
        /// <summary>
        /// Implements delete based on a query result
        /// </summary>
        public int Delete(string colName, Query query)
        {
            return this.Transaction<int>(colName, false, (col) =>
            {
                if (col == null) return 0;

                var nodes = query.Run(col, _indexer);
                var count = 0;

                foreach (var node in nodes)
                {
                    _log.Write(Logger.COMMAND, "delete document on '{0}' :: _id = {1}", colName, node.Key);

                    // get all indexes nodes from this data block
                    var allNodes = _indexer.GetNodeList(node, true).ToArray();

                    // lets remove all indexes that point to this in dataBlock
                    foreach (var linkNode in allNodes)
                    {
                        var index = col.Indexes[linkNode.Slot];

                        _indexer.Delete(index, linkNode.Position);
                    }

                    // remove object data
                    _data.Delete(col, node.DataBlock);

                    _trans.CheckPoint();

                    count++;
                }

                return count;
            });
        }
Esempio n. 6
0
        /// <summary>
        /// Check if has at least one node in a query execution - do not deserialize documents to check
        /// </summary>
        public bool Exists(string colName, Query query)
        {
            lock (_locker)
            {
                // get collection page (no col, not exists)
                var col = this.GetCollectionPage(colName, false);

                if (col == null) return false;

                // run query in this collection
                var nodes = query.Run(col, _indexer);

                // check if has at least first
                return nodes.FirstOrDefault() != null;
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Check if has at least one node in a query execution - do not deserialize documents to check
        /// </summary>
        public bool Exists(string colName, Query query)
        {
            using (_locker.Read())
            {
                var col = GetCollectionPage(colName, false);

                if (col == null) return false;

                // run query in this collection
                var nodes = query.Run(col, _indexer);

                var first = nodes.FirstOrDefault();

                // check if has at least first
                return first != null;
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Count all nodes from a query execution - do not deserialize documents to count. If query is null, use Collection counter variable
        /// </summary>
        public long Count(string colName, Query query)
        {
            using (_locker.Read())
            {
                var col = GetCollectionPage(colName, false);

                if (col == null) return 0;

                if (query == null) return col.DocumentCount;

                // run query in this collection
                var nodes = query.Run(col, _indexer);

                // count distinct nodes based on DataBlock
                return nodes
                    .Select(x => x.DataBlock)
                    .Distinct()
                    .LongCount();
            }
        }
Esempio n. 9
0
File: Find.cs Progetto: apkd/LiteDB
        /// <summary>
        /// Find for documents in a collection using Query definition
        /// </summary>
        public IEnumerable<BsonDocument> Find(string colName, Query query, int skip = 0, int limit = int.MaxValue)
        {
            using(_locker.Read())
            {
                // get my collection page
                var col = this.GetCollectionPage(colName, false);

                // no collection, no documents
                if (col == null) yield break;

                // get nodes from query executor to get all IndexNodes
                var nodes = query.Run(col, _indexer);

                // skip first N nodes
                if (skip > 0) nodes = nodes.Skip(skip);

                // limit in M nodes
                if (limit != int.MaxValue) nodes = nodes.Take(limit);

                // for each document, read data and deserialize as document
                foreach (var node in nodes)
                {
                    _log.Write(Logger.QUERY, "read document on '{0}' :: _id = {1}", colName, node.Key);

                    byte[] buffer;
                    BsonDocument doc;

                    // encapsulate read operation inside a try/catch (yeild do not support try/catch)
                    buffer = _data.Read(node.DataBlock);
                    doc = BsonSerializer.Deserialize(buffer).AsDocument;

                    _trans.CheckPoint();

                    yield return doc;
                }
            }
        }
Esempio n. 10
0
 public int Count(LiteDB.Query query)
 {
     return(this._collection.Count(query));
 }
Esempio n. 11
0
 public QueryNot(Query query, int order)
     : base("_id")
 {
     _query = query;
     _order = order;
 }
Esempio n. 12
0
 public QueryOr(Query left, Query right)
     : base(null)
 {
     _left = left;
     _right = right;
 }
Esempio n. 13
0
File: Query.cs Progetto: apkd/LiteDB
 /// <summary>
 /// Returns all documents that in query result (not result)
 /// </summary>
 public static Query Not(Query query, int order = Query.Ascending)
 {
     return new QueryNot(query, order);
 }
Esempio n. 14
0
 public IEnumerable <TEntity> Find(LiteDB.Query query, int skip = 0, int limit = int.MaxValue)
 {
     return(this._collection.Find(query, skip, limit));
 }
Esempio n. 15
0
        private void EntityTracker_EntityUpdated(IEntity obj)
        {
            if (obj is UserEntity entity)
            {
                if (PacketProcessor.Instance.EntityTracker.CompassUser.Id != obj.Id)
                {
                    if (Services.CompassSettings.MarkGuildAsAlly && entity.Relation == RelationType.PK &&
                        entity.GuildName == Services.CompassSettings.MyGuildName)
                    {
                        entity.Relation = RelationType.GuildMember;
                    }
                    PlayerModels[entity.Id] = entity;
                }
            }
            else if (Services.CompassSettings.ShowGatherting && obj is CollectionEntity collection)
            {
                CollectionModels[collection.Id] = collection;
                Task.Factory.StartNew(() =>
                {
                    try
                    {
                        CollectionEntity result = CollectionDatabase.Collection.FindOne(Query.And(Query.EQ("$.Position[0]", collection.Position.X),
                                                                                                  Query.EQ("$.Position[1]", collection.Position.Y), Query.EQ("$.Position[2]", collection.Position.Z)));

                        if (result == null)
                        {
                            CollectionDatabase.Collection.Insert(collection);
                        }
                    }
                    catch (Exception ex)
                    {
                        Trace.Write(ex.ToString());
                    }
                });
            }
        }
Esempio n. 16
0
 public bool Exists(LiteDB.Query query)
 {
     return(this._collection.Exists(query));
 }
Esempio n. 17
0
 public TEntity FindOne(LiteDB.Query query)
 {
     return(this._collection.FindOne(query));
 }
Esempio n. 18
0
 /// <summary>
 /// Returns documents that exists in ANY queries results (Union).
 /// </summary>
 public static Query Or(Query left, Query right)
 {
     return new QueryOr(left, right);
 }
Esempio n. 19
0
 public long LongCount(LiteDB.Query query)
 {
     return(this._collection.LongCount(query));
 }