Skip to content

Commit

Permalink
refactor: protocol module
Browse files Browse the repository at this point in the history
  • Loading branch information
fengjiachun committed Feb 4, 2024
1 parent 86f79c4 commit a8ed400
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 47 deletions.
6 changes: 4 additions & 2 deletions ingester-protocol/src/main/java/io/greptime/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ public enum Status {
//
TableColumnNotFound(4002), //
TableColumnExists(4003), //
DatabaseNotFound(4004), RegionNotFound(4005), RegionAlreadyExists(4006), RegionReadonly(4007),
DatabaseNotFound(4004), //
RegionNotFound(4005), //
RegionAlreadyExists(4006), //
RegionReadonly(4007),
// ====== End of catalog related status code =======

// ====== Begin of storage related status code =====
Expand Down Expand Up @@ -125,7 +128,6 @@ public int getStatusCode() {
/**
* Returns {@code true} if the status code is {@link #Success}.
*/
@SuppressWarnings("unused")
public static boolean isSuccess(int statusCode) {
return statusCode == Success.getStatusCode();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,6 @@ public IntervalMonthDayNano(int months, int days, long nanoseconds) {
this.nanoseconds = nanoseconds;
}

public int getMonths() {
return months;
}

public int getDays() {
return days;
}

public long getNanoseconds() {
return nanoseconds;
}

@Override
public Common.IntervalMonthDayNano into() {
return Common.IntervalMonthDayNano.newBuilder() //
Expand Down
30 changes: 15 additions & 15 deletions ingester-protocol/src/main/java/io/greptime/models/RowHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static void addValue(RowData.Row.Builder builder, //
valueBuilder.setI32Value((int) value);
break;
case INT64:
valueBuilder.setI64Value(Util.getLongValue(value));
valueBuilder.setI64Value(ValueUtil.getLongValue(value));
break;
case UINT8:
valueBuilder.setU8Value((int) value);
Expand All @@ -59,7 +59,7 @@ public static void addValue(RowData.Row.Builder builder, //
valueBuilder.setU32Value((int) value);
break;
case UINT64:
valueBuilder.setU64Value(Util.getLongValue(value));
valueBuilder.setU64Value(ValueUtil.getLongValue(value));
break;
case FLOAT32:
valueBuilder.setF32Value(((Number) value).floatValue());
Expand All @@ -77,46 +77,46 @@ public static void addValue(RowData.Row.Builder builder, //
valueBuilder.setStringValue((String) value);
break;
case DATE:
valueBuilder.setDateValue(Util.getDateValue(value));
valueBuilder.setDateValue(ValueUtil.getDateValue(value));
break;
case DATETIME:
valueBuilder.setDatetimeValue(Util.getDateTimeValue(value));
valueBuilder.setDatetimeValue(ValueUtil.getDateTimeValue(value));
break;
case TIMESTAMP_SECOND:
valueBuilder.setTimestampSecondValue(Util.getLongValue(value));
valueBuilder.setTimestampSecondValue(ValueUtil.getLongValue(value));
break;
case TIMESTAMP_MILLISECOND:
valueBuilder.setTimestampMillisecondValue(Util.getLongValue(value));
valueBuilder.setTimestampMillisecondValue(ValueUtil.getLongValue(value));
break;
case TIMESTAMP_MICROSECOND:
valueBuilder.setTimestampMicrosecondValue(Util.getLongValue(value));
valueBuilder.setTimestampMicrosecondValue(ValueUtil.getLongValue(value));
break;
case TIMESTAMP_NANOSECOND:
valueBuilder.setTimestampNanosecondValue(Util.getLongValue(value));
valueBuilder.setTimestampNanosecondValue(ValueUtil.getLongValue(value));
break;
case TIME_SECOND:
valueBuilder.setTimeSecondValue(Util.getLongValue(value));
valueBuilder.setTimeSecondValue(ValueUtil.getLongValue(value));
break;
case TIME_MILLISECOND:
valueBuilder.setTimeMillisecondValue(Util.getLongValue(value));
valueBuilder.setTimeMillisecondValue(ValueUtil.getLongValue(value));
break;
case TIME_MICROSECOND:
valueBuilder.setTimeMicrosecondValue(Util.getLongValue(value));
valueBuilder.setTimeMicrosecondValue(ValueUtil.getLongValue(value));
break;
case TIME_NANOSECOND:
valueBuilder.setTimeNanosecondValue(Util.getLongValue(value));
valueBuilder.setTimeNanosecondValue(ValueUtil.getLongValue(value));
break;
case INTERVAL_YEAR_MONTH:
valueBuilder.setIntervalYearMonthValue((int) value);
break;
case INTERVAL_DAY_TIME:
valueBuilder.setIntervalDayTimeValue(Util.getLongValue(value));
valueBuilder.setIntervalDayTimeValue(ValueUtil.getLongValue(value));
break;
case INTERVAL_MONTH_DAY_NANO:
valueBuilder.setIntervalMonthDayNanoValue(Util.getIntervalMonthDayNanoValue(value));
valueBuilder.setIntervalMonthDayNanoValue(ValueUtil.getIntervalMonthDayNanoValue(value));
break;
case DECIMAL128:
valueBuilder.setDecimal128Value(Util.getDecimal128Value(dataTypeExtension, value));
valueBuilder.setDecimal128Value(ValueUtil.getDecimal128Value(dataTypeExtension, value));
break;
default:
throw new IllegalArgumentException(String.format("Unsupported `data_type`: %s", dataType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/**
* @author jiachun.fjc
*/
public class Util {
public class ValueUtil {

static int ONE_DAY_IN_SECONDS = 86400;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void handle(String signalName) {
try {
File file = FileOutputHelper.getOutputFile(BASE_NAME);

LOG.info("Printing GreptimeDB client metrics triggered by signal: {} to file: {}.", signalName,
LOG.info("Printing GreptimeDB clients metrics triggered by signal: {} to file: {}.", signalName,
file.getAbsoluteFile());

try (PrintStream out = new PrintStream(new FileOutputStream(file, true))) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2023 Greptime Team
*
* Licensed 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 io.greptime.limit;

import org.junit.Assert;
import org.junit.Test;

/**
* @author jiachun.fjc
*/
public class InFlightLimiterTest {

@Test
public void testAcquire() {
InFlightLimiter limiter = new InFlightLimiter(10, "test");
Assert.assertEquals(10, limiter.maxPermits());
limiter.acquire(1);
Assert.assertEquals(9, limiter.availablePermits());
limiter.release(1);
Assert.assertEquals(10, limiter.availablePermits());
}

@Test
public void testTryAcquire() {
InFlightLimiter limiter = new InFlightLimiter(10, "test");
Assert.assertEquals(10, limiter.maxPermits());
Assert.assertTrue(limiter.tryAcquire(1));
Assert.assertEquals(9, limiter.availablePermits());
limiter.release(1);
Assert.assertEquals(10, limiter.availablePermits());
Assert.assertFalse(limiter.tryAcquire(11));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@
/**
* @author jiachun.fjc
*/
public class UtilTest {
public class ValueUtilTest {

@Test
public void testGetLongValue() {
Assert.assertEquals(1L, Util.getLongValue(1));
Assert.assertEquals(1L, Util.getLongValue(1L));
Assert.assertEquals(1L, Util.getLongValue(1.0));
Assert.assertEquals(1L, Util.getLongValue(1.0f));
Assert.assertEquals(1L, Util.getLongValue(BigInteger.valueOf(1)));
Assert.assertEquals(1L, Util.getLongValue(BigDecimal.valueOf(1)));
Assert.assertEquals(1L, ValueUtil.getLongValue(1));
Assert.assertEquals(1L, ValueUtil.getLongValue(1L));
Assert.assertEquals(1L, ValueUtil.getLongValue(1.0));
Assert.assertEquals(1L, ValueUtil.getLongValue(1.0f));
Assert.assertEquals(1L, ValueUtil.getLongValue(BigInteger.valueOf(1)));
Assert.assertEquals(1L, ValueUtil.getLongValue(BigDecimal.valueOf(1)));
}

@Test
Expand All @@ -48,10 +48,10 @@ public void testGetDateValue() {
TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT");
cal.setTimeZone(gmtTimeZone);
cal.set(1970, Calendar.JANUARY, 2);
Assert.assertEquals(1, Util.getDateValue(cal.getTime()));
Assert.assertEquals(1, Util.getDateValue(Instant.ofEpochSecond(86400)));
Assert.assertEquals(1, Util.getDateValue(LocalDate.ofEpochDay(1)));
Assert.assertEquals(1, Util.getDateValue(1));
Assert.assertEquals(1, ValueUtil.getDateValue(cal.getTime()));
Assert.assertEquals(1, ValueUtil.getDateValue(Instant.ofEpochSecond(86400)));
Assert.assertEquals(1, ValueUtil.getDateValue(LocalDate.ofEpochDay(1)));
Assert.assertEquals(1, ValueUtil.getDateValue(1));
}

@Test
Expand All @@ -61,10 +61,27 @@ public void testGetDateTimeValue() {
cal.setTimeZone(gmtTimeZone);
cal.set(1970, Calendar.JANUARY, 2, 0, 0, 0);
cal.set(Calendar.MILLISECOND, 111);
Assert.assertEquals(86400111, Util.getDateTimeValue(cal.getTime()));
Assert.assertEquals(86400111, Util.getDateTimeValue(cal.getTime().toInstant()));
Assert.assertEquals(86400000, Util.getDateTimeValue(Instant.ofEpochSecond(86400)));
Assert.assertEquals(86400, Util.getDateTimeValue(86400));
Assert.assertEquals(86400111, ValueUtil.getDateTimeValue(cal.getTime()));
Assert.assertEquals(86400111, ValueUtil.getDateTimeValue(cal.getTime().toInstant()));
Assert.assertEquals(86400000, ValueUtil.getDateTimeValue(Instant.ofEpochSecond(86400)));
Assert.assertEquals(86400, ValueUtil.getDateTimeValue(86400));
}

@Test
public void testGetIntervalMonthDayNanoValue() {
Common.IntervalMonthDayNano result = ValueUtil.getIntervalMonthDayNanoValue(new IntervalMonthDayNano(1, 2, 3));
Assert.assertEquals(1, result.getMonths());
Assert.assertEquals(2, result.getDays());
Assert.assertEquals(3, result.getNanoseconds());

// test invalid type
try {
ValueUtil.getIntervalMonthDayNanoValue(1);
Assert.fail();
} catch (IllegalArgumentException e) {
Assert.assertEquals("Expected type: `IntervalMonthDayNano`, actual: class java.lang.Integer",
e.getMessage());
}
}

@Test
Expand All @@ -84,7 +101,7 @@ public void testGetDecimal128Value() {
BigInteger bigInt = BigInteger.valueOf(new Random().nextLong()).shiftLeft(64);
bigInt = bigInt.add(BigInteger.valueOf(new Random().nextLong()));
BigDecimal value = new BigDecimal(bigInt, scale);
Common.Decimal128 result = Util.getDecimal128Value(dataTypeExtension, value);
Common.Decimal128 result = ValueUtil.getDecimal128Value(dataTypeExtension, value);

BigDecimal value2 = TestUtil.getDecimal(result, scale);

Expand Down

0 comments on commit a8ed400

Please sign in to comment.