AE86 5 anni fa
parent
commit
3b0fea73be

+ 7 - 0
dbsyncer-storage/src/main/java/org/dbsyncer/storage/lucene/Shard.java

@@ -77,6 +77,13 @@ public class Shard {
         }
         }
     }
     }
 
 
+    public void insertBatch(List<Document> docs) throws IOException {
+        if (null != docs) {
+            indexWriter.addDocuments(docs);
+            indexWriter.commit();
+        }
+    }
+
     public void update(Term term, Document doc) throws IOException {
     public void update(Term term, Document doc) throws IOException {
         if (null != term && null != doc) {
         if (null != term && null != doc) {
             indexWriter.updateDocument(term, doc);
             indexWriter.updateDocument(term, doc);

+ 5 - 3
dbsyncer-storage/src/main/java/org/dbsyncer/storage/support/DiskStorageServiceImpl.java

@@ -25,6 +25,7 @@ import java.util.Collections;
 import java.util.List;
 import java.util.List;
 import java.util.Map;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.stream.Collectors;
 
 
 /**
 /**
  * @author AE86
  * @author AE86
@@ -78,14 +79,14 @@ public class DiskStorageServiceImpl extends AbstractStorageService {
     @Override
     @Override
     public void insert(String collectionId, Map params) throws IOException {
     public void insert(String collectionId, Map params) throws IOException {
         createShardIfNotExist(collectionId);
         createShardIfNotExist(collectionId);
-        Document doc = ParamsUtil.convertParamsToDocument(params);
+        Document doc = ParamsUtil.convertParams2Doc(params);
         map.get(collectionId).insert(doc);
         map.get(collectionId).insert(doc);
     }
     }
 
 
     @Override
     @Override
     public void update(String collectionId, Map params) throws IOException {
     public void update(String collectionId, Map params) throws IOException {
         createShardIfNotExist(collectionId);
         createShardIfNotExist(collectionId);
-        Document doc = ParamsUtil.convertParamsToDocument(params);
+        Document doc = ParamsUtil.convertParams2Doc(params);
         IndexableField field = doc.getField(ConfigConstant.CONFIG_MODEL_ID);
         IndexableField field = doc.getField(ConfigConstant.CONFIG_MODEL_ID);
         map.get(collectionId).update(new Term(ConfigConstant.CONFIG_MODEL_ID, field.stringValue()), doc);
         map.get(collectionId).update(new Term(ConfigConstant.CONFIG_MODEL_ID, field.stringValue()), doc);
     }
     }
@@ -108,7 +109,8 @@ public class DiskStorageServiceImpl extends AbstractStorageService {
 //        createShardIfNotExist(collectionId);
 //        createShardIfNotExist(collectionId);
         // TODO 实现数据写入
         // TODO 实现数据写入
         logger.info(list.toString());
         logger.info(list.toString());
-
+//        List<Document> docs = list.parallelStream().map(r -> ParamsUtil.convertData2Doc(r)).collect(Collectors.toList());
+//        map.get(collectionId).insertBatch(docs);
     }
     }
 
 
     /**
     /**

+ 20 - 1
dbsyncer-storage/src/main/java/org/dbsyncer/storage/util/ParamsUtil.java

@@ -14,7 +14,7 @@ import java.util.Map;
 public abstract class ParamsUtil {
 public abstract class ParamsUtil {
     private ParamsUtil(){}
     private ParamsUtil(){}
 
 
-    public static Document convertParamsToDocument(Map params) {
+    public static Document convertParams2Doc(Map params) {
         Assert.notNull(params, "Params can not be null.");
         Assert.notNull(params, "Params can not be null.");
         Document doc = new Document();
         Document doc = new Document();
         String id = (String) params.get(ConfigConstant.CONFIG_MODEL_ID);
         String id = (String) params.get(ConfigConstant.CONFIG_MODEL_ID);
@@ -32,4 +32,23 @@ public abstract class ParamsUtil {
         doc.add(new StoredField(ConfigConstant.CONFIG_MODEL_JSON, json));
         doc.add(new StoredField(ConfigConstant.CONFIG_MODEL_JSON, json));
         return doc;
         return doc;
     }
     }
+
+    public static Document convertData2Doc(Map params) {
+        Assert.notNull(params, "Params can not be null.");
+        Document doc = new Document();
+        String id = (String) params.get(ConfigConstant.CONFIG_MODEL_ID);
+        Boolean success = (Boolean) params.get(ConfigConstant.DATA_SUCCESS);
+        String event = (String) params.get(ConfigConstant.DATA_EVENT);
+        String error = (String) params.get(ConfigConstant.DATA_ERROR);
+        String json = (String) params.get(ConfigConstant.CONFIG_MODEL_JSON);
+        Long createTime = (Long) params.get(ConfigConstant.CONFIG_MODEL_CREATE_TIME);
+
+        doc.add(new StringField(ConfigConstant.CONFIG_MODEL_ID, id, Field.Store.YES));
+        doc.add(new StringField(ConfigConstant.DATA_SUCCESS, String.valueOf(success), Field.Store.YES));
+        doc.add(new StringField(ConfigConstant.DATA_EVENT, event, Field.Store.YES));
+        doc.add(new TextField(ConfigConstant.DATA_ERROR, error, Field.Store.YES));
+        doc.add(new StoredField(ConfigConstant.CONFIG_MODEL_JSON, json));
+        doc.add(new LongPoint(ConfigConstant.CONFIG_MODEL_CREATE_TIME, createTime));
+        return doc;
+    }
 }
 }