public UpdateResults UpdateListItems(string listName, UpdateBatch updateBatch) { if (String.IsNullOrEmpty(listName)) throw new ArgumentNullException("listName"); if (updateBatch.Count == 0) throw new ArgumentException("Batch contains no updates", "updateBatch"); // OLD VERSION //XmlNode response = listService.UpdateListItems(listName, updateBatch.GetCamlUpdateBatch()); //return response.GetXElement().GetCamlUpdateResults(); //NEW UpdateResults results = null; var batch = updateBatch.GetCamlUpdateBatch(out results); if (batch.ChildNodes.Count > 0) { try { XmlNode response = listService.UpdateListItems(listName, batch); var serverResults = response.GetXElement().GetCamlUpdateResults(); if (serverResults == null) return results; if (results != null) foreach (var r in results) serverResults.Add(r); return serverResults; } catch (Exception ex) { //(ex as System.Net.WebException).Status = Timeout , Internalstatus = RequestFatal , Response = null //connection exception UpdateResults errorresults = new UpdateResults(); foreach (UpdateItem item in updateBatch) errorresults.Add(new UpdateResult() { ErrorCode = UpdateResult.GenericError, ErrorMessage = "Connection Timeout", Command = item.Command, ItemData = item.ChangedItemData, UpdateItemID = item.ID }); return errorresults; } } return results; //END OF NEW }
/// <summary> /// /// </summary> /// <param name="inserts"></param> /// <param name="updates"></param> /// <param name="deletes"></param> /// <param name="connection"></param> /// #UPLOAD 3 public void Update(DataTable changes, SpConnection connection, out Collection<SyncConflict> errors) { errors = new Collection<SyncConflict>(); if (changes == null) throw new ArgumentNullException("changes"); if (connection == null) throw new ArgumentNullException("connection"); int _batchSize = 25; int segmentsCount = (int)Math.Round( Math.Ceiling((double)changes.Rows.Count/ _batchSize),0); if (IgnoreColumnsOnUpdate != null) { // case to be handled // cannot remove Sharepoint ID. // cannot remove Primary Key of DataTable? foreach (string ignoredColumn in IgnoreColumnsOnUpdate) { string clientColumn = GetClientColumnFromServerColumn(ignoredColumn); if (clientColumn != null && changes.Columns.Contains(clientColumn)) { changes.Columns.Remove(clientColumn); } } } DataTable changesTotal = changes.Copy(); for (int i = 0; i < segmentsCount; i++) { changes.Rows.Clear(); CopyRows(changesTotal, changes, i * _batchSize, _batchSize); //SEND SEGMENT UpdateBatch batch = new UpdateBatch(); string clientIdColumn = GetClientColumnFromServerColumn("ID"); if (!changes.Columns.Contains(clientIdColumn)) throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Messages.ColumnIDNotContained, clientIdColumn)); IDictionary<int, DataRow> IdMapping = new Dictionary<int, DataRow>(); foreach (DataRow row in changes.Rows) { UpdateItem u = batch.CreateNewItem(); switch (row.RowState) { case DataRowState.Added: u.Command = UpdateCommands.Insert; break; case DataRowState.Deleted: u.Command = UpdateCommands.Delete; break; case DataRowState.Modified: u.Command = UpdateCommands.Update; break; case DataRowState.Unchanged: continue; } if (u.Command == UpdateCommands.Delete) row.RejectChanges(); if (u.Command != UpdateCommands.Insert) { if (!(row[clientIdColumn] is DBNull)) { u.ListItemID = (int)row[clientIdColumn]; } else { continue; } } if (u.Command != UpdateCommands.Delete) { ListItem item = new ListItem(); Exception e; MapDataRowToListItem(row, item, out e); u.ChangedItemData = item; if (e != null && SyncTracer.IsErrorEnabled()) SyncTracer.Error(e.ToString()); } batch.Add(u); IdMapping[u.ID] = row; if (u.Command == UpdateCommands.Delete) row.Delete(); } if (batch.Count != 0) { //try //{ UpdateResults results = connection.UpdateListItems(this.ListName, batch); // FIX: errors must be handled appropriately foreach (UpdateResult r in results) { if (!r.IsSuccess()) { if (!IdMapping.ContainsKey(r.UpdateItemID)) throw new InvalidOperationException( String.Format(CultureInfo.CurrentCulture, Messages.NoIDMapping, r.UpdateItemID)); DataRow clientRow = IdMapping[r.UpdateItemID]; errors.Add(CreateSyncError(r, clientRow)); } } //} //catch (Exception ex) //{ ////usually connection error // foreach (UpdateItem item in batch) // { // if (!IdMapping.ContainsKey(item.ID)) // throw new InvalidOperationException( // String.Format(CultureInfo.CurrentCulture, Messages.NoIDMapping, r.UpdateItemID)); // DataRow clientRow = IdMapping[item.ID]; // errors.Add(CreateSyncError(new UpdateResult(, clientRow)); // } //} } //END SEND SEGMENT } if (errors.Count == 0) errors = null; }
public void UpdateBatch_Works() { var person = new HistoricalPerson { FirstName = "John", LastName = "Doe", BirthDate = new DateTime(1920, 3, 1), DeathDate = new DateTime(1978, 10, 5), }; int numberOfPropertyChangedEvents = 0; int numberOfPropertyValueChangedEvents = 0; person.PropertyChanged += (s, e) => { numberOfPropertyChangedEvents++; }; person.PropertyValueChanged += (s, e) => numberOfPropertyValueChangedEvents++; using(var updateBatch = new UpdateBatch()) { updateBatch.Register(person); person.FirstName = "Jane"; person.FirstName = "Jack"; person.BirthDate = new DateTime(1919, 3, 2); person.DeathDate = new DateTime(1991, 5, 6); person.DeathDate = new DateTime(1986, 7, 12); } Assert.AreEqual(4, numberOfPropertyChangedEvents); Assert.AreEqual(8, numberOfPropertyValueChangedEvents); }
public void ManyRowBatchInsertTest() { var client = new PhoenixClient(_credentials); string connId = GenerateRandomConnId(); RequestOptions options = RequestOptions.GetGatewayDefaultOptions(); // In gateway mode, url format will be https://<cluster dns name>.azurehdinsight.net/hbasephoenix<N>/ // Requests sent to hbasephoenix0/ will be forwarded to PQS on workernode0 options.AlternativeEndpoint = "hbasephoenix0/"; string tableName = "Persons" + connId; OpenConnectionResponse openConnResponse = null; CreateStatementResponse createStatementResponse = null; try { // Opening connection pbc::MapField<string, string> info = new pbc::MapField<string, string>(); openConnResponse = client.OpenConnectionRequestAsync(connId, info, options).Result; // Syncing connection ConnectionProperties connProperties = new ConnectionProperties { HasAutoCommit = true, AutoCommit = true, HasReadOnly = true, ReadOnly = false, TransactionIsolation = 0, Catalog = "", Schema = "", IsDirty = true }; client.ConnectionSyncRequestAsync(connId, connProperties, options).Wait(); // Creating statement createStatementResponse = client.CreateStatementRequestAsync(connId, options).Result; // Running query 1 string sql1 = "CREATE TABLE " + tableName + " (LastName varchar(255) PRIMARY KEY,FirstName varchar(255))"; ExecuteResponse execResponse1 = client.PrepareAndExecuteRequestAsync(connId, sql1, createStatementResponse.StatementId, long.MaxValue, int.MaxValue, options).Result; // Running query 2 // Batching two sqls in a single HTTP request pbc::RepeatedField<string> list = new pbc.RepeatedField<string>(); list.Add("UPSERT INTO " + tableName + " VALUES('d1','x1')"); list.Add("UPSERT INTO " + tableName + " VALUES('d2','x2')"); ExecuteBatchResponse execResponse2 = client.PrepareAndExecuteBatchRequestAsync(connId, createStatementResponse.StatementId, list, options).Result; // Running query 3 string sql3 = "select count(*) from " + tableName; ExecuteResponse execResponse3 = client.PrepareAndExecuteRequestAsync(connId, sql3, createStatementResponse.StatementId, long.MaxValue, int.MaxValue, options).Result; long count3 = execResponse3.Results[0].FirstFrame.Rows[0].Value[0].ScalarValue.NumberValue; Assert.AreEqual(2, count3); // Running query 4 // Batching two sqls in a single HTTP request // Creating statement 2 string sql4 = "UPSERT INTO " + tableName + " VALUES (?,?)"; PrepareResponse prepareResponse = client.PrepareRequestAsync(connId, sql4, int.MaxValue, options).Result; StatementHandle statementHandle = prepareResponse.Statement; pbc::RepeatedField<UpdateBatch> updates = new pbc.RepeatedField<UpdateBatch>(); for (int i = 3; i < 10; i++) { pbc::RepeatedField<TypedValue> parameterValues = new pbc.RepeatedField<TypedValue>(); TypedValue v1 = new TypedValue { StringValue = "d" + i, Type = Rep.String }; TypedValue v2 = new TypedValue { StringValue = "x" + i, Type = Rep.String }; parameterValues.Add(v1); parameterValues.Add(v2); UpdateBatch batch = new UpdateBatch { ParameterValues = parameterValues }; updates.Add(batch); } ExecuteBatchResponse execResponse4 = client.ExecuteBatchRequestAsync(connId, statementHandle.Id, updates, options).Result; // Running query 5 string sql5 = "select count(*) from " + tableName; ExecuteResponse execResponse5 = client.PrepareAndExecuteRequestAsync(connId, sql5, createStatementResponse.StatementId, long.MaxValue, int.MaxValue, options).Result; long count5 = execResponse5.Results[0].FirstFrame.Rows[0].Value[0].ScalarValue.NumberValue; Assert.AreEqual(9, count5); // Running query 5 string sql6 = "DROP TABLE " + tableName; client.PrepareAndExecuteRequestAsync(connId, sql6, createStatementResponse.StatementId, long.MaxValue, int.MaxValue, options).Wait(); } catch (Exception ex) { Assert.Fail(ex.Message); } finally { if (createStatementResponse != null) { client.CloseStatementRequestAsync(connId, createStatementResponse.StatementId, options).Wait(); createStatementResponse = null; } if (openConnResponse != null) { client.CloseConnectionRequestAsync(connId, options).Wait(); openConnResponse = null; } } }
public void RegisterUpdate(UpdateBatch batch, object entity) { _parent.RegisterUpdate(batch, entity); }
public void RegisterUpdate(UpdateBatch batch, object entity, string json) { throw new NotImplementedException(); }