ztzg commented on a change in pull request #934: ZOOKEEPER-3301:Enforce the
quota limit
URL: https://github.com/apache/zookeeper/pull/934#discussion_r357042243
##########
File path: zookeeper-server/src/main/java/org/apache/zookeeper/StatsTrack.java
##########
@@ -34,22 +45,23 @@
public StatsTrack() {
this(null);
}
+
/**
- * the stat string should be of the form count=int,bytes=long
- * if stats is called with null the count and bytes are initialized
- * to -1.
- * @param stats the stat string to be intialized with
+ * the stat string should be of the form key1str=long,key2str=long,..
+ * where either , or ; are valid separators
+ * uninitialized values are returned as -1
+ * @param stats the stat string to be initialized with
*/
public StatsTrack(String stats) {
- if (stats == null) {
- stats = "count=-1,bytes=-1";
+ this.stats.clear();
+ if (stats == null || stats.length() == 0) {
+ return;
}
- String[] split = stats.split(",");
- if (split.length != 2) {
- throw new IllegalArgumentException("invalid string " + stats);
+ String[] keyValuePairs = stats.split("[,;]+");
Review comment:
The [documentation for
`String.split`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-int-)
says:
> An invocation of this method of the form `str.split(regex, n)` yields the
same result as the expression `Pattern.compile(regex).split(str, n)`
Looking that `String.java`, it seems that anything simpler than a single
character and/or escaped character does indeed lead to the instantiation of a
new `Pattern` object. I haven't measured the cost, but it doesn't seem trivial.
So I'd perhaps suggest keeping complex compiled patterns around; something
like this (untested):
```java
private static final PAIRS_SEPARATOR = Pattern.compile("[,;]+");
[...]
String[] keyValuePairs = PAIRS_SEPARATOR.split(stats);
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services