committed by
GitHub
87 changed files with 1134 additions and 504 deletions
@ -0,0 +1,108 @@ |
|||
-- |
|||
-- Copyright © 2016-2023 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. |
|||
-- |
|||
|
|||
|
|||
-- create new attribute_kv table schema |
|||
DO |
|||
$$ |
|||
BEGIN |
|||
-- in case of running the upgrade script a second time: |
|||
IF EXISTS(SELECT 1 FROM information_schema.columns WHERE table_name = 'attribute_kv' and column_name='entity_type') THEN |
|||
DROP VIEW IF EXISTS device_info_view; |
|||
DROP VIEW IF EXISTS device_info_active_attribute_view; |
|||
ALTER INDEX IF EXISTS idx_attribute_kv_by_key_and_last_update_ts RENAME TO idx_attribute_kv_by_key_and_last_update_ts_old; |
|||
IF EXISTS(SELECT 1 FROM pg_constraint WHERE conname = 'attribute_kv_pkey') THEN |
|||
ALTER TABLE attribute_kv RENAME CONSTRAINT attribute_kv_pkey TO attribute_kv_pkey_old; |
|||
END IF; |
|||
ALTER TABLE attribute_kv RENAME TO attribute_kv_old; |
|||
CREATE TABLE IF NOT EXISTS attribute_kv |
|||
( |
|||
entity_id uuid, |
|||
attribute_type int, |
|||
attribute_key int, |
|||
bool_v boolean, |
|||
str_v varchar(10000000), |
|||
long_v bigint, |
|||
dbl_v double precision, |
|||
json_v json, |
|||
last_update_ts bigint, |
|||
CONSTRAINT attribute_kv_pkey PRIMARY KEY (entity_id, attribute_type, attribute_key) |
|||
); |
|||
END IF; |
|||
END; |
|||
$$; |
|||
|
|||
-- rename ts_kv_dictionary table to key_dictionary or create table if not exists |
|||
DO |
|||
$$ |
|||
BEGIN |
|||
IF EXISTS(SELECT 1 FROM information_schema.tables WHERE table_name = 'ts_kv_dictionary') THEN |
|||
ALTER TABLE ts_kv_dictionary RENAME CONSTRAINT ts_key_id_pkey TO key_dictionary_id_pkey; |
|||
ALTER TABLE ts_kv_dictionary RENAME TO key_dictionary; |
|||
ELSE CREATE TABLE IF NOT EXISTS key_dictionary( |
|||
key varchar(255) NOT NULL, |
|||
key_id serial UNIQUE, |
|||
CONSTRAINT key_dictionary_id_pkey PRIMARY KEY (key) |
|||
); |
|||
END IF; |
|||
END; |
|||
$$; |
|||
|
|||
-- insert keys into key_dictionary |
|||
DO |
|||
$$ |
|||
BEGIN |
|||
IF EXISTS(SELECT 1 FROM information_schema.tables WHERE table_name = 'attribute_kv_old') THEN |
|||
INSERT INTO key_dictionary(key) SELECT DISTINCT attribute_key FROM attribute_kv_old ON CONFLICT DO NOTHING; |
|||
END IF; |
|||
END; |
|||
$$; |
|||
|
|||
-- migrate attributes from attribute_kv_old to attribute_kv |
|||
DO |
|||
$$ |
|||
DECLARE |
|||
row_num_old integer; |
|||
row_num integer; |
|||
BEGIN |
|||
IF EXISTS(SELECT 1 FROM information_schema.tables WHERE table_name = 'attribute_kv_old') THEN |
|||
INSERT INTO attribute_kv(entity_id, attribute_type, attribute_key, bool_v, str_v, long_v, dbl_v, json_v, last_update_ts) |
|||
SELECT a.entity_id, CASE |
|||
WHEN a.attribute_type = 'CLIENT_SCOPE' THEN 1 |
|||
WHEN a.attribute_type = 'SERVER_SCOPE' THEN 2 |
|||
WHEN a.attribute_type = 'SHARED_SCOPE' THEN 3 |
|||
ELSE 0 |
|||
END, |
|||
k.key_id, a.bool_v, a.str_v, a.long_v, a.dbl_v, a.json_v, a.last_update_ts |
|||
FROM attribute_kv_old a INNER JOIN key_dictionary k ON (a.attribute_key = k.key); |
|||
SELECT COUNT(*) INTO row_num_old FROM attribute_kv_old; |
|||
SELECT COUNT(*) INTO row_num FROM attribute_kv; |
|||
RAISE NOTICE 'Migrated % of % rows', row_num, row_num_old; |
|||
|
|||
IF row_num != 0 THEN |
|||
DROP TABLE IF EXISTS attribute_kv_old; |
|||
ELSE |
|||
RAISE EXCEPTION 'Table attribute_kv is empty'; |
|||
END IF; |
|||
|
|||
CREATE INDEX IF NOT EXISTS idx_attribute_kv_by_key_and_last_update_ts ON attribute_kv(entity_id, attribute_key, last_update_ts desc); |
|||
END IF; |
|||
EXCEPTION |
|||
WHEN others THEN |
|||
ROLLBACK; |
|||
RAISE EXCEPTION 'Error during COPY: %', SQLERRM; |
|||
END |
|||
$$; |
|||
@ -0,0 +1,32 @@ |
|||
/** |
|||
* Copyright © 2016-2023 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.common.data; |
|||
|
|||
import lombok.Getter; |
|||
|
|||
public enum AttributeScope { |
|||
|
|||
CLIENT_SCOPE(1), |
|||
SERVER_SCOPE(2), |
|||
SHARED_SCOPE(3); |
|||
@Getter |
|||
private final int id; |
|||
|
|||
AttributeScope(int id) { |
|||
this.id = id; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
/** |
|||
* Copyright © 2016-2023 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.dao.dictionary; |
|||
|
|||
|
|||
public interface KeyDictionaryDao { |
|||
|
|||
Integer getOrSaveKeyId(String strKey); |
|||
|
|||
String getKey(Integer keyId); |
|||
|
|||
} |
|||
@ -0,0 +1,92 @@ |
|||
/** |
|||
* Copyright © 2016-2023 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.dao.sqlts.dictionary; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.hibernate.exception.ConstraintViolationException; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.dao.DataIntegrityViolationException; |
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.server.dao.dictionary.KeyDictionaryDao; |
|||
import org.thingsboard.server.dao.model.sqlts.dictionary.KeyDictionaryCompositeKey; |
|||
import org.thingsboard.server.dao.model.sqlts.dictionary.KeyDictionaryEntry; |
|||
import org.thingsboard.server.dao.sql.JpaAbstractDaoListeningExecutorService; |
|||
import org.thingsboard.server.dao.util.SqlDao; |
|||
|
|||
import java.util.Optional; |
|||
import java.util.concurrent.ConcurrentHashMap; |
|||
import java.util.concurrent.ConcurrentMap; |
|||
import java.util.concurrent.locks.ReentrantLock; |
|||
|
|||
@Component |
|||
@Slf4j |
|||
@SqlDao |
|||
public class JpaKeyDictionaryDao extends JpaAbstractDaoListeningExecutorService implements KeyDictionaryDao { |
|||
|
|||
private final ConcurrentMap<String, Integer> keyDictionaryMap = new ConcurrentHashMap<>(); |
|||
protected static final ReentrantLock creationLock = new ReentrantLock(); |
|||
|
|||
@Autowired |
|||
private KeyDictionaryRepository keyDictionaryRepository; |
|||
|
|||
@Override |
|||
public Integer getOrSaveKeyId(String strKey) { |
|||
Integer keyId = keyDictionaryMap.get(strKey); |
|||
if (keyId == null) { |
|||
Optional<KeyDictionaryEntry> tsKvDictionaryOptional; |
|||
tsKvDictionaryOptional = keyDictionaryRepository.findById(new KeyDictionaryCompositeKey(strKey)); |
|||
if (tsKvDictionaryOptional.isEmpty()) { |
|||
creationLock.lock(); |
|||
try { |
|||
keyId = keyDictionaryMap.get(strKey); |
|||
if (keyId != null) { |
|||
return keyId; |
|||
} |
|||
tsKvDictionaryOptional = keyDictionaryRepository.findById(new KeyDictionaryCompositeKey(strKey)); |
|||
if (tsKvDictionaryOptional.isEmpty()) { |
|||
KeyDictionaryEntry keyDictionaryEntry = new KeyDictionaryEntry(); |
|||
keyDictionaryEntry.setKey(strKey); |
|||
try { |
|||
KeyDictionaryEntry saved = keyDictionaryRepository.save(keyDictionaryEntry); |
|||
keyDictionaryMap.put(saved.getKey(), saved.getKeyId()); |
|||
keyId = saved.getKeyId(); |
|||
} catch (DataIntegrityViolationException | ConstraintViolationException e) { |
|||
tsKvDictionaryOptional = keyDictionaryRepository.findById(new KeyDictionaryCompositeKey(strKey)); |
|||
KeyDictionaryEntry dictionary = tsKvDictionaryOptional.orElseThrow(() -> new RuntimeException("Failed to get KeyDictionaryEntry entity from DB!")); |
|||
keyDictionaryMap.put(dictionary.getKey(), dictionary.getKeyId()); |
|||
keyId = dictionary.getKeyId(); |
|||
} |
|||
} else { |
|||
keyId = tsKvDictionaryOptional.get().getKeyId(); |
|||
} |
|||
} finally { |
|||
creationLock.unlock(); |
|||
} |
|||
} else { |
|||
keyId = tsKvDictionaryOptional.get().getKeyId(); |
|||
keyDictionaryMap.put(strKey, keyId); |
|||
} |
|||
} |
|||
return keyId; |
|||
} |
|||
|
|||
@Override |
|||
public String getKey(Integer keyId) { |
|||
Optional<KeyDictionaryEntry> byKeyId = keyDictionaryRepository.findByKeyId(keyId); |
|||
return byKeyId.map(KeyDictionaryEntry::getKey).orElse(null); |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue