FileMetaTest.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import cn.reghao.dfs.store.DfsStoreApplication;
  2. import cn.reghao.dfs.store.db.mapper.*;
  3. import cn.reghao.dfs.store.model.po.*;
  4. import cn.reghao.dfs.store.service.ObjectNameService;
  5. import cn.reghao.dfs.store.util.UserContext;
  6. import cn.reghao.jutil.jdk.db.Page;
  7. import cn.reghao.jutil.jdk.security.DigestUtil;
  8. import cn.reghao.jutil.jdk.thread.ThreadPoolWrapper;
  9. import cn.reghao.oss.api.constant.ObjectScope;
  10. import cn.reghao.oss.api.constant.UploadChannel;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.apache.commons.io.FileUtils;
  13. import org.junit.Test;
  14. import org.junit.runner.RunWith;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.boot.test.context.SpringBootTest;
  17. import org.springframework.test.context.ActiveProfiles;
  18. import org.springframework.test.context.junit4.SpringRunner;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.nio.file.FileVisitResult;
  22. import java.nio.file.FileVisitor;
  23. import java.nio.file.Files;
  24. import java.nio.file.Path;
  25. import java.nio.file.attribute.BasicFileAttributes;
  26. import java.security.NoSuchAlgorithmException;
  27. import java.util.List;
  28. import java.util.concurrent.ExecutorService;
  29. /**
  30. * @author reghao
  31. * @date 2023-07-16 19:19:12
  32. */
  33. @Slf4j
  34. @ActiveProfiles("dev")
  35. @SpringBootTest(classes = DfsStoreApplication.class)
  36. @RunWith(SpringRunner.class)
  37. public class FileMetaTest {
  38. @Autowired
  39. FileMetaMapper fileMetaMapper;
  40. // 正则表达式查询测试
  41. public void regexpTest() {
  42. String bucket = "";
  43. String prefix = "abc/d/";
  44. String startAfter = "abc/d/e";
  45. Integer maxKeys = 10;
  46. StringBuilder regex = new StringBuilder();
  47. regex.append("^").append(prefix).append("([^/])+/?$");
  48. if (startAfter.isBlank()) {
  49. fileMetaMapper.findAll0(bucket, maxKeys, regex.toString());
  50. } else {
  51. fileMetaMapper.findAll2(bucket, prefix, startAfter, maxKeys);
  52. }
  53. }
  54. /**
  55. * 分页处理
  56. *
  57. * @param
  58. * @return
  59. * @date 2023-05-30 00:26:07
  60. */
  61. @Test
  62. public void processByPage() {
  63. int pageSize = 10_000;
  64. int pageNumber = 1;
  65. Page page = new Page(pageNumber, pageSize);
  66. List<FileMeta> list = fileMetaMapper.findFileMetaByPage(page);
  67. while (!list.isEmpty()) {
  68. list.forEach(fileMeta -> {
  69. });
  70. pageNumber++;
  71. page = new Page(pageNumber, pageSize);
  72. list = fileMetaMapper.findFileMetaByPage(page);
  73. log.info("page -> {}", pageNumber);
  74. }
  75. }
  76. public void walkDir(Path path) throws IOException {
  77. Files.walkFileTree(path, new FileVisitor<>() {
  78. @Override
  79. public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
  80. return FileVisitResult.CONTINUE;
  81. }
  82. @Override
  83. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  84. String absolutePath = file.toString();
  85. process(absolutePath);
  86. return FileVisitResult.CONTINUE;
  87. }
  88. @Override
  89. public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
  90. return FileVisitResult.CONTINUE;
  91. }
  92. @Override
  93. public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
  94. return FileVisitResult.CONTINUE;
  95. }
  96. });
  97. }
  98. private void process(String absolutePath) {
  99. try {
  100. String sha256sum = DigestUtil.sha256sum(absolutePath);
  101. FileMeta fileMeta = fileMetaMapper.findBySha256sum(sha256sum);
  102. if (fileMeta != null) {
  103. FileUtils.deleteQuietly(new File(absolutePath));
  104. log.info("{} 删除 {} -> {}", Thread.currentThread().getName(), total++, absolutePath);
  105. } else {
  106. //log.info("{} 在 db 中不存在", absolutePath);
  107. }
  108. } catch (IOException | NoSuchAlgorithmException e) {
  109. e.printStackTrace();
  110. }
  111. }
  112. ExecutorService threadPool = ThreadPoolWrapper.threadPool("delete-pool", 10);
  113. int total = 1;
  114. @Test
  115. public void deleteFile() throws IOException, InterruptedException {
  116. String baseDir = "/home/reghao/mnt1/spider/bili/img/vidcover";
  117. //baseDir = "/run/media/reghao/7c43343d-c1e2-4e68-bc18-89bd3f61b0c8/cover";
  118. //baseDir = "/home/reghao/mnt1/spider/bili/img/avatar";
  119. Path path = Path.of(baseDir);
  120. //walkDir(path);
  121. threadPool.submit(new Task("/home/reghao/mnt1/spider/bili/img/avatar"));
  122. threadPool.submit(new Task("/run/media/reghao/7c43343d-c1e2-4e68-bc18-89bd3f61b0c8/cover"));
  123. threadPool.submit(new Task("/run/media/reghao/7c43343d-c1e2-4e68-bc18-89bd3f61b0c8/avatar"));
  124. Thread.sleep(3600_000*24*7);
  125. }
  126. class Task implements Runnable {
  127. private final String baseDir;
  128. public Task(String baseDir) {
  129. this.baseDir = baseDir;
  130. }
  131. @Override
  132. public void run() {
  133. Path path = Path.of(baseDir);
  134. try {
  135. walkDir(path);
  136. } catch (IOException e) {
  137. e.printStackTrace();
  138. }
  139. }
  140. }
  141. @Autowired
  142. ObjectNameService objectNameService;
  143. @Test
  144. public void initUploadChannel() {
  145. UserContext userContext = new UserContext(10001L);
  146. for (UploadChannel channel : UploadChannel.values()) {
  147. String objectName = channel.getPrefix();
  148. objectNameService.createParentDirs(objectName, ObjectScope.PUBLIC.getCode());
  149. }
  150. }
  151. @Autowired
  152. DataBlockMapper dataBlockMapper;
  153. @Test
  154. public void dataBlockTest() {
  155. int pageSize = 10000;
  156. int nextId = 0;
  157. List<DataBlock> list = dataBlockMapper.findDataBlocks(pageSize, nextId);
  158. while (!list.isEmpty()) {
  159. process(list);
  160. nextId = list.get(list.size()-1).getId();
  161. list = dataBlockMapper.findDataBlocks(pageSize, nextId);
  162. log.info("nextId -> {}", nextId);
  163. }
  164. }
  165. private void process(List<DataBlock> list) {
  166. for (DataBlock dataBlock : list) {
  167. String contentId = dataBlock.getContentId();
  168. List<FileMeta> list1 = fileMetaMapper.findByContentId(contentId);
  169. if (list1.isEmpty()) {
  170. log.info("{} not exist in file_meta", contentId);
  171. }
  172. }
  173. }
  174. }