Skip to content

Commit

Permalink
add ut
Browse files Browse the repository at this point in the history
Signed-off-by: gengjun-git <[email protected]>
  • Loading branch information
gengjun-git committed Sep 4, 2024
1 parent 01fce42 commit d29c044
Show file tree
Hide file tree
Showing 9 changed files with 387 additions and 32 deletions.
3 changes: 3 additions & 0 deletions fe/fe-core/src/main/java/com/starrocks/catalog/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
Expand All @@ -97,6 +98,8 @@ public class Database extends MetaObject implements Writable {
// assume that the time a lock is held by thread is less than 100ms
public static final long TRY_LOCK_TIMEOUT_MS = 100L;

private ArrayBlockingQueue<String> errorMessages;

@SerializedName(value = "i")
private long id;
@SerializedName(value = "n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ public interface SRMetaBlockReader {

long readLong() throws IOException, SRMetaBlockEOFException;

byte readByte() throws IOException, SRMetaBlockEOFException;

short readShort() throws IOException, SRMetaBlockEOFException;

double readDouble() throws IOException, SRMetaBlockEOFException;

float readFloat() throws IOException, SRMetaBlockEOFException;

char readChar() throws IOException, SRMetaBlockEOFException;

boolean readBoolean() throws IOException, SRMetaBlockEOFException;

String readString() throws IOException, SRMetaBlockEOFException;

<T> T readJson(Type returnType) throws IOException, SRMetaBlockEOFException;

<T> T readJson(Class<T> classType) throws IOException, SRMetaBlockEOFException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,41 @@ public long readLong() throws IOException, SRMetaBlockEOFException {
return readJson(long.class);
}

@Override
public byte readByte() throws IOException, SRMetaBlockEOFException {
return readJson(byte.class);
}

@Override
public short readShort() throws IOException, SRMetaBlockEOFException {
return readJson(short.class);
}

@Override
public double readDouble() throws IOException, SRMetaBlockEOFException {
return readJson(double.class);
}

@Override
public float readFloat() throws IOException, SRMetaBlockEOFException {
return readJson(float.class);
}

@Override
public char readChar() throws IOException, SRMetaBlockEOFException {
return readJson(char.class);
}

@Override
public boolean readBoolean() throws IOException, SRMetaBlockEOFException {
return readJson(boolean.class);
}

@Override
public String readString() throws IOException, SRMetaBlockEOFException {
return readJson(String.class);
}

@Override
public <T> T readJson(Type returnType) throws IOException, SRMetaBlockEOFException {
byte[] bytes = readJsonBytes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,48 @@ public long readLong() throws IOException, SRMetaBlockEOFException {
return object.getValue();
}

@Override
public byte readByte() throws IOException, SRMetaBlockEOFException {
PrimitiveObject<Byte> object = readJson(new TypeToken<PrimitiveObject<Byte>>() {}.getType());
return object.getValue();
}

@Override
public short readShort() throws IOException, SRMetaBlockEOFException {
PrimitiveObject<Short> object = readJson(new TypeToken<PrimitiveObject<Short>>() {}.getType());
return object.getValue();
}

@Override
public double readDouble() throws IOException, SRMetaBlockEOFException {
PrimitiveObject<Double> object = readJson(new TypeToken<PrimitiveObject<Double>>() {}.getType());
return object.getValue();
}

@Override
public float readFloat() throws IOException, SRMetaBlockEOFException {
PrimitiveObject<Float> object = readJson(new TypeToken<PrimitiveObject<Float>>() {}.getType());
return object.getValue();
}

@Override
public char readChar() throws IOException, SRMetaBlockEOFException {
PrimitiveObject<Character> object = readJson(new TypeToken<PrimitiveObject<Character>>() {}.getType());
return object.getValue();
}

@Override
public boolean readBoolean() throws IOException, SRMetaBlockEOFException {
PrimitiveObject<Boolean> object = readJson(new TypeToken<PrimitiveObject<Boolean>>() {}.getType());
return object.getValue();
}

@Override
public String readString() throws IOException, SRMetaBlockEOFException {
PrimitiveObject<String> object = readJson(new TypeToken<PrimitiveObject<String>>() {}.getType());
return object.getValue();
}

@Override
public <T> T readJson(Type returnType) throws IOException, SRMetaBlockEOFException {
checkEOF();
Expand Down Expand Up @@ -207,6 +249,11 @@ private <T> T readMapElement(Type typeOfT) {
new TypeToken<PrimitiveObject<Boolean>>() {}.getType());
return (T) object.getValue();
}
if (typeOfT == String.class) {
PrimitiveObject<String> object = GsonUtils.GSON.fromJson(jsonReader,
new TypeToken<PrimitiveObject<String>>() {}.getType());
return (T) object.getValue();
}
return GsonUtils.GSON.fromJson(jsonReader, typeOfT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,19 @@ public interface SRMetaBlockWriter {

void writeLong(long value) throws IOException, SRMetaBlockException;

void writeByte(byte value) throws IOException, SRMetaBlockException;

void writeShort(short value) throws IOException, SRMetaBlockException;

void writeDouble(double value) throws IOException, SRMetaBlockException;

void writeFloat(float value) throws IOException, SRMetaBlockException;

void writeChar(char value) throws IOException, SRMetaBlockException;

void writeBoolean(boolean value) throws IOException, SRMetaBlockException;

void writeString(String value) throws IOException, SRMetaBlockException;

void close() throws IOException, SRMetaBlockException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,41 @@ public void writeLong(long value) throws IOException, SRMetaBlockException {
writeJson(value);
}

@Override
public void writeByte(byte value) throws IOException, SRMetaBlockException {
writeJson(value);
}

@Override
public void writeShort(short value) throws IOException, SRMetaBlockException {
writeJson(value);
}

@Override
public void writeDouble(double value) throws IOException, SRMetaBlockException {
writeJson(value);
}

@Override
public void writeFloat(float value) throws IOException, SRMetaBlockException {
writeJson(value);
}

@Override
public void writeChar(char value) throws IOException, SRMetaBlockException {
writeJson(value);
}

@Override
public void writeBoolean(boolean value) throws IOException, SRMetaBlockException {
writeJson(value);
}

@Override
public void writeString(String value) throws IOException, SRMetaBlockException {
writeJson(value);
}

@Override
public void close() throws IOException, SRMetaBlockException {
// check if write as many json string as expect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,41 @@ public void writeLong(long value) throws IOException, SRMetaBlockException {
writeJson(new PrimitiveObject<>(value));
}

@Override
public void writeByte(byte value) throws IOException, SRMetaBlockException {
writeJson(new PrimitiveObject<>(value));
}

@Override
public void writeShort(short value) throws IOException, SRMetaBlockException {
writeJson(new PrimitiveObject<>(value));
}

@Override
public void writeDouble(double value) throws IOException, SRMetaBlockException {
writeJson(new PrimitiveObject<>(value));
}

@Override
public void writeFloat(float value) throws IOException, SRMetaBlockException {
writeJson(new PrimitiveObject<>(value));
}

@Override
public void writeChar(char value) throws IOException, SRMetaBlockException {
writeJson(new PrimitiveObject<>(value));
}

@Override
public void writeBoolean(boolean value) throws IOException, SRMetaBlockException {
writeJson(new PrimitiveObject<>(value));
}

@Override
public void writeString(String value) throws IOException, SRMetaBlockException {
writeJson(new PrimitiveObject<>(value));
}

@Override
public void close() throws IOException, SRMetaBlockException {
// check if write as many json string as expect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.starrocks.catalog.MysqlTable;
import com.starrocks.catalog.Table;
import com.starrocks.common.Config;
import com.starrocks.persist.gson.SubtypeNotFoundException;
import com.starrocks.utframe.UtFrameUtils;
import org.apache.commons.io.FileUtils;
import org.junit.AfterClass;
Expand Down Expand Up @@ -394,16 +395,25 @@ public void testSubTypeRollbackInReadCollection() throws Exception {
writer.writeJson(new TestTable());
writer.close();

final List<Table> tables = new ArrayList<>();
final SRMetaBlockReader reader = new SRMetaBlockReaderV1(image.getDataInputStream());
Assert.assertThrows(SubtypeNotFoundException.class,
() -> reader.readCollection(Table.class, tables::add));

List<Table> tables2 = new ArrayList<>();
SRMetaBlockReader reader2 = new SRMetaBlockReaderV1(image.getDataInputStream());
Config.ignore_unknown_subtype = true;
List<Table> tables = new ArrayList<>();
SRMetaBlockReader reader = new SRMetaBlockReaderV1(image.getDataInputStream());
reader.readCollection(Table.class, tables::add);
reader.close();
try {
reader2.readCollection(Table.class, tables2::add);
reader2.close();
} finally {
Config.ignore_unknown_subtype = false;
}

Assert.assertEquals(3, tables.size());
Assert.assertTrue(tables.get(0) instanceof MysqlTable);
Assert.assertTrue(tables.get(1) instanceof HiveTable);
Assert.assertTrue(tables.get(2) instanceof JDBCTable);
Assert.assertEquals(3, tables2.size());
Assert.assertTrue(tables2.get(0) instanceof MysqlTable);
Assert.assertTrue(tables2.get(1) instanceof HiveTable);
Assert.assertTrue(tables2.get(2) instanceof JDBCTable);
}

@Test
Expand All @@ -422,15 +432,53 @@ public void testSubTypeRollbackInMapEntry() throws Exception {
writer.writeJson(new TestTable());
writer.close();

final Map<Long, Table> tables = new HashMap<>();
final SRMetaBlockReader reader = new SRMetaBlockReaderV1(image.getDataInputStream());
Assert.assertThrows(SubtypeNotFoundException.class,
() -> reader.readMapEntry(Long.class, Table.class, tables::put));

Map<Long, Table> tables2 = new HashMap<>();
SRMetaBlockReader reader2 = new SRMetaBlockReaderV1(image.getDataInputStream());
Config.ignore_unknown_subtype = true;
Map<Long, Table> tables = new HashMap<>();
SRMetaBlockReader reader = new SRMetaBlockReaderV1(image.getDataInputStream());
reader.readMapEntry(Long.class, Table.class, tables::put);
reader.close();
try {
reader2.readMapEntry(Long.class, Table.class, tables2::put);
reader2.close();
} finally {
Config.ignore_unknown_subtype = false;
}

Assert.assertEquals(3, tables.size());
Assert.assertTrue(tables.get(1L) instanceof MysqlTable);
Assert.assertTrue(tables.get(2L) instanceof HiveTable);
Assert.assertTrue(tables.get(3L) instanceof JDBCTable);
Assert.assertEquals(3, tables2.size());
Assert.assertTrue(tables2.get(1L) instanceof MysqlTable);
Assert.assertTrue(tables2.get(2L) instanceof HiveTable);
Assert.assertTrue(tables2.get(3L) instanceof JDBCTable);
}

@Test
public void testPrimitiveType() throws Exception {
UtFrameUtils.PseudoImage image = new UtFrameUtils.PseudoImage();
SRMetaBlockWriter writer = new SRMetaBlockWriterV1(image.getDataOutputStream(),
SRMetaBlockID.LOCAL_META_STORE, 9);
writer.writeByte((byte) 1);
writer.writeString("xxx");
writer.writeInt(3);
writer.writeLong(4L);
writer.writeBoolean(false);
writer.writeChar('a');
writer.writeDouble(3.3);
writer.writeFloat(3.4f);
writer.writeShort((short) 6);
writer.close();

final SRMetaBlockReader reader = new SRMetaBlockReaderV1(image.getDataInputStream());
Assert.assertEquals((byte) 1, reader.readByte());
Assert.assertEquals("xxx", reader.readString());
Assert.assertEquals(3, reader.readInt());
Assert.assertEquals(4L, reader.readLong());
Assert.assertFalse(reader.readBoolean());
Assert.assertEquals('a', reader.readChar());
Assert.assertTrue(Math.abs(reader.readDouble() - 3.3) < 1e-6);
Assert.assertTrue(Math.abs(reader.readFloat() - 3.4f) < 1e-6);
Assert.assertEquals((short) 6, reader.readShort());
reader.close();
}
}
Loading

0 comments on commit d29c044

Please sign in to comment.