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

Binary allocator #340

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion cpp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.apache.tsfile</groupId>
<artifactId>tsfile-parent</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>1.3.0-SNAPSHOT</version>
</parent>
<artifactId>tsfile-cpp</artifactId>
<packaging>pom</packaging>
Expand Down
2 changes: 1 addition & 1 deletion java/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>org.apache.tsfile</groupId>
<artifactId>tsfile-java</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>1.3.0-SNAPSHOT</version>
</parent>
<artifactId>common</artifactId>
<name>TsFile: Java: Common</name>
Expand Down
40 changes: 36 additions & 4 deletions java/common/src/main/java/org/apache/tsfile/utils/Binary.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class Binary implements Comparable<Binary>, Serializable, Accountable {
private static final long serialVersionUID = 6394197743397020735L;
public static final Binary EMPTY_VALUE = new Binary(new byte[0]);

private byte[] values;
byte[] values;

/** if the bytes v is modified, the modification is visible to this binary. */
public Binary(byte[] v) {
Expand Down Expand Up @@ -70,7 +70,7 @@ public int compareTo(Binary other) {
}

// avoid overflow
private char getChar(byte[] val, int index) {
protected char getChar(byte[] val, int index) {
return (char) (val[index] & 0xff);
}

Expand All @@ -92,9 +92,13 @@ public int hashCode() {
}

/**
* get length.
* Gets the actual payload length.
*
* @return length
* <p>This method returns the effective length of the data (payload) stored in the byte array. If
* the byte array is null, it returns -1. Note that this length may be less than the total
* capacity of the byte array.
*
* @return the actual payload length, or -1 if the byte array is null
*/
public int getLength() {
if (this.values == null) {
Expand All @@ -103,6 +107,22 @@ public int getLength() {
return this.values.length;
}

/**
* Gets the total capacity of the byte array.
*
* <p>This method returns the total capacity of the underlying byte array. If the byte array is
* null, it returns -1. Note that the effective payload length (actual valid data) may be smaller
* than the total capacity of the array.
*
* @return the total capacity of the byte array, or -1 if the array is null
*/
public int getCapacity() {
if (this.values == null) {
return -1;
}
return this.values.length;
}

public String getStringValue(Charset charset) {
return new String(this.values, charset);
}
Expand All @@ -117,6 +137,10 @@ public byte[] getValues() {
return values;
}

public Pair<byte[], Integer> getValuesAndLength() {
return new Pair<>(values, values.length);
}

public void setValues(byte[] values) {
this.values = values;
}
Expand All @@ -125,4 +149,12 @@ public void setValues(byte[] values) {
public long ramBytesUsed() {
return INSTANCE_SIZE + sizeOf(values);
}

public long ramShallowBytesUsed() {
return INSTANCE_SIZE;
}

public boolean isNull() {
return values == null;
}
}
85 changes: 27 additions & 58 deletions java/common/src/main/java/org/apache/tsfile/utils/PooledBinary.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
*/
package org.apache.tsfile.utils;

import java.io.Serializable;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import static org.apache.tsfile.utils.RamUsageEstimator.shallowSizeOfInstance;
import static org.apache.tsfile.utils.RamUsageEstimator.sizeOf;

/**
* This class represents a pooled binary object for application layer. It is designed to improve
Expand All @@ -31,64 +31,32 @@
* length of the underlying byte array. Always use getLength() instead of getValue().length to get
* the correct length.
*/
public class PooledBinary implements Comparable<PooledBinary>, Serializable, Accountable {
public class PooledBinary extends Binary {

private static final long INSTANCE_SIZE = shallowSizeOfInstance(PooledBinary.class);
private static final long serialVersionUID = 6394197743397020735L;
public static final PooledBinary EMPTY_VALUE = new PooledBinary(new byte[0]);

private Binary binary;

private int length;

private int arenaIndex = -1;

/** if the bytes v is modified, the modification is visible to this binary. */
public PooledBinary(byte[] v) {
this.binary = new Binary(v);
this.length = binary.getLength();
super(v);
this.length = values.length;
}

public PooledBinary(String s, Charset charset) {
this.binary = new Binary(s, charset);
this.length = binary.getLength();
super(s, charset);
this.length = values.length;
}

public PooledBinary(byte[] v, int length, int arenaIndex) {
this.binary = new Binary(v);
super(v);
this.length = length;
this.arenaIndex = arenaIndex;
}

@Override
public int compareTo(PooledBinary other) {
if (other == null) {
if (this.binary.getValues() == null) {
return 0;
} else {
return 1;
}
}

// copied from StringLatin1.compareT0
int len1 = getLength();
int len2 = other.getLength();
int lim = Math.min(len1, len2);
byte[] v0 = this.binary.getValues();
byte[] v1 = other.binary.getValues();
for (int k = 0; k < lim; k++) {
if (v0[k] != v1[k]) {
return getChar(v0, k) - getChar(v1, k);
}
}
return len1 - len2;
}

// avoid overflow
private char getChar(byte[] val, int index) {
return (char) (val[index] & 0xff);
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -103,8 +71,8 @@ public boolean equals(Object o) {
return false;
}

byte[] v0 = binary.getValues();
byte[] v1 = otherBinary.binary.getValues();
byte[] v0 = values;
byte[] v1 = otherBinary.values;

for (int i = 0; i < length; i++) {
if (v0[i] != v1[i]) {
Expand All @@ -118,26 +86,24 @@ public boolean equals(Object o) {
@Override
public int hashCode() {
// copied from Arrays.hashCode
if (binary.getValues() == null) return 0;
if (values == null) return 0;

int result = 1;
byte[] val = binary.getValues();
for (int i = 0; i < length; i++) result = 31 * result + val[i];
for (int i = 0; i < length; i++) {
result = 31 * result + values[i];
}

return result;
}

/**
* get length.
*
* @return length
*/
@Override
public int getLength() {
return this.length;
}

@Override
public String getStringValue(Charset charset) {
return new String(this.binary.getValues(), 0, length, charset);
return new String(values, 0, length, charset);
}

@Override
Expand All @@ -146,17 +112,19 @@ public String toString() {
return getStringValue(StandardCharsets.UTF_8);
}

public byte[] getValues() {
return binary.getValues();
@Override
public Pair<byte[], Integer> getValuesAndLength() {
return new Pair<>(values, length);
}

@Override
public void setValues(byte[] values) {
this.binary.setValues(values);
this.length = this.binary.getLength();
super.setValues(values);
this.length = values.length;
}

public void setValues(byte[] values, int length) {
this.binary.setValues(values);
super.setValues(values);
this.length = length;
}

Expand All @@ -166,10 +134,11 @@ public int getArenaIndex() {

@Override
public long ramBytesUsed() {
return INSTANCE_SIZE + binary.ramBytesUsed();
return INSTANCE_SIZE + sizeOf(values);
}

public Binary toBinary() {
return binary;
@Override
public long ramShallowBytesUsed() {
return INSTANCE_SIZE;
}
}
4 changes: 2 additions & 2 deletions java/examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>org.apache.tsfile</groupId>
<artifactId>tsfile-java</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>1.3.0-SNAPSHOT</version>
</parent>
<artifactId>examples</artifactId>
<packaging>pom</packaging>
Expand All @@ -37,7 +37,7 @@
<dependency>
<groupId>org.apache.tsfile</groupId>
<artifactId>tsfile</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>1.3.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
Expand Down
4 changes: 2 additions & 2 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
<parent>
<groupId>org.apache.tsfile</groupId>
<artifactId>tsfile-parent</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>1.3.0-SNAPSHOT</version>
</parent>
<artifactId>tsfile-java</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>1.3.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>TsFile: Java</name>
<modules>
Expand Down
6 changes: 3 additions & 3 deletions java/tools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
<parent>
<groupId>org.apache.tsfile</groupId>
<artifactId>tsfile-java</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>1.3.0-SNAPSHOT</version>
</parent>
<artifactId>tools</artifactId>
<name>TsFile: Java: Tools</name>
<dependencies>
<dependency>
<groupId>org.apache.tsfile</groupId>
<artifactId>common</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>1.3.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
Expand All @@ -50,7 +50,7 @@
<dependency>
<groupId>org.apache.tsfile</groupId>
<artifactId>tsfile</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>1.3.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
Expand Down
4 changes: 2 additions & 2 deletions java/tsfile/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<parent>
<groupId>org.apache.tsfile</groupId>
<artifactId>tsfile-java</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>1.3.0-SNAPSHOT</version>
</parent>
<artifactId>tsfile</artifactId>
<name>TsFile: Java: TsFile</name>
Expand All @@ -38,7 +38,7 @@
<dependency>
<groupId>org.apache.tsfile</groupId>
<artifactId>common</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>1.3.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github.luben</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.tsfile.file.metadata.enums.TSEncoding;
import org.apache.tsfile.utils.Binary;
import org.apache.tsfile.utils.BinaryUtils;
import org.apache.tsfile.utils.ReadWriteForEncodingUtils;

import org.slf4j.Logger;
Expand Down Expand Up @@ -99,7 +100,7 @@ private void writeMap(ByteArrayOutputStream out) throws IOException {
ReadWriteForEncodingUtils.writeVarInt(indexEntry.size(), out);
for (Binary value : indexEntry) {
ReadWriteForEncodingUtils.writeVarInt(value.getLength(), out);
out.write(value.getValues());
BinaryUtils.serializeBytes(out, value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.tsfile.exception.encoding.TsFileEncodingException;
import org.apache.tsfile.file.metadata.enums.TSEncoding;
import org.apache.tsfile.utils.Binary;
import org.apache.tsfile.utils.BinaryUtils;
import org.apache.tsfile.utils.ReadWriteForEncodingUtils;

import org.slf4j.Logger;
Expand Down Expand Up @@ -92,7 +93,7 @@ public void encode(Binary value, ByteArrayOutputStream out) {
// write the length of the bytes
encode(value.getLength(), out);
// write value
out.write(value.getValues());
BinaryUtils.serializeBytes(out, value);
} catch (IOException e) {
logger.error(
"tsfile-encoding PlainEncoder: error occurs when encode Binary value {}", value, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public TSDataType getType() {
/** The output of this method should be identical to the method "serializeStats(outputStream)". */
@Override
public int getStatsSize() {
return 4 + firstValue.getValues().length + 4 + lastValue.getValues().length;
return 4 + firstValue.getLength() + 4 + lastValue.getLength();
}

@Override
Expand Down
Loading
Loading