|
|
@@ -1,5 +1,6 @@
|
|
|
package cn.reghao.dfs.store.service;
|
|
|
|
|
|
+import cn.reghao.dfs.store.cache.LocalCache;
|
|
|
import cn.reghao.dfs.store.db.mapper.DataBlockMapper;
|
|
|
import cn.reghao.dfs.store.db.mapper.FileMetaMapper;
|
|
|
import cn.reghao.dfs.store.model.po.ContentRange;
|
|
|
@@ -38,13 +39,14 @@ public class ObjectBasicService {
|
|
|
private final int blockSize = 1024*1024*20;
|
|
|
// 10MiB
|
|
|
private final long partLength = 1024*1024*10;
|
|
|
- private FileStoreService fileStoreService;
|
|
|
- private FileUrlService fileUrlService;
|
|
|
- private FileTypeService fileTypeService;
|
|
|
+ private final FileStoreService fileStoreService;
|
|
|
+ private final FileUrlService fileUrlService;
|
|
|
+ private final FileTypeService fileTypeService;
|
|
|
+ private final LocalCache localCache;
|
|
|
|
|
|
public ObjectBasicService(FileMetaMapper fileMetaMapper, DataBlockMapper dataBlockMapper,
|
|
|
FileStoreService fileStoreService, FileUrlService fileUrlService,
|
|
|
- FileTypeService fileTypeService) {
|
|
|
+ FileTypeService fileTypeService, LocalCache localCache) {
|
|
|
this.objectIdGenerator = new IdGenerator(32, "object-id");
|
|
|
this.blockIdGenerator = new IdGenerator(32, "block-id");
|
|
|
this.fileMetaMapper = fileMetaMapper;
|
|
|
@@ -52,16 +54,17 @@ public class ObjectBasicService {
|
|
|
this.fileStoreService = fileStoreService;
|
|
|
this.fileUrlService = fileUrlService;
|
|
|
this.fileTypeService = fileTypeService;
|
|
|
+ this.localCache = localCache;
|
|
|
}
|
|
|
|
|
|
public void putObject(String objectName, File file, String contentType, String sha256sum) throws Exception {
|
|
|
String[] names = objectName.split("/");
|
|
|
String filename = names[names.length-1];
|
|
|
|
|
|
- FileMeta fileMeta2 = fileMetaMapper.findByObjectName(objectName);
|
|
|
- log.info("暂未实现 PUT 存储");
|
|
|
+ /*FileMeta fileMeta2 = fileMetaMapper.findByObjectName(objectName);
|
|
|
+ log.info("暂未实现 PUT 存储");*/
|
|
|
|
|
|
- /*FileMeta fileMeta = fileMetaMapper.findBySha256sum(sha256sum);
|
|
|
+ FileMeta fileMeta = fileMetaMapper.findBySha256sum(sha256sum);
|
|
|
if (fileMeta == null) {
|
|
|
String objectId = objectIdGenerator.stringId();
|
|
|
long len = file.length();
|
|
|
@@ -74,7 +77,7 @@ public class ObjectBasicService {
|
|
|
} else {
|
|
|
FileMeta fileMeta1 = new FileMeta(objectName, filename, fileMeta);
|
|
|
fileMetaMapper.save(fileMeta1);
|
|
|
- }*/
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public void postObject(String objectName, long len, String contentType, InputStream inputStream) throws Exception {
|
|
|
@@ -135,11 +138,7 @@ public class ObjectBasicService {
|
|
|
OutputStream outputStream = response.getOutputStream();
|
|
|
outputStream.flush();
|
|
|
outputStream.close();
|
|
|
- return;
|
|
|
}
|
|
|
-
|
|
|
- String objectId = fileMeta.getObjectId();
|
|
|
-
|
|
|
}
|
|
|
|
|
|
public void getObject(String objectName) throws IOException {
|
|
|
@@ -234,11 +233,25 @@ public class ObjectBasicService {
|
|
|
response.setHeader("Content-Range", "bytes "+start+"-"+(len-1)+"/"+len);
|
|
|
|
|
|
OutputStream outputStream = response.getOutputStream();
|
|
|
- List<DataBlock> list = dataBlockMapper.findByObjectId(objectId);
|
|
|
+ RandomAccessFile raf;
|
|
|
+ String cachePath = localCache.getCache(objectId);
|
|
|
+ if (cachePath != null) {
|
|
|
+ raf = new RandomAccessFile(cachePath, "r");
|
|
|
+ } else {
|
|
|
+ List<DataBlock> list = dataBlockMapper.findByObjectId(objectId);
|
|
|
+ DataBlock dataBlock = list.get(0);
|
|
|
+ String blockId = dataBlock.getBlockId();
|
|
|
+ String absolutePath = dataBlock.getAbsolutePath();
|
|
|
+ localCache.putCache(objectId, absolutePath);
|
|
|
+ raf = new RandomAccessFile(absolutePath, "r");
|
|
|
+ }
|
|
|
+
|
|
|
+ /*List<DataBlock> list = dataBlockMapper.findByObjectId(objectId);
|
|
|
DataBlock dataBlock = list.get(0);
|
|
|
String blockId = dataBlock.getBlockId();
|
|
|
String absolutePath = dataBlock.getAbsolutePath();
|
|
|
- RandomAccessFile raf = new RandomAccessFile(absolutePath, "r");
|
|
|
+
|
|
|
+ RandomAccessFile raf = new RandomAccessFile(absolutePath, "r");*/
|
|
|
raf.seek(start);
|
|
|
|
|
|
// 10MiB
|