2、FlashTsDB时序数据库-SDT算法(Java)实现

2024-03-15 18:10

本文主要是介绍2、FlashTsDB时序数据库-SDT算法(Java)实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

FlashTsDB时序数据库-SDT算法(Java)实现

FLashDB基于开源模式开发,地址:https://github.com/amon1991/flashdb

1、SDT原理

在这里插入图片描述

2、压缩算法

Point对象

public class Point implements Comparable<Point> {private long x;     // 时间戳private double y; // 测点值// ... 此处省略set,get方法
}

SdtPeriod对象

public class SdtPeriod {private long bgTime;     // 压缩段开始时间private double bgValue; // 压缩段开始值private long endTime;    // 压缩段结束时间private double endValue;// 压缩段结束值private double gradient; // 梯度// ... 此处省略set,get方法}

压缩算法

/*** SDT algorithm compress function** @param originPoints 待压缩的点集* @param accuracyE    旋转门大小* @return*/
public List<SdtPeriod> sdtCompress(List<Point> originPoints, double accuracyE) {List<SdtPeriod> sdtPeriodList = new ArrayList<>();if (null != originPoints && originPoints.size() > 0) {SdtPoints sdtPoints = new SdtPoints();double upGate = -Double.MAX_VALUE;double downGate = Double.MAX_VALUE;for (int i = 0, size = originPoints.size(); i < size; i++) {Point currentPoint = originPoints.get(i);if (i == 0) {sdtPoints.setLastPoint(currentPoint);sdtPoints.setBeginPoint(currentPoint);} else {double nowUpGate = (currentPoint.getY() - sdtPoints.getBeginPoint().getY() - accuracyE) /(currentPoint.getX() - sdtPoints.getBeginPoint().getX());if (nowUpGate > upGate) {upGate = nowUpGate;}double nowDownGate = (currentPoint.getY() - sdtPoints.getBeginPoint().getY() + accuracyE) /(currentPoint.getX() - sdtPoints.getBeginPoint().getX());if (nowDownGate < downGate) {downGate = nowDownGate;}if (upGate > downGate) {// create new SdtPeriod(one)sdtPeriodList.add(structSdtPeriod(sdtPoints));// update gatesupGate = (currentPoint.getY() - sdtPoints.getLastPoint().getY() - accuracyE) /(currentPoint.getX() - sdtPoints.getLastPoint().getX());downGate = (currentPoint.getY() - sdtPoints.getLastPoint().getY() + accuracyE) /(currentPoint.getX() - sdtPoints.getLastPoint().getX());sdtPoints.setBeginPoint(sdtPoints.getLastPoint());}sdtPoints.setLastPoint(currentPoint);}if (i == size - 1) {// save last periodsdtPeriodList.add(structSdtPeriod(sdtPoints));}}}return sdtPeriodList;
}

3、解压算法

/*** SDT algorithm uncompress function** @param sdtPeriodList 压缩段集合* @param bgTime        返回点集开始时间* @param endTime       返回点集结束时间* @param interval      返回点集2点间隔* @return*/
public List<Point> sdtUnCompress(List<SdtPeriod> sdtPeriodList, long bgTime, long endTime, long interval) {List<Point> pointList = new ArrayList<>();if (null != sdtPeriodList && sdtPeriodList.size() > 0) {// recalculate bgTime and endTimelong globalBgTime = sdtPeriodList.get(0).getBgTime();long globalEndTime = sdtPeriodList.get(sdtPeriodList.size() - 1).getEndTime();if (bgTime > globalEndTime || endTime < globalBgTime) {return pointList;}if (bgTime < globalBgTime && endTime < globalEndTime) {while (bgTime < globalBgTime) {bgTime += interval;}}if (globalBgTime < bgTime && globalEndTime < endTime) {long tempEndTime = bgTime;while (tempEndTime < globalEndTime) {tempEndTime += interval;}endTime = tempEndTime - interval;}if (bgTime < globalBgTime && endTime > globalEndTime) {while (bgTime < globalBgTime) {bgTime += interval;}long tempEndTime = bgTime;while (tempEndTime < globalEndTime) {tempEndTime += interval;}endTime = tempEndTime - interval;}// final checkif (bgTime > globalEndTime || endTime < globalBgTime) {return pointList;}long interpolatingTime = bgTime;for (SdtPeriod sdtPeriod : sdtPeriodList) {long periodBgTime = sdtPeriod.getBgTime();double periodBgValue = sdtPeriod.getBgValue();long periodEndTime = sdtPeriod.getEndTime();double grad = sdtPeriod.getGradient();if (interpolatingTime >= periodBgTime && interpolatingTime <= periodEndTime) {while (interpolatingTime <= periodEndTime && interpolatingTime <= endTime) {Point point = new Point();point.setX(interpolatingTime);point.setY(periodBgValue + (interpolatingTime - periodBgTime) * grad);pointList.add(point);interpolatingTime += interval;}}if (interpolatingTime > endTime) {break;}}}return pointList;
}

这篇关于2、FlashTsDB时序数据库-SDT算法(Java)实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/812818

相关文章

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

Spring @Scheduled注解及工作原理

《Spring@Scheduled注解及工作原理》Spring的@Scheduled注解用于标记定时任务,无需额外库,需配置@EnableScheduling,设置fixedRate、fixedDe... 目录1.@Scheduled注解定义2.配置 @Scheduled2.1 开启定时任务支持2.2 创建

PostgreSQL数据库密码被遗忘时的操作步骤

《PostgreSQL数据库密码被遗忘时的操作步骤》密码遗忘是常见的用户问题,因此提供一种安全的遗忘密码找回机制是十分必要的,:本文主要介绍PostgreSQL数据库密码被遗忘时的操作步骤的相关资... 目录前言一、背景知识二、Windows环境下的解决步骤1. 找到PostgreSQL安装目录2. 修改p

SpringBoot中使用Flux实现流式返回的方法小结

《SpringBoot中使用Flux实现流式返回的方法小结》文章介绍流式返回(StreamingResponse)在SpringBoot中通过Flux实现,优势包括提升用户体验、降低内存消耗、支持长连... 目录背景流式返回的核心概念与优势1. 提升用户体验2. 降低内存消耗3. 支持长连接与实时通信在Sp

Conda虚拟环境的复制和迁移的四种方法实现

《Conda虚拟环境的复制和迁移的四种方法实现》本文主要介绍了Conda虚拟环境的复制和迁移的四种方法实现,包括requirements.txt,environment.yml,conda-pack,... 目录在本机复制Conda虚拟环境相同操作系统之间复制环境方法一:requirements.txt方法

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

Mac系统下卸载JAVA和JDK的步骤

《Mac系统下卸载JAVA和JDK的步骤》JDK是Java语言的软件开发工具包,它提供了开发和运行Java应用程序所需的工具、库和资源,:本文主要介绍Mac系统下卸载JAVA和JDK的相关资料,需... 目录1. 卸载系统自带的 Java 版本检查当前 Java 版本通过命令卸载系统 Java2. 卸载自定

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义

SpringMVC高效获取JavaBean对象指南

《SpringMVC高效获取JavaBean对象指南》SpringMVC通过数据绑定自动将请求参数映射到JavaBean,支持表单、URL及JSON数据,需用@ModelAttribute、@Requ... 目录Spring MVC 获取 JavaBean 对象指南核心机制:数据绑定实现步骤1. 定义 Ja