在 daemon 优化了异常处理行为; 为编辑器等也设置了可定义的环境变量; 更新 README;
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
package work.slhaf.snippet;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.snippet.common.Constant;
|
||||
import work.slhaf.snippet.entity.db.Index;
|
||||
import work.slhaf.snippet.entity.file.RebuildEntity;
|
||||
import work.slhaf.snippet.exception.LaunchCheckException;
|
||||
import work.slhaf.snippet.gateway.CodeSnippetSocketServer;
|
||||
import work.slhaf.snippet.service.IndexManager;
|
||||
import work.slhaf.snippet.service.SnippetManager;
|
||||
@@ -13,9 +11,6 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
public class App{
|
||||
@@ -53,14 +48,7 @@ public class App{
|
||||
HashMap<String, String> sha2PathMD = snippetManager.getFileStatus();
|
||||
if (!sha2PathMD.equals(sha2PathDB)) {
|
||||
log.info("数据库与文件目录不一致,重建索引数据库");
|
||||
List<RebuildEntity> snippets = snippetManager.listAll();
|
||||
Set<Index> indexSet = snippets.stream().map(entity -> {
|
||||
Index index = new Index();
|
||||
BeanUtil.copyProperties(entity, index);
|
||||
return index;
|
||||
})
|
||||
.collect(Collectors.toSet());
|
||||
indexManager.rebuildIndex(indexSet);
|
||||
indexManager.rebuildIndex();
|
||||
}
|
||||
log.info("索引数据库检查通过");
|
||||
}
|
||||
@@ -82,7 +70,7 @@ public class App{
|
||||
|
||||
boolean ok = file.mkdirs();
|
||||
if (!ok) {
|
||||
throw new RuntimeException("创建目录失败: " + dir);
|
||||
throw new LaunchCheckException("创建目录失败: " + dir);
|
||||
} else {
|
||||
log.info("创建目录成功: {}", dir);
|
||||
}
|
||||
@@ -91,7 +79,7 @@ public class App{
|
||||
|
||||
private String getEnvOrThrow(String key) {
|
||||
String value = System.getenv(key);
|
||||
if (value == null) throw new RuntimeException("未找到环境变量: " + key);
|
||||
if (value == null) throw new LaunchCheckException("未找到环境变量: " + key);
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package work.slhaf.snippet.exception;
|
||||
|
||||
public class ActionHandleException extends RuntimeException
|
||||
{
|
||||
public ActionHandleException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public ActionHandleException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package work.slhaf.snippet.exception;
|
||||
|
||||
public class LaunchCheckException extends RuntimeException{
|
||||
public LaunchCheckException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public LaunchCheckException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package work.slhaf.snippet.exception;
|
||||
|
||||
public class SnippetManagerException extends RuntimeException{
|
||||
public SnippetManagerException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public SnippetManagerException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import work.slhaf.snippet.entity.file.AddEntity;
|
||||
import work.slhaf.snippet.entity.file.EditEntity;
|
||||
import work.slhaf.snippet.entity.file.ListEntity;
|
||||
import work.slhaf.snippet.entity.file.MetaDataEntity;
|
||||
import work.slhaf.snippet.exception.ActionHandleException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
@@ -53,16 +54,22 @@ public class ActionHandler {
|
||||
|
||||
private SocketOutputData handleDelete(String path) throws SQLException, IOException {
|
||||
if (path.isEmpty()) {
|
||||
return new SocketOutputData(Constant.Status.FAILED, "Path不能为空!");
|
||||
throw new ActionHandleException("Path不能为空!");
|
||||
}
|
||||
try {
|
||||
indexManager.delete(path);
|
||||
snippetManager.delete(path);
|
||||
return new SocketOutputData(Constant.Status.SUCCESS, "删除成功: " + path);
|
||||
} catch (Exception e) {
|
||||
log.warn("代码片段删除失败,尝试重建索引");
|
||||
indexManager.rebuildIndex();
|
||||
throw new ActionHandleException("代码片段删除失败,路径: " + path, e);
|
||||
}
|
||||
indexManager.delete(path);
|
||||
snippetManager.delete(path);
|
||||
return new SocketOutputData(Constant.Status.SUCCESS, "删除成功: " + path);
|
||||
}
|
||||
|
||||
private SocketOutputData handleEdit(EditEntity entity) throws IOException {
|
||||
if (entity.checkEmpty()) {
|
||||
return new SocketOutputData(Constant.Status.FAILED, "Id、Path、代码内容均不能为空!");
|
||||
throw new ActionHandleException("Id、Path、代码内容均不能为空!");
|
||||
}
|
||||
try {
|
||||
snippetManager.update(entity, SnippetManager.UpdateAction.EDIT);
|
||||
@@ -75,14 +82,13 @@ public class ActionHandler {
|
||||
return new SocketOutputData(Constant.Status.SUCCESS, "文件编辑成功: " + entity.getPath());
|
||||
} catch (Exception e) {
|
||||
snippetManager.update(entity, SnippetManager.UpdateAction.FALLBACK);
|
||||
log.error("文件编辑失败, 已回滚: {}", e.getLocalizedMessage());
|
||||
return new SocketOutputData(Constant.Status.FAILED, e.getLocalizedMessage());
|
||||
throw new ActionHandleException("文件编辑失败,已回滚: " + entity, e);
|
||||
}
|
||||
}
|
||||
|
||||
private SocketOutputData handleAdd(AddEntity entity) throws IOException {
|
||||
if (entity.checkEmpty()) {
|
||||
return new SocketOutputData(Constant.Status.FAILED, "Language、Name、代码片段内容均不能为空!");
|
||||
throw new ActionHandleException("Language、Name、代码片段内容均不能为空!");
|
||||
}
|
||||
Path path = Path.of(System.getenv(Constant.Property.DIR), entity.getLanguage().toLowerCase(), entity.getName() + ".md");
|
||||
try {
|
||||
@@ -94,8 +100,7 @@ public class ActionHandler {
|
||||
return new SocketOutputData(Constant.Status.SUCCESS, "代码片段已添加, 路径: " + path);
|
||||
} catch (Exception e) {
|
||||
Files.deleteIfExists(path);
|
||||
log.error("文件添加失败: {}", e.getLocalizedMessage());
|
||||
return new SocketOutputData(Constant.Status.FAILED, e.getLocalizedMessage());
|
||||
throw new ActionHandleException("文件添加失败: " + entity, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
package work.slhaf.snippet.service;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.snippet.common.Constant;
|
||||
import work.slhaf.snippet.entity.db.Index;
|
||||
import work.slhaf.snippet.entity.file.ListEntity;
|
||||
import work.slhaf.snippet.entity.file.RebuildEntity;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
public class IndexManager {
|
||||
@@ -14,6 +18,7 @@ public class IndexManager {
|
||||
private static volatile IndexManager instance;
|
||||
|
||||
private final Connection connection;
|
||||
private final SnippetManager snippetManager = new SnippetManager();
|
||||
|
||||
public static IndexManager getInstance() throws SQLException {
|
||||
if (instance == null) {
|
||||
@@ -47,9 +52,16 @@ public class IndexManager {
|
||||
statement.close();
|
||||
}
|
||||
|
||||
public void rebuildIndex(Set<Index> indexSet) throws SQLException {
|
||||
public void rebuildIndex() throws SQLException, IOException {
|
||||
log.info("重建索引数据库");
|
||||
resetIndex();
|
||||
List<RebuildEntity> snippets = snippetManager.listAll();
|
||||
Set<Index> indexSet = snippets.stream().map(entity -> {
|
||||
Index index = new Index();
|
||||
BeanUtil.copyProperties(entity, index);
|
||||
return index;
|
||||
})
|
||||
.collect(Collectors.toSet());
|
||||
for (Index index : indexSet) {
|
||||
add(index);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import work.slhaf.snippet.common.Constant;
|
||||
import work.slhaf.snippet.entity.Snippet;
|
||||
import work.slhaf.snippet.entity.file.EditEntity;
|
||||
import work.slhaf.snippet.entity.file.RebuildEntity;
|
||||
import work.slhaf.snippet.exception.SnippetManagerException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
@@ -52,7 +53,7 @@ public class SnippetManager {
|
||||
String markdown = snippet.toMarkdown();
|
||||
File file = path.toFile();
|
||||
if (file.exists()) {
|
||||
throw new RuntimeException("文件已存在: " + file.getAbsolutePath());
|
||||
throw new SnippetManagerException("文件已存在: " + file.getAbsolutePath());
|
||||
}
|
||||
file.getParentFile().mkdirs();
|
||||
Files.writeString(file.toPath(), markdown, StandardCharsets.UTF_8);
|
||||
@@ -102,7 +103,7 @@ public class SnippetManager {
|
||||
public String sha(String filePath) {
|
||||
File file = new File(filePath);
|
||||
if (!file.exists()) {
|
||||
throw new RuntimeException("文件不存在: " + filePath);
|
||||
throw new SnippetManagerException("文件不存在: " + filePath);
|
||||
}
|
||||
return DigestUtil.sha256Hex(file);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user