Skip to content

Commit

Permalink
ImportedCase choose compressMode
Browse files Browse the repository at this point in the history
Signed-off-by: yichen88 <[email protected]>
  • Loading branch information
yichen88 committed Nov 5, 2020
1 parent 4a20cb8 commit 9eeed50
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ private DecompressOnceBinaryDataInputStream(UUID nodeUuid, String name, Row firs
ByteArrayInputStream bais = new ByteArrayInputStream(tmp.toByteArray());
gzis = new GZIPInputStream(bais);
} catch (IOException e) {
e.printStackTrace();
throw new UncheckedIOException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,8 @@ public void test() throws IOException {
assertTrue(storage.removeData(nodeInfo.getId(), "a"));
assertTrue(storage.getDataNames(nodeInfo.getId()).isEmpty());
assertFalse(storage.readBinaryData(nodeInfo.getId(), "a").isPresent());
// }
//
// @Test
// public void testGzipOnce() throws IOException {

String data = "aaaaaaaaaabbbbbbbbbbccccccccccddddddddddddddddddeeeeeeeeeeeeeeeeeeeeefffffffffffffffffffffffff";
// CassandraAppStorage storage = new CassandraAppStorage("test", () -> new CassandraTestContext(cassandraCQLUnit),
// new CassandraAppStorageConfig().setBinaryDataChunkSize(10), new InMemoryEventsBus());
NodeInfo rootNodeId2 = storage.createRootNodeIfNotExists("test", "folder");
NodeInfo nodeInfo2 = storage.createNode(rootNodeId2.getId(), "test1", "folder", "", 0, new NodeGenericMetadata());
try (OutputStream os = storage.writeBinaryData(nodeInfo2.getId(), "gzipOnce", AppStorage.CompressionMode.ONCE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class ImportedCaseBuilder implements ProjectFileBuilder<ImportedCase> {

private Importer importer;

private AppStorage.CompressionMode mode = AppStorage.CompressionMode.MULTI;

private final Properties parameters = new Properties();

public ImportedCaseBuilder(ProjectFileBuildContext context, ImportersLoader importersLoader, ImportConfig importConfig) {
Expand Down Expand Up @@ -122,6 +124,11 @@ public ImportedCaseBuilder withParameters(Map<String, String> parameters) {
return this;
}

public ImportedCaseBuilder setCompressMode(AppStorage.CompressionMode mode) {
this.mode = Objects.requireNonNull(mode);
return this;
}

@Override
public ImportedCase build() {
if (dataSource == null) {
Expand All @@ -140,7 +147,7 @@ public ImportedCase build() {
new NodeGenericMetadata().setString(ImportedCase.FORMAT, importer.getFormat()));

// store case data
importer.copy(dataSource, new AppStorageDataSource(context.getStorage(), info.getId(), info.getName()));
importer.copy(dataSource, new AppStorageDataSource(context.getStorage(), info.getId(), info.getName(), mode));

// store parameters
try (Writer writer = new OutputStreamWriter(context.getStorage().writeBinaryData(info.getId(), ImportedCase.PARAMETERS), StandardCharsets.UTF_8)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
*/
package com.powsybl.afs.storage;

import com.powsybl.commons.datasource.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand All @@ -15,11 +19,6 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.powsybl.commons.datasource.DataSource;

/**
* A datasource corresponding to a data blob stored in the file system.
* A data blob is associated to a node and a name identifying it among data blobs of this node.
Expand All @@ -30,6 +29,10 @@ public class AppStorageDataSource implements DataSource {

private static final String SEPARATOR = "__";

private static final AppStorage.CompressionMode DEFAULT_COMPRESS_MODE = AppStorage.CompressionMode.MULTI;

private final AppStorage.CompressionMode compressMode;

public interface Name {

static Name parse(String text) {
Expand Down Expand Up @@ -132,9 +135,14 @@ public String toString() {
private final String nodeName;

public AppStorageDataSource(AppStorage storage, String nodeId, String nodeName) {
this(storage, nodeId, nodeName, DEFAULT_COMPRESS_MODE);
}

public AppStorageDataSource(AppStorage storage, String nodeId, String nodeName, AppStorage.CompressionMode mode) {
this.storage = Objects.requireNonNull(storage);
this.nodeId = Objects.requireNonNull(nodeId);
this.nodeName = Objects.requireNonNull(nodeName);
this.compressMode = Objects.requireNonNull(mode);
}

@Override
Expand All @@ -147,7 +155,7 @@ public OutputStream newOutputStream(final String suffix, final String ext, boole
if (append) {
throw new UnsupportedOperationException("Append mode not supported");
}
return storage.writeBinaryData(nodeId, new SuffixAndExtension(suffix, ext).toString());
return storage.writeBinaryData(nodeId, new SuffixAndExtension(suffix, ext).toString(), compressMode);
}

@Override
Expand All @@ -156,7 +164,7 @@ public OutputStream newOutputStream(String fileName, boolean append) {
if (append) {
throw new UnsupportedOperationException("Append mode not supported");
}
return storage.writeBinaryData(nodeId, new FileName(fileName).toString());
return storage.writeBinaryData(nodeId, new FileName(fileName).toString(), compressMode);
}

@Override
Expand Down

0 comments on commit 9eeed50

Please sign in to comment.