Skip to content

Commit

Permalink
Transient Repositories #10689
Browse files Browse the repository at this point in the history
  • Loading branch information
anatol-sialitski committed Dec 2, 2024
1 parent fad5d17 commit ad3ba7e
Show file tree
Hide file tree
Showing 21 changed files with 139 additions and 11 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ graalvm-js = { module = "org.graalvm.polyglot:js", version = "23.1.3" }
graalvm-jsse = { module = "org.graalvm.js:js-scriptengine", version = "23.1.3" }

commons-lang = { module = "commons-lang:commons-lang", version = "2.6" }
commons-lang3 = { module = "org.apache.commons:commons-lang3", version = "3.17.0" }
commons-io = { module = "commons-io:commons-io", version = "2.18.0" }
commons-compress = { module = "org.apache.commons:commons-compress", version = "1.27.1" }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ public class CreateRepositoryParams

private final ChildOrder rootChildOrder;

private final Boolean transientFlag;

private CreateRepositoryParams( final Builder builder )
{
repositoryId = builder.repositoryId;
repositorySettings = builder.repositorySettings == null ? RepositorySettings.create().build() : builder.repositorySettings;
rootPermissions = builder.rootPermissions;
data = builder.data;
rootChildOrder = builder.rootChildOrder;
transientFlag = builder.transientFlag;
}

public RepositoryId getRepositoryId()
Expand Down Expand Up @@ -59,6 +62,11 @@ public static Builder create()
return new Builder();
}

public Boolean getTransientFlag()
{
return transientFlag;
}

@Override
public boolean equals( final Object o )
{
Expand All @@ -73,13 +81,13 @@ public boolean equals( final Object o )
final CreateRepositoryParams that = (CreateRepositoryParams) o;
return Objects.equals( repositoryId, that.repositoryId ) && Objects.equals( repositorySettings, that.repositorySettings ) &&
Objects.equals( data, that.data ) && Objects.equals( rootPermissions, that.rootPermissions ) &&
Objects.equals( rootChildOrder, that.rootChildOrder );
Objects.equals( rootChildOrder, that.rootChildOrder ) && Objects.equals( transientFlag, that.transientFlag );
}

@Override
public int hashCode()
{
return Objects.hash( repositoryId, repositorySettings, data, rootPermissions, rootChildOrder );
return Objects.hash( repositoryId, repositorySettings, data, rootPermissions, rootChildOrder, transientFlag );

Check warning on line 90 in modules/core/core-api/src/main/java/com/enonic/xp/repository/CreateRepositoryParams.java

View check run for this annotation

Codecov / codecov/patch

modules/core/core-api/src/main/java/com/enonic/xp/repository/CreateRepositoryParams.java#L90

Added line #L90 was not covered by tests
}

public static final class Builder
Expand All @@ -94,6 +102,8 @@ public static final class Builder

private ChildOrder rootChildOrder = RepositoryConstants.DEFAULT_CHILD_ORDER;

private Boolean transientFlag;

private Builder()
{
}
Expand Down Expand Up @@ -134,6 +144,12 @@ public Builder rootChildOrder( final ChildOrder rootChildOrder )
return this;
}

public Builder transientFlag( final Boolean value )
{
this.transientFlag = value;
return this;
}

private void validate()
{
Preconditions.checkNotNull( repositoryId, "repositoryId cannot be null" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ public final class EditableRepository

public List<BinaryAttachment> binaryAttachments = new ArrayList<>();

public Boolean transientFlag;

public EditableRepository( final Repository source )
{
this.source = source;
this.data = source.getData().copy();
this.transientFlag = source.isTransient();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ public final class Repository

private final AttachedBinaries attachments;

private final boolean transientFlag;

private Repository( Builder builder )
{
this.id = builder.id;
this.branches = builder.branches;
this.settings = builder.settings == null ? RepositorySettings.create().build() : builder.settings;
this.data = Objects.requireNonNullElseGet( builder.data, PropertyTree::new );
this.attachments = Objects.requireNonNullElseGet( builder.attachments, AttachedBinaries::empty );
this.transientFlag = Objects.requireNonNullElse( builder.transientFlag, false );
}

public RepositoryId getId()
Expand Down Expand Up @@ -57,6 +60,11 @@ public AttachedBinaries getAttachments()
return attachments;
}

public boolean isTransient()
{
return transientFlag;
}

public static Builder create()
{
return new Builder();
Expand Down Expand Up @@ -102,6 +110,8 @@ public static final class Builder

private AttachedBinaries attachments;

private Boolean transientFlag;

private Builder()
{
}
Expand All @@ -112,6 +122,7 @@ public Builder( final Repository source )
branches = source.branches;
settings = source.settings;
data = source.data;
transientFlag = source.transientFlag;

Check warning on line 125 in modules/core/core-api/src/main/java/com/enonic/xp/repository/Repository.java

View check run for this annotation

Codecov / codecov/patch

modules/core/core-api/src/main/java/com/enonic/xp/repository/Repository.java#L125

Added line #L125 was not covered by tests
}

public Builder id( final RepositoryId id )
Expand Down Expand Up @@ -145,6 +156,12 @@ public Builder data( final PropertyTree data )
return this;
}

public Builder transientFlag( final Boolean value )
{
this.transientFlag = value;
return this;
}

public Builder attachments( final AttachedBinaries attachments )
{
this.attachments = attachments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,12 @@ public SystemDumpResult dump( final SystemDumpParams params )
: FileDumpWriter.create( basePath, params.getDumpName(), blobStore );
try (writer)
{
final List<Repository> repositories = repositoryEntryService.findRepositoryEntryIds().stream().
map( repositoryEntryService::getRepositoryEntry ).filter( Objects::nonNull ).collect( Collectors.toList() );
final List<Repository> repositories = repositoryEntryService.findRepositoryEntryIds()
.stream()
.map( repositoryEntryService::getRepositoryEntry )
.filter( Objects::nonNull )
.filter( repository -> !repository.isTransient() )
.collect( Collectors.toList() );

if ( params.getListener() != null )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.time.Instant;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;

Expand Down Expand Up @@ -84,13 +84,32 @@ public SnapshotResult snapshot( final SnapshotParams snapshotParams )
return NodeHelper.runAsAdmin( () -> doSnapshot( snapshotParams ) );
}

private RepositoryIds resolveRepositoriesIdsToSnapshot( final RepositoryId repositoryId )
{
if ( repositoryId != null )
{
return RepositoryIds.from( repositoryId );

Check warning on line 91 in modules/core/core-repo/src/main/java/com/enonic/xp/repo/impl/elasticsearch/snapshot/SnapshotServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

modules/core/core-repo/src/main/java/com/enonic/xp/repo/impl/elasticsearch/snapshot/SnapshotServiceImpl.java#L91

Added line #L91 was not covered by tests
}
else
{
final RepositoryIds.Builder result = RepositoryIds.create();

repositoryEntryService.findRepositoryEntryIds()
.stream()
.map( repositoryEntryService::getRepositoryEntry )
.filter( Objects::nonNull )
.filter( repository -> !repository.isTransient() )
.forEach( repository -> result.add( repository.getId() ) );

return result.build();
}
}

private SnapshotResult doSnapshot( final SnapshotParams snapshotParams )
{
checkSnapshotRepository();

final RepositoryIds repositoriesToSnapshot = Optional.ofNullable( snapshotParams.getRepositoryId() )
.map( RepositoryIds::from )
.orElseGet( repositoryEntryService::findRepositoryEntryIds );
final RepositoryIds repositoriesToSnapshot = resolveRepositoriesIdsToSnapshot( snapshotParams.getRepositoryId() );

return SnapshotExecutor.create()
.snapshotName( snapshotParams.getSnapshotName() )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.enonic.xp.repository.Repository;
import com.enonic.xp.repository.RepositoryConstants;
import com.enonic.xp.repository.RepositoryId;
import com.enonic.xp.repository.RepositoryIds;
import com.enonic.xp.repository.RepositorySettings;
import com.enonic.xp.security.SystemConstants;

Expand All @@ -36,13 +37,19 @@ public class RepositoryNodeTranslator

private static final String DATA_KEY = "data";

private static final String TRANSIENT_KEY = "transient";

public static Node toNode( final Repository repository )
{
final PropertyTree repositoryNodeData = new PropertyTree();
toNodeData( repository.getBranches(), repositoryNodeData );
final RepositorySettings repositorySettings = repository.getSettings();
toNodeData( repositorySettings.getIndexDefinitions(), repositoryNodeData );
toNodeData( repository.getData(), repositoryNodeData );
if ( !repository.getId().toString().startsWith( "system." ) )
{
repositoryNodeData.setBoolean( TRANSIENT_KEY, repository.isTransient() );
}

return Node.create().
id( NodeId.from( repository.getId() ) ).
Expand All @@ -62,7 +69,13 @@ public static NodeEditor toCreateBranchNodeEditor( final Branch branch )

public static NodeEditor toUpdateRepositoryNodeEditor( UpdateRepositoryEntryParams params )
{
return toBeEdited -> toBeEdited.data.setSet( DATA_KEY, params.getRepositoryData().getRoot().copy( toBeEdited.data ) );
return toBeEdited -> {
if ( !params.getRepositoryId().toString().startsWith( "system." ) )
{
toBeEdited.data.setBoolean( TRANSIENT_KEY, params.getTransientFlag() );
}
toBeEdited.data.setSet( DATA_KEY, params.getRepositoryData().getRoot().copy( toBeEdited.data ) );
};
}

public static NodeEditor toDeleteBranchNodeEditor( final Branch branch )
Expand Down Expand Up @@ -139,6 +152,7 @@ public static Repository toRepository( final Node node )
settings( repositorySettings ).
data( repositoryData ).
attachments( node.getAttachedBinaries() ).
transientFlag( nodeData.getBoolean( TRANSIENT_KEY ) ).
build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ private Repository doUpdateRepository( final UpdateRepositoryParams updateReposi
repositoryId( repositoryId ).
repositoryData( editableRepository.data ).
attachments( editableRepository.binaryAttachments ).
transientFlag( editableRepository.transientFlag ).
build();

return repositoryEntryService.updateRepositoryEntry( params );
Expand Down Expand Up @@ -349,6 +350,7 @@ private Repository createRepositoryObject( final CreateRepositoryParams params )
branches( Branches.from( RepositoryConstants.MASTER_BRANCH ) ).
settings( params.getRepositorySettings() ).
data( params.getData() ).
transientFlag( params.getTransientFlag() ).
build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ public final class UpdateRepositoryEntryParams

private final BinaryAttachments attachments;

private final Boolean transientFlag;

private UpdateRepositoryEntryParams( Builder builder )
{
this.repositoryId = builder.repositoryId;
this.repositoryData = builder.repositoryData;
this.attachments = builder.attachments.build();
this.transientFlag = builder.transientFlag;
}

public RepositoryId getRepositoryId()
Expand All @@ -35,6 +38,11 @@ public BinaryAttachments getAttachments()
return attachments;
}

public Boolean getTransientFlag()
{
return transientFlag;
}

public static Builder create()
{
return new Builder();
Expand All @@ -48,6 +56,8 @@ public static class Builder

private final BinaryAttachments.Builder attachments = BinaryAttachments.create();

private Boolean transientFlag;

public Builder repositoryId( RepositoryId repositoryId )
{
this.repositoryId = repositoryId;
Expand All @@ -69,6 +79,12 @@ public Builder attachments( Iterable<BinaryAttachment> attachments )
return this;
}

public Builder transientFlag( final Boolean value )
{
this.transientFlag = value;
return this;
}

public UpdateRepositoryEntryParams build()
{
return new UpdateRepositoryEntryParams( this );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class CreateRepositoryHandler

private ChildOrder rootChildOrder;

private Boolean transientFlag;

private Supplier<RepositoryService> repositoryServiceSupplier;

public void setRepositoryId( final String repositoryId )
Expand Down Expand Up @@ -99,6 +101,11 @@ public void setIndexDefinitions( final ScriptValue data )
}
}

public void setTransient( final Boolean value )
{
this.transientFlag = value;
}

public RepositoryMapper execute()
{
final RepositorySettings repositorySettings = RepositorySettings.create().
Expand All @@ -110,6 +117,7 @@ public RepositoryMapper execute()
repositorySettings( repositorySettings ).
rootPermissions( rootPermissions ).
rootChildOrder( rootChildOrder ).
transientFlag( transientFlag ).
build();

final Repository repository = repositoryServiceSupplier.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ private void updateRepositoryData( final EditableRepository target, final Script

final PropertyTree propertyTree = scriptValueTranslatorResult.getPropertyTree();
target.data = propertyTree.getRoot().getSet( "data" ).toTree();

final Boolean transientFlag = propertyTree.getBoolean( "transient" );
if ( transientFlag != null )
{
target.transientFlag = transientFlag;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public RepositoryMapper( final Repository repository )
public void serialize( final MapGenerator gen )
{
gen.value( "id", repository.getId() );
gen.value( "transient", repository.isTransient() );
serialize( gen, repository.getBranches() );
serialize( gen, repository.getSettings() );
serialize( gen, repository.getData() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ var result2 = repoLib.create({
'deny': []
}
],
rootChildOrder: '_ts DESC'
rootChildOrder: '_ts DESC',
transient: true,
});

log.info('Repository created with id ' + result2.id);
Expand All @@ -39,6 +40,7 @@ log.info('Repository created with id ' + result2.id);
// First repository created.
var expected1 = {
'id': 'test-repo',
'transient': false,
'branches': [
'master'
],
Expand All @@ -51,6 +53,7 @@ assert.assertJsonEquals(expected1, result1);
var expected2 =
{
'id': 'test-repo2',
'transient': true,
'branches': [
'master'
],
Expand Down
Loading

0 comments on commit ad3ba7e

Please sign in to comment.