Example #1
0
        internal override bool ExecuteFullScan(BsonDocument doc, IndexOptions options)
        {
            var val = doc.Get(this.Field).Normalize(options);

            return(val.CompareTo(_value) == 0);
        }
Example #2
0
File: Index.cs Project: Rizx/LiteDB
        /// <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 virtual 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);
            }

            // do not create collection at this point
            var col = this.GetCollectionPage(false);

            if (col != null)
            {
                // check if index already exists but has diferent options
                var existsIndex = col.GetIndex(field);

                if (existsIndex != null)
                {
                    if (!options.Equals(existsIndex.Options))
                    {
                        // drop index and create another
                        this.DropIndex(field);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            ;

            // start transaction
            this.Database.Transaction.Begin();

            try
            {
                // if not collection yet, create a new now
                if (col == null)
                {
                    col     = this.Database.Collections.Add(this.Name);
                    _pageID = col.PageID;
                }

                // create index head
                var index = this.Database.Indexer.CreateIndex(col);

                index.Field   = field;
                index.Options = options;

                // read all objects (read from PK index)
                foreach (var node in new QueryAll("_id", 1).Run(this))
                {
                    var dataBlock = this.Database.Data.Read(node.DataBlock, true);

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

                    // adding index
                    var key = doc.Get(field);

                    var newNode = this.Database.Indexer.AddNode(index, key);

                    // adding this new index Node to indexRef
                    dataBlock.IndexRef[index.Slot] = newNode.Position;

                    // link index node to datablock
                    newNode.DataBlock = dataBlock.Position;

                    // mark datablock page as dirty
                    dataBlock.Page.IsDirty = true;
                }

                this.Database.Transaction.Commit();
            }
            catch
            {
                this.Database.Transaction.Rollback();
                throw;
            }

            return(true);
        }
Example #3
0
 internal override void NormalizeValues(IndexOptions options)
 {
     _value = _value.Normalize(options);
 }
Example #4
0
File: Index.cs Project: Rizx/LiteDB
        /// <summary>
        /// Create a new permanent index in all documents inside this collections if index not exists already.
        /// </summary>
        /// <param name="property">Property linq expression</param>
        /// <param name="options">Use all indexes options</param>
        public virtual bool EnsureIndex <K>(Expression <Func <T, K> > property, IndexOptions options)
        {
            var field = _visitor.GetBsonField(property);

            return(this.EnsureIndex(field, options));
        }