Пример #1
0
 private bool SendTransactions(SyncContext context, Func<SyncContext, Message, bool> messageSink, IEnumerable<ITransactionInfo> transactionsToSend, string storeId,
                               Guid prevId)
 {
     foreach (var txn in transactionsToSend.Where(t => t.TransactionStatus == TransactionStatus.CompletedOk))
     {
         if (txn.TransactionType == TransactionType.UpdateTransaction)
         {
             var job = _serverCore.LoadTransaction(storeId, txn);
             if (job is UpdateTransaction)
             {
                 var updateJob = job as UpdateTransaction;
                 var syncTransaction = new ClusterUpdateTransaction
                                           {
                                               StoreId = storeId,
                                               PrevTxnId = prevId,
                                               JobId = txn.JobId,
                                               Preconditions = updateJob.Preconditions,
                                               Deletes = updateJob.DeletePatterns,
                                               Inserts = updateJob.InsertData
                                           };
                 if (!messageSink(context, syncTransaction.AsMessage()))
                 {
                     // If message sink stops accepting messages, fail the entire sync process immediately
                     return false;
                 }
             }
             else
             {
                 return false;
             }
         }
         else if (txn.TransactionType == TransactionType.SparqlUpdateTransaction)
         {
             var job = _serverCore.LoadTransaction(storeId, txn);
             if (job is SparqlUpdateJob)
             {
                 var sparqlJob = job as SparqlUpdateJob;
                 var syncTransaction = new ClusterSparqlTransaction
                                           {
                                               StoreId = storeId,
                                               PrevTxnId = prevId,
                                               JobId = txn.JobId,
                                               Expression = sparqlJob.Expression
                                           };
                 if (!messageSink(context, syncTransaction.AsMessage()))
                 {
                     // If message sink stops accepting messages, fail the entire sync process immediately
                     return false;
                 }
             }
         }
         else
         {
             return false;
         }
     }
     return true;
 }
Пример #2
0
        public bool SyncSlave(Dictionary<string, string> lastTxn, SyncContext context, Func<SyncContext, Message, bool> messageSink )
        {
            bool syncOk = true;
            // Push existing transactions down the pipe
            foreach(var storeId in _stores)
            {
                IEnumerable<ITransactionInfo> transactionsToSend;
                if (lastTxn.ContainsKey(storeId))
                {
                    Guid lastJobId = Guid.Parse(lastTxn[storeId]);
                    transactionsToSend = _serverCore.GetTransactions(storeId).SkipWhile(t => !t.JobId.Equals(lastJobId)).Skip(1);
                    syncOk &= SendTransactions(context, messageSink, transactionsToSend, storeId, lastJobId);
                }
                else
                {
                    // For all stores that aren't listed in the lastTxn dictionary
                    // Send a create store message followed byt all transactions
                    var createMessage = new Message("+store", storeId);
                    if (messageSink(context, createMessage))
                    {
                        syncOk &= SendTransactions(context, messageSink, _serverCore.GetTransactions(storeId), storeId,
                                                   Guid.Empty);
                    }
                    else
                    {
                        syncOk = false;
                    }
                }

                
            }
            
            // For all stores that are listed in lastTxn dictionary and not in our store list
            // send a delete store message
            foreach (var storeId in lastTxn.Keys.Where(s=>!_stores.Contains(s)))
            {
                var deleteMessage = new Message("-store", storeId);
                messageSink(context, deleteMessage);
            }

            return syncOk;
        }