@Override public FileData getFileByIndex(FileData fileIndex) { FileData.Builder builder = FileData.newBuilder().setId(fileIndex.getId()); FileData data = getFileById(fileIndex.getId()); if (null != data) { List<DataSlice> slices = FileDataHelper.extract(data, fileIndex); builder.addAllSlice(slices); } return builder.build(); }
@Override public FileData storeFile(FileData fileData) { FileData.Builder builder = FileData.newBuilder().mergeFrom(fileData); FileData prevData = fileCache.get(fileData.getId()); if (null != prevData) { builder.addAllSlice(prevData.getSliceList()); } FileData data = builder.build(); if (FileDataHelper.isSane(data)) { FileData fixed = FileDataHelper.removeDuplicate(data); fileStore.put(fileData.getId(), fixed.toByteArray()); // This cache can reserved for performance fileCache.remove(fileData.getId()); } else { fileCache.put(fileData.getId(), data); if (FileDataHelper.nearComplete(data)) { List<DataSlice> missingSlices = FileDataHelper.findMissing(data); FileData missing = FileData.newBuilder().setId(data.getId()).addAllSlice(missingSlices).build(); return missing; } } return FileData.newBuilder().setId(data.getId()).build(); }