public string[] Persist(IEnumerable <global::FormABC.Input> insert, IEnumerable <KeyValuePair <global::FormABC.Input, global::FormABC.Input> > update, IEnumerable <global::FormABC.Input> delete) { var insertedData = insert != null?insert.ToArray() : new global::FormABC.Input[0]; var updatedData = update != null?update.ToList() : new List <KeyValuePair <global::FormABC.Input, global::FormABC.Input> >(); var deletedData = delete != null?delete.ToArray() : new global::FormABC.Input[0]; if (insertedData.Length == 0 && updatedData.Count == 0 && deletedData.Length == 0) { return(new string[0]); } if (updatedData.Count > 0 && updatedData.Any(it => it.Key == null)) { //TODO fetch only null values var oldValues = Find(updatedData.Select(it => it.Value.URI)).ToDictionary(it => it.URI); if (oldValues.Count != updatedData.Count) { throw new ArgumentException("Can't find update value. Requested: {0}, found: {1}. Missing URI: {2}".With( updatedData.Count, oldValues.Count, string.Join(", ", updatedData.Select(it => it.Value.URI).Except(oldValues.Keys)))); } global::FormABC.Input _val; for (int i = 0; i < updatedData.Count; i++) { _val = updatedData[i].Value; updatedData[i] = new KeyValuePair <global::FormABC.Input, global::FormABC.Input>(oldValues[_val.URI], _val); } } updatedData.RemoveAll(kv => kv.Key.Equals(kv.Value)); var cms = Common.Utility.UseThreadLocalStream(); for (int i = 0; i < insertedData.Length; i++) { insertedData[i].__InternalPrepareInsert(); } foreach (var it in updatedData) { it.Value.__InternalPrepareUpdate(); } for (int i = 0; i < deletedData.Length; i++) { deletedData[i].__InternalPrepareDelete(); } for (int i = 0; i < insertedData.Length; i++) { var it = insertedData[i]; it.__ReapplyReferences(); it.URI = _DatabaseCommon.FactoryFormABC_Input.InputConverter.BuildURI(cms.CharBuffer, it.ID); if (it.SubmissionID != null) { it._SubmissionURI = _DatabaseCommon.FactoryUseCase1_Submission.SubmissionConverter.BuildURI(cms.CharBuffer, it.SubmissionID); } } foreach (var kv in updatedData) { kv.Value.__ReapplyReferences(); kv.Value.URI = _DatabaseCommon.FactoryFormABC_Input.InputConverter.BuildURI(cms.CharBuffer, kv.Value.ID); if (kv.Value.SubmissionID != null) { kv.Value._SubmissionURI = _DatabaseCommon.FactoryUseCase1_Submission.SubmissionConverter.BuildURI(cms.CharBuffer, kv.Value.SubmissionID); } } _InternalDoPersist(cms, insertedData, updatedData, deletedData); var resultURI = new string[insertedData.Length]; for (int i = 0; i < resultURI.Length; i++) { resultURI[i] = insertedData[i].URI; } if (DatabaseQuery.InTransaction) { if (insertedData.Length > 0) { NotifyQueue.Enqueue(new NotifyInfo("FormABC.Input", NotifyInfo.OperationEnum.Insert, resultURI)); } if (updatedData.Count > 0) { NotifyQueue.Enqueue(new NotifyInfo("FormABC.Input", NotifyInfo.OperationEnum.Update, updatedData.Select(it => it.Key.URI).ToArray())); if (updatedData.Any(kv => kv.Key.URI != kv.Value.URI)) { NotifyQueue.Enqueue(new NotifyInfo("FormABC.Input", NotifyInfo.OperationEnum.Change, updatedData.Where(kv => kv.Key.URI != kv.Value.URI).Select(it => it.Value.URI).ToArray())); } } if (deletedData.Length > 0) { NotifyQueue.Enqueue(new NotifyInfo("FormABC.Input", NotifyInfo.OperationEnum.Delete, deletedData.Select(it => it.URI).ToArray())); } } else { if (insertedData.Length > 0) { Notifications.Notify(new NotifyInfo("FormABC.Input", NotifyInfo.OperationEnum.Insert, resultURI)); } if (updatedData.Count > 0) { Notifications.Notify(new NotifyInfo("FormABC.Input", NotifyInfo.OperationEnum.Update, updatedData.Select(it => it.Key.URI).ToArray())); if (updatedData.Any(kv => kv.Key.URI != kv.Value.URI)) { Notifications.Notify(new NotifyInfo("FormABC.Input", NotifyInfo.OperationEnum.Change, updatedData.Where(kv => kv.Key.URI != kv.Value.URI).Select(it => it.Value.URI).ToArray())); } } if (deletedData.Length > 0) { Notifications.Notify(new NotifyInfo("FormABC.Input", NotifyInfo.OperationEnum.Delete, deletedData.Select(it => it.URI).ToArray())); } } DataCache.Invalidate(updatedData.Select(it => it.Key.URI).Union(deletedData.Select(it => it.URI))); return(resultURI); }
public string[] Persist(IEnumerable <global::UseCase1.Form> insert, IEnumerable <KeyValuePair <global::UseCase1.Form, global::UseCase1.Form> > update, IEnumerable <global::UseCase1.Form> delete) { var insertedData = insert != null?insert.ToArray() : new global::UseCase1.Form[0]; var updatedData = update != null?update.ToList() : new List <KeyValuePair <global::UseCase1.Form, global::UseCase1.Form> >(); var deletedData = delete != null?delete.ToArray() : new global::UseCase1.Form[0]; if (insertedData.Length == 0 && updatedData.Count == 0 && deletedData.Length == 0) { return(new string[0]); } if (updatedData.Count > 0 && updatedData.Any(it => it.Key == null)) { //TODO fetch only null values var oldValues = Find(updatedData.Select(it => it.Value.URI)).ToDictionary(it => it.URI); if (oldValues.Count != updatedData.Count) { throw new ArgumentException("Can't find update value. Requested: {0}, found: {1}. Missing URI: {2}".With( updatedData.Count, oldValues.Count, string.Join(", ", updatedData.Select(it => it.Value.URI).Except(oldValues.Keys)))); } global::UseCase1.Form _val; for (int i = 0; i < updatedData.Count; i++) { _val = updatedData[i].Value; updatedData[i] = new KeyValuePair <global::UseCase1.Form, global::UseCase1.Form>(oldValues[_val.URI], _val); } } updatedData.RemoveAll(kv => kv.Key.Equals(kv.Value)); var cms = Common.Utility.UseThreadLocalStream(); for (int i = 0; i < insertedData.Length; i++) { insertedData[i].__InternalPrepareInsert(); } foreach (var it in updatedData) { it.Value.__InternalPrepareUpdate(); } for (int i = 0; i < deletedData.Length; i++) { deletedData[i].__InternalPrepareDelete(); } for (int i = 0; i < insertedData.Length; i++) { var it = insertedData[i]; it.__ReapplyReferences(); it.URI = _DatabaseCommon.FactoryUseCase1_Form.FormConverter.BuildURI(cms.CharBuffer, it.ID); if (it.GroupID != null) { it._GroupURI = _DatabaseCommon.FactoryUseCase1_FormGroup.FormGroupConverter.BuildURI(cms.CharBuffer, it.GroupID); } } foreach (var kv in updatedData) { kv.Value.__ReapplyReferences(); kv.Value.URI = _DatabaseCommon.FactoryUseCase1_Form.FormConverter.BuildURI(cms.CharBuffer, kv.Value.ID); if (kv.Value.GroupID != null) { kv.Value._GroupURI = _DatabaseCommon.FactoryUseCase1_FormGroup.FormGroupConverter.BuildURI(cms.CharBuffer, kv.Value.GroupID); } } var totalCount = insertedData.Length + updatedData.Count + deletedData.Length; var totalSize = insertedData.Sum(i => i._InternalGetSizeApproximation()) + updatedData.Sum(i => i.Key._InternalGetSizeApproximation()) + updatedData.Sum(i => i.Value._InternalGetSizeApproximation()) + deletedData.Sum(i => i._InternalGetSizeApproximation()); if (DatabaseQuery.InTransaction && (totalCount >= MinBatchSize || totalSize >= MaxObjectSize) && DatabaseQuery is IPostgresDatabaseQuery) { _InternalDoPersistLarge(cms, insertedData, updatedData, deletedData); } else { _InternalDoPersistStandard(cms, insertedData, updatedData, deletedData); } var resultURI = new string[insertedData.Length]; for (int i = 0; i < resultURI.Length; i++) { resultURI[i] = insertedData[i].URI; } if (DatabaseQuery.InTransaction) { if (insertedData.Length > 0) { NotifyQueue.Enqueue(new NotifyInfo("UseCase1.Form", NotifyInfo.OperationEnum.Insert, resultURI)); } if (updatedData.Count > 0) { NotifyQueue.Enqueue(new NotifyInfo("UseCase1.Form", NotifyInfo.OperationEnum.Update, updatedData.Select(it => it.Key.URI).ToArray())); if (updatedData.Any(kv => kv.Key.URI != kv.Value.URI)) { NotifyQueue.Enqueue(new NotifyInfo("UseCase1.Form", NotifyInfo.OperationEnum.Change, updatedData.Where(kv => kv.Key.URI != kv.Value.URI).Select(it => it.Value.URI).ToArray())); } } if (deletedData.Length > 0) { NotifyQueue.Enqueue(new NotifyInfo("UseCase1.Form", NotifyInfo.OperationEnum.Delete, deletedData.Select(it => it.URI).ToArray())); } } else { if (insertedData.Length > 0) { Notifications.Notify(new NotifyInfo("UseCase1.Form", NotifyInfo.OperationEnum.Insert, resultURI)); } if (updatedData.Count > 0) { Notifications.Notify(new NotifyInfo("UseCase1.Form", NotifyInfo.OperationEnum.Update, updatedData.Select(it => it.Key.URI).ToArray())); if (updatedData.Any(kv => kv.Key.URI != kv.Value.URI)) { Notifications.Notify(new NotifyInfo("UseCase1.Form", NotifyInfo.OperationEnum.Change, updatedData.Where(kv => kv.Key.URI != kv.Value.URI).Select(it => it.Value.URI).ToArray())); } } if (deletedData.Length > 0) { Notifications.Notify(new NotifyInfo("UseCase1.Form", NotifyInfo.OperationEnum.Delete, deletedData.Select(it => it.URI).ToArray())); } } DataCache.Invalidate(updatedData.Select(it => it.Key.URI).Union(deletedData.Select(it => it.URI))); return(resultURI); }