/// <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(); } }
/// <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; } } }
/// <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(); } }
/// <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; }); }
/// <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; }); }
/// <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; } }
/// <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; } }
/// <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(); } }
/// <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; } } }
public int Count(LiteDB.Query query) { return(this._collection.Count(query)); }
public QueryNot(Query query, int order) : base("_id") { _query = query; _order = order; }
public QueryOr(Query left, Query right) : base(null) { _left = left; _right = right; }
/// <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); }
public IEnumerable <TEntity> Find(LiteDB.Query query, int skip = 0, int limit = int.MaxValue) { return(this._collection.Find(query, skip, limit)); }
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()); } }); } }
public bool Exists(LiteDB.Query query) { return(this._collection.Exists(query)); }
public TEntity FindOne(LiteDB.Query query) { return(this._collection.FindOne(query)); }
/// <summary> /// Returns documents that exists in ANY queries results (Union). /// </summary> public static Query Or(Query left, Query right) { return new QueryOr(left, right); }
public long LongCount(LiteDB.Query query) { return(this._collection.LongCount(query)); }