|
33 | 33 | package org.opensearch.core.common.io.stream;
|
34 | 34 |
|
35 | 35 | import org.apache.lucene.store.BufferedChecksum;
|
| 36 | +import org.opensearch.common.Nullable; |
36 | 37 | import org.opensearch.common.annotation.PublicApi;
|
37 | 38 |
|
38 | 39 | import java.io.IOException;
|
| 40 | +import java.util.ArrayList; |
| 41 | +import java.util.Arrays; |
| 42 | +import java.util.Collection; |
| 43 | +import java.util.Collections; |
| 44 | +import java.util.List; |
| 45 | +import java.util.Map; |
| 46 | +import java.util.TreeMap; |
| 47 | +import java.util.stream.Collectors; |
39 | 48 | import java.util.zip.CRC32;
|
40 | 49 | import java.util.zip.Checksum;
|
41 | 50 |
|
@@ -90,4 +99,75 @@ public void reset() throws IOException {
|
90 | 99 | public void resetDigest() {
|
91 | 100 | digest.reset();
|
92 | 101 | }
|
| 102 | + |
| 103 | + @Override |
| 104 | + public void writeMap(@Nullable Map<String, Object> map) throws IOException { |
| 105 | + Map<String, Object> newMap = new TreeMap<>(map); |
| 106 | + writeGenericValue(newMap); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public <K, V> void writeMap(Map<K, V> map, final Writeable.Writer<K> keyWriter, final Writeable.Writer<V> valueWriter) |
| 111 | + throws IOException { |
| 112 | + writeVInt(map.size()); |
| 113 | + map.keySet().stream().sorted().forEachOrdered(key -> { |
| 114 | + try { |
| 115 | + keyWriter.write(this, key); |
| 116 | + valueWriter.write(this, map.get(key)); |
| 117 | + } catch (IOException e) { |
| 118 | + throw new RuntimeException("Failed to write map values.", e); |
| 119 | + } |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + public <K, V> void writeMapValues(Map<K, V> map, final Writeable.Writer<V> valueWriter) throws IOException { |
| 124 | + writeVInt(map.size()); |
| 125 | + map.keySet().stream().sorted().forEachOrdered(key -> { |
| 126 | + try { |
| 127 | + valueWriter.write(this, map.get(key)); |
| 128 | + } catch (IOException e) { |
| 129 | + throw new RuntimeException("Failed to write map values.", e); |
| 130 | + } |
| 131 | + }); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public void writeStringArray(String[] array) throws IOException { |
| 136 | + String[] copyArray = Arrays.copyOf(array, array.length); |
| 137 | + Arrays.sort(copyArray); |
| 138 | + super.writeStringArray(copyArray); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public void writeVLongArray(long[] values) throws IOException { |
| 143 | + long[] copyValues = Arrays.copyOf(values, values.length); |
| 144 | + Arrays.sort(copyValues); |
| 145 | + super.writeVLongArray(copyValues); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public void writeCollection(final Collection<? extends Writeable> collection) throws IOException { |
| 150 | + List<? extends Writeable> sortedList = collection.stream().sorted().collect(Collectors.toList()); |
| 151 | + super.writeCollection(sortedList, (o, v) -> v.writeTo(o)); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public void writeStringCollection(final Collection<String> collection) throws IOException { |
| 156 | + List<String> listCollection = new ArrayList<>(collection); |
| 157 | + Collections.sort(listCollection); |
| 158 | + writeCollection(listCollection, StreamOutput::writeString); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void writeOptionalStringCollection(final Collection<String> collection) throws IOException { |
| 163 | + if (collection != null) { |
| 164 | + List<String> listCollection = new ArrayList<>(collection); |
| 165 | + Collections.sort(listCollection); |
| 166 | + writeBoolean(true); |
| 167 | + writeCollection(listCollection, StreamOutput::writeString); |
| 168 | + } else { |
| 169 | + writeBoolean(false); |
| 170 | + } |
| 171 | + } |
| 172 | + |
93 | 173 | }
|
0 commit comments