public static IWriteableStore SaveItem(this IWriteableStore store, IHasId obj) { if (store == null) { return(store); } store.Commit(CommitBag.New().MarkItemSaved(obj)); return(store); }
public static IWriteableStore DeleteItems(this IWriteableStore store, params StoredObjectId[] objs) { if (store == null) { return(store); } var commitBag = CommitBag.New(); commitBag.MarkItemsDeleted(objs.ToList()); store.Commit(commitBag); return(store); }
/// <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); }
public virtual void Commit(CommitBag bag) { Condition.Requires(bag).IsNotNull(); this.CommitStrategy(bag); }
public CommitException(CommitBag bagOfBadItems) : base() { this.BagOfBadItems = bagOfBadItems; }
/// <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); }); }
/// <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); } }
/// <summary> /// registers an item with a specific eviction condition. If the condition is mutable, every touch/get of the item will result /// in a condition mutation. /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="evictionCondition">may be null</param> public virtual void Commit(ICommitBag cb, IExpirable evictionCondition) { lock (this._stateLock) { //commit first. if it kacks we don't do anything this.Decorated.Commit(cb); //build and commit the conditions CommitBag conditionCommitBag = new CommitBag(); //foreach add, register a condition cb.ItemsToSave.WithEach(x => { //save the eviction condition in the eviction store, keyed by the storedobjectid of the item to save var conditionToSave = x.GetStoredObjectId().BuildAsId().HasContext(evictionCondition); 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); } }