popduke commented on code in PR #170:
URL: https://github.com/apache/bifromq/pull/170#discussion_r2280719866


##########
base-rpc/base-rpc-common/src/main/java/org/apache/bifromq/baserpc/marshaller/HLCStampedInputStream.java:
##########
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.bifromq.baserpc.marshaller;
+
+import com.google.protobuf.CodedOutputStream;
+import com.google.protobuf.WireFormat;
+import io.grpc.Drainable;
+import io.grpc.KnownLength;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import org.apache.bifromq.basehlc.HLC;
+
+class HLCStampedInputStream extends InputStream implements Drainable, 
KnownLength {
+    public static final int HLC_FIELD_ID = Short.MAX_VALUE;
+    // varint encoded key: unknown fieldset_number=32767, wire_type=1 (fixed64)
+    public static final byte[] HLC_TAG = new byte[3];
+    public static final int HLC_FIELD_LENGTH = HLC_TAG.length + Long.BYTES; // 
3 bytes tag + 8 bytes fixed64
+
+    static {
+        CodedOutputStream cos = CodedOutputStream.newInstance(HLC_TAG);
+        try {
+            cos.writeTag(HLC_FIELD_ID, WireFormat.WIRETYPE_FIXED64);
+        } catch (IOException e) {
+            // never happens
+        }
+    }
+
+    private final long hlc;
+    private final InputStream protoStream;
+    private int cursor = 0;
+
+    HLCStampedInputStream(InputStream protoStream) {
+        assert protoStream instanceof Drainable;
+        assert protoStream instanceof KnownLength;
+        this.hlc = HLC.INST.get();
+        this.protoStream = protoStream;
+    }
+
+    @Override
+    public int drainTo(OutputStream target) throws IOException {
+        while (cursor < HLC_FIELD_LENGTH) {
+            target.write(read());
+        }
+        return HLC_FIELD_LENGTH + ((Drainable) protoStream).drainTo(target);
+    }
+
+    @Override
+    public int available() throws IOException {
+        return (HLC_FIELD_LENGTH - cursor) + protoStream.available();
+    }
+
+    @Override
+    public int read() throws IOException {
+        if (cursor < HLC_TAG.length) {
+            return HLC_TAG[cursor++] & 0xFF;
+        }
+        if (cursor < HLC_FIELD_LENGTH) {

Review Comment:
   one of the HLC usages is the timestamp of message to help tracking causality.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to