ソースを参照

更新 mysql 批量 update 操作

reghao 2 年 前
コミット
fd5e7f4719

+ 11 - 10
dfs-store/src/main/java/cn/reghao/dfs/store/config/FileMessageConverter.java

@@ -31,20 +31,21 @@ public class FileMessageConverter extends AbstractHttpMessageConverter<File> {
     protected File readInternal(Class<? extends File> clazz, HttpInputMessage inputMessage)
             throws IOException, HttpMessageNotReadableException {
         InputStream inputStream = inputMessage.getBody();
+        return saveStream(inputStream);
+    }
+
+    private File saveStream(InputStream inputStream) throws IOException {
         String tmpPath = String.format("/opt/tmp/tomcat/%s.dat", UUID.randomUUID());
         File tmpFile = new File(tmpPath);
         tmpFile.createNewFile();
-        if (inputStream != null) {
-            FileOutputStream outputStream = new FileOutputStream(tmpFile);
-
-            byte[] buffer = new byte[1024];
-            int bytesRead;
-            while ((bytesRead = inputStream.read(buffer)) > 0) {
-                outputStream.write(buffer, 0, bytesRead);
-            }
-            outputStream.flush();
-            outputStream.close();
+        FileOutputStream outputStream = new FileOutputStream(tmpFile);
+        byte[] buffer = new byte[1024];
+        int bytesRead;
+        while ((bytesRead = inputStream.read(buffer)) > 0) {
+            outputStream.write(buffer, 0, bytesRead);
         }
+        outputStream.flush();
+        outputStream.close();
         return tmpFile;
     }
 

+ 2 - 0
dfs-store/src/main/java/cn/reghao/dfs/store/db/mapper/DataBlockMapper.java

@@ -16,6 +16,8 @@ import java.util.List;
 public interface DataBlockMapper extends BaseMapper<DataBlock> {
     @Deprecated
     void updateContentId(@Param("objectId") String objectId, @Param("contentId") String contentId);
+    void updateBatch(List<DataBlock> list);
+
     List<DataBlock> findByObjectId(String objectId);
     List<DataBlock> findDataBlockByPage(Page page);
 }

+ 2 - 2
dfs-store/src/main/java/cn/reghao/dfs/store/util/mysql/DataSourceConfig.java

@@ -36,10 +36,10 @@ public class DataSourceConfig {
         org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
         configuration.setMapUnderscoreToCamelCase(true);
         configuration.setDefaultFetchSize(100);
-        configuration.setDefaultStatementTimeout(30);
+        configuration.setDefaultStatementTimeout(300);
         factoryBean.setConfiguration(configuration);
 
-        String location = "classpath*:mapper/**/**.xml";
+        String location = "classpath*:mapper/**.xml";
         factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(location));
         return factoryBean.getObject();
     }

+ 7 - 7
dfs-store/src/main/resources/application-dev.yml

@@ -1,22 +1,22 @@
 dubbo:
   registry:
-    address: zookeeper://127.0.0.1:2181
+    address: zookeeper://192.168.0.110:2181
 spring:
   redis:
     database: 0
-    host: 127.0.0.1
+    host: 192.168.0.110
     port: 6379
-    password: Dev@123456
+    password: Test@123456
   datasource:
-    url: jdbc:mysql://localhost:3306/reghao_oss_rdb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8
-    username: dev
-    password: Dev@123456
+    url: jdbc:mysql://192.168.0.110:3306/reghao_oss_tdb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8&allowMultiQueries=true
+    username: test
+    password: Test@123456
 dfs:
   domain: oss.reghao.cn
   group: 0
   node: 0
   baseDirs:
-    - /opt/oss/disk/00b989fc-991b-4d4e-959e-9b6e19299b72/
+    - /opt/oss/disk/13f654c8-af87-4710-aac9-7aa086c99aec/
   encryptKey: 5JCdi68CulSDu0TqD4jR
   metaDir: /opt/oss/meta
   cacheDir: /opt/oss/cache

+ 10 - 0
dfs-store/src/main/resources/mapper/DataBlockMapper.xml

@@ -22,6 +22,16 @@
         set content_id=#{contentId}
         where object_id=#{objectId}
     </update>
+    <update id="updateBatch">
+        update data_block
+        <trim prefix="set" suffixOverrides=",">
+            <trim prefix="content_id =case" suffix="end,">
+                <foreach collection="list" item="item" index="index">
+                    when object_id=#{item.objectId} then #{item.contentId}
+                </foreach>
+            </trim>
+        </trim>
+    </update>
 
     <select id="findAll" resultType="cn.reghao.dfs.store.model.po.DataBlock">
         select * from data_block

+ 8 - 3
dfs-store/src/test/java/RedisTest.java

@@ -51,7 +51,7 @@ public class RedisTest {
 
     @Test
     public void test1() {
-        int pageSize = 10000;
+        int pageSize = 1000;
         int pageNumber = 1;
         Page page = new Page(pageNumber, pageSize);
         List<ObjectMeta> list = fileMetaMapper.findObjectMetaByPage(page);
@@ -80,7 +80,7 @@ public class RedisTest {
     FileContentMapper fileContentMapper;
     @Test
     public void test2() {
-        int pageSize = 10000;
+        int pageSize = 1000;
         int pageNumber = 1;
         Page page = new Page(pageNumber, pageSize);
         List<DataBlock> dataBlocks = dataBlockMapper.findDataBlockByPage(page);
@@ -91,11 +91,16 @@ public class RedisTest {
                 String contentId = UUID.randomUUID().toString().replace("-", "");
                 list.add(new FileContent(contentId, objectId));
                 dataBlock.setContentId(contentId);
-                dataBlockMapper.updateContentId(objectId, contentId);
+                //dataBlockMapper.updateContentId(objectId, contentId);
             });
 
+            long start = System.currentTimeMillis();
+            dataBlockMapper.updateBatch(dataBlocks);
+            log.info("cost: {}", System.currentTimeMillis()-start);
             if (!list.isEmpty()) {
+                start = System.currentTimeMillis();
                 fileContentMapper.saveAll(list);
+                log.info("cost: {}", System.currentTimeMillis()-start);
             }
 
             pageNumber++;