use of com.google.common.collect.Multiset in project sonarqube by SonarSource.
the class ProxyBulkRequestBuilder method toString.
@Override
public String toString() {
StringBuilder message = new StringBuilder();
message.append("Bulk[");
Multiset<BulkRequestKey> groupedRequests = LinkedHashMultiset.create();
for (int i = 0; i < request.requests().size(); i++) {
ActionRequest<?> item = request.requests().get(i);
String requestType;
String index;
String docType;
if (item instanceof IndexRequest) {
IndexRequest request = (IndexRequest) item;
requestType = "index";
index = request.index();
docType = request.type();
} else if (item instanceof UpdateRequest) {
UpdateRequest request = (UpdateRequest) item;
requestType = "update";
index = request.index();
docType = request.type();
} else if (item instanceof DeleteRequest) {
DeleteRequest request = (DeleteRequest) item;
requestType = "delete";
index = request.index();
docType = request.type();
} else {
// Cannot happen, not allowed by BulkRequest's contract
throw new IllegalStateException("Unsupported bulk request type: " + item.getClass());
}
groupedRequests.add(new BulkRequestKey(requestType, index, docType));
}
Set<Multiset.Entry<BulkRequestKey>> entrySet = groupedRequests.entrySet();
int size = entrySet.size();
int current = 0;
for (Multiset.Entry<BulkRequestKey> requestEntry : entrySet) {
message.append(requestEntry.getCount()).append(" ").append(requestEntry.getElement().toString());
current++;
if (current < size) {
message.append(", ");
}
}
message.append("]");
return message.toString();
}
use of com.google.common.collect.Multiset in project zm-mailbox by Zimbra.
the class ProvUtil method keyValueArrayToMultiMap.
/**
* Convert an array of the form:
*
* a1 v1 a2 v2 a2 v3
*
* to a map of the form:
*
* a1 -> v1 a2 -> [v2, v3]
*
* For binary attribute, the argument following an attribute name will be treated as a file path and value for the
* attribute will be the base64 encoded string of the content of the file.
*/
private Map<String, Object> keyValueArrayToMultiMap(String[] args, int offset, boolean isCreateCmd) throws IOException, ServiceException {
AttributeManager attrMgr = AttributeManager.getInstance();
Map<String, Object> attrs = new HashMap<String, Object>();
String safeguarded_attrs_prop = LC.get("zmprov_safeguarded_attrs");
Set<String> safeguarded_attrs = safeguarded_attrs_prop == null ? Sets.<String>newHashSet() : Sets.newHashSet(safeguarded_attrs_prop.toLowerCase().split(","));
Multiset<String> multiValAttrsToCheck = HashMultiset.create();
for (int i = offset; i < args.length; i += 2) {
String n = args[i];
if (i + 1 >= args.length) {
throw new IllegalArgumentException("not enough arguments");
}
String v = args[i + 1];
String attrName = n;
if (n.charAt(0) == '+' || n.charAt(0) == '-') {
attrName = attrName.substring(1);
} else if (safeguarded_attrs.contains(attrName.toLowerCase()) && attrMgr.isMultiValued(attrName)) {
multiValAttrsToCheck.add(attrName.toLowerCase());
}
if (needsBinaryIO(attrMgr, attrName) && v.length() > 0) {
File file = new File(v);
byte[] bytes = ByteUtil.getContent(file);
v = ByteUtil.encodeLDAPBase64(bytes);
}
StringUtil.addToMultiMap(attrs, n, v);
}
if (!allowMultiValuedAttrReplacement && !isCreateCmd) {
for (Multiset.Entry<String> entry : multiValAttrsToCheck.entrySet()) {
if (entry.getCount() == 1) {
// If multiple values are being assigned to an attr as part of the same command
// then we don't consider it an unsafe replacement
printError("error: cannot replace multi-valued attr value unless -r is specified");
System.exit(2);
}
}
}
return attrs;
}
use of com.google.common.collect.Multiset in project jackrabbit-oak by apache.
the class SegmentGraph method writeSegmentGraph.
/**
* Write the segment graph of a file store to a stream.
* <p>
* The graph is written in
* <a href="https://gephi.github.io/users/supported-graph-formats/gdf-format/">the Guess GDF format</a>,
* which is easily imported into <a href="https://gephi.github.io/">Gephi</a>.
* As GDF only supports integers but the segment time stamps are encoded as long
* the {@code epoch} argument is used as a negative offset translating all timestamps
* into a valid int range.
*
* @param fileStore file store to graph
* @param out stream to write the graph to
* @param epoch epoch (in milliseconds)
* @param pattern regular expression specifying inclusion of nodes or {@code null}
* for all nodes.
* @throws Exception
*/
public static void writeSegmentGraph(@Nonnull ReadOnlyFileStore fileStore, @Nonnull OutputStream out, @Nonnull Date epoch, @CheckForNull String pattern) throws Exception {
checkNotNull(epoch);
PrintWriter writer = new PrintWriter(checkNotNull(out));
try {
SegmentNodeState root = checkNotNull(fileStore).getHead();
Predicate<UUID> filter = pattern == null ? Predicates.<UUID>alwaysTrue() : createRegExpFilter(pattern, fileStore.getSegmentIdProvider());
Graph<UUID> segmentGraph = parseSegmentGraph(fileStore, filter);
Graph<UUID> headGraph = parseHeadGraph(fileStore.getReader(), root.getRecordId());
writer.write("nodedef>name VARCHAR, label VARCHAR, type VARCHAR, wid VARCHAR, gc INT, t INT, size INT, head BOOLEAN\n");
for (UUID segment : segmentGraph.vertices()) {
writeNode(segment, writer, headGraph.containsVertex(segment), epoch, fileStore.getSegmentIdProvider());
}
writer.write("edgedef>node1 VARCHAR, node2 VARCHAR, head BOOLEAN\n");
for (Entry<UUID, Multiset<UUID>> edge : segmentGraph.edges()) {
UUID from = edge.getKey();
for (UUID to : edge.getValue()) {
if (!from.equals(to)) {
Multiset<UUID> he = headGraph.getEdge(from);
boolean inHead = he != null && he.contains(to);
writer.write(from + "," + to + "," + inHead + "\n");
}
}
}
} finally {
writer.close();
}
}
use of com.google.common.collect.Multiset in project jackrabbit-oak by apache.
the class SegmentGraph method writeGCGraph.
/**
* Write the gc generation graph of a file store to a stream.
* <p>
* The graph is written in
* <a href="https://gephi.github.io/users/supported-graph-formats/gdf-format/">the Guess GDF format</a>,
* which is easily imported into <a href="https://gephi.github.io/">Gephi</a>.
*
* @param fileStore file store to graph
* @param out stream to write the graph to
* @throws Exception
*/
public static void writeGCGraph(@Nonnull ReadOnlyFileStore fileStore, @Nonnull OutputStream out) throws Exception {
PrintWriter writer = new PrintWriter(checkNotNull(out));
try {
Graph<String> gcGraph = parseGCGraph(checkNotNull(fileStore));
writer.write("nodedef>name VARCHAR\n");
for (String gen : gcGraph.vertices()) {
writer.write(gen + "\n");
}
writer.write("edgedef>node1 VARCHAR, node2 VARCHAR, weight INT\n");
for (Entry<String, Multiset<String>> edge : gcGraph.edges()) {
String from = edge.getKey();
Multiset<String> tos = edge.getValue();
for (String to : tos.elementSet()) {
if (!from.equals(to) && !to.isEmpty()) {
writer.write(from + "," + to + "," + tos.count(to) + "\n");
}
}
}
} finally {
writer.close();
}
}
use of com.google.common.collect.Multiset in project jackrabbit-oak by apache.
the class SegmentGraphTest method testGCGraph.
@Test
public void testGCGraph() throws Exception {
// TODO Improve test coverage to non trivial cases with more than a single generation
// This is quite tricky as there is no easy way to construct a file store with
// a segment graphs having edges between generations (OAK-3348)
ReadOnlyFileStore store = fileStoreBuilder(getStoreFolder()).buildReadOnly();
try {
Graph<String> gcGraph = SegmentGraph.parseGCGraph(store);
assertEquals(ImmutableSet.of("0"), newHashSet(gcGraph.vertices()));
Map<String, Set<String>> map = newHashMap();
for (Entry<String, Multiset<String>> entry : gcGraph.edges()) {
map.put(entry.getKey(), entry.getValue().elementSet());
}
assertEquals(ImmutableMap.of("0", singleton("0")), map);
} finally {
store.close();
}
}
Aggregations