2 changed files with 94 additions and 0 deletions
@ -0,0 +1,30 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2024 The Thingsboard Authors |
||||
|
* |
||||
|
* 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 org.thingsboard.server.service.cf.ctx; |
||||
|
|
||||
|
import java.util.Map; |
||||
|
|
||||
|
public interface CalculatedFieldStateService { |
||||
|
|
||||
|
Map<CalculatedFieldEntityCtxId, CalculatedFieldEntityCtx> restoreStates(); |
||||
|
|
||||
|
CalculatedFieldEntityCtx restoreState(CalculatedFieldEntityCtxId ctxId); |
||||
|
|
||||
|
void persistState(CalculatedFieldEntityCtxId ctxId, CalculatedFieldEntityCtx state); |
||||
|
|
||||
|
void removeState(CalculatedFieldEntityCtxId ctxId); |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,64 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2024 The Thingsboard Authors |
||||
|
* |
||||
|
* 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 org.thingsboard.server.service.cf.ctx.state; |
||||
|
|
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.thingsboard.common.util.JacksonUtil; |
||||
|
import org.thingsboard.server.service.cf.RocksDBService; |
||||
|
import org.thingsboard.server.service.cf.ctx.CalculatedFieldEntityCtx; |
||||
|
import org.thingsboard.server.service.cf.ctx.CalculatedFieldEntityCtxId; |
||||
|
import org.thingsboard.server.service.cf.ctx.CalculatedFieldStateService; |
||||
|
|
||||
|
import java.util.Map; |
||||
|
import java.util.Optional; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
@Service |
||||
|
@RequiredArgsConstructor |
||||
|
@ConditionalOnExpression("'${service.type:null}'=='monolith'") |
||||
|
public class RocksDBStateService implements CalculatedFieldStateService { |
||||
|
|
||||
|
private final RocksDBService rocksDBService; |
||||
|
|
||||
|
@Override |
||||
|
public Map<CalculatedFieldEntityCtxId, CalculatedFieldEntityCtx> restoreStates() { |
||||
|
return rocksDBService.getAll().entrySet().stream() |
||||
|
.collect(Collectors.toMap( |
||||
|
entry -> JacksonUtil.fromString(entry.getKey(), CalculatedFieldEntityCtxId.class), |
||||
|
entry -> JacksonUtil.fromString(entry.getValue(), CalculatedFieldEntityCtx.class) |
||||
|
)); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public CalculatedFieldEntityCtx restoreState(CalculatedFieldEntityCtxId ctxId) { |
||||
|
return Optional.ofNullable(rocksDBService.get(JacksonUtil.writeValueAsString(ctxId))) |
||||
|
.map(storedState -> JacksonUtil.fromString(storedState, CalculatedFieldEntityCtx.class)) |
||||
|
.orElse(null); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void persistState(CalculatedFieldEntityCtxId ctxId, CalculatedFieldEntityCtx state) { |
||||
|
rocksDBService.put(JacksonUtil.writeValueAsString(ctxId), JacksonUtil.writeValueAsString(state)); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void removeState(CalculatedFieldEntityCtxId ctxId) { |
||||
|
rocksDBService.delete(JacksonUtil.writeValueAsString(ctxId)); |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue