This is an automated email from the ASF dual-hosted git repository.

panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 0015326  Perfect LocalDistMetaDataPersistRepository (#11570)
0015326 is described below

commit 0015326924abd32489b0b50389d7b45984f39444
Author: Haoran Meng <[email protected]>
AuthorDate: Fri Jul 30 10:55:52 2021 +0800

    Perfect LocalDistMetaDataPersistRepository (#11570)
    
    * Perfect LocalDistMetaDataPersistRepository
    
    * modify local meta data persist path
---
 .../local/LocalDistMetaDataPersistRepository.java  | 40 +++++++++++++++++++---
 1 file changed, 36 insertions(+), 4 deletions(-)

diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/persist/repository/local/LocalDistMetaDataPersistRepository.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/persist/repository/local/LocalDistMetaDataPersistRepository.java
index bde797c..f1e16d6 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/persist/repository/local/LocalDistMetaDataPersistRepository.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/persist/repository/local/LocalDistMetaDataPersistRepository.java
@@ -18,37 +18,69 @@
 package org.apache.shardingsphere.infra.config.persist.repository.local;
 
 import com.google.common.base.Strings;
+import lombok.extern.slf4j.Slf4j;
 import 
org.apache.shardingsphere.infra.config.persist.repository.DistMetaDataPersistRepository;
 
-import java.util.Collections;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Optional;
 import java.util.Properties;
+import java.util.stream.Collectors;
 
 /**
  * Local dist meta data persist repository.
  */
-// TODO finish me
+@Slf4j
 public final class LocalDistMetaDataPersistRepository implements 
DistMetaDataPersistRepository {
     
     private String path;
     
     @Override
     public String get(final String key) {
+        if (!Files.exists(Paths.get(path, key))) {
+            return "";
+        }
+        try {
+            return Files.readAllLines(Paths.get(path, key)).stream().map(each 
-> each + System.lineSeparator()).collect(Collectors.joining());
+        } catch (final IOException ex) {
+            //TODO process exception
+            log.error("Get local dist meta data by key: {} failed", key, ex);
+        }
         return "";
     }
     
     @Override
     public List<String> getChildrenKeys(final String key) {
-        return Collections.emptyList();
+        return Arrays.stream(new File(path, 
key).listFiles()).map(File::getName).collect(Collectors.toList());
     }
     
     @Override
     public void persist(final String key, final String value) {
+        File file = new File(path, key);
+        if (!file.exists()) {
+            file.getParentFile().mkdirs();
+        }
+        try (BufferedWriter bufferedWriter = 
Files.newBufferedWriter(Paths.get(path, key))) {
+            bufferedWriter.write(value);
+            bufferedWriter.flush();
+        } catch (final IOException ex) {
+            //TODO process exception
+            log.error("Persist local dist meta data to key: {} failed", key, 
ex);
+        }
     }
     
     @Override
     public void delete(final String key) {
+        boolean isDeleted = new File(path, key).delete();
+        if (!isDeleted) {
+            //TODO process exception
+            log.error("Delete local dist meta data key: {} failed", key);
+        }
     }
     
     @Override
@@ -64,6 +96,6 @@ public final class LocalDistMetaDataPersistRepository 
implements DistMetaDataPer
     public void setProps(final Properties props) {
         LocalRepositoryProperties localRepositoryProperties = new 
LocalRepositoryProperties(props);
         path = 
Optional.ofNullable(Strings.emptyToNull(localRepositoryProperties.getValue(LocalRepositoryPropertyKey.PATH)))
-                .orElse(System.getProperty("user.home"));
+                .orElse(System.getProperty("user.dir"));
     }
 }

Reply via email to