
com.eventsourcing.repository.commands.IntroduceEntityLayouts Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of eventsourcing-repository Show documentation
Show all versions of eventsourcing-repository Show documentation
Event capture and querying framework for Java
/**
* Copyright (c) 2016, All Contributors (see CONTRIBUTORS file)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.eventsourcing.repository.commands;
import com.eventsourcing.*;
import com.eventsourcing.layout.Layout;
import com.eventsourcing.layout.LayoutConstructor;
import com.eventsourcing.migrations.events.EntityLayoutIntroduced;
import com.googlecode.cqengine.query.Query;
import com.googlecode.cqengine.resultset.ResultSet;
import lombok.SneakyThrows;
import java.util.Optional;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static com.eventsourcing.index.EntityQueryFactory.equal;
public class IntroduceEntityLayouts extends StandardCommand {
private Iterable> entities;
@LayoutConstructor
public IntroduceEntityLayouts() {}
public IntroduceEntityLayouts(Iterable> entities) {this.entities = entities;}
@Override public EventStream events(Repository repository, LockProvider lockProvider) throws Exception {
Lock lock = lockProvider.lock(getClass().getName());
Stream> stream = StreamSupport
.stream(Spliterators.spliteratorUnknownSize(entities.iterator(), Spliterator.IMMUTABLE), false);
return EventStream.ofWithState(lock, stream.flatMap(new EntityStreamFunction(repository)));
}
@Override public Void result(Lock lock) {
lock.unlock();
return null;
}
private static class EntityStreamFunction implements Function, Stream> {
private final Repository repository;
public EntityStreamFunction(Repository repository) {this.repository = repository;}
@SneakyThrows
@Override public Stream apply(Class extends Entity> entity) {
Layout extends Entity> layout = Layout.forClass(entity);
byte[] fingerprint = layout.getHash();
Query> query = equal(EntityLayoutIntroduced.FINGERPRINT, fingerprint);
try (ResultSet> resultSet = repository
.query(EntityLayoutIntroduced.class,
query)) {
if (resultSet.isEmpty()) {
return Stream.of(new EntityLayoutIntroduced(fingerprint, Optional.of(layout)));
} else {
return Stream.empty();
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy