/// <summary> /// Adds a key/value pair to the System.Collections.Concurrent.ConcurrentDictionary<TKey,TValue> /// if the key does not already exist, or updates a key/value pair in the System.Collections.Concurrent.ConcurrentDictionary<TKey,TValue> /// if the key already exists. /// </summary> /// <param name="key">The key to be added or whose value should be updated</param> /// <param name="value">The value to be added or updated for an absent key</param> /// <returns></returns> public override int AddOrUpdate(string key, IPersistItem value) { int res = 0; //dictionary.AddOrUpdate(key, value, (oldkey, oldValue) => //{ // isExists = true; // return value; //}); bool iscommited = false; try { switch (_CommitMode) { case CommitMode.OnDisk: using (var db = new DbLite(ConnectionString, DBProvider.SQLite)) { var cmdText = DbUpsertCommand(); res = db.ExecuteTransCommandNonQuery(cmdText, DataParameter.Get <SQLiteParameter>("Identifier", value.Identifier, "Body", value.ItemBinary, "Header", value.Header, "Retry", value.Retry, "ArrivedTime", value.ArrivedTime, "Expiration", value.Duration, "MessageState", (byte)value.MessageState), (result, trans) => { if (result > 0) { dictionary[key] = value; trans.Commit(); iscommited = true; } }); } break; default: dictionary.AddOrUpdate(key, value, (oldkey, oldValue) => { return(value); }); if (_CommitMode == CommitMode.OnMemory) { var cmdText = DbUpsertCommand(); res = ExecuteAsync(cmdText, DataParameter.Get <SQLiteParameter>("Identifier", value.Identifier, "Body", value.ItemBinary, "Header", value.Header, "Retry", value.Retry, "ArrivedTime", value.ArrivedTime, "Expiration", value.Duration, "MessageState", (byte)value.MessageState)); } res = 1; iscommited = true; break; } if (iscommited) { OnItemChanged("AddOrUpdate", null, null); } } catch (Exception ex) { OnErrorOcurred("AddOrUpdate", ex.Message); } return(res); }
/// <summary> /// Summary: /// Attempts to remove and return the value with the specified key from the System.Collections.Concurrent.ConcurrentDictionary<TKey,TValue>. /// /// Parameters: /// key: /// The key of the element to remove and return. /// /// value: /// When this method returns, value contains the object removed from the System.Collections.Concurrent.ConcurrentDictionary<TKey,TValue> /// or the default value of if the operation failed. /// /// Returns: /// true if an object was removed successfully; otherwise, false. /// /// Exceptions: /// System.ArgumentNullException: /// key is a null reference (Nothing in Visual Basic). /// </summary> public override bool TryRemove(string key, out IPersistItem value) { bool iscommited = false; IPersistItem outval = value = null; try { switch (_CommitMode) { case CommitMode.OnDisk: using (var db = new DbLite(ConnectionString, DBProvider.SQLite)) { var cmdText = DbDeleteCommand(); db.ExecuteTransCommandNonQuery(cmdText, DataParameter.Get <SQLiteParameter>("Identifier", value.Identifier), (result, trans) => { if (result > 0) { if (dictionary.TryRemove(key, out outval)) { trans.Commit(); iscommited = true; } } }); } break; default: if (dictionary.TryRemove(key, out outval)) { if (_CommitMode == CommitMode.OnMemory) { var cmdText = DbDeleteCommand(); var res = ExecuteAsync(cmdText, DataParameter.Get <SQLiteParameter>("Identifier", value.Identifier)); } iscommited = true; } break; } value = outval; if (iscommited) { OnItemChanged("TryRemove", key, value); } } catch (Exception ex) { OnErrorOcurred("TryRemove", ex.Message); } return(iscommited); }
/// <summary> /// Summary: /// Compares the existing value for the specified key with a specified value, /// and if they are equal, updates the key with a third value. /// /// Parameters: /// key: /// The key whose value is compared with comparisonValue and possibly replaced. /// /// newValue: /// The value that replaces the value of the element with key if the comparison /// results in equality. /// /// comparisonValue: /// The value that is compared to the value of the element with key. /// /// Returns: /// true if the value with key was equal to comparisonValue and replaced with /// newValue; otherwise, false. /// /// Exceptions: /// System.ArgumentNullException: /// key is a null reference. /// </summary> public override bool TryUpdate(string key, IPersistItem newValue, IPersistItem comparisonValue) { bool iscommited = false; try { switch (_CommitMode) { case CommitMode.OnDisk: using (var db = new DbLite(ConnectionString, DBProvider.SQLite)) { var cmdText = DbUpdateCommand(); db.ExecuteTransCommandNonQuery(cmdText, DataParameter.Get <SQLiteParameter>("Identifier", newValue.Identifier, "Body", newValue.ItemBinary, "Header", newValue.Header, "Retry", newValue.Retry, "ArrivedTime", newValue.ArrivedTime, "Expiration", newValue.Duration, "MessageState", newValue.MessageState), (result, trans) => { if (result > 0) { if (dictionary.TryUpdate(key, newValue, comparisonValue)) { trans.Commit(); iscommited = true; } } }); } break; default: if (dictionary.TryUpdate(key, newValue, comparisonValue)) { if (_CommitMode == CommitMode.OnMemory) { var cmdText = DbUpdateCommand(); var res = ExecuteAsync(cmdText, DataParameter.Get <SQLiteParameter>("Identifier", newValue.Identifier, "Body", newValue.ItemBinary, "Header", newValue.Header, "Retry", newValue.Retry, "ArrivedTime", newValue.ArrivedTime, "Expiration", newValue.Duration, "MessageState", newValue.MessageState)); } iscommited = true; } break; } if (iscommited) { OnItemChanged("TryUpdate", key, newValue); } } catch (Exception ex) { string err = ex.Message; OnErrorOcurred("TryUpdate", ex.Message); } return(iscommited); }
/// <summary> /// updates a key/value pair to the System.Collections.Concurrent.ConcurrentDictionary<TKey,TValue> /// if the key already exists. /// </summary> /// <param name="key">The key to be added or whose value should be updated</param> /// <param name="value">The value to be added or updated for an absent key</param> /// <returns></returns> public int UpdateState(string key, int state) { bool iscommited = false; int res = 0; IPersistItem val = null; try { switch (_CommitMode) { case CommitMode.OnDisk: using (var db = new DbLite(ConnectionString, DBProvider.SQLite)) { var cmdText = DbUpdateStateCommand(); res = db.ExecuteTransCommandNonQuery(cmdText, DataParameter.Get <SQLiteParameter>("Identifier", key, "MessageState", state), (result, trans) => { if (result > 0) { if (dictionary.TryGetValue(key, out val)) { val.MessageState = (MessageState)state; dictionary[key] = val; } //if (dictionary.ContainsKey(key)) // dictionary[key].State = state; trans.Commit(); iscommited = true; } }); } break; default: //if (dictionary.ContainsKey(key)) // dictionary[key].State = state; if (dictionary.TryGetValue(key, out val)) { val.MessageState = (MessageState)state; dictionary[key] = val; } if (_CommitMode == CommitMode.OnMemory) { var task = new PersistanceTask() { CommandText = DbUpdateStateCommand(), CommandType = "DbUpdateState", ConnectionString = ConnectionString, Parameters = DataParameter.Get <SQLiteParameter>("Identifier", key, "MessageState", state) }; task.ExecuteTask(_EnableTasker); } res = 1; iscommited = true; break; } if (iscommited) { OnItemChanged("Update", null, val); } } catch (Exception ex) { OnErrorOcurred("Update", ex.Message); } return(res); }