aboutsummaryrefslogtreecommitdiff
path: root/s2storage/src/readonly/java/com/android/timezone/location/storage/tzs2range/read/PopulatedSuffixTableBlock.java
blob: 2b719da1b3a24b6d5d9d2cc78cb1d5b97a2b5d63 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package com.android.timezone.location.storage.tzs2range.read;

import static com.android.timezone.location.storage.s2.S2Support.MAX_FACE_ID;
import static com.android.timezone.location.storage.s2.S2Support.cellIdToString;
import static com.android.timezone.location.storage.util.Conditions.checkStateInRange;

import com.android.timezone.location.storage.table.packed.read.IntValueTypedPackedTable;
import com.android.timezone.location.storage.table.reader.IntValueTable;
import com.android.timezone.location.storage.tzs2range.SuffixTableRange;
import com.android.timezone.location.storage.tzs2range.SuffixTableSharedData;
import com.android.timezone.location.storage.tzs2range.TzS2RangeFileFormat;

import java.util.Objects;

/**
 * An implementation of {@link SuffixTableBlock.SuffixTableBlockDelegate} for tables that are backed
 * by real block data, i.e. have one or more entries.
 *
 * <p>Logically, each populated suffix table block holds one or more entries for S2 ranges that
 * have an associated set of time zone IDs, e.g.:
 * <pre>
 *     startCellId=X, endCellId=Y, tzIdSet={"America/Los_Angeles", "America/Phoenix"}
 * </pre>
 *
 * <p>The storage of the range entries is as follows:
 * <ul>
 *     <li>The prefix bits are all the same so need not be stored in the individual entries. Only
 *     the suffix bits of X are stored. The prefix determines the block ID when first locating the
 *     suffix table, but it is also (redundantly) stored in the table's header for simplicity /
 *     easy debugging.</li>
 *     <li>Each range is expected to be relatively short, so Y is stored as a offset adjustment to
 *     X, i.e. Y is calculated by advancing X by {length of range} cell IDs.</li>
 *     <li>The "tzIdSet" is stored by referring to a "set ID" from a {@link
 *     com.android.timezone.location.storage.tzs2range.BankedTzIdSets.Bank} read from the file's
 *     header. The specific bank to use for a table is held in the suffix table's header. This means
 *     that identifying the set ID for each range can be done using a comparatively small number of
 *     bits.</li>
 * </ul>
 */
final class PopulatedSuffixTableBlock implements SuffixTableBlock.SuffixTableBlockDelegate {

    private final TzS2RangeFileFormat mFileFormat;

    private final IntValueTypedPackedTable mPackedTable;

    private final SuffixTableSharedData mSuffixTableSharedData;

    private final int mPrefix;

    PopulatedSuffixTableBlock(
            TzS2RangeFileFormat fileFormat, IntValueTypedPackedTable packedTable) {
        mFileFormat = Objects.requireNonNull(fileFormat);
        mPackedTable = Objects.requireNonNull(packedTable);
        mSuffixTableSharedData = SuffixTableSharedData.fromBytes(packedTable.getSharedData());

        // Obtain the prefix. All cellIds in this table will share the same prefix except for end
        // range values (which are exclusive so can be for mPrefix + 1 with a suffix value of 0).
        mPrefix = mSuffixTableSharedData.getTablePrefix();
    }

    @Override
    public int getPrefix() {
        return mPrefix;
    }

    @Override
    public SuffixTableBlock.Entry findEntryByCellId(long cellId) {
        int suffixValue = mFileFormat.extractSuffixValueFromCellId(cellId);
        S2CellMatcher matcher = new S2CellMatcher(mFileFormat, suffixValue);
        return findEntryWithMatcher(matcher);
    }

    @Override
    public SuffixTableBlock.Entry findEntryByIndex(int i) {
        return new Entry(mPackedTable.getEntryByIndex(i));
    }

    @Override
    public int getEntryCount() {
        return mPackedTable.getEntryCount();
    }

    @Override
    public int getTzIdSetBank() {
        return mSuffixTableSharedData.getTzIdSetBank();
    }

    /**
     * Returns an entry that matches the supplied matcher. If multiple entries match, an arbitrary
     * matching entry is returned. If no entries match then {@code null} is returned.
     */
    private SuffixTableBlock.Entry findEntryWithMatcher(
            IntValueTable.IntValueEntryMatcher matcher) {
        IntValueTable.TableEntry suffixTableEntry = mPackedTable.findEntry(matcher);
        if (suffixTableEntry == null) {
            return null;
        }
        return new Entry(suffixTableEntry);
    }

    /**
     * An {@link IntValueTable.IntValueEntryMatcher} capable of interpreting and matching the
     * key/value from the underlying table against a search suffix value.
     */
    private static final class S2CellMatcher implements IntValueTable.IntValueEntryMatcher {

        private final TzS2RangeFileFormat mFileFormat;

        private final int mSuffixSearchValue;

        public S2CellMatcher(TzS2RangeFileFormat fileFormat, int suffixSearchValue) {
            mFileFormat = Objects.requireNonNull(fileFormat);
            mSuffixSearchValue = suffixSearchValue;
        }

        @Override
        public int compare(int key, int value) {
            int rangeStartCellIdOffset = key;
            if (mSuffixSearchValue < rangeStartCellIdOffset) {
                return -1;
            } else {
                int rangeLength = mFileFormat.extractRangeLengthFromTableEntryValue(value);
                int rangeEndCellIdOffset = rangeStartCellIdOffset + rangeLength;
                if (mSuffixSearchValue >= rangeEndCellIdOffset) {
                    return 1;
                } else {
                    return 0;
                }
            }
        }
    }

    /**
     * An entry from the {@link SuffixTableBlock}. Use {@link #getSuffixTableRange()} to get the
     * full, interpreted entry data.
     */
    public final class Entry extends SuffixTableBlock.Entry {

        private final IntValueTable.TableEntry mSuffixTableEntry;

        private SuffixTableRange suffixTableRange;

        Entry(IntValueTable.TableEntry suffixTableEntry) {
            mSuffixTableEntry = Objects.requireNonNull(suffixTableEntry);
        }

        @Override
        public int getIndex() {
            return mSuffixTableEntry.getIndex();
        }

        @Override
        public int getTzIdSetBank() {
            return mSuffixTableSharedData.getTzIdSetBank();
        }

        /** Returns the data for this entry. */
        @Override
        public SuffixTableRange getSuffixTableRange() {
            // Creating SuffixTableRange is relatively expensive so it is created lazily and
            // memoized.
            if (suffixTableRange == null) {
                // Create the range to return.
                int startCellIdSuffix = mSuffixTableEntry.getKey();
                checkStateInRange("startCellIdSuffixBits", startCellIdSuffix,
                        "minSuffixValue", 0, "maxSuffixValue", mFileFormat.getMaxSuffixValue());
                long startCellId = mFileFormat.createCellId(mPrefix, startCellIdSuffix);

                int value = mSuffixTableEntry.getValue();
                int rangeLength = mFileFormat.extractRangeLengthFromTableEntryValue(value);
                checkStateInRange("rangeLength", rangeLength, "minRangeLength", 0, "maxRangeLength",
                        mFileFormat.getTableEntryMaxRangeLengthValue());
                int endCellIdSuffix = startCellIdSuffix + rangeLength;

                int endCellPrefixValue = mPrefix;
                if (endCellIdSuffix > mFileFormat.getMaxSuffixValue()) {
                    // Handle the special case where the range ends in the next prefix. This is
                    // because the range end is exclusive, so the end value is allowed to be first
                    // cell ID from the next prefix.
                    if (endCellIdSuffix != mFileFormat.getMaxSuffixValue() + 1) {
                        throw new IllegalStateException("Range exceeds allowable cell IDs:"
                                + " startCellId=" + cellIdToString(startCellId)
                                + ", rangeLength=" + rangeLength);
                    }
                    endCellPrefixValue += 1;

                    // Check to see if the face ID has overflowed, and wrap to face zero if it has.
                    if (mFileFormat.extractFaceIdFromPrefix(endCellPrefixValue) > MAX_FACE_ID) {
                        endCellPrefixValue = 0;
                    }
                    endCellIdSuffix = 0;
                }
                long endCellId = mFileFormat.createCellId(endCellPrefixValue, endCellIdSuffix);

                int tzIdSetId = mFileFormat.extractTzIdSetIdFromTableEntryValue(value);
                suffixTableRange = new SuffixTableRange(startCellId, endCellId, tzIdSetId);
            }
            return suffixTableRange;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            Entry entry = (Entry) o;
            return mSuffixTableEntry.equals(entry.mSuffixTableEntry);
        }

        @Override
        public int hashCode() {
            return Objects.hash(mSuffixTableEntry);
        }

        @Override
        public String toString() {
            return "Entry{"
                    + "mSuffixTableEntry=" + mSuffixTableEntry
                    + '}';
        }
    }
}