Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
rymsha committed Oct 31, 2023
1 parent 35c55ca commit 25d1c6b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.google.common.io.MoreFiles;

import com.enonic.xp.blob.BlobKey;
import com.enonic.xp.blob.BlobRecord;
import com.enonic.xp.blob.BlobStore;
import com.enonic.xp.blob.BlobStoreException;
import com.enonic.xp.blob.Segment;
Expand All @@ -32,7 +33,12 @@ public ByteSource getBytes( final BlobReference reference )

public void addRecord( final BlobReference reference )
{
writeBlob( reference, sourceBlobStore.getRecord( reference.getSegment(), reference.getKey() ).getBytes() );
final BlobRecord record = sourceBlobStore.getRecord( reference.getSegment(), reference.getKey() );
if ( record == null )
{
throw new BlobStoreException( "Blob not found: " + reference );

Check warning on line 39 in modules/core/core-repo/src/main/java/com/enonic/xp/repo/impl/dump/blobstore/FileDumpBlobStore.java

View check run for this annotation

Codecov / codecov/patch

modules/core/core-repo/src/main/java/com/enonic/xp/repo/impl/dump/blobstore/FileDumpBlobStore.java#L39

Added line #L39 was not covered by tests
}
writeBlob( reference, record.getBytes() );
}

public BlobKey addRecord( final Segment segment, final ByteSource data )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.HashSet;
import java.util.Set;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipMethod;

import com.google.common.base.Preconditions;

import com.enonic.xp.blob.BlobRecord;
import com.enonic.xp.blob.BlobStore;
import com.enonic.xp.blob.BlobStoreException;
import com.enonic.xp.core.internal.FileNames;
import com.enonic.xp.repo.impl.dump.DefaultFilePaths;
import com.enonic.xp.repo.impl.dump.FilePaths;
import com.enonic.xp.repo.impl.dump.PathRef;
import com.enonic.xp.repo.impl.dump.blobstore.BlobReference;
import com.enonic.xp.repo.impl.dump.blobstore.DumpBlobStoreUtils;
Expand All @@ -30,14 +35,14 @@ public class ZipDumpWriter

private final ZipArchiveOutputStream zipArchiveOutputStream;

private final Set<BlobReference> records;
private final Map<String, BlobReference> records;

private final BlobStore sourceBlobStore;

private ZipDumpWriter( final PathRef basePathInZip, final BlobStore sourceBlobStore,
final ZipArchiveOutputStream zipArchiveOutputStream, final Set<BlobReference> records )
private ZipDumpWriter( final FilePaths filePaths, final BlobStore sourceBlobStore,
final ZipArchiveOutputStream zipArchiveOutputStream, final Map<String, BlobReference> records )
{
super( new DefaultFilePaths( basePathInZip ), records::add );
super( filePaths, reference -> records.put( DumpBlobStoreUtils.getBlobPathRef( filePaths.basePath(), reference ).asString(), reference ) );
this.records = records;
this.sourceBlobStore = sourceBlobStore;
this.zipArchiveOutputStream = zipArchiveOutputStream;
Expand All @@ -52,7 +57,7 @@ public static ZipDumpWriter create( final Path basePath, final String dumpName,
Files.newByteChannel( basePath.resolve( dumpName + ZIP_FILE_EXTENSION ), StandardOpenOption.CREATE,
StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.TRUNCATE_EXISTING ) );

return new ZipDumpWriter( PathRef.of( dumpName ), sourceBlobStore, zipArchiveOutputStream, new HashSet<>() );
return new ZipDumpWriter( new DefaultFilePaths( PathRef.of( dumpName ) ), sourceBlobStore, zipArchiveOutputStream, new HashMap<>() );
}
catch ( IOException e )
{
Expand Down Expand Up @@ -83,18 +88,31 @@ public void close()
public void close()
throws IOException
{

final List<BlobReference> notFound = new ArrayList<>();
try
{
// It is not possible to write into multiple zip entries at the same time.
// We delay writing the blobs until the very end, when writing of metadata zip-entry is already complete.
for ( BlobReference reference : records )
for ( var entry : records.entrySet() )
{
final String zipEntryName = DumpBlobStoreUtils.getBlobPathRef( filePaths.basePath(), reference ).asString();
final String zipEntryName = entry.getKey();
final BlobReference reference = entry.getValue();

final BlobRecord record = sourceBlobStore.getRecord( reference.getSegment(), reference.getKey() );
if ( record == null )
{
notFound.add( reference );
continue;

Check warning on line 105 in modules/core/core-repo/src/main/java/com/enonic/xp/repo/impl/dump/writer/ZipDumpWriter.java

View check run for this annotation

Codecov / codecov/patch

modules/core/core-repo/src/main/java/com/enonic/xp/repo/impl/dump/writer/ZipDumpWriter.java#L104-L105

Added lines #L104 - L105 were not covered by tests
}

zipArchiveOutputStream.putArchiveEntry( new ZipArchiveEntry( zipEntryName ) );
sourceBlobStore.getRecord( reference.getSegment(), reference.getKey() ).getBytes().copyTo( zipArchiveOutputStream );
record.getBytes().copyTo( zipArchiveOutputStream );
zipArchiveOutputStream.closeArchiveEntry();
}
if ( !notFound.isEmpty() )
{
throw new BlobStoreException( "Blobs not found: " + notFound );

Check warning on line 114 in modules/core/core-repo/src/main/java/com/enonic/xp/repo/impl/dump/writer/ZipDumpWriter.java

View check run for this annotation

Codecov / codecov/patch

modules/core/core-repo/src/main/java/com/enonic/xp/repo/impl/dump/writer/ZipDumpWriter.java#L114

Added line #L114 was not covered by tests
}
}
finally
{
Expand Down

0 comments on commit 25d1c6b

Please sign in to comment.