Skip to content

Commit

Permalink
query service operations by names (#285)
Browse files Browse the repository at this point in the history
* query service operations by names

* fix regex problem
  • Loading branch information
binyu1005 authored Dec 23, 2024
1 parent 3f622de commit cd10915
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import com.arextest.config.model.dto.application.ApplicationOperationConfiguration;
import com.arextest.config.repository.ConfigRepositoryProvider;
import com.arextest.config.utils.MongoHelper;
import com.arextest.config.utils.RegexUtils;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
Expand Down Expand Up @@ -144,5 +146,18 @@ public List<ApplicationOperationConfiguration> queryByOperationIdList(
.collect(Collectors.toList());
}

public List<ApplicationOperationConfiguration> queryLikeOperationName(
String appid, String operationName) {
if (StringUtils.isEmpty(appid)) {
return Collections.emptyList();
}
Query query = new Query(Criteria.where(ServiceOperationCollection.Fields.appId).is(appid)
.and(ServiceOperationCollection.Fields.operationName)
.regex(RegexUtils.getRegexForFuzzySearch(operationName), "i"));
return mongoTemplate.find(query, ServiceOperationCollection.class)
.stream().map(ServiceOperationMapper.INSTANCE::dtoFromDao)
.collect(Collectors.toList());

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.arextest.config.utils;

import java.util.HashSet;
import java.util.Set;

/**
* @author b_yu
* @since 2024/12/20
*/

public class RegexUtils {

private static final Set<Character> SPECIAL_CHARS = new HashSet<>();

static {
char[] specialCharsArray = {'.', '\\', '+', '*', '?', '[', '^', ']', '$', '(', ')', '{', '}',
'=', '!', '<', '>', '|', ':', '-'};
for (char c : specialCharsArray) {
SPECIAL_CHARS.add(c);
}
}

public static String getRegexForFuzzySearch(String keyword) {
if (keyword == null || keyword.isEmpty()) {
return keyword;
}
return ".*?" + escapeSpecialChars(keyword) + ".*";
}

private static String escapeSpecialChars(String keyword) {
if (keyword == null || keyword.isEmpty()) {
return keyword;
}
StringBuilder escapedKeyword = new StringBuilder();
for (char c : keyword.toCharArray()) {
if (SPECIAL_CHARS.contains(c)) {
escapedKeyword.append('\\');
}
escapedKeyword.append(c);
}
return escapedKeyword.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ logging:
arex:
storage:
enableDiscoveryEntryPoint: true
mongodbUri: mongodb://arex:[email protected]:37017/arex_storage_db
mongodbUri: ${arex.mongo.uri:mongodb://arex:[email protected]:37017/arex_storage_db}
recordEnv: TEST
expirationDurationMap:
ConfigFile: 3456000000
Expand Down
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -443,12 +443,13 @@
<id>jar-local</id>
<properties>
<packagingType>jar</packagingType>
<snapshots.repo></snapshots.repo>
</properties>
</profile>
</profiles>

<properties>
<revision>2.0.3</revision>
<revision>2.0.5</revision>
<commons-lang3.version>3.3.2</commons-lang3.version>
<java.version>1.8</java.version>

Expand Down

0 comments on commit cd10915

Please sign in to comment.