summaryrefslogtreecommitdiffstats
path: root/mobile/android/services/src/main/java/org/mozilla/gecko/sync/repositories/uploaders/BufferSizeTracker.java
blob: 7f4c305f3a78b4f018c6fa673dfd69351c4ded7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.sync.repositories.uploaders;

import android.support.annotation.CallSuper;
import android.support.annotation.CheckResult;

/**
 * Implements functionality shared by BatchMeta and Payload objects, namely:
 * - keeping track of byte and record counts
 * - incrementing those counts when records are added
 * - checking if a record can fit
 */
/* @ThreadSafe */
public abstract class BufferSizeTracker {
    protected final Object accessLock;

    /* @GuardedBy("accessLock") */ private long byteCount = BatchingUploader.PER_PAYLOAD_OVERHEAD_BYTE_COUNT;
    /* @GuardedBy("accessLock") */ private long recordCount = 0;
    /* @GuardedBy("accessLock") */ protected Long smallestRecordByteCount;

    protected final long maxBytes;
    protected final long maxRecords;

    public BufferSizeTracker(Object accessLock, long maxBytes, long maxRecords) {
        this.accessLock = accessLock;
        this.maxBytes = maxBytes;
        this.maxRecords = maxRecords;
    }

    @CallSuper
    protected boolean canFit(long recordDeltaByteCount) {
        synchronized (accessLock) {
            return canFitRecordByteDelta(recordDeltaByteCount, recordCount, byteCount);
        }
    }

    protected boolean isEmpty() {
        synchronized (accessLock) {
            return recordCount == 0;
        }
    }

    /**
     * Adds a record and returns a boolean indicating whether batch is estimated to be full afterwards.
     */
    @CheckResult
    protected boolean addAndEstimateIfFull(long recordDeltaByteCount) {
        synchronized (accessLock) {
            // Sanity check. Calling this method when buffer won't fit the record is an error.
            if (!canFitRecordByteDelta(recordDeltaByteCount, recordCount, byteCount)) {
                throw new IllegalStateException("Buffer size exceeded");
            }

            byteCount += recordDeltaByteCount;
            recordCount += 1;

            if (smallestRecordByteCount == null || smallestRecordByteCount > recordDeltaByteCount) {
                smallestRecordByteCount = recordDeltaByteCount;
            }

            // See if we're full or nearly full after adding a record.
            // We're halving smallestRecordByteCount because we're erring
            // on the side of "can hopefully fit". We're trying to upload as soon as we know we
            // should, but we also need to be mindful of minimizing total number of uploads we make.
            return !canFitRecordByteDelta(smallestRecordByteCount / 2, recordCount, byteCount);
        }
    }

    protected long getByteCount() {
        synchronized (accessLock) {
            // Ensure we account for payload overhead twice when the batch is empty.
            // Payload overhead is either RECORDS_START ("[") or RECORDS_END ("]"),
            // and for an empty payload we need account for both ("[]").
            if (recordCount == 0) {
                return byteCount + BatchingUploader.PER_PAYLOAD_OVERHEAD_BYTE_COUNT;
            }
            return byteCount;
        }
    }

    protected long getRecordCount() {
        synchronized (accessLock) {
            return recordCount;
        }
    }

    @CallSuper
    protected void reset() {
        synchronized (accessLock) {
            byteCount = BatchingUploader.PER_PAYLOAD_OVERHEAD_BYTE_COUNT;
            recordCount = 0;
        }
    }

    @CallSuper
    protected boolean canFitRecordByteDelta(long byteDelta, long recordCount, long byteCount) {
        return recordCount < maxRecords
                && (byteCount + byteDelta) <= maxBytes;
    }
}