Esempio n. 1
0
        public Task CommitTransactionAsync(string message, bool restart = true)
        {
            return(RunOnMain(() =>
            {
                if (m_options.Dryrun)
                {
                    if (!restart)
                    {
                        if (m_transaction != null)
                        {
                            m_transaction.Rollback();
                        }
                        m_transaction = null;
                    }
                    return;
                }

                using (new Logging.Timer(LOGTAG, "CommitTransactionAsync", message))
                    m_transaction.Commit();
                if (restart)
                {
                    m_transaction = m_db.BeginTransaction();
                }
                else
                {
                    m_transaction = null;
                }
            }));
        }
Esempio n. 2
0
 public DatabaseCommon(LocalDatabase db, Options options)
     : base()
 {
     m_db          = db;
     m_options     = options;
     m_transaction = db.BeginTransaction();
 }
Esempio n. 3
0
        /// <summary>This will be called when _revsToInsert fills up:</summary>
        public void InsertRevisions(IList <IList <Object> > revs)
        {
            Log.I(Tag, this + " inserting " + revs.Count + " revisions...");
            //Log.v(Database.TAG, String.format("%s inserting %s", this, revs));

            revs.Sort(new RevisionComparer());

            if (LocalDatabase == null)
            {
                AsyncTaskFinished(revs.Count);
                return;
            }

            LocalDatabase.BeginTransaction();

            var success = false;

            try
            {
                foreach (var revAndHistory in revs)
                {
                    var rev          = (PulledRevision)revAndHistory[0];
                    var fakeSequence = rev.GetSequence();
                    var history      = (IList <String>)revAndHistory[1];

                    // Insert the revision:
                    try
                    {
                        LocalDatabase.ForceInsert(rev, history, RemoteUrl);
                    }
                    catch (CouchbaseLiteException e)
                    {
                        if (e.GetCBLStatus().GetCode() == StatusCode.Forbidden)
                        {
                            Log.I(Tag, this + ": Remote rev failed validation: " + rev);
                        }
                        else
                        {
                            Log.W(Tag, this + " failed to write " + rev + ": status=" + e.GetCBLStatus().GetCode());
                            LastError = new HttpException((Int32)e.GetCBLStatus().GetCode(), null);
                            continue;
                        }
                    }
                    pendingSequences.RemoveSequence(fakeSequence);
                }

                Log.W(Tag, this + " finished inserting " + revs.Count + " revisions");

                LastSequence = pendingSequences.GetCheckpointedValue();
                success      = true;
            }
            catch (SQLException e)
            {
                Log.E(Tag, this + ": Exception inserting revisions", e);
            }
            finally
            {
                LocalDatabase.EndTransaction(success);
                Log.D(Tag, this + "|" + Thread.CurrentThread() + ": insertRevisions() calling asyncTaskFinished()");
                AsyncTaskFinished(revs.Count);
            }
            CompletedChangesCount += revs.Count;
        }
Esempio n. 4
0
        /// <summary>This will be called when _revsToInsert fills up:</summary>
        public void InsertDownloads(IList <RevisionInternal> downloads)
        {
            Log.V(Tag, "Inserting " + downloads.Count + " revisions...");

            var time = DateTime.UtcNow;

            downloads.Sort(new RevisionComparer());

            if (LocalDatabase == null)
            {
                AsyncTaskFinished(downloads.Count);
                return;
            }

            LocalDatabase.BeginTransaction();

            var success = false;

            try
            {
                foreach (var rev in downloads)
                {
                    var fakeSequence = rev.GetSequence();
                    var history      = Database.ParseCouchDBRevisionHistory(rev.GetProperties());
                    if (history.Count == 0 && rev.GetGeneration() > 1)
                    {
                        Log.W(Tag, String.Format("{0}: Missing revision history in response for: {1}", this, rev));
                        SetLastError(new CouchbaseLiteException(StatusCode.UpStreamError));
                        RevisionFailed();
                        continue;
                    }

                    Log.V(Tag, String.Format("{0}: inserting {1} {2}", this, rev.GetDocId(), history));

                    // Insert the revision:
                    try
                    {
                        LocalDatabase.ForceInsert(rev, history, RemoteUrl);
                    }
                    catch (CouchbaseLiteException e)
                    {
                        if (e.GetCBLStatus().GetCode() == StatusCode.Forbidden)
                        {
                            Log.I(Tag, "Remote rev failed validation: " + rev);
                        }
                        else
                        {
                            Log.W(Tag, " failed to write " + rev + ": status=" + e.GetCBLStatus().GetCode());
                            RevisionFailed();
                            SetLastError(e);
                            continue;
                        }
                    }
                    pendingSequences.RemoveSequence(fakeSequence);
                }

                Log.W(Tag, " finished inserting " + downloads.Count + " revisions");

                success = true;
            }
            catch (Exception e)
            {
                Log.E(Tag, "Exception inserting revisions", e);
            }
            finally
            {
                LocalDatabase.EndTransaction(success);

                Log.D(Tag, "InsertRevisions() calling AsyncTaskFinished()");
                AsyncTaskFinished(downloads.Count);
            }

            // Checkpoint:
            LastSequence = pendingSequences.GetCheckpointedValue();

            var delta = (DateTime.UtcNow - time).TotalMilliseconds;

            Log.D(Tag, "inserted {0} revs in {1} milliseconds", downloads.Count, delta);

            var newCompletedChangesCount = CompletedChangesCount + downloads.Count;

            Log.D(Tag, "InsertDownloads() updating CompletedChangesCount from {0} -> {1}", CompletedChangesCount, newCompletedChangesCount);

            SafeAddToCompletedChangesCount(downloads.Count);

            Log.D(Tag, "InsertDownloads updating completedChangesCount from " + newCompletedChangesCount + " -> " + CompletedChangesCount);
        }