Skip to content

Commit

Permalink
Merge pull request #190 from thingsboard/search-text-remove
Browse files Browse the repository at this point in the history
Search text column remove
  • Loading branch information
dmytro-landiak authored Dec 17, 2024
2 parents adce77c + 07136f3 commit e3566e0
Show file tree
Hide file tree
Showing 27 changed files with 89 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -189,7 +190,16 @@ public void upgradeDatabase(String fromVersion) {
}
break;
case "2.0.0":
updateSchema("2.0.0", 2000000, "2.0.1", 2000001);
updateSchema("2.0.0", 2000000, "2.0.1", 2000001, conn -> {

for (String table : List.of("broker_user", "mqtt_client_credentials", "application_shared_subscription", "websocket_connection")) {
try {
conn.createStatement().execute("ALTER TABLE " + table + " DROP COLUMN IF EXISTS search_text");
} catch (Exception ignored) {
}
}

});
break;
default:
throw new RuntimeException("Unable to upgrade SQL database, unsupported fromVersion: " + fromVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Profile;
Expand All @@ -26,6 +27,11 @@

import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import static org.thingsboard.mqtt.broker.cache.CacheConstants.BASIC_CREDENTIALS_PASSWORD_CACHE;
import static org.thingsboard.mqtt.broker.cache.CacheConstants.MQTT_CLIENT_CREDENTIALS_CACHE;
import static org.thingsboard.mqtt.broker.cache.CacheConstants.SSL_REGEX_BASED_CREDENTIALS_CACHE;

@RequiredArgsConstructor
@Service
Expand All @@ -36,6 +42,9 @@ public class DefaultCacheCleanupService implements CacheCleanupService {
private final CacheManager cacheManager;
private final Optional<RedisTemplate<String, Object>> redisTemplate;

@Value("${cache.cache-prefix:}")
private String cachePrefix;

/**
* Cleanup caches that can not deserialize anymore due to schema upgrade or data update using sql scripts.
* Refer to SqlDatabaseUpgradeService and /data/upgrade/*.sql
Expand All @@ -48,6 +57,20 @@ public void clearCache(String fromVersion) throws Exception {
log.info("Clearing cache to upgrade from version 1.3.0 to 1.4.0");
clearAll();
break;
case "2.0.0":
log.info("Clearing cache to upgrade from version 2.0.0 to 2.0.1");

Set<String> cacheNamesToFlush = Set.of(
cachePrefix + MQTT_CLIENT_CREDENTIALS_CACHE,
cachePrefix + BASIC_CREDENTIALS_PASSWORD_CACHE,
cachePrefix + SSL_REGEX_BASED_CREDENTIALS_CACHE);

cacheManager.getCacheNames().forEach(cacheName -> {
if (cacheManager.getCache(cacheName) != null && cacheNamesToFlush.contains(cacheName)) {
clearCacheByName(cacheName);
}
});

default:
//Do nothing since cache cleanup is optional.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
@Data
@ToString
@EqualsAndHashCode(callSuper = true)
public class ApplicationSharedSubscription extends SearchTextBased {
public class ApplicationSharedSubscription extends BaseData {

@Serial
private static final long serialVersionUID = -3332462179399001894L;
Expand All @@ -44,11 +44,6 @@ public ApplicationSharedSubscription(UUID id) {
super(id);
}

@Override
public String getSearchText() {
return getName();
}

public ApplicationSharedSubscription(ApplicationSharedSubscription mqttClientCredentials) {
super(mqttClientCredentials);
this.name = mqttClientCredentials.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.thingsboard.mqtt.broker.common.data;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.Setter;
import org.thingsboard.mqtt.broker.common.data.id.IdBased;
Expand All @@ -26,6 +27,8 @@
@Setter
public abstract class BaseData extends IdBased {

public static final ObjectMapper mapper = new ObjectMapper();

@Serial
private static final long serialVersionUID = 3948809716795694300L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.extern.slf4j.Slf4j;
import org.thingsboard.mqtt.broker.common.data.validation.NoXss;

Expand All @@ -29,12 +29,12 @@
import java.util.Objects;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

@Slf4j
public abstract class SearchTextBasedWithAdditionalInfo extends SearchTextBased implements HasAdditionalInfo {
public abstract class BaseDataWithAdditionalInfo extends BaseData implements HasAdditionalInfo {

public static final ObjectMapper mapper = new ObjectMapper();
@Serial
private static final long serialVersionUID = 1019819528913153295L;

Expand All @@ -43,17 +43,17 @@ public abstract class SearchTextBasedWithAdditionalInfo extends SearchTextBased
@JsonIgnore
private byte[] additionalInfoBytes;

public SearchTextBasedWithAdditionalInfo() {
public BaseDataWithAdditionalInfo() {
super();
}

public SearchTextBasedWithAdditionalInfo(UUID id) {
public BaseDataWithAdditionalInfo(UUID id) {
super(id);
}

public SearchTextBasedWithAdditionalInfo(SearchTextBasedWithAdditionalInfo searchTextBased) {
super(searchTextBased);
setAdditionalInfo(searchTextBased.getAdditionalInfo());
public BaseDataWithAdditionalInfo(BaseDataWithAdditionalInfo baseData) {
super(baseData);
setAdditionalInfo(baseData.getAdditionalInfo());
}

@Override
Expand All @@ -65,12 +65,29 @@ public void setAdditionalInfo(JsonNode addInfo) {
setJson(addInfo, json -> this.additionalInfo = json, bytes -> this.additionalInfoBytes = bytes);
}

public void setAdditionalInfoField(String field, JsonNode value) {
JsonNode additionalInfo = getAdditionalInfo();
if (!(additionalInfo instanceof ObjectNode)) {
additionalInfo = mapper.createObjectNode();
}
((ObjectNode) additionalInfo).set(field, value);
setAdditionalInfo(additionalInfo);
}

public <T> T getAdditionalInfoField(String field, Function<JsonNode, T> mapper, T defaultValue) {
JsonNode additionalInfo = getAdditionalInfo();
if (additionalInfo != null && additionalInfo.has(field)) {
return mapper.apply(additionalInfo.get(field));
}
return defaultValue;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
SearchTextBasedWithAdditionalInfo that = (SearchTextBasedWithAdditionalInfo) o;
BaseDataWithAdditionalInfo that = (BaseDataWithAdditionalInfo) o;
return Arrays.equals(additionalInfoBytes, that.additionalInfoBytes);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,18 @@
import java.io.Serial;
import java.util.UUID;

@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
public class User extends SearchTextBasedWithAdditionalInfo {
public class User extends BaseDataWithAdditionalInfo {

@Serial
private static final long serialVersionUID = 8250339805336035966L;

@Getter
@Setter
private String email;
@Getter
@Setter
private Authority authority;
@Getter
@Setter
@NoXss
private String firstName;
@Getter
@Setter
@NoXss
private String lastName;

Expand All @@ -52,11 +46,6 @@ public User(UUID id) {
super(id);
}

@Override
public String getSearchText() {
return getEmail();
}

public User(User user) {
super(user);
this.email = user.email;
Expand All @@ -65,7 +54,6 @@ public User(User user) {
this.lastName = user.lastName;
}


@Override
public String toString() {
return "User [email=" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.thingsboard.mqtt.broker.common.data.BaseData;
import org.thingsboard.mqtt.broker.common.data.ClientType;
import org.thingsboard.mqtt.broker.common.data.SearchTextBased;
import org.thingsboard.mqtt.broker.common.data.validation.NoXss;

import java.io.Serial;
Expand All @@ -28,7 +28,7 @@
@Data
@ToString
@EqualsAndHashCode(callSuper = true)
public class MqttClientCredentials extends SearchTextBased {
public class MqttClientCredentials extends BaseData {

@Serial
private static final long serialVersionUID = -8551302106113554112L;
Expand All @@ -51,11 +51,6 @@ public MqttClientCredentials(UUID id) {
super(id);
}

@Override
public String getSearchText() {
return getName();
}

public MqttClientCredentials(MqttClientCredentials mqttClientCredentials) {
super(mqttClientCredentials);
this.name = mqttClientCredentials.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,18 @@
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.thingsboard.mqtt.broker.common.data.SearchTextBased;
import org.thingsboard.mqtt.broker.common.data.BaseData;
import org.thingsboard.mqtt.broker.common.data.validation.NoXss;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.Serial;
import java.util.UUID;

import static org.thingsboard.mqtt.broker.common.data.SearchTextBasedWithAdditionalInfo.mapper;

@Data
@EqualsAndHashCode(callSuper = true)
@Slf4j
public class WebSocketConnection extends SearchTextBased {
public class WebSocketConnection extends BaseData {

@Serial
private static final long serialVersionUID = -2995421139460181824L;
Expand All @@ -47,11 +45,6 @@ public class WebSocketConnection extends SearchTextBased {
@JsonIgnore
private byte[] configurationBytes;

@Override
public String getSearchText() {
return getName();
}

public WebSocketConnectionConfiguration getConfiguration() {
if (configuration != null) {
return configuration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import java.io.Serial;
import java.util.UUID;

import static org.thingsboard.mqtt.broker.common.data.SearchTextBasedWithAdditionalInfo.mapper;

@Data
@EqualsAndHashCode(callSuper = true)
@Slf4j
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ public abstract class AbstractDao<E extends BaseEntity<D>, D>

protected abstract CrudRepository<E, UUID> getCrudRepository();

protected void setSearchText(E entity) {
}

@Override
@Transactional
public D save(D domain) {
Expand All @@ -48,7 +45,6 @@ public D save(D domain) {
log.error("Can't create entity for domain object {}", domain, e);
throw new IllegalArgumentException("Can't create entity for domain object {" + domain + "}", e);
}
setSearchText(entity);
if (log.isDebugEnabled()) {
log.debug("Saving entity {}", entity);
}
Expand Down
Loading

0 comments on commit e3566e0

Please sign in to comment.