Skip to content

Commit bf29f7f

Browse files
authored
HDDS-13235. The equals/hashCode methods in anonymous KeyValue classes may not work. (#8607)
1 parent 6ff3ad6 commit bf29f7f

18 files changed

Lines changed: 121 additions & 360 deletions

File tree

hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/metadata/SchemaOneDeletedBlocksTable.java

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
package org.apache.hadoop.ozone.container.metadata;
1919

2020
import java.io.IOException;
21-
import java.util.ArrayList;
2221
import java.util.List;
22+
import java.util.stream.Collectors;
2323
import org.apache.hadoop.hdds.utils.MetadataKeyFilters;
2424
import org.apache.hadoop.hdds.utils.db.BatchOperation;
2525
import org.apache.hadoop.hdds.utils.db.Table;
@@ -145,39 +145,13 @@ private static String unprefix(String key) {
145145
private static List<KeyValue<String, ChunkInfoList>> unprefix(
146146
List<? extends KeyValue<String, ChunkInfoList>> kvs) {
147147

148-
List<KeyValue<String, ChunkInfoList>> processedKVs = new ArrayList<>();
149-
kvs.forEach(kv -> processedKVs.add(new UnprefixedKeyValue(kv)));
150-
151-
return processedKVs;
148+
return kvs.stream()
149+
.map(kv -> Table.newKeyValue(unprefix(kv.getKey()), kv.getValue()))
150+
.collect(Collectors.toList());
152151
}
153152

154153
private static MetadataKeyFilters.KeyPrefixFilter getDeletedFilter() {
155154
return (new MetadataKeyFilters.KeyPrefixFilter())
156155
.addFilter(DELETED_KEY_PREFIX);
157156
}
158-
159-
/**
160-
* {@link KeyValue} implementation that removes the deleted key prefix from
161-
* an existing {@link KeyValue}.
162-
*/
163-
private static class UnprefixedKeyValue implements KeyValue<String,
164-
ChunkInfoList> {
165-
166-
private final KeyValue<String, ChunkInfoList> prefixedKeyValue;
167-
168-
UnprefixedKeyValue(
169-
KeyValue<String, ChunkInfoList> prefixedKeyValue) {
170-
this.prefixedKeyValue = prefixedKeyValue;
171-
}
172-
173-
@Override
174-
public String getKey() throws IOException {
175-
return unprefix(prefixedKeyValue.getKey());
176-
}
177-
178-
@Override
179-
public ChunkInfoList getValue() throws IOException {
180-
return prefixedKeyValue.getValue();
181-
}
182-
}
183157
}

hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/db/RDBStoreByteArrayIterator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ byte[] key() {
4040
@Override
4141
Table.KeyValue<byte[], byte[]> getKeyValue() {
4242
final ManagedRocksIterator i = getRocksDBIterator();
43-
return RawKeyValue.create(i.get().key(), i.get().value());
43+
return Table.newKeyValue(i.get().key(), i.get().value());
4444
}
4545

4646
@Override

hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/db/RawKeyValue.java

Lines changed: 0 additions & 85 deletions
This file was deleted.

hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/db/Table.java

Lines changed: 40 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -325,94 +325,58 @@ void deleteBatchWithPrefix(BatchOperation batch, KEY prefix)
325325
/**
326326
* Class used to represent the key and value pair of a db entry.
327327
*/
328-
interface KeyValue<KEY, VALUE> {
329-
330-
KEY getKey() throws IOException;
328+
final class KeyValue<K, V> {
329+
private final K key;
330+
private final V value;
331+
private final int rawSize;
332+
333+
private KeyValue(K key, V value, int rawSize) {
334+
this.key = key;
335+
this.value = value;
336+
this.rawSize = rawSize;
337+
}
331338

332-
VALUE getValue() throws IOException;
339+
public K getKey() {
340+
return key;
341+
}
333342

334-
default int getRawSize() throws IOException {
335-
return 0;
343+
public V getValue() {
344+
return value;
336345
}
337-
}
338346

339-
static <K, V> KeyValue<K, V> newKeyValue(K key, V value) {
340-
return new KeyValue<K, V>() {
341-
@Override
342-
public K getKey() {
343-
return key;
344-
}
347+
public int getRawSize() {
348+
return rawSize;
349+
}
345350

346-
@Override
347-
public V getValue() {
348-
return value;
349-
}
351+
@Override
352+
public String toString() {
353+
return "(key=" + key + ", value=" + value + ")";
354+
}
350355

351-
@Override
352-
public String toString() {
353-
return "(key=" + key + ", value=" + value + ")";
356+
@Override
357+
public boolean equals(Object obj) {
358+
if (this == obj) {
359+
return true;
360+
} else if (!(obj instanceof KeyValue)) {
361+
return false;
354362
}
363+
final KeyValue<?, ?> that = (KeyValue<?, ?>) obj;
364+
return this.getKey().equals(that.getKey())
365+
&& this.getValue().equals(that.getValue());
366+
}
355367

356-
@Override
357-
public boolean equals(Object obj) {
358-
if (!(obj instanceof KeyValue)) {
359-
return false;
360-
}
361-
KeyValue<?, ?> kv = (KeyValue<?, ?>) obj;
362-
try {
363-
return getKey().equals(kv.getKey()) && getValue().equals(kv.getValue());
364-
} catch (IOException e) {
365-
throw new RuntimeException(e);
366-
}
367-
}
368+
@Override
369+
public int hashCode() {
370+
return Objects.hash(getKey(), getValue());
371+
}
372+
}
368373

369-
@Override
370-
public int hashCode() {
371-
return Objects.hash(getKey(), getValue());
372-
}
373-
};
374+
static <K, V> KeyValue<K, V> newKeyValue(K key, V value) {
375+
return newKeyValue(key, value, 0);
374376
}
375377

376378
static <K, V> KeyValue<K, V> newKeyValue(K key, V value, int rawSize) {
377-
return new KeyValue<K, V>() {
378-
@Override
379-
public K getKey() {
380-
return key;
381-
}
382-
383-
@Override
384-
public V getValue() {
385-
return value;
386-
}
387-
388-
@Override
389-
public int getRawSize() throws IOException {
390-
return rawSize;
391-
}
392-
393-
@Override
394-
public String toString() {
395-
return "(key=" + key + ", value=" + value + ")";
396-
}
397-
398-
@Override
399-
public boolean equals(Object obj) {
400-
if (!(obj instanceof KeyValue)) {
401-
return false;
402-
}
403-
KeyValue<?, ?> kv = (KeyValue<?, ?>) obj;
404-
try {
405-
return getKey().equals(kv.getKey()) && getValue().equals(kv.getValue());
406-
} catch (IOException e) {
407-
throw new RuntimeException(e);
408-
}
409-
}
410-
411-
@Override
412-
public int hashCode() {
413-
return Objects.hash(getKey(), getValue());
414-
}
415-
};
379+
return new KeyValue<>(key, value, rawSize);
416380
}
417381

418382
/** A {@link TableIterator} to iterate {@link KeyValue}s. */

0 commit comments

Comments
 (0)