Skip to content

Commit fc814d4

Browse files
authored
Replace string device id with interface IDeviceID
1 parent 4c0aa7c commit fc814d4

40 files changed

+899
-407
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.tsfile.utils;
21+
22+
public interface Accountable {
23+
/** Return the memory usage of this object in bytes. Negative values are illegal. */
24+
long ramBytesUsed();
25+
}

common/src/main/java/org/apache/tsfile/utils/RamUsageEstimator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,9 @@ private static long sizeOfObject(Object o, int depth, long defSize) {
382382
return 0;
383383
}
384384
long size;
385-
if (o instanceof String) {
385+
if (o instanceof Accountable) {
386+
size = ((Accountable) o).ramBytesUsed();
387+
} else if (o instanceof String) {
386388
size = sizeOf((String) o);
387389
} else if (o instanceof boolean[]) {
388390
size = sizeOf((boolean[]) o);

examples/src/main/java/org/apache/tsfile/TsFileSequenceRead.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.tsfile.file.header.ChunkHeader;
3030
import org.apache.tsfile.file.header.PageHeader;
3131
import org.apache.tsfile.file.metadata.ChunkMetadata;
32+
import org.apache.tsfile.file.metadata.IDeviceID;
3233
import org.apache.tsfile.file.metadata.enums.TSEncoding;
3334
import org.apache.tsfile.fileSystem.FSFactoryProducer;
3435
import org.apache.tsfile.read.TsFileSequenceReader;
@@ -188,7 +189,7 @@ public static void main(String[] args) throws IOException {
188189
}
189190
}
190191
System.out.println("[Metadata]");
191-
for (String device : reader.getAllDevices()) {
192+
for (IDeviceID device : reader.getAllDevices()) {
192193
Map<String, List<ChunkMetadata>> seriesMetaData = reader.readChunkMetadataInDevice(device);
193194
System.out.printf(
194195
"\t[Device]Device %s, Number of Measurements %d%n", device, seriesMetaData.size());
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.tsfile.file;
21+
22+
import java.io.IOException;
23+
import java.io.OutputStream;
24+
25+
public interface IMetadataIndexEntry {
26+
27+
long getOffset();
28+
29+
void setOffset(long offset);
30+
31+
int serializeTo(OutputStream outputStream) throws IOException;
32+
33+
Comparable getCompareKey();
34+
35+
boolean isDeviceLevel();
36+
}

tsfile/src/main/java/org/apache/tsfile/file/header/ChunkGroupHeader.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import org.apache.tsfile.common.conf.TSFileConfig;
2323
import org.apache.tsfile.file.MetaMarker;
24+
import org.apache.tsfile.file.metadata.IDeviceID;
25+
import org.apache.tsfile.file.metadata.PlainDeviceID;
2426
import org.apache.tsfile.read.reader.TsFileInput;
2527
import org.apache.tsfile.utils.ReadWriteForEncodingUtils;
2628
import org.apache.tsfile.utils.ReadWriteIOUtils;
@@ -33,7 +35,7 @@ public class ChunkGroupHeader {
3335

3436
private static final byte MARKER = MetaMarker.CHUNK_GROUP_HEADER;
3537

36-
private final String deviceID;
38+
private final IDeviceID deviceID;
3739

3840
// this field does not need to be serialized.
3941
private final int serializedSize;
@@ -43,7 +45,7 @@ public class ChunkGroupHeader {
4345
*
4446
* @param deviceID device ID
4547
*/
46-
public ChunkGroupHeader(String deviceID) {
48+
public ChunkGroupHeader(IDeviceID deviceID) {
4749
this.deviceID = deviceID;
4850
this.serializedSize = getSerializedSize(deviceID);
4951
}
@@ -52,8 +54,10 @@ public int getSerializedSize() {
5254
return serializedSize;
5355
}
5456

55-
private int getSerializedSize(String deviceID) {
56-
int length = deviceID.getBytes(TSFileConfig.STRING_CHARSET).length;
57+
private int getSerializedSize(IDeviceID deviceID) {
58+
// TODO: add an interface in IDeviceID
59+
int length =
60+
((PlainDeviceID) deviceID).toStringID().getBytes(TSFileConfig.STRING_CHARSET).length;
5761
return Byte.BYTES + ReadWriteForEncodingUtils.varIntSize(length) + length;
5862
}
5963

@@ -72,11 +76,12 @@ public static ChunkGroupHeader deserializeFrom(InputStream inputStream, boolean
7276
}
7377
}
7478

79+
// TODO: add an interface in IDeviceID
7580
String deviceID = ReadWriteIOUtils.readVarIntString(inputStream);
7681
if (deviceID == null || deviceID.isEmpty()) {
7782
throw new IOException("DeviceId is empty");
7883
}
79-
return new ChunkGroupHeader(deviceID);
84+
return new ChunkGroupHeader(new PlainDeviceID(deviceID));
8085
}
8186

8287
/**
@@ -91,11 +96,12 @@ public static ChunkGroupHeader deserializeFrom(TsFileInput input, long offset, b
9196
if (!markerRead) {
9297
offsetVar++;
9398
}
99+
// TODO: add an interface in IDeviceID
94100
String deviceID = input.readVarIntString(offsetVar);
95-
return new ChunkGroupHeader(deviceID);
101+
return new ChunkGroupHeader(new PlainDeviceID(deviceID));
96102
}
97103

98-
public String getDeviceID() {
104+
public IDeviceID getDeviceID() {
99105
return deviceID;
100106
}
101107

@@ -109,15 +115,15 @@ public String getDeviceID() {
109115
public int serializeTo(OutputStream outputStream) throws IOException {
110116
int length = 0;
111117
length += ReadWriteIOUtils.write(MARKER, outputStream);
112-
length += ReadWriteIOUtils.writeVar(deviceID, outputStream);
118+
length += ReadWriteIOUtils.writeVar(((PlainDeviceID) deviceID).toStringID(), outputStream);
113119
return length;
114120
}
115121

116122
@Override
117123
public String toString() {
118124
return "ChunkGroupHeader{"
119125
+ "deviceID='"
120-
+ deviceID
126+
+ ((PlainDeviceID) deviceID).toStringID()
121127
+ '\''
122128
+ ", serializedSize="
123129
+ serializedSize

tsfile/src/main/java/org/apache/tsfile/file/metadata/ChunkGroupMetadata.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@
2424
/** Only maintained when writing, not serialized to TsFile. */
2525
public class ChunkGroupMetadata {
2626

27-
private String device;
27+
private IDeviceID device;
2828

2929
private List<ChunkMetadata> chunkMetadataList;
3030

31-
public ChunkGroupMetadata(String device, List<ChunkMetadata> chunkMetadataList) {
31+
public ChunkGroupMetadata(IDeviceID device, List<ChunkMetadata> chunkMetadataList) {
3232
this.device = device;
3333
this.chunkMetadataList = chunkMetadataList;
3434
}
3535

36-
public String getDevice() {
36+
public IDeviceID getDevice() {
3737
return device;
3838
}
3939

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.tsfile.file.metadata;
21+
22+
import org.apache.tsfile.file.IMetadataIndexEntry;
23+
import org.apache.tsfile.utils.ReadWriteIOUtils;
24+
25+
import java.io.IOException;
26+
import java.io.InputStream;
27+
import java.io.OutputStream;
28+
import java.nio.ByteBuffer;
29+
30+
public class DeviceMetadataIndexEntry implements IMetadataIndexEntry {
31+
private IDeviceID deviceID;
32+
private long offset;
33+
34+
public DeviceMetadataIndexEntry(IDeviceID deviceID, long offset) {
35+
this.deviceID = deviceID;
36+
this.offset = offset;
37+
}
38+
39+
public IDeviceID getDeviceID() {
40+
return deviceID;
41+
}
42+
43+
@Override
44+
public long getOffset() {
45+
return offset;
46+
}
47+
48+
public void setDeviceID(IDeviceID deviceID) {
49+
this.deviceID = deviceID;
50+
}
51+
52+
@Override
53+
public void setOffset(long offset) {
54+
this.offset = offset;
55+
}
56+
57+
@Override
58+
public int serializeTo(OutputStream outputStream) throws IOException {
59+
int byteLen = 0;
60+
byteLen += deviceID.serialize(outputStream);
61+
byteLen += ReadWriteIOUtils.write(offset, outputStream);
62+
return byteLen;
63+
}
64+
65+
@Override
66+
public Comparable getCompareKey() {
67+
return deviceID;
68+
}
69+
70+
@Override
71+
public boolean isDeviceLevel() {
72+
return true;
73+
}
74+
75+
public static DeviceMetadataIndexEntry deserializeFrom(ByteBuffer buffer) {
76+
IDeviceID device = IDeviceID.deserializeFrom(buffer);
77+
long offset = ReadWriteIOUtils.readLong(buffer);
78+
return new DeviceMetadataIndexEntry(device, offset);
79+
}
80+
81+
public static DeviceMetadataIndexEntry deserializeFrom(InputStream inputStream)
82+
throws IOException {
83+
IDeviceID device = IDeviceID.deserializeFrom(inputStream);
84+
long offset = ReadWriteIOUtils.readLong(inputStream);
85+
return new DeviceMetadataIndexEntry(device, offset);
86+
}
87+
88+
@Override
89+
public String toString() {
90+
return "<" + deviceID + "," + offset + ">";
91+
}
92+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.tsfile.file.metadata;
21+
22+
import org.apache.tsfile.utils.Accountable;
23+
import org.apache.tsfile.utils.ReadWriteIOUtils;
24+
25+
import java.io.IOException;
26+
import java.io.InputStream;
27+
import java.io.OutputStream;
28+
import java.nio.ByteBuffer;
29+
30+
/** Device id interface. */
31+
public interface IDeviceID extends Comparable<IDeviceID>, Accountable {
32+
33+
int serialize(ByteBuffer byteBuffer);
34+
35+
int serialize(OutputStream outputStream) throws IOException;
36+
37+
byte[] getBytes();
38+
39+
boolean isEmpty();
40+
41+
static IDeviceID deserializeFrom(ByteBuffer byteBuffer) {
42+
return new PlainDeviceID(ReadWriteIOUtils.readVarIntString(byteBuffer));
43+
}
44+
45+
static IDeviceID deserializeFrom(InputStream inputStream) throws IOException {
46+
return new PlainDeviceID(ReadWriteIOUtils.readVarIntString(inputStream));
47+
}
48+
}

0 commit comments

Comments
 (0)