Skip to content
This repository was archived by the owner on Apr 21, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions ffwd-client/src/main/java/com/spotify/ffwd/v1/Metric.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

package com.spotify.ffwd.v1;

import com.google.protobuf.ByteString;
import com.spotify.ffwd.protocol1.Protocol1;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -132,8 +131,8 @@ public byte[] serialize() {
builder.setValue(Protocol1.Value.newBuilder().setDoubleValue(doubleValue.getValue()));
} else if (value instanceof Value.DistributionValue) {
Value.DistributionValue distributionValue = (Value.DistributionValue) value;
ByteString byteString = ByteString.copyFrom(distributionValue.getValue().array());
builder.setValue(Protocol1.Value.newBuilder().setDistributionValue(byteString));
builder.setValue(Protocol1.Value.newBuilder()
.setDistributionValue(distributionValue.getValue()));
} else {
throw new IllegalArgumentException("Failed to identify distribution type : [" + value
+ "]");
Expand Down
16 changes: 8 additions & 8 deletions ffwd-client/src/main/java/com/spotify/ffwd/v1/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
package com.spotify.ffwd.v1;

import com.google.auto.value.AutoValue;
import java.nio.ByteBuffer;
import com.google.protobuf.ByteString;
import java.util.function.Function;


Expand All @@ -40,14 +40,14 @@ public static Value doubleValue(double value) {
return DoubleValue.create(value);
}

public static Value distributionValue(ByteBuffer byteBuffer) {
return DistributionValue.create(byteBuffer);
public static Value distributionValue(ByteString byteString) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a breaking change, which I think we can live with if we are sure no one is depending on this.
Otherwise, we would need to keep the old method signature too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No one is using it. Nothing will break.

return DistributionValue.create(byteString);
}


public abstract <T> T match(
Function<? super Double, T> doubleFunction,
Function<? super ByteBuffer, T> distributionFunction,
Function<? super ByteString, T> distributionFunction,
Function<? super Value, T> defaultFunction);


Expand All @@ -59,7 +59,7 @@ abstract static class DoubleValue extends Value {
@Override
public final <T> T match(
Function<? super Double, T> doubleFunction,
Function<? super ByteBuffer, T> distributionFunction,
Function<? super ByteString, T> distributionFunction,
Function<? super Value, T> defaultFunction) {
return doubleFunction.apply(getValue());
}
Expand All @@ -81,18 +81,18 @@ abstract static class DistributionValue extends Value {
@Override
public final <T> T match(
Function<? super Double, T> doubleFunction,
Function<? super ByteBuffer, T> distributionFunction,
Function<? super ByteString, T> distributionFunction,
Function<? super Value, T> defaultFunction) {
return distributionFunction.apply(getValue());
}


static DistributionValue create(ByteBuffer value) {
static DistributionValue create(ByteString value) {
return new AutoValue_Value_DistributionValue(value);
}


abstract ByteBuffer getValue();
abstract ByteString getValue();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public void testSerializeDistributionValue() throws InvalidProtocolBufferExcepti
final byte [] pointVal = DISTRIBUTION_DATA_POINT.getBytes(StandardCharsets.UTF_8);
ByteBuffer byteBuffer = ByteBuffer.allocate(pointVal.length);
byteBuffer.put(pointVal);
Metric metricV1 = (new Metric()).value(Value.distributionValue(byteBuffer));
Metric metricV1 = (new Metric()).value(Value
.distributionValue(ByteString.copyFrom(byteBuffer.array())));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: ByteString.copyFrom(byteBuffer) might be cleaner, but perhaps you would then first need to flip the buffer

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually - just do ByteString.copyFromUtf8(DISTRIBUTION_DATA_POINT) instead

byte[] bytes = metricV1.serialize();
Protocol1.Metric out = Protocol1.Message.newBuilder().mergeFrom(bytes).build().getMetric();
Protocol1.Value outVal = out.getValue();
Expand Down
16 changes: 9 additions & 7 deletions ffwd-client/src/test/java/com/spotify/ffwd/v1/ValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;


import com.google.protobuf.ByteString;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand All @@ -32,7 +34,7 @@

public class ValueTest {
private static final double DOUBLE_VAL = 0.976;
private static final ByteBuffer DISTRIBUTION_VAL = createSample();
private static final ByteString DISTRIBUTION_VAL = createSample();


@Test
Expand Down Expand Up @@ -66,9 +68,9 @@ public Object apply(Double arg) {
return null;
}
},
new Function<ByteBuffer, Object>() {
new Function<ByteString, Object>() {
@Override
public Object apply(ByteBuffer arg) {
public Object apply(ByteString arg) {
actual.add(Value.distributionValue(arg));
return null;
}
Expand All @@ -86,12 +88,12 @@ public Object apply(Value arg) {

}

private static ByteBuffer createSample(){
final String fakeDistData = "FAKEDISTRIBUTIONFAKEDISTRIBUTION";
final byte [] bytes = fakeDistData.getBytes(StandardCharsets.UTF_8);
private static ByteString createSample(){
final String data = "FAKEDISTRIBUTIONFAKEDISTRIBUTION";
final byte [] bytes = data.getBytes(StandardCharsets.UTF_8);
ByteBuffer out = ByteBuffer.allocate(bytes.length);
out.put(bytes);
return out;
return ByteString.copyFrom(out.array());
Copy link
Member

@spkrka spkrka Sep 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems unnecessary to go from byte[] -> ByteBuffer -> ByteString

Simplify to ByteString.copyFromUtf8(...)?

}


Expand Down