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

leonchen83 / redis-replicator / #2223

07 Jun 2025 04:23PM UTC coverage: 71.325% (-0.03%) from 71.356%
#2223

push

leonchen83
redis-8.0

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

3 existing lines in 2 files now uncovered.

6900 of 9674 relevant lines covered (71.33%)

0.71 hits per line

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

22.41
/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) {
88
        if (value == null) return false;
×
89
        if (value instanceof TTLValue) {
×
90
            TTLValue ev = (TTLValue) value;
×
91
            return map.containsValue(ev);
×
92
        }
93
        if (value instanceof byte[]) {
×
NEW
94
            return map.containsValue(new TTLValue((byte[]) value));
×
95
        }
96
        return false;
×
97
    }
98
    
99
    @Override
100
    public TTLValue get(Object key) {
101
        if (key != null && !(key instanceof byte[])) return null;
×
102
        return map.get(new Element((byte[]) key));
×
103
    }
104
    
105
    @Override
106
    public TTLValue put(byte[] key, TTLValue value) {
107
        return map.put(new Element(key), value);
1✔
108
    }
109
    
110
    @Override
111
    public void putAll(Map<? extends byte[], ? extends TTLValue> m) {
112
        if (m == null) return;
×
113
        for (Entry<? extends byte[], ? extends TTLValue> entry : m.entrySet()) {
×
114
            put(entry.getKey(), entry.getValue());
×
115
        }
×
116
    }
×
117
    
118
    @Override
119
    public TTLValue remove(Object key) {
120
        if (key != null && !(key instanceof byte[])) return null;
×
121
        return map.remove(new Element((byte[]) key));
×
122
    }
123
    
124
    @Override
125
    public void clear() {
126
        map.clear();
×
127
    }
×
128
    
129
    @Override
130
    public Set<byte[]> keySet() {
131
        return new KeySet();
×
132
    }
133
    
134
    @Override
135
    public Collection<TTLValue> values() {
136
        return new Values();
×
137
    }
138
    
139
    @Override
140
    public Set<Entry<byte[], TTLValue>> entrySet() {
141
        return new EntrySet();
1✔
142
    }
143
    
144
    private final class EntrySet extends AbstractSet<Entry<byte[], TTLValue>> {
1✔
145
        
146
        @Override
147
        public final int size() {
148
            return TTLByteArrayMap.this.size();
×
149
        }
150
        
151
        @Override
152
        public final void clear() {
153
            TTLByteArrayMap.this.clear();
×
154
        }
×
155
        
156
        @Override
157
        public final Iterator<Entry<byte[], TTLValue>> iterator() {
158
            return new EntryIterator();
1✔
159
        }
160
        
161
        @Override
162
        public final boolean contains(Object o) {
163
            if (!(o instanceof Map.Entry)) return false;
×
164
            Entry<?, ?> e = (Entry<?, ?>) o;
×
165
            Object k = e.getKey();
×
166
            Object v = e.getValue();
×
167
            if (k != null && !(k instanceof byte[])) return false;
×
168
            if (v != null && !(v instanceof TTLValue)) return false;
×
169
            byte[] key = (byte[]) k;
×
170
            TTLValue value = (TTLValue) v;
×
171
            if (!TTLByteArrayMap.this.containsKey(key)) return false;
×
172
            TTLValue val = TTLByteArrayMap.this.get(key);
×
173
            return Objects.equals(val, value);
×
174
        }
175
        
176
        @Override
177
        public final boolean remove(Object o) {
178
            if (!(o instanceof Map.Entry)) return false;
×
179
            Entry<?, ?> e = (Entry<?, ?>) o;
×
180
            Object k = e.getKey();
×
181
            Object v = e.getValue();
×
182
            if (k != null && !(k instanceof byte[])) return false;
×
183
            if (v != null && !(v instanceof TTLValue)) return false;
×
184
            byte[] key = (byte[]) k;
×
185
            TTLValue value = (TTLValue) v;
×
186
            if (!TTLByteArrayMap.this.containsKey(key)) return false;
×
187
            TTLValue val = TTLByteArrayMap.this.get(key);
×
188
            if (Objects.equals(val, value))
×
189
                return TTLByteArrayMap.this.remove(key) != null;
×
190
            return false;
×
191
        }
192
    }
193
    
194
    private final class KeySet extends AbstractSet<byte[]> {
×
195
        
196
        @Override
197
        public final int size() {
198
            return TTLByteArrayMap.this.size();
×
199
        }
200
        
201
        @Override
202
        public final void clear() {
203
            TTLByteArrayMap.this.clear();
×
204
        }
×
205
        
206
        @Override
207
        public final Iterator<byte[]> iterator() {
208
            return new KeyIterator();
×
209
        }
210
        
211
        @Override
212
        public final boolean contains(Object o) {
213
            return TTLByteArrayMap.this.containsKey(o);
×
214
        }
215
        
216
        @Override
217
        public final boolean remove(Object key) {
218
            return TTLByteArrayMap.this.remove(key) != null;
×
219
        }
220
    }
221
    
222
    private final class Values extends AbstractCollection<TTLValue> {
×
223
        
224
        @Override
225
        public final int size() {
226
            return TTLByteArrayMap.this.size();
×
227
        }
228
        
229
        @Override
230
        public final void clear() {
231
            TTLByteArrayMap.this.clear();
×
232
        }
×
233
        
234
        @Override
235
        public final Iterator<TTLValue> iterator() {
236
            return new ValueIterator();
×
237
        }
238
        
239
        @Override
240
        public final boolean contains(Object o) {
241
            return containsValue(o);
×
242
        }
243
        
244
    }
245
    
246
    private final class KeyIterator implements Iterator<byte[]> {
×
247
    
248
        private final Iterator<Element> iterator = map.keySet().iterator();
×
249
        
250
        @Override
251
        public boolean hasNext() {
252
            return iterator.hasNext();
×
253
        }
254
    
255
        @Override
256
        public byte[] next() {
257
            return iterator.next().bytes;
×
258
        }
259
    
260
        @Override
261
        public void remove() {
262
            iterator.remove();
×
263
        }
×
264
    }
265
    
266
    private final class ValueIterator implements Iterator<TTLValue> {
×
267
        
268
        private final Iterator<TTLValue> iterator = map.values().iterator();
×
269
        
270
        @Override
271
        public boolean hasNext() {
272
            return iterator.hasNext();
×
273
        }
274
        
275
        @Override
276
        public TTLValue next() {
277
            return iterator.next();
×
278
        }
279
        
280
        @Override
281
        public void remove() {
282
            iterator.remove();
×
283
        }
×
284
    }
285
    
286
    private final class EntryIterator implements Iterator<Entry<byte[], TTLValue>> {
1✔
287
        
288
        private final Iterator<Entry<Element, TTLValue>> iterator = map.entrySet().iterator();
1✔
289
        
290
        @Override
291
        public boolean hasNext() {
292
            return iterator.hasNext();
1✔
293
        }
294
        
295
        @Override
296
        public Entry<byte[], TTLValue> next() {
297
            Entry<Element, TTLValue> v = iterator.next();
1✔
298
            return new Node(v.getKey().bytes, v.getValue());
1✔
299
        }
300
        
301
        @Override
302
        public void remove() {
303
            iterator.remove();
×
304
        }
×
305
    }
306
    
307
    public static final class Node implements Entry<byte[], TTLValue>, Serializable {
308
        private static final long serialVersionUID = 1L;
309
        
310
        private final byte[] key;
311
        private TTLValue value;
312
        
313
        private Node(byte[] key, TTLValue value) {
1✔
314
            this.key = key;
1✔
315
            this.value = value;
1✔
316
        }
1✔
317
        
318
        @Override
319
        public byte[] getKey() {
320
            return key;
1✔
321
        }
322
        
323
        @Override
324
        public TTLValue getValue() {
325
            return this.value;
1✔
326
        }
327
        
328
        @Override
329
        public TTLValue setValue(TTLValue value) {
330
            TTLValue oldValue = this.value;
×
331
            this.value = value;
×
332
            return oldValue;
×
333
        }
334
        
335
        @Override
336
        public boolean equals(Object o) {
337
            if (this == o) return true;
×
338
            if (o == null || getClass() != o.getClass()) return false;
×
339
            Node node = (Node) o;
×
340
            return Objects.equals(value, node.value) && Arrays.equals(key, node.key);
×
341
        }
342
        
343
        @Override
344
        public int hashCode() {
345
            return Objects.hash(value, Arrays.hashCode(key));
×
346
        }
347
    }
348
}
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