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 ded6e53 commit 77bcaed
Show file tree
Hide file tree
Showing 17 changed files with 155 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@

public final class PathRef
{

private static final String SEPARATOR = "/";

private static final PathRef EMPTY_PATH_REF = new PathRef( "" );

private final String path;

private PathRef( final String path )
Expand All @@ -16,7 +19,7 @@ private PathRef( final String path )

public static PathRef of()
{
return new PathRef( "" );
return EMPTY_PATH_REF;
}

public static PathRef of( String root )
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.enonic.xp.blob.Segment;

public final class BlobReference
implements BlobContainer
{
private final Segment segment;

Expand All @@ -28,12 +27,6 @@ public BlobKey getKey()
return key;
}

@Override
public BlobReference getReference()
{
return this;
}

@Override
public boolean equals( final Object o )
{
Expand All @@ -54,4 +47,10 @@ public int hashCode()
{
return Objects.hash( segment, key );
}

@Override
public String toString()
{
return segment + ":" + key;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L54 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.enonic.xp.repo.impl.dump.blobstore;

import com.google.common.io.ByteSink;
import java.io.IOException;

import com.google.common.io.ByteSource;

import com.enonic.xp.blob.BlobKey;
Expand All @@ -10,15 +11,15 @@ public class DumpBlobRecord
{
private final BlobReference reference;

private final AbstractDumpBlobStore dumpBlobStore;
private final FileDumpBlobStore dumpBlobStore;

public DumpBlobRecord( final Segment segment, final BlobKey key, final AbstractDumpBlobStore dumpBlobStore )
public DumpBlobRecord( final Segment segment, final BlobKey key, final FileDumpBlobStore dumpBlobStore )
{
this.reference = new BlobReference( segment, key );
this.dumpBlobStore = dumpBlobStore;
}

public final BlobKey getKey()
public BlobKey getKey()
{
return reference.getKey();

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L24 was not covered by tests
}
Expand All @@ -28,8 +29,9 @@ public ByteSource getBytes()
return dumpBlobStore.getBytes( reference );
}

public ByteSink getByteSink()
public void override( final byte[] bytes )
throws IOException
{
return dumpBlobStore.getByteSink( reference );
dumpBlobStore.overrideBlob( reference, bytes );
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.enonic.xp.repo.impl.dump.blobstore;

import com.enonic.xp.blob.BlobKey;
import com.enonic.xp.blob.Segment;
import com.google.common.io.ByteSource;

public interface DumpBlobStore
{
DumpBlobRecord getRecord( Segment segment, BlobKey key );
ByteSource getBytes( BlobReference reference );

void addRecord( BlobContainer blobContainer );
void addRecord( BlobReference blobContainer );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.enonic.xp.repo.impl.dump.blobstore;

import com.google.common.io.ByteSource;

import com.enonic.xp.blob.BlobStore;
import com.enonic.xp.repo.impl.dump.PathRef;
import com.enonic.xp.repository.RepositorySegmentUtils;

public final class DumpBlobStoreUtils
{
private DumpBlobStoreUtils()
{
}

public static PathRef getBlobPathRef( final PathRef parent, final BlobReference reference )
{
final String id = reference.getKey().toString();
return parent.resolve( reference.getSegment().getLevel( RepositorySegmentUtils.BLOB_TYPE_LEVEL ).getValue() )
.resolve( id.substring( 0, 2 ) )
.resolve( id.substring( 2, 4 ) )
.resolve( id.substring( 4, 6 ) )
.resolve( id );
}

public static ByteSource getBytesByReference( final BlobStore sourceBlobStore, final BlobReference reference )
{
return sourceBlobStore.getRecord( reference.getSegment(), reference.getKey() ).getBytes();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,82 @@
import java.nio.file.Files;
import java.nio.file.Path;

import com.google.common.io.ByteSink;
import com.google.common.io.ByteSource;
import com.google.common.io.MoreFiles;

import com.enonic.xp.blob.BlobKey;
import com.enonic.xp.blob.BlobStore;
import com.enonic.xp.blob.BlobStoreException;
import com.enonic.xp.blob.Segment;
import com.enonic.xp.repo.impl.dump.PathRef;

public class FileDumpBlobStore
extends AbstractDumpBlobStore
implements DumpBlobStore
{
private final Path baseDir;

public FileDumpBlobStore( final Path baseDir, BlobStore sourceBlobStore )
private final BlobStore sourceBlobStore;

public FileDumpBlobStore( final Path baseDir, final BlobStore sourceBlobStore )
{
super( PathRef.of(), sourceBlobStore );
this.baseDir = baseDir;
this.sourceBlobStore = sourceBlobStore;
}

@Override
protected ByteSource getBytes( final BlobReference reference )
public ByteSource getBytes( final BlobReference reference )
{
return MoreFiles.asByteSource( getBlobPathRef( reference ).asPath( baseDir ) );
return MoreFiles.asByteSource( toPath( reference ) );
}

@Override
protected ByteSink getByteSink( final BlobReference reference )
public void addRecord( final BlobReference reference )
{
return MoreFiles.asByteSink( getBlobPathRef( reference ).asPath( baseDir ) );
final ByteSource data = DumpBlobStoreUtils.getBytesByReference( sourceBlobStore, reference );
writeBlob( reference, data );
}

@Override
public void addRecord( final BlobContainer blobContainer )
public BlobKey addRecord( final Segment segment, final ByteSource data )
{
final BlobReference reference = new BlobReference( segment, BlobKey.from( data ) );

writeBlob( reference, data );
return reference.getKey();
}

public DumpBlobRecord getRecord( final Segment segment, final BlobKey key )
{
return new DumpBlobRecord( segment, key, this );
}
void overrideBlob( final BlobReference reference, byte[] bytes )
throws IOException
{
Files.write( toPath( reference ), bytes );
}

private void writeBlob( final BlobReference reference, final ByteSource data )
{
final Path path = toPath( reference );
try
{
final Path file = getBlobPathRef( blobContainer.getReference() ).asPath( baseDir );
if ( !Files.exists( file ) )
if ( !Files.exists( path ) )
{
Files.createDirectories( file.getParent() );
copyBlob( blobContainer, Files.newOutputStream( file ) );
Files.createDirectories( path.getParent() );

try (var output = Files.newOutputStream( path ))
{
data.copyTo( output );
}
}
}
catch ( final IOException e )

Check warning on line 75 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#L75

Added line #L75 was not covered by tests
{
throw new BlobStoreException( "Failed to add blob", e );

Check warning on line 77 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#L77

Added line #L77 was not covered by tests
}
}

private Path toPath( final BlobReference reference )
{
return DumpBlobStoreUtils.getBlobPathRef( PathRef.of(), reference ).asPath( baseDir );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,35 @@
import com.enonic.xp.repo.impl.dump.PathRef;

public class ZipDumpReadBlobStore
extends AbstractDumpBlobStore
implements DumpBlobStore
{
private final ZipFile zipFile;

public ZipDumpReadBlobStore( ZipFile zipFile, PathRef basePath )
{
super( basePath, null );
private final PathRef basePath;

public ZipDumpReadBlobStore( final ZipFile zipFile, final PathRef basePath )
{
this.basePath = basePath;
this.zipFile = zipFile;
}

@Override
protected ByteSource getBytes( final BlobReference reference )
public ByteSource getBytes( final BlobReference reference )
{
return new ByteSource()
{
@Override
public InputStream openStream()
throws IOException
{
return zipFile.getInputStream( zipFile.getEntry( getBlobPathRef( reference ).asString() ) );
return zipFile.getInputStream( zipFile.getEntry( DumpBlobStoreUtils.getBlobPathRef( basePath, reference ).asString() ) );
}
};
}

@Override
public void addRecord( final BlobReference reference )
{
throw new UnsupportedOperationException();
}
}
Loading

0 comments on commit 77bcaed

Please sign in to comment.