~ubuntu-branches/ubuntu/saucy/geary/saucy-updates

« back to all changes in this revision

Viewing changes to src/engine/db/db.vala

  • Committer: Package Import Robot
  • Author(s): Sebastien Bacher
  • Date: 2013-03-14 13:48:23 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20130314134823-gyk5av1g508zyj8a
Tags: 0.3.0~pr1-0ubuntu1
New upstream version (FFE lp: #1154316), supports multiple account as
well as full conversation views with inline replies

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
public const int64 INVALID_ROWID = -1;
22
22
 
 
23
private const int MAX_RETRY_SLEEP_MSEC = 1000;
 
24
private const int MIN_RETRY_SLEEP_MSEC = 200;
 
25
private const int RETRY_SLEEP_DEC_MSEC = 200;
 
26
 
23
27
[Flags]
24
28
public enum DatabaseFlags {
25
29
    NONE = 0,
51
55
 */
52
56
public delegate TransactionOutcome TransactionMethod(Connection cx, Cancellable? cancellable) throws Error;
53
57
 
 
58
// Used by exec_retry_locked().
 
59
private delegate int SqliteExecOperation();
 
60
 
54
61
/**
55
62
 * See http://www.sqlite.org/c3ref/threadsafe.html
56
63
 */
77
84
        throw new IOError.CANCELLED("%s cancelled", !String.is_empty(method) ? method : "Operation");
78
85
}
79
86
 
 
87
// This method is useful for dealing with BUSY retries in a consistent manner.
 
88
private int exec_retry_locked(Context ctx, string? method, SqliteExecOperation op, string? raw = null)
 
89
    throws Error {
 
90
    int count = 0;
 
91
    int sleep_msec = MAX_RETRY_SLEEP_MSEC;
 
92
    int total_msec = 0;
 
93
    int max_retry_msec = ctx.get_max_retry_msec();
 
94
    for (;;) {
 
95
        try {
 
96
            return throw_on_error(ctx, method, op(), raw);
 
97
        } catch (DatabaseError derr) {
 
98
            // if not BUSY, then immediately throw
 
99
            if (!(derr is DatabaseError.BUSY))
 
100
                throw derr;
 
101
            
 
102
            // if BUSY and total time has elapsed, throw
 
103
            if ((max_retry_msec > 0) && (total_msec >= max_retry_msec))
 
104
                throw derr;
 
105
        }
 
106
        
 
107
        // sleep and retry
 
108
        Thread.usleep(sleep_msec * 1000);
 
109
        
 
110
        total_msec += sleep_msec;
 
111
        sleep_msec = Numeric.int_floor(sleep_msec - RETRY_SLEEP_DEC_MSEC, MIN_RETRY_SLEEP_MSEC);
 
112
        
 
113
        debug("%s retrying: [%d] total_msec=%d %s", method, ++count, total_msec,
 
114
            (raw != null) ? raw : "");
 
115
    }
 
116
}
 
117
 
80
118
// Returns result if exception is not thrown
81
119
private int throw_on_error(Context ctx, string? method, int result, string? raw = null) throws DatabaseError {
82
120
    // fast-fail