implement stable naming for labeled elements

This commit is contained in:
Jonathan Shook 2022-07-26 01:15:01 -05:00
parent c98d5ffc66
commit c20261bceb

View File

@ -16,6 +16,8 @@
package io.nosqlbench.api.labels; package io.nosqlbench.api.labels;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
@ -29,6 +31,7 @@ public interface Labeled {
} }
return map; return map;
} }
default Map<String, String> getLabelsAnd(Map<String,String> extra) { default Map<String, String> getLabelsAnd(Map<String,String> extra) {
LinkedHashMap<String,String> map = new LinkedHashMap<>(getLabels()); LinkedHashMap<String,String> map = new LinkedHashMap<>(getLabels());
map.putAll(extra); map.putAll(extra);
@ -51,4 +54,20 @@ public interface Labeled {
return labels; return labels;
} }
} }
default String linearized(Map<String,String> and) {
StringBuilder sb= new StringBuilder();
Map<String, String> allLabels = this.getLabelsAnd(and);
ArrayList<String> sortedLabels = new ArrayList<>(allLabels.keySet());
Collections.sort(sortedLabels);
for (String label : sortedLabels) {
sb.append(label).append(":").append(allLabels.get(label)).append((","));
}
sb.setLength(sb.length()-",".length());
return sb.toString();
}
default String linearized(String... and) {
return linearized(getLabelsAnd(and));
}
} }