• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

leonchen83 / redis-replicator / #2226

07 Jun 2025 04:27PM UTC coverage: 71.414% (+0.06%) from 71.356%
#2226

push

leonchen83
redis-8.0

0 of 2 new or added lines in 1 file covered. (0.0%)

3 existing lines in 2 files now uncovered.

6905 of 9669 relevant lines covered (71.41%)

0.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

23.42
/src/main/java/com/moilioncircle/redis/replicator/util/TTLByteArrayMap.java
1
/*
2
 * Copyright 2016-2018 Leon Chen
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package com.moilioncircle.redis.replicator.util;
18

19
import java.io.Serializable;
20
import java.util.AbstractCollection;
21
import java.util.AbstractSet;
22
import java.util.Arrays;
23
import java.util.Collection;
24
import java.util.HashMap;
25
import java.util.Iterator;
26
import java.util.LinkedHashMap;
27
import java.util.Map;
28
import java.util.Objects;
29
import java.util.Set;
30

31
import com.moilioncircle.redis.replicator.rdb.datatype.TTLValue;
32
import static com.moilioncircle.redis.replicator.util.ByteArrayMap.Element;
33

34
/**
35
 * @author Leon Chen
36
 * @since 3.9.0
37
 */
38
//@NonThreadSafe
39
public class TTLByteArrayMap implements Map<byte[], TTLValue>, Serializable {
40
    private static final long serialVersionUID = 1L;
41
    
42
    protected final Map<Element, TTLValue> map;
43
    
44
    public TTLByteArrayMap(Map<? extends byte[], ? extends TTLValue> m) {
45
        this(true, m);
×
46
    }
×
47
    
48
    public TTLByteArrayMap(boolean ordered, Map<? extends byte[], ? extends TTLValue> m) {
49
        this(ordered, m == null ? 0 : m.size(), 0.75f);
×
50
        putAll(m);
×
51
    }
×
52
    
53
    public TTLByteArrayMap() {
54
        this(true);
1✔
55
    }
1✔
56
    
57
    public TTLByteArrayMap(boolean ordered) {
58
        this(ordered, 16);
1✔
59
    }
1✔
60
    
61
    public TTLByteArrayMap(boolean ordered, int initialCapacity) {
62
        this(ordered, initialCapacity, 0.75f);
1✔
63
    }
1✔
64
    
65
    public TTLByteArrayMap(boolean ordered, int initialCapacity, float loadFactor) {
1✔
66
        if (ordered) map = new LinkedHashMap<>(initialCapacity, loadFactor);
1✔
67
        else map = new HashMap<>(initialCapacity, loadFactor);
×
68
    }
1✔
69
    
70
    @Override
71
    public int size() {
72
        return map.size();
1✔
73
    }
74
    
75
    @Override
76
    public boolean isEmpty() {
77
        return map.isEmpty();
1✔
78
    }
79
    
80
    @Override
81
    public boolean containsKey(Object key) {
82
        if (key != null && !(key instanceof byte[])) return false;
×
83
        return map.containsKey(new Element((byte[]) key));
×
84
    }
85
    
86
    @Override
87
    public boolean containsValue(Object value) {
NEW
88
        if (value != null && !(value instanceof TTLValue)) return false;
×
NEW
89
        return map.containsValue(new TTLValue((byte[]) value));
×
90
    }
91
    
92
    @Override
93
    public TTLValue get(Object key) {
94
        if (key != null && !(key instanceof byte[])) return null;
×
95
        return map.get(new Element((byte[]) key));
×
96
    }
97
    
98
    @Override
99
    public TTLValue put(byte[] key, TTLValue value) {
100
        return map.put(new Element(key), value);
1✔
101
    }
102
    
103
    @Override
104
    public void putAll(Map<? extends byte[], ? extends TTLValue> m) {
105
        if (m == null) return;
×
106
        for (Entry<? extends byte[], ? extends TTLValue> entry : m.entrySet()) {
×
107
            put(entry.getKey(), entry.getValue());
×
108
        }
×
109
    }
×
110
    
111
    @Override
112
    public TTLValue remove(Object key) {
113
        if (key != null && !(key instanceof byte[])) return null;
×
114
        return map.remove(new Element((byte[]) key));
×
115
    }
116
    
117
    @Override
118
    public void clear() {
119
        map.clear();
×
120
    }
×
121
    
122
    @Override
123
    public Set<byte[]> keySet() {
124
        return new KeySet();
×
125
    }
126
    
127
    @Override
128
    public Collection<TTLValue> values() {
129
        return new Values();
×
130
    }
131
    
132
    @Override
133
    public Set<Entry<byte[], TTLValue>> entrySet() {
134
        return new EntrySet();
1✔
135
    }
136
    
137
    private final class EntrySet extends AbstractSet<Entry<byte[], TTLValue>> {
1✔
138
        
139
        @Override
140
        public final int size() {
141
            return TTLByteArrayMap.this.size();
×
142
        }
143
        
144
        @Override
145
        public final void clear() {
146
            TTLByteArrayMap.this.clear();
×
147
        }
×
148
        
149
        @Override
150
        public final Iterator<Entry<byte[], TTLValue>> iterator() {
151
            return new EntryIterator();
1✔
152
        }
153
        
154
        @Override
155
        public final boolean contains(Object o) {
156
            if (!(o instanceof Map.Entry)) return false;
×
157
            Entry<?, ?> e = (Entry<?, ?>) o;
×
158
            Object k = e.getKey();
×
159
            Object v = e.getValue();
×
160
            if (k != null && !(k instanceof byte[])) return false;
×
161
            if (v != null && !(v instanceof TTLValue)) return false;
×
162
            byte[] key = (byte[]) k;
×
163
            TTLValue value = (TTLValue) v;
×
164
            if (!TTLByteArrayMap.this.containsKey(key)) return false;
×
165
            TTLValue val = TTLByteArrayMap.this.get(key);
×
166
            return Objects.equals(val, value);
×
167
        }
168
        
169
        @Override
170
        public final boolean remove(Object o) {
171
            if (!(o instanceof Map.Entry)) return false;
×
172
            Entry<?, ?> e = (Entry<?, ?>) o;
×
173
            Object k = e.getKey();
×
174
            Object v = e.getValue();
×
175
            if (k != null && !(k instanceof byte[])) return false;
×
176
            if (v != null && !(v instanceof TTLValue)) return false;
×
177
            byte[] key = (byte[]) k;
×
178
            TTLValue value = (TTLValue) v;
×
179
            if (!TTLByteArrayMap.this.containsKey(key)) return false;
×
180
            TTLValue val = TTLByteArrayMap.this.get(key);
×
181
            if (Objects.equals(val, value))
×
182
                return TTLByteArrayMap.this.remove(key) != null;
×
183
            return false;
×
184
        }
185
    }
186
    
187
    private final class KeySet extends AbstractSet<byte[]> {
×
188
        
189
        @Override
190
        public final int size() {
191
            return TTLByteArrayMap.this.size();
×
192
        }
193
        
194
        @Override
195
        public final void clear() {
196
            TTLByteArrayMap.this.clear();
×
197
        }
×
198
        
199
        @Override
200
        public final Iterator<byte[]> iterator() {
201
            return new KeyIterator();
×
202
        }
203
        
204
        @Override
205
        public final boolean contains(Object o) {
206
            return TTLByteArrayMap.this.containsKey(o);
×
207
        }
208
        
209
        @Override
210
        public final boolean remove(Object key) {
211
            return TTLByteArrayMap.this.remove(key) != null;
×
212
        }
213
    }
214
    
215
    private final class Values extends AbstractCollection<TTLValue> {
×
216
        
217
        @Override
218
        public final int size() {
219
            return TTLByteArrayMap.this.size();
×
220
        }
221
        
222
        @Override
223
        public final void clear() {
224
            TTLByteArrayMap.this.clear();
×
225
        }
×
226
        
227
        @Override
228
        public final Iterator<TTLValue> iterator() {
229
            return new ValueIterator();
×
230
        }
231
        
232
        @Override
233
        public final boolean contains(Object o) {
234
            return containsValue(o);
×
235
        }
236
        
237
    }
238
    
239
    private final class KeyIterator implements Iterator<byte[]> {
×
240
    
241
        private final Iterator<Element> iterator = map.keySet().iterator();
×
242
        
243
        @Override
244
        public boolean hasNext() {
245
            return iterator.hasNext();
×
246
        }
247
    
248
        @Override
249
        public byte[] next() {
250
            return iterator.next().bytes;
×
251
        }
252
    
253
        @Override
254
        public void remove() {
255
            iterator.remove();
×
256
        }
×
257
    }
258
    
259
    private final class ValueIterator implements Iterator<TTLValue> {
×
260
        
261
        private final Iterator<TTLValue> iterator = map.values().iterator();
×
262
        
263
        @Override
264
        public boolean hasNext() {
265
            return iterator.hasNext();
×
266
        }
267
        
268
        @Override
269
        public TTLValue next() {
270
            return iterator.next();
×
271
        }
272
        
273
        @Override
274
        public void remove() {
275
            iterator.remove();
×
276
        }
×
277
    }
278
    
279
    private final class EntryIterator implements Iterator<Entry<byte[], TTLValue>> {
1✔
280
        
281
        private final Iterator<Entry<Element, TTLValue>> iterator = map.entrySet().iterator();
1✔
282
        
283
        @Override
284
        public boolean hasNext() {
285
            return iterator.hasNext();
1✔
286
        }
287
        
288
        @Override
289
        public Entry<byte[], TTLValue> next() {
290
            Entry<Element, TTLValue> v = iterator.next();
1✔
291
            return new Node(v.getKey().bytes, v.getValue());
1✔
292
        }
293
        
294
        @Override
295
        public void remove() {
296
            iterator.remove();
×
297
        }
×
298
    }
299
    
300
    public static final class Node implements Entry<byte[], TTLValue>, Serializable {
301
        private static final long serialVersionUID = 1L;
302
        
303
        private final byte[] key;
304
        private TTLValue value;
305
        
306
        private Node(byte[] key, TTLValue value) {
1✔
307
            this.key = key;
1✔
308
            this.value = value;
1✔
309
        }
1✔
310
        
311
        @Override
312
        public byte[] getKey() {
313
            return key;
1✔
314
        }
315
        
316
        @Override
317
        public TTLValue getValue() {
318
            return this.value;
1✔
319
        }
320
        
321
        @Override
322
        public TTLValue setValue(TTLValue value) {
323
            TTLValue oldValue = this.value;
×
324
            this.value = value;
×
325
            return oldValue;
×
326
        }
327
        
328
        @Override
329
        public boolean equals(Object o) {
330
            if (this == o) return true;
×
331
            if (o == null || getClass() != o.getClass()) return false;
×
332
            Node node = (Node) o;
×
333
            return Objects.equals(value, node.value) && Arrays.equals(key, node.key);
×
334
        }
335
        
336
        @Override
337
        public int hashCode() {
338
            return Objects.hash(value, Arrays.hashCode(key));
×
339
        }
340
    }
341
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc