used version + 1 instead of sequence next value

This commit is contained in:
YevhenBondarenko 2024-06-13 19:18:01 +02:00
parent e2be5c6492
commit e57b9471dc
6 changed files with 12 additions and 23 deletions

View File

@ -31,14 +31,14 @@ import java.util.List;
@SqlDao @SqlDao
public class AttributeKvInsertRepository extends AbstractVersionedInsertRepository<AttributeKvEntity> { public class AttributeKvInsertRepository extends AbstractVersionedInsertRepository<AttributeKvEntity> {
private static final String BATCH_UPDATE = "UPDATE attribute_kv SET str_v = ?, long_v = ?, dbl_v = ?, bool_v = ?, json_v = cast(? AS json), last_update_ts = ?, version = nextval('attribute_kv_version_seq') " + private static final String BATCH_UPDATE = "UPDATE attribute_kv SET str_v = ?, long_v = ?, dbl_v = ?, bool_v = ?, json_v = cast(? AS json), last_update_ts = ?, version = version + 1 " +
"WHERE entity_id = ? and attribute_type =? and attribute_key = ? RETURNING version;"; "WHERE entity_id = ? and attribute_type =? and attribute_key = ? RETURNING version;";
private static final String INSERT_OR_UPDATE = private static final String INSERT_OR_UPDATE =
"INSERT INTO attribute_kv (entity_id, attribute_type, attribute_key, str_v, long_v, dbl_v, bool_v, json_v, last_update_ts, version) " + "INSERT INTO attribute_kv (entity_id, attribute_type, attribute_key, str_v, long_v, dbl_v, bool_v, json_v, last_update_ts) " +
"VALUES(?, ?, ?, ?, ?, ?, ?, cast(? AS json), ?, nextval('attribute_kv_version_seq')) " + "VALUES(?, ?, ?, ?, ?, ?, ?, cast(? AS json), ?) " +
"ON CONFLICT (entity_id, attribute_type, attribute_key) " + "ON CONFLICT (entity_id, attribute_type, attribute_key) " +
"DO UPDATE SET str_v = ?, long_v = ?, dbl_v = ?, bool_v = ?, json_v = cast(? AS json), last_update_ts = ?, version = nextval('attribute_kv_version_seq') RETURNING version;"; "DO UPDATE SET str_v = ?, long_v = ?, dbl_v = ?, bool_v = ?, json_v = cast(? AS json), last_update_ts = ?, version = attribute_kv.version + 1 RETURNING version;";
@Override @Override
protected void setOnBatchUpdateValues(PreparedStatement ps, int i, List<AttributeKvEntity> entities) throws SQLException { protected void setOnBatchUpdateValues(PreparedStatement ps, int i, List<AttributeKvEntity> entities) throws SQLException {

View File

@ -30,7 +30,6 @@ import java.sql.SQLException;
import java.sql.Types; import java.sql.Types;
import java.util.List; import java.util.List;
@SqlTsLatestAnyDao @SqlTsLatestAnyDao
@Repository @Repository
@Transactional @Transactional
@ -41,11 +40,11 @@ public class SqlLatestInsertTsRepository extends AbstractVersionedInsertReposito
private Boolean updateByLatestTs; private Boolean updateByLatestTs;
private static final String BATCH_UPDATE = private static final String BATCH_UPDATE =
"UPDATE ts_kv_latest SET ts = ?, bool_v = ?, str_v = ?, long_v = ?, dbl_v = ?, json_v = cast(? AS json), version = nextval('ts_kv_latest_version_seq') WHERE entity_id = ? AND key = ?"; "UPDATE ts_kv_latest SET ts = ?, bool_v = ?, str_v = ?, long_v = ?, dbl_v = ?, json_v = cast(? AS json), version = version + 1 WHERE entity_id = ? AND key = ?";
private static final String INSERT_OR_UPDATE = private static final String INSERT_OR_UPDATE =
"INSERT INTO ts_kv_latest (entity_id, key, ts, bool_v, str_v, long_v, dbl_v, json_v, version) VALUES(?, ?, ?, ?, ?, ?, ?, cast(? AS json), nextval('ts_kv_latest_version_seq')) " + "INSERT INTO ts_kv_latest (entity_id, key, ts, bool_v, str_v, long_v, dbl_v, json_v) VALUES(?, ?, ?, ?, ?, ?, ?, cast(? AS json)) " +
"ON CONFLICT (entity_id, key) DO UPDATE SET ts = ?, bool_v = ?, str_v = ?, long_v = ?, dbl_v = ?, json_v = cast(? AS json), version = nextval('ts_kv_latest_version_seq')"; "ON CONFLICT (entity_id, key) DO UPDATE SET ts = ?, bool_v = ?, str_v = ?, long_v = ?, dbl_v = ?, json_v = cast(? AS json), version = ts_kv_latest.version + 1";
private static final String BATCH_UPDATE_BY_LATEST_TS = BATCH_UPDATE + " AND ts_kv_latest.ts <= ?"; private static final String BATCH_UPDATE_BY_LATEST_TS = BATCH_UPDATE + " AND ts_kv_latest.ts <= ?";

View File

@ -102,8 +102,6 @@ CREATE TABLE IF NOT EXISTS audit_log (
action_failure_details varchar(1000000) action_failure_details varchar(1000000)
) PARTITION BY RANGE (created_time); ) PARTITION BY RANGE (created_time);
CREATE SEQUENCE IF NOT EXISTS attribute_kv_version_seq cache 1000;
CREATE TABLE IF NOT EXISTS attribute_kv ( CREATE TABLE IF NOT EXISTS attribute_kv (
entity_id uuid, entity_id uuid,
attribute_type int, attribute_type int,
@ -114,7 +112,7 @@ CREATE TABLE IF NOT EXISTS attribute_kv (
dbl_v double precision, dbl_v double precision,
json_v json, json_v json,
last_update_ts bigint, last_update_ts bigint,
version bigint, version bigint default 0,
CONSTRAINT attribute_kv_pkey PRIMARY KEY (entity_id, attribute_type, attribute_key) CONSTRAINT attribute_kv_pkey PRIMARY KEY (entity_id, attribute_type, attribute_key)
); );
@ -541,8 +539,6 @@ CREATE TABLE IF NOT EXISTS entity_view (
CONSTRAINT entity_view_external_id_unq_key UNIQUE (tenant_id, external_id) CONSTRAINT entity_view_external_id_unq_key UNIQUE (tenant_id, external_id)
); );
CREATE SEQUENCE IF NOT EXISTS ts_kv_latest_version_seq cache 1000;
CREATE TABLE IF NOT EXISTS ts_kv_latest CREATE TABLE IF NOT EXISTS ts_kv_latest
( (
entity_id uuid NOT NULL, entity_id uuid NOT NULL,
@ -553,7 +549,7 @@ CREATE TABLE IF NOT EXISTS ts_kv_latest
long_v bigint, long_v bigint,
dbl_v double precision, dbl_v double precision,
json_v json, json_v json,
version bigint, version bigint default 0,
CONSTRAINT ts_kv_latest_pkey PRIMARY KEY (entity_id, key) CONSTRAINT ts_kv_latest_pkey PRIMARY KEY (entity_id, key)
); );

View File

@ -34,8 +34,6 @@ CREATE TABLE IF NOT EXISTS key_dictionary (
CONSTRAINT key_dictionary_id_pkey PRIMARY KEY (key) CONSTRAINT key_dictionary_id_pkey PRIMARY KEY (key)
); );
CREATE SEQUENCE IF NOT EXISTS ts_kv_latest_version_seq cache 1000;
CREATE TABLE IF NOT EXISTS ts_kv_latest ( CREATE TABLE IF NOT EXISTS ts_kv_latest (
entity_id uuid NOT NULL, entity_id uuid NOT NULL,
key int NOT NULL, key int NOT NULL,
@ -45,7 +43,7 @@ CREATE TABLE IF NOT EXISTS ts_kv_latest (
long_v bigint, long_v bigint,
dbl_v double precision, dbl_v double precision,
json_v json, json_v json,
version bigint, version bigint default 0,
CONSTRAINT ts_kv_latest_pkey PRIMARY KEY (entity_id, key) CONSTRAINT ts_kv_latest_pkey PRIMARY KEY (entity_id, key)
); );

View File

@ -14,8 +14,6 @@
-- limitations under the License. -- limitations under the License.
-- --
CREATE SEQUENCE IF NOT EXISTS ts_kv_latest_version_seq cache 1000;
CREATE TABLE IF NOT EXISTS ts_kv_latest CREATE TABLE IF NOT EXISTS ts_kv_latest
( (
entity_id uuid NOT NULL, entity_id uuid NOT NULL,
@ -26,6 +24,6 @@ CREATE TABLE IF NOT EXISTS ts_kv_latest
long_v bigint, long_v bigint,
dbl_v double precision, dbl_v double precision,
json_v json, json_v json,
version bigint, version bigint default 0,
CONSTRAINT ts_kv_latest_pkey PRIMARY KEY (entity_id, key) CONSTRAINT ts_kv_latest_pkey PRIMARY KEY (entity_id, key)
); );

View File

@ -23,7 +23,6 @@ DROP TABLE IF EXISTS alarm_type;
DROP TABLE IF EXISTS asset; DROP TABLE IF EXISTS asset;
DROP TABLE IF EXISTS audit_log; DROP TABLE IF EXISTS audit_log;
DROP TABLE IF EXISTS attribute_kv; DROP TABLE IF EXISTS attribute_kv;
DROP SEQUENCE IF EXISTS attribute_kv_version_seq;
DROP TABLE IF EXISTS component_descriptor; DROP TABLE IF EXISTS component_descriptor;
DROP TABLE IF EXISTS customer; DROP TABLE IF EXISTS customer;
DROP TABLE IF EXISTS device; DROP TABLE IF EXISTS device;
@ -38,7 +37,6 @@ DROP TABLE IF EXISTS tenant;
DROP TABLE IF EXISTS ts_kv; DROP TABLE IF EXISTS ts_kv;
DROP TABLE IF EXISTS ts_kv_latest; DROP TABLE IF EXISTS ts_kv_latest;
DROP TABLE IF EXISTS ts_kv_dictionary; DROP TABLE IF EXISTS ts_kv_dictionary;
DROP SEQUENCE IF EXISTS ts_kv_latest_version_seq;
DROP TABLE IF EXISTS user_credentials; DROP TABLE IF EXISTS user_credentials;
DROP TABLE IF EXISTS widgets_bundle_widget; DROP TABLE IF EXISTS widgets_bundle_widget;
DROP TABLE IF EXISTS widget_type; DROP TABLE IF EXISTS widget_type;