REM:
1. 优化MsgSerial存储方式
This commit is contained in:
chenlinghy 2021-09-15 16:45:43 +08:00
parent f8778ad438
commit 4e8389df01
8 changed files with 24 additions and 42 deletions

View File

@ -11,7 +11,6 @@ import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO;
import com.dispose.pojo.dto.protocol.kafka.EmosAlarmInfo;
import com.dispose.pojo.dto.protocol.kafka.AlarmInfoReq;
import com.dispose.pojo.entity.AlarmInformation;
import com.dispose.pojo.entity.MsgSerial;
import com.dispose.pojo.po.MulReturnType;
import com.dispose.security.annotation.Decryption;
import com.dispose.security.annotation.Encryption;
@ -121,8 +120,7 @@ public class kafkaController {
try {
long dbIncrement = msgSerialService.getMaxMessageSerial();
long increment = dbIncrement + 1;
MsgSerial msgSerial = MsgSerial.builder().msgSerial(increment).build();
MulReturnType<ErrorCode, Long> returnType = msgSerialService.addMessageSerial(msgSerial);
MulReturnType<ErrorCode, Long> returnType = msgSerialService.updateMessageSerial(increment);
if (returnType.getFirstParam() == ErrorCode.ERR_OK) {
increment = returnType.getSecondParam();
} else {

View File

@ -1,7 +1,6 @@
package com.dispose.manager;
import com.dispose.common.ErrorCode;
import com.dispose.pojo.entity.MsgSerial;
public interface MsgSerialManager {
/**
@ -10,7 +9,7 @@ public interface MsgSerialManager {
* @param msgSerial the message serial
* @return the error code
*/
ErrorCode addMsgSerialNumber(MsgSerial msgSerial);
ErrorCode updateMsgSerialNumber(Long msgSerial);
/**
* get new max message serial.

View File

@ -3,7 +3,6 @@ package com.dispose.manager.impl;
import com.dispose.common.ErrorCode;
import com.dispose.manager.MsgSerialManager;
import com.dispose.mapper.MsgSerialMapper;
import com.dispose.pojo.entity.MsgSerial;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@ -30,8 +29,8 @@ public class MsgSerialManagerImpl implements MsgSerialManager {
* @return the error code
*/
@Override
public ErrorCode addMsgSerialNumber(MsgSerial msgSerial) {
if (msgSerialMapper.addMsgSerial(msgSerial) == 1) {
public ErrorCode updateMsgSerialNumber(Long msgSerial) {
if (msgSerialMapper.updateMsgSerial(msgSerial) == 1) {
return ErrorCode.ERR_OK;
} else {
return ErrorCode.ERR_DATABASE;

View File

@ -1,7 +1,5 @@
package com.dispose.mapper;
import com.dispose.pojo.entity.MsgSerial;
/**
* The interface message serial mapper.
*
@ -10,12 +8,12 @@ import com.dispose.pojo.entity.MsgSerial;
public interface MsgSerialMapper {
/**
* Add new task int.
* Update new task int.
*
* @param msgSerial the msgSerial number
* @return the int
*/
int addMsgSerial(MsgSerial msgSerial);
int updateMsgSerial(Long msgSerial);
/**
* get new max message serial.

View File

@ -1,17 +1,16 @@
package com.dispose.service;
import com.dispose.common.ErrorCode;
import com.dispose.pojo.entity.MsgSerial;
import com.dispose.pojo.po.MulReturnType;
public interface MsgSerialService {
/**
* add message serial.
* update message serial.
*
* @param msgSerial the message serial
* @return the mul return type
*/
MulReturnType<ErrorCode, Long> addMessageSerial(MsgSerial msgSerial);
MulReturnType<ErrorCode, Long> updateMessageSerial(Long msgSerial);
/**
* get new max message serial.

View File

@ -2,7 +2,6 @@ package com.dispose.service.impl;
import com.dispose.common.ErrorCode;
import com.dispose.manager.MsgSerialManager;
import com.dispose.pojo.entity.MsgSerial;
import com.dispose.pojo.po.MulReturnType;
import com.dispose.service.MsgSerialService;
import org.springframework.stereotype.Service;
@ -21,18 +20,17 @@ public class MsgSerialServiceImpl implements MsgSerialService {
* @return the mul return type
*/
@Override
public MulReturnType<ErrorCode, Long> addMessageSerial(MsgSerial msgSerial) {
public MulReturnType<ErrorCode, Long> updateMessageSerial(Long msgSerial) {
//告警序号的最大值2^32-1
long indexEnd = 4294967295L;
long indexStart = 1L;
long currentSerial = msgSerial.getMsgSerial();
//编号从1开始以实时消息发布通道为单位进行编号如果编号超过最大正整数(2^32-1)重新从1开始编号
if (currentSerial > indexEnd) {
currentSerial = indexStart;
if (msgSerial > indexEnd) {
msgSerial = indexStart;
}
if (msgSerialManager.addMsgSerialNumber(msgSerial) == ErrorCode.ERR_OK) {
return new MulReturnType<>(ErrorCode.ERR_OK, currentSerial);
if (msgSerialManager.updateMsgSerialNumber(msgSerial) == ErrorCode.ERR_OK) {
return new MulReturnType<>(ErrorCode.ERR_OK, msgSerial);
} else {
return new MulReturnType<>(ErrorCode.ERR_DATABASE, null);
}

View File

@ -6,14 +6,12 @@
<result column="msgSerial" property="msgSerial"/>
</resultMap>
<insert id="addMsgSerial" useGeneratedKeys="true" keyProperty="id"
parameterType="com.dispose.pojo.entity.MsgSerial">
INSERT
IGNORE INTO msg_serial(msgSerial)
VALUES (
#{msgSerial}
)
</insert>
<update id="updateMsgSerial">
UPDATE
msg_serial
SET msgSerial = #{msgSerial}
WHERE id = 1;
</update>
<select id="getMaxMsgSerial" resultType="java.lang.Long">
SELECT msgSerial

View File

@ -1,7 +1,6 @@
package com.dispose.test.dev.mapper;
import com.dispose.mapper.MsgSerialMapper;
import com.dispose.pojo.entity.MsgSerial;
import com.dispose.test.dev.Global.InitTestEnvironment;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
@ -36,22 +35,16 @@ public class MsgSerialMapperTest extends InitTestEnvironment {
*/
@Test
public void a1_addMsgSerial() {
for (long i = 1L; i <= 10L; i++) {
MsgSerial msgSerial = MsgSerial.builder()
.msgSerial(i)
.build();
log.info("++++++++++++++++++MsgSerial {}", msgSerial.toString());
msgSerialMapper.addMsgSerial(msgSerial);
for (long msgSerial = 1L; msgSerial <= 10L; msgSerial++) {
log.info("++++++++++++++++++MsgSerial {}", msgSerial);
msgSerialMapper.updateMsgSerial(msgSerial);
}
}
@Test
public void a2_getMaxMsgSerial() {
for (long i = 1L; i <= 16L; i++) {
MsgSerial msgSerial = MsgSerial.builder()
.msgSerial(i)
.build();
msgSerialMapper.addMsgSerial(msgSerial);
for (long msgSerial = 1L; msgSerial <= 16L; msgSerial++) {
msgSerialMapper.updateMsgSerial(msgSerial);
}
long maxMsgSerial = msgSerialMapper.getMaxMsgSerial();
log.info("+++++++++++++++++++ max MsgSerial {}", maxMsgSerial);