Example #1
0
        public IHasBits GetIndex(StoredObjectId id)
        {
            var item = this._storeOfIndices.Get<IndexedEntry>(id);
            if (item == null)
                return null;

            return item;
        }
Example #2
0
        public static IWriteableStore DeleteItem(this IWriteableStore store, StoredObjectId soid)
        {
            if (store == null)
                return store;

            store.Commit(new CommitBag().MarkItemDeleted(soid));
            return store;
        }
Example #3
0
        /// <summary>
        /// Marks an item for deletion.  Can use IHasId(ie. instance) or StoredObjectId(ie. key) here
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public ICommitBag MarkItemDeleted(StoredObjectId key)
        {
            if (key == null) { return this; }

            lock (this._stateLock)
            {
                this.ItemsToDelete.Remove(key);
                this.ItemsToDelete.Add(key);
            }
            return this;
        }
Example #4
0
        /// <summary>
        /// copies an item from one store to the other
        /// </summary>
        /// <param name="store"></param>
        /// <param name="storeToMoveTo"></param>
        /// <param name="itemToMove"></param>
        public static void CopyItem(this IStore store, IStore storeToMoveTo, StoredObjectId itemToMove)
        {
            if (store == null)
                return;

            if (storeToMoveTo == null)
                return;


            var item = store.Get(itemToMove);
            if (item != null)
                storeToMoveTo.SaveItem(item);
        }
Example #5
0
        /// <summary>
        /// moves an item from one store to the other
        /// </summary>
        /// <param name="store"></param>
        /// <param name="storeToMoveTo"></param>
        /// <param name="itemToMove"></param>
        public static void MoveItem(this IStore store, IStore storeToMoveTo, StoredObjectId itemToMove)
        {
            if (store == null)
                return;

            if (storeToMoveTo == null)
                return;

            var item = store.Get(itemToMove);
            if (item != null)
            {
                storeToMoveTo.SaveItem(item);
                store.DeleteItem(item.GetStoredObjectId());
            }
        }
Example #6
0
        /// <summary>
        /// examines all eviction conditions and removes items that have an eviction condition of true.
        /// If an item's eviction condition is mutable, it will be mutated (eg. touched) on every get
        /// </summary>
        public void Evict()
        {
            List <ContextualIHasIdDecoration> itemsToEvict = new List <ContextualIHasIdDecoration>();

            //search the eviction store for evicts
            var evictions = this.ExpirableStore.GetAll();

            foreach (var each in evictions)
            {
                var eachItem = each as ContextualIHasIdDecoration;
                if (eachItem != null && eachItem.Context != null)
                {
                    IExpirable exp = eachItem.Context as IExpirable;
                    if (exp.IsExpired())
                    {
                        itemsToEvict.Add(eachItem);
                    }
                }
            }

            //build deletes and commit them
            var mainCommitBag = new CommitBag();
            var expCommitBag  = new CommitBag();

            itemsToEvict.WithEach(x =>
            {
                StoredObjectId soid = x.Id as StoredObjectId;
                mainCommitBag.MarkItemDeleted(soid);
                expCommitBag.MarkItemDeleted(x.GetStoredObjectId());
            });

            //remove the item specified by the expirable policy.
            this.Commit(mainCommitBag);

            //now delete from the expiry store
            this.ExpirableStore.Commit(expCommitBag);

            //raise events (outside of state lock)
            itemsToEvict.WithEach(x =>
            {
                this.OnItemEvicted(x, x.Context as IExpirable);
            });
        }
Example #7
0
        /// <summary>
        /// overrides, supplies the default condition
        /// </summary>
        /// <param name="bag"></param>
        public override void Commit(ICommitBag cb)
        {
            lock (this._stateLock)
            {
                //commit first. if it kacks we don't do anything
                base.Commit(cb);

                //build and commit the conditions
                CommitBag conditionCommitBag = new CommitBag();

                //foreach add, register a condition
                if (this.ExpirableFactory != null)
                {
                    cb.ItemsToSave.WithEach(x =>
                    {
                        //generate the eviction condition
                        var evictionConditionLogic = this.ExpirableFactory.Perform(x) as LogicOfTo <IHasId, IExpirable>;

                        //save the eviction condition in the eviction store, keyed by the storedobjectid of the item to save
                        var conditionToSave = x.GetStoredObjectId().BuildAsId().HasContext(evictionConditionLogic.Result);
                        //var soid = conditionToSave.GetStoredObjectId();
                        //var soid2 = StoredObjectId.New(typeof(ContextualIHasIdDecoration), x.GetStoredObjectId());
                        //bool isEq = soid.Equals(soid2);
                        conditionCommitBag.MarkItemSaved(conditionToSave);
                    });
                }

                //foreach remove, remove a condition
                cb.ItemsToDelete.WithEach(x =>
                {
                    var delId = StoredObjectId.New(typeof(ContextualIHasIdDecoration), x);
                    conditionCommitBag.MarkItemDeleted(delId);
                });

                this.ExpirableStore.Commit(conditionCommitBag);
            }
        }
Example #8
0
 public void RemoveIndex(StoredObjectId id)
 {
     this._storeOfIndices.DeleteItem(id);
 }
Example #9
0
        public static string NewId(StoredObjectId id)
        {
            var val = _serializer.Serialize(id);

            return(val);
        }
Example #10
0
 public static bool Contains(this IStore store, StoredObjectId soId)
 {
     var item = store.Get(soId);
     return item != null;
 }
Example #11
0
        /// <summary>
        /// Modifies the id of an object in a store
        /// </summary>
        /// <param name="store"></param>
        /// <param name="idToChange"></param>
        /// <param name="newId"></param>
        public static void ChangeStoredObjectId(this IStore store, StoredObjectId idToChange, object newId)
        {
            if (store == null)
                return;

            if (idToChange == null)
                return;

            //if we're not changing anything, skip
            if (idToChange.ObjectId.Equals(newId))
                return;

            //get the object to ensure it exists
            var obj = store.Get(idToChange);
            if (obj == null)
                return;
            
            //mark delete
            var cb = CommitBag.New();
            cb.MarkItemDeleted(idToChange);

            //change the id
            if (obj is IHasSettableId)
            {
                IHasSettableId ihs = obj as IHasSettableId;
                ihs.SetId(newId);
            }
            else
            {
                PropertyInfo pi = obj.GetType().GetProperty("Id", BindingFlags.Public | BindingFlags.Instance);
                pi.SetValue(obj, newId);
            }
            //mark save new item and commit
            cb.MarkItemSaved(obj);
            store.Commit(cb);
        }
Example #12
0
 public void RemoveIndex(StoredObjectId id)
 {
     this._storeOfIndices.DeleteItem(id);
 }
Example #13
0
        /// <summary>
        /// mutates an item in the eviction condition store, and returns true if it's not flagged for eviction
        /// </summary>
        /// <param name="soId"></param>
        /// <returns></returns>
        protected bool TouchExpirable(StoredObjectId soId)
        {
            if (soId == null)
                return false;

            bool isExpired = false;
            //get the item's condition from the condition store
            var record = this.ExpirableStore.Get<ContextualId<StoredObjectId, IExpirable>>(soId);

            //if the eviction condition is touchable, touch it
            if (record != null && record.Context != null)
            {
                isExpired = record.Context.IsExpired();

                //if the expirable is touchable, touch it
                if (record.Context is ITouchable)
                {
                    ITouchable touch = (ITouchable)record.Context;
                    touch.Touch();
                }
                else if (record.Context is IPolyfacing) //or if it's polyfacing with a touchable face
                {
                    IPolyfacing poly = (IPolyfacing)record.Context;
                    var touch = poly.RootFace.AsHasTouchable();
                    if (touch != null)
                    {
                        touch.Touch();
                    }
                }
            }
            return !isExpired;
        }