Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit 8aa6b67

Browse files
authored
Merge pull request #8 from sebright/census-context-proto
Serializes CensusContextImpl as a Protocol Buffer.
2 parents 6bd5ee6 + 38f0340 commit 8aa6b67

File tree

12 files changed

+93
-38
lines changed

12 files changed

+93
-38
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "proto"]
2+
path = proto
3+
url = https://github.com/google/census-proto

BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ java_library(
2626
srcs = glob(["core_native/java/com/google/census/*.java"]),
2727
deps = [
2828
":census-core",
29+
"//proto:census_context-proto-java",
2930
"@guava//jar",
3031
"@jsr305//jar",
32+
"@protobuf//jar",
3133
],
3234
)
3335

WORKSPACE

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,18 @@ maven_jar(
2424
artifact = "junit:junit:4.11",
2525
)
2626

27+
maven_jar(
28+
name = "protobuf",
29+
artifact = "com.google.protobuf:protobuf-java:3.0.0",
30+
)
31+
2732
maven_jar(
2833
name = "truth",
2934
artifact = "com.google.truth:truth:0.28",
30-
)
35+
)
36+
37+
git_repository(
38+
name = "io_bazel",
39+
remote = "https://github.com/bazelbuild/bazel",
40+
tag = "0.3.1",
41+
)

core/java/com/google/census/Census.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ public static CensusContext getCurrent() {
4141
return CONTEXT_FACTORY.getCurrent();
4242
}
4343

44-
/** Creates a {@link CensusContext} from the given on-the-wire encoded representation. */
44+
/**
45+
* Creates a {@link CensusContext} from the given on-the-wire encoded representation.
46+
*
47+
* <p>Should be the inverse of {@link CensusContext#serialize()}. The serialized representation
48+
* should be based on the {@link CensusContext} protobuf representation.
49+
*/
4550
@Nullable
4651
public static CensusContext deserialize(ByteBuffer buffer) {
4752
return CONTEXT_FACTORY.deserialize(buffer);

core/java/com/google/census/CensusContext.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public final CensusContext with(
4949
/**
5050
* Serializes the {@link CensusContext} into the on-the-wire representation.
5151
*
52+
* <p>The inverse of {@link CensusContextFactory#deserialize()} and should be based on the
53+
* {@link CensusContext} protobuf representation.
54+
*
5255
* @return serialized bytes.
5356
*/
5457
public abstract ByteBuffer serialize();

core/java/com/google/census/CensusContextFactory.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@
1414
package com.google.census;
1515

1616
import java.nio.ByteBuffer;
17-
1817
import javax.annotation.Nullable;
1918

2019
/**
2120
* Factory class for {@link CensusContext}.
2221
*/
2322
public abstract class CensusContextFactory {
24-
/** Creates a new {@link CensusContext} built from the given on-the-wire encoded representation.
23+
/**
24+
* Creates a {@link CensusContext} from the given on-the-wire encoded representation.
25+
*
26+
* <p>Should be the inverse of {@link CensusContext#serialize()}. The serialized representation
27+
* should be based on the {@link CensusContext} protobuf representation.
2528
*
2629
* @param buffer on-the-wire representation of a {@link CensusContext}
2730
* @return a {@link CensusContext} deserialized from {@code buffer}

core/javatests/com/google/census/CensusContextFactoryTest.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@
1515

1616
import static com.google.common.truth.Truth.assertThat;
1717

18-
import static java.nio.charset.StandardCharsets.UTF_8;
19-
18+
import java.nio.ByteBuffer;
2019
import org.junit.Test;
2120
import org.junit.runner.RunWith;
2221
import org.junit.runners.JUnit4;
2322

24-
import java.nio.ByteBuffer;
25-
2623
/**
2724
* Tests for {@link CensusContextFactory}.
2825
*/
@@ -33,12 +30,6 @@ public void testDeserializeEmpty() {
3330
assertThat(Census.deserialize(ByteBuffer.wrap(new byte[0]))).isEqualTo(Census.getDefault());
3431
}
3532

36-
@Test
37-
public void testDeserializeBadData() {
38-
assertThat(Census.deserialize(ByteBuffer.wrap("\2as\3df\2".getBytes(UTF_8))))
39-
.isNull();
40-
}
41-
4233
@Test
4334
public void testGetCurrent() {
4435
assertThat(Census.getCurrent()).isEqualTo(Census.getDefault());

core/javatests/com/google/census/CensusContextTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public void testMultipleThreadsWithContext() throws Exception {
147147
// Tests for Object overrides.
148148
@Test
149149
public void testEquals() {
150-
assertThat(DEFAULT).isEqualTo(DEFAULT);
150+
assertThat(DEFAULT).isEqualTo(DEFAULT.builder().build());
151151
assertThat(DEFAULT.with(K1, V1)).isEqualTo(DEFAULT.with(K1, V1));
152152
assertThat(DEFAULT.with(K1, V1, K2, V2)).isEqualTo(DEFAULT.with(K1, V1, K2, V2));
153153
assertThat(DEFAULT.with(K1, V1, K2, V2)).isEqualTo(DEFAULT.with(K2, V2, K1, V1));

core_native/java/com/google/census/CensusContextFactoryImpl.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,28 @@
1515

1616
import java.nio.ByteBuffer;
1717
import java.util.HashMap;
18-
1918
import javax.annotation.Nullable;
2019

2120
/** Native Implementation of {@link CensusContextFactory} */
2221
final class CensusContextFactoryImpl extends CensusContextFactory {
2322
static final CensusContextImpl DEFAULT = new CensusContextImpl(new HashMap<String, String>(0));
2423

2524
/**
26-
* The serialized tags are of the form:
27-
* {@code (<tag prefix> + 'key' + <tag delim> + 'value')}
25+
* Deserializes a {@link CensusContextImpl} from a serialized {@link CensusContextProto}.
26+
*
27+
* <p>The encoded tags are of the form: {@code <tag prefix> + 'key' + <tag delim> + 'value'}*
2828
*/
2929
@Override
3030
@Nullable
3131
public CensusContextImpl deserialize(ByteBuffer buffer) {
3232
return CensusSerializer.deserialize(buffer);
3333
}
3434

35-
/** Returns the current thread-local {@link CensusContext}. */
3635
@Override
3736
public CensusContext getCurrent() {
3837
return CensusCurrentContext.get();
3938
}
4039

41-
/** Returns the default {@link CensusContext}. */
4240
@Override
4341
public CensusContext getDefault() {
4442
return DEFAULT;

core_native/java/com/google/census/CensusContextImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public CensusContextImpl record(MetricMap stats) {
3434
return this;
3535
}
3636

37+
/**
38+
* Serializes a {@link CensusContextImpl} into {@link CensusContextProto} serialized format.
39+
*
40+
* <p>The encoded tags are of the form: {@code <tag prefix> + 'key' + <tag delim> + 'value'}*
41+
*/
3742
@Override
3843
public ByteBuffer serialize() {
3944
return CensusSerializer.serialize(this);

0 commit comments

Comments
 (0)