Beispiel #1
0
        private async Task <PushDataStoreResponse <T> > PushMultiPostActionsAsync()
        {
            var response           = new PushDataStoreResponse <T>();
            var limit              = 10 * Constants.NUMBER_LIMIT_OF_ENTITIES;
            var offset             = 0;
            var pendingPostActions = SyncQueue.GetFirstN(limit, offset, "POST");

            while (pendingPostActions != null && pendingPostActions.Count > 0)
            {
                var tasks = new List <Task <Tuple <PushDataStoreResponse <T>, int> > >();

                var realCountOfMultiInsertOperations = pendingPostActions.Count / (double)Constants.NUMBER_LIMIT_OF_ENTITIES;
                realCountOfMultiInsertOperations = Math.Ceiling(realCountOfMultiInsertOperations);

                for (var index = 0; index < realCountOfMultiInsertOperations; index++)
                {
                    var pendingWritePostActionsForPush = pendingPostActions.Skip(index * Constants.NUMBER_LIMIT_OF_ENTITIES).Take(Constants.NUMBER_LIMIT_OF_ENTITIES).ToList();

                    if (pendingWritePostActionsForPush.Count > 0)
                    {
                        tasks.Add(HandlePushMultiPOST(pendingWritePostActionsForPush));
                    }
                }

                await Task.WhenAll(tasks.ToArray()).ConfigureAwait(false);

                foreach (var task in tasks)
                {
                    response.AddEntities(task.Result.Item1.PushEntities);
                    response.AddExceptions(task.Result.Item1.KinveyExceptions);
                    offset += task.Result.Item2;
                }

                response.PushCount += pendingPostActions.Count;

                pendingPostActions = SyncQueue.GetFirstN(limit, offset, "POST");
            }
            return(response);
        }
Beispiel #2
0
        private async Task <PushDataStoreResponse <T> > PushSingleActionsAsync(string action = null)
        {
            var response = new PushDataStoreResponse <T>();
            var offset   = 0;
            var limit    = 10;

            var pendingActions = string.IsNullOrEmpty(action) ? SyncQueue.GetFirstN(limit, offset) :
                                 SyncQueue.GetFirstN(limit, offset, action);

            while (pendingActions != null && pendingActions.Count > 0)
            {
                var tasks = new List <Task <Tuple <T, KinveyException, int> > >();
                foreach (PendingWriteAction pwa in pendingActions)
                {
                    if (string.Equals("POST", pwa.action))
                    {
                        tasks.Add(HandlePushPOST(pwa));
                    }
                    else if (string.Equals("PUT", pwa.action))
                    {
                        tasks.Add(HandlePushPUT(pwa));
                    }
                    else if (string.Equals("DELETE", pwa.action))
                    {
                        tasks.Add(HandlePushDELETE(pwa));
                    }
                }

                try
                {
                    await Task.WhenAll(tasks.ToArray()).ConfigureAwait(false);
                }
                catch (Exception e)
                {
                    response.AddKinveyException(new KinveyException(EnumErrorCategory.ERROR_DATASTORE_NETWORK,
                                                                    EnumErrorCode.ERROR_JSON_RESPONSE,
                                                                    e.Message,
                                                                    e));
                }

                var resultEntities   = new List <T>();
                var kinveyExceptions = new List <KinveyException>();
                var resultCount      = 0;
                foreach (var task in tasks)
                {
                    if (!EqualityComparer <T> .Default.Equals(task.Result.Item1, default(T)))
                    {
                        resultEntities.Add(task.Result.Item1);
                    }

                    if (task.Result.Item2 != null)
                    {
                        kinveyExceptions.Add(task.Result.Item2);
                    }

                    offset += task.Result.Item3;

                    resultCount++;
                }

                response.AddEntities(resultEntities);
                response.AddExceptions(kinveyExceptions);
                response.PushCount += resultCount;

                pendingActions = string.IsNullOrEmpty(action) ? SyncQueue.GetFirstN(limit, offset) :
                                 SyncQueue.GetFirstN(limit, offset, action);
            }

            return(response);
        }