Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/rw logs #89

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
30 changes: 30 additions & 0 deletions alibabacloud-gateway-sls/util/java/logs.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
syntax = "proto2";

package com.aliyun.gateway.sls.util.model;

message LogContent
{
required string Key = 1;
required string Value = 2;
}

message Log
{
required uint32 Time = 1;
repeated LogContent Contents= 2;
optional fixed32 TimeNs = 4;
}

message LogTag
{
required string Key = 1;
required string Value = 2;
}

message LogGroup
{
repeated Log Logs= 1;
optional string Topic = 3;
optional string Source = 4;
repeated LogTag LogTags = 6;
}
6 changes: 6 additions & 0 deletions alibabacloud-gateway-sls/util/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
<artifactId>lz4</artifactId>
<version>1.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.25.4</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>tea</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Map;

import com.aliyun.gateway.sls.util.model.PostLogStoreLogsRequest;
import com.aliyun.gateway.sls.util.model.PostLogStoreLogsResponse;

public class Client {

Expand All @@ -13,4 +17,11 @@ public static InputStream readAndUncompressBlock(InputStream stream, String comp
String data = new String(rawData, "UTF-8");
return new ByteArrayInputStream(data.getBytes());
}
public static byte[] SerializeToPbBytes(PostLogStoreLogsRequest request) throws Exception {
return request.serializeToPbBytes();
}

public static PostLogStoreLogsResponse.PullLogsResponseBody DeserializeFromPbBytes(byte[] uncompressedData, int statusCode, Map<String, String> headers) throws Exception {
return new PostLogStoreLogsResponse.PullLogsResponseBody(uncompressedData, statusCode, headers);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.aliyun.gateway.sls.util.model;

import java.util.ArrayList;
import java.util.List;

public class FastLog {

private final byte[] rawBytes;
// [beginOffset, endOffset)
private final int beginOffset;
private final int endOffset;
private final List<FastLogContent> contents;
private int time = -1;
private int timeNsPart = 0;

public FastLog(byte[] rawBytes, int offset, int length) {
this.rawBytes = rawBytes;
this.beginOffset = offset;
this.endOffset = offset + length;
this.contents = new ArrayList<FastLogContent>();
if (!parse()) {
this.contents.clear();
}
}

private boolean parse() {
int pos = this.beginOffset;
int mode, index;
boolean findTime = false;
while (pos < this.endOffset) {
int[] value = VarintUtil.DecodeVarInt32(this.rawBytes, pos, this.endOffset);
if (value[0] == 0) {
return false;
}
mode = value[1] & 0x7;
index = value[1] >> 3;
if (mode == 0) {
pos = value[2];
value = VarintUtil.DecodeVarInt32(this.rawBytes, pos, this.endOffset);
if (value[0] == 0) {
return false;
}
pos = value[2];
if (index == 1) {
this.time = value[1];
findTime = true;
}
} else if (mode == 1) {
pos = value[2] + 8;
} else if (mode == 2) {
pos = value[2];
value = VarintUtil.DecodeVarInt32(this.rawBytes, pos, this.endOffset);
if (value[0] == 0) {
return false;
}
pos = value[2] + value[1];
if (index == 2) {
this.contents.add(new FastLogContent(this.rawBytes, value[2], value[1]));
}
} else if (mode == 5) {
if (index == 4) {
timeNsPart = this.rawBytes[value[2]] & 255 | (this.rawBytes[value[2] + 1] & 255) << 8 | (this.rawBytes[value[2] + 2] & 255) << 16 | (this.rawBytes[value[2] + 3] & 255) << 24;
}
pos = value[2] + 4;
} else {
return false;
}
}
return findTime && (pos == this.endOffset);
}

public int getTime() {
return this.time;
}

public int getTimeNsPart() {
return this.timeNsPart;
}

public int getContentsCount() {
return this.contents.size();
}

public List<FastLogContent> getContents() {
return contents;
}

public FastLogContent getContents(int i) {
if (i < this.contents.size()) {
return this.contents.get(i);
} else {
return null;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.aliyun.gateway.sls.util.model;

import java.io.UnsupportedEncodingException;

public class FastLogContent {

private final byte[] rawBytes;
// [beginOffset, endOffset)
private final int beginOffset;
private final int endOffset;
private int keyOffset = -1;
private int keyLength = -1;
private int valueOffset = -1;
private int valueLength = -1;

public FastLogContent(byte[] rawBytes, int offset, int length) {
this.rawBytes = rawBytes;
this.beginOffset = offset;
this.endOffset = offset + length;
if (!parse()) {
this.keyOffset = -1;
this.keyLength = -1;
this.valueOffset = -1;
this.valueLength = -1;
}
}

private boolean parse() {
int pos = this.beginOffset;
int index, mode;
while (pos < this.endOffset) {
int[] value = VarintUtil.DecodeVarInt32(this.rawBytes, pos, this.endOffset);
if (value[0] == 0) {
return false;
}
mode = value[1] & 0x7;
index = value[1] >> 3;
pos = value[2];
if (mode == 0) {
value = VarintUtil.DecodeVarInt32(this.rawBytes, pos, this.endOffset);
if (value[0] == 0) {
return false;
}
pos = value[2];
} else if (mode == 1) {
pos += 8;
} else if (mode == 2) {
value = VarintUtil.DecodeVarInt32(this.rawBytes, pos, this.endOffset);
if (value[0] == 0) {
return false;
}
pos = value[2] + value[1];
if (index == 1) {
keyOffset = value[2];
keyLength = value[1];
} else if (index == 2) {
valueOffset = value[2];
valueLength = value[1];
}
} else if (mode == 5) {
pos += 4;
} else {
return false;
}
}
return (keyOffset != -1 && valueOffset != -1 && pos == this.endOffset);
}

public String getKey() {
return decodeString(keyOffset, keyLength);
}

public String getKey(final String charset) throws UnsupportedEncodingException {
return decodeString(keyOffset, keyLength, charset);
}

public String getValue() {
return decodeString(valueOffset, valueLength);
}

public String getValue(final String charset) throws UnsupportedEncodingException {
return decodeString(valueOffset, valueLength, charset);
}

private String decodeString(int offset, int length) {
return offset < 0 ? null : new String(rawBytes, offset, length);
}

private String decodeString(int offset, int length, String charset) throws UnsupportedEncodingException {
return offset < 0 ? null : new String(this.rawBytes, offset, length, charset);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package com.aliyun.gateway.sls.util.model;

import java.util.ArrayList;
import java.util.List;

public class FastLogGroup {
private final byte[] rawBytes;
// [beginOffset, endOffset)
private final int beginOffset;
private final int endOffset;
private final List<FastLog> logs;
private final List<FastLogTag> tags;
private final String requestId;
private int topicOffset;
private int sourceOffset;

public FastLogGroup(byte[] rawBytes, int offset, int length, String requestId) {
this.rawBytes = rawBytes;
this.beginOffset = offset;
this.endOffset = offset + length;
this.topicOffset = -1;
this.sourceOffset = -1;
this.logs = new ArrayList<FastLog>();
this.tags = new ArrayList<FastLogTag>();
this.requestId = requestId;
if (!parse()) {
throw new LogException("InitFastLogGroupError", "invalid logGroup data", this.requestId);
}
}

private boolean parse() {
int pos = this.beginOffset;
int mode, index;
while (pos < this.endOffset) {
int[] value = VarintUtil.DecodeVarInt32(this.rawBytes, pos, this.endOffset);
if (value[0] == 0) {
return false;
}
mode = value[1] & 0x7;
index = value[1] >> 3;
if (mode == 0) {
pos = value[2];
value = VarintUtil.DecodeVarInt32(this.rawBytes, pos, this.endOffset);
if (value[0] == 0) {
return false;
}
pos = value[2];
} else if (mode == 1) {
pos = value[2] + 8;
} else if (mode == 2) {
switch (index) {
case 1:
//logs
break;
case 3:
this.topicOffset = value[2];
break;
case 4:
this.sourceOffset = value[2];
break;
case 6:
//tags
break;
default:
}
pos = value[2];
value = VarintUtil.DecodeVarInt32(this.rawBytes, pos, this.endOffset);
if (value[0] == 0) {
return false;
}
pos = value[2] + value[1];
if (index == 1) {
this.logs.add(new FastLog(this.rawBytes, value[2], value[1]));
} else if (index == 6) {
this.tags.add(new FastLogTag(this.rawBytes, value[2], value[1]));
}
} else if (mode == 5) {
pos = value[2] + 4;
} else {
return false;
}
}
return (pos == this.endOffset);
}

public int getLogTagsCount() {
return this.tags.size();
}

public FastLogTag getLogTags(int i) {
if (i < this.tags.size()) {
return this.tags.get(i);
} else {
return null;
}
}

public List<FastLog> getLogs() {
return logs;
}

public List<FastLogTag> getTags() {
return tags;
}

public int getLogsCount() {
return this.logs.size();
}

public FastLog getLogs(int i) {
if (i < this.logs.size()) {
return this.logs.get(i);
} else {
return null;
}
}

public boolean hasTopic() {
return this.topicOffset >= 0;
}

public boolean hasSource() {
return this.sourceOffset >= 0;
}
}

Loading
Loading