Skip to content
Permalink
Browse files
fix: output string value of log-output (#133)
* fix: output string value of log-output
  • Loading branch information
javeme committed Nov 3, 2021
1 parent 17ba9f4 commit 161cc89b4439635dd76db8d7d014453a579e5827
Showing 9 changed files with 92 additions and 35 deletions.
@@ -96,8 +96,8 @@ public void compute(ComputationContext context, Vertex vertex,
this.danglingVertexNumAggr.aggregateValue(1L);
this.danglingCumulativeAggr.aggregateValue(rank);
} else {
DoubleValue contribValue = new DoubleValue(rank / edgeCount);
context.sendMessageToAllEdges(vertex, contribValue);
this.contribValue.value(rank / edgeCount);
context.sendMessageToAllEdges(vertex, this.contribValue);
}
}

@@ -39,7 +39,7 @@

private final Config config;
private final PeekableIterator<KvEntry> messages;
private T value;
private final T value;

public MessageInput(ComputerContext context,
PeekableIterator<KvEntry> messages) {
@@ -23,6 +23,7 @@
import java.util.Arrays;
import java.util.UUID;

import com.baidu.hugegraph.computer.core.common.Constants;
import com.baidu.hugegraph.computer.core.common.SerialEnum;
import com.baidu.hugegraph.computer.core.common.exception.ComputerException;
import com.baidu.hugegraph.computer.core.graph.value.Value;
@@ -38,15 +39,16 @@

public class BytesId implements Id {

public static final BytesId EMPTY = BytesId.of(Constants.EMPTY_STR);

private IdType idType;
private byte[] bytes;
private int length;

public BytesId() {
BytesId id = BytesId.of(0L);
this.idType = id.idType;
this.bytes = id.bytes;
this.length = id.length;
this.idType = EMPTY.idType;
this.bytes = EMPTY.bytes;
this.length = EMPTY.length;
}

public BytesId(IdType idType, byte[] bytes) {
@@ -80,7 +82,7 @@ public static BytesId of(UUID value) {
E.checkArgument(value != null, "The value can't be null");
long high = value.getMostSignificantBits();
long low = value.getLeastSignificantBits();
BytesOutput output = IOFactory.createBytesOutput(18);
BytesOutput output = IOFactory.createBytesOutput(16);
try {
output.writeLong(high);
output.writeLong(low);
@@ -92,13 +94,13 @@ public static BytesId of(UUID value) {
}

@Override
public IdType idType() {
return this.idType;
public ValueType valueType() {
return ValueType.ID_VALUE;
}

@Override
public ValueType valueType() {
return ValueType.ID_VALUE;
public IdType idType() {
return this.idType;
}

@Override
@@ -111,15 +113,20 @@ public void assign(Value<Id> other) {

@Override
public Id copy() {
byte[] bytes = Arrays.copyOf(this.bytes, this.length);
return new BytesId(this.idType, bytes, this.length);
byte[] copyBytes = Arrays.copyOf(this.bytes, this.length);
return new BytesId(this.idType, copyBytes, this.length);
}

@Override
public int length() {
return this.length;
}

@Override
public Object value() {
return this.asObject();
}

@Override
public Object asObject() {
switch (this.idType) {
@@ -152,6 +159,7 @@ public Object asObject() {

@Override
public void read(RandomAccessInput in) throws IOException {
assert this != EMPTY : "can't read to EMPTY id";
this.idType = SerialEnum.fromCode(IdType.class, in.readByte());
int len = in.readInt();
this.bytes = BytesUtil.ensureCapacityWithoutCopy(this.bytes, len);
@@ -199,9 +207,4 @@ public int hashCode() {
public String toString() {
return this.asObject().toString();
}

@Override
public Object value() {
return this.bytes;
}
}
@@ -129,10 +129,10 @@ public Pair<Id, Value<?>> readMessage() throws IOException {
}

private Value<?> readMessage(RandomAccessInput in) throws IOException {
Value<?> v = this.config.createObject(
ComputerOptions.ALGORITHM_MESSAGE_CLASS);
v.read(in);
return v;
Value<?> value = this.config.createObject(
ComputerOptions.ALGORITHM_MESSAGE_CLASS);
value.read(in);
return value;
}

@Override
@@ -47,7 +47,7 @@ public void init(Config config, int partition) {
@Override
public void write(Vertex vertex) {
if (this.logged < this.limit) {
LOG.info("'{}': '{}'", vertex.id(), vertex.value());
LOG.info("'{}': '{}'", vertex.id(), vertex.value().string());
this.logged++;
}
}
@@ -35,7 +35,7 @@ public class LogOutput extends AbstractComputerOutput {

@Override
public void write(Vertex vertex) {
LOG.info("'{}': '{}'", vertex.id(), vertex.value());
LOG.info("'{}': '{}'", vertex.id(), vertex.value().string());
}

@Override
@@ -42,7 +42,7 @@
public class AlgorithmTestBase extends UnitTestBase {

public static void runAlgorithm(String algorithmParams, String ... options)
throws InterruptedException {
throws InterruptedException {
final Logger log = Log.logger(AlgorithmTestBase.class);
ExecutorService pool = Executors.newFixedThreadPool(2);
CountDownLatch countDownLatch = new CountDownLatch(2);
@@ -55,7 +55,7 @@ public static void runAlgorithm(String algorithmParams, String ... options)
params.put(RpcOptions.RPC_REMOTE_URL.name(),
"127.0.0.1:8090");
params.put(ComputerOptions.JOB_ID.name(),
"local_002");
"algo_test_job1");
params.put(ComputerOptions.JOB_WORKERS_COUNT.name(),
"1");
params.put(ComputerOptions.TRANSPORT_SERVER_PORT.name(),
@@ -102,7 +102,7 @@ public static void runAlgorithm(String algorithmParams, String ... options)
params.put(RpcOptions.RPC_SERVER_PORT.name(),
"8090");
params.put(ComputerOptions.JOB_ID.name(),
"local_002");
"algo_test_job1");
params.put(ComputerOptions.JOB_WORKERS_COUNT.name(),
"1");
params.put(ComputerOptions.BSP_REGISTER_TIMEOUT.name(),
@@ -43,11 +43,15 @@ public void testLongId() {
Id longId6 = BytesId.of(0L);
Id longId7 = BytesId.of(-100L);

Id longId8 = new BytesId();
Id longId9 = new BytesId();
Id longId8 = BytesId.of(0L);

Assert.assertEquals(IdType.LONG, longId1.idType());
Assert.assertEquals(ValueType.ID_VALUE, longId1.valueType());
Assert.assertEquals(1, longId1.length());

Assert.assertEquals(IdType.LONG, longId2.idType());
Assert.assertEquals(ValueType.ID_VALUE, longId2.valueType());
Assert.assertEquals(2, longId2.length());

Assert.assertEquals(new Long(123L), longId1.asObject());
Assert.assertEquals(123L, longId1.asObject());
@@ -64,9 +68,9 @@ public void testLongId() {
Assert.assertTrue(longId6.compareTo(longId5) < 0);
Assert.assertEquals(0, longId5.compareTo(longId7));

Assert.assertEquals(0, longId8.compareTo(longId9));
Assert.assertEquals(0, longId8.compareTo(longId6));
Assert.assertTrue(longId8.compareTo(longId1) < 0);
Assert.assertTrue(longId1.compareTo(longId9) > 0);
Assert.assertTrue(longId1.compareTo(longId8) > 0);

Assert.assertEquals(longId1, longId3);
Assert.assertNotEquals(longId1, longId2);
@@ -83,16 +87,30 @@ public void testUtf8Id() {
Id utf8Id4 = BytesId.of("abd");
Id utf8Id5 = BytesId.of("abc");
Id utf8Id6 = BytesId.of("100");
Id utf8Id7 = new BytesId();

Assert.assertEquals(IdType.UTF8, utf8Id1.idType());
Assert.assertEquals(3, utf8Id2.length());
Assert.assertEquals(ValueType.ID_VALUE, utf8Id1.valueType());
Assert.assertEquals(0, utf8Id1.length());

Assert.assertEquals(IdType.UTF8, utf8Id2.idType());
Assert.assertEquals(ValueType.ID_VALUE, utf8Id2.valueType());
Assert.assertEquals(3, utf8Id2.length());

Assert.assertEquals("abc", utf8Id2.asObject());
Assert.assertEquals(IdType.UTF8, utf8Id3.idType());
Assert.assertEquals(ValueType.ID_VALUE, utf8Id3.valueType());
Assert.assertEquals(4, utf8Id3.length());

Assert.assertEquals(IdType.UTF8, utf8Id7.idType());
Assert.assertEquals(ValueType.ID_VALUE, utf8Id7.valueType());
Assert.assertEquals(0, utf8Id7.length());

Assert.assertEquals("", utf8Id1.asObject());
Assert.assertEquals("abc", utf8Id2.asObject());
Assert.assertEquals("abcd", utf8Id3.asObject());
Assert.assertEquals("100", utf8Id6.asObject());
Assert.assertEquals("", utf8Id7.asObject());

Assert.assertTrue(utf8Id3.compareTo(utf8Id2) > 0);
Assert.assertTrue(utf8Id2.compareTo(utf8Id3) < 0);
Assert.assertEquals(0, utf8Id2.compareTo(utf8Id2));
@@ -101,6 +119,7 @@ public void testUtf8Id() {

Assert.assertEquals(utf8Id2, utf8Id5);
Assert.assertNotEquals(utf8Id2, utf8Id4);
Assert.assertEquals(utf8Id1, utf8Id7);

Assert.assertEquals(utf8Id2.hashCode(), utf8Id5.hashCode());
Assert.assertNotEquals(utf8Id2.hashCode(), utf8Id3.hashCode());
@@ -116,6 +135,11 @@ public void testUuidId() {

Assert.assertEquals(IdType.UUID, uuidId1.idType());
Assert.assertEquals(ValueType.ID_VALUE, uuidId1.valueType());
Assert.assertEquals(19, uuidId1.length());

Assert.assertEquals(IdType.UUID, uuidId2.idType());
Assert.assertEquals(ValueType.ID_VALUE, uuidId2.valueType());
Assert.assertEquals(19, uuidId2.length());

Assert.assertEquals(uuid1, uuidId1.asObject());
ByteBuffer buffer = ByteBuffer.allocate(16);
@@ -40,9 +40,13 @@ public class IdValueTest extends UnitTestBase {
public void testType() {
Value<Id> value1 = new BytesId();
Value<Id> value2 = new BytesId(IdType.LONG, new byte[]{1, 2, 3});
Value<Id> value3 = BytesId.of(1L);
Value<Id> value4 = BytesId.of("1");

Assert.assertEquals(ValueType.ID_VALUE, value1.valueType());
Assert.assertEquals(ValueType.ID_VALUE, value2.valueType());
Assert.assertEquals(ValueType.ID_VALUE, value3.valueType());
Assert.assertEquals(ValueType.ID_VALUE, value4.valueType());
}

@Test
@@ -95,6 +99,32 @@ public void testCopy() {
Assert.assertEquals(value2, copy);
}

@Test
public void testValue() {
Value<Id> value1 = BytesId.of(1234L);
Value<Id> value2 = BytesId.of("1234");
Value<Id> value3 = BytesId.of("12345");
Value<Id> value4 = new BytesId();

Assert.assertEquals(1234L, value1.value());
Assert.assertEquals("1234", value2.value());
Assert.assertEquals("12345", value3.value());
Assert.assertEquals("", value4.value());
}

@Test
public void testString() {
Value<Id> value1 = BytesId.of(1234L);
Value<Id> value2 = BytesId.of("1234");
Value<Id> value3 = BytesId.of("12345");
Value<Id> value4 = new BytesId();

Assert.assertEquals("1234", value1.string());
Assert.assertEquals("1234", value2.string());
Assert.assertEquals("12345", value3.string());
Assert.assertEquals("", value4.string());
}

@Test
public void testReadWrite() throws IOException {
assertValueEqualAfterWriteAndRead(BytesId.of("text"));
@@ -177,7 +207,7 @@ public void testToString() {
Value<Id> value2 = BytesId.of(123);
Value<Id> value3 = BytesId.of("123");

Assert.assertEquals("0", value1.toString());
Assert.assertEquals("", value1.toString());
Assert.assertEquals("123", value2.toString());
Assert.assertEquals("123", value3.toString());
}

0 comments on commit 161cc89

Please sign in to comment.