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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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