Skip to content

Commit

Permalink
Much work towards a sensible API for updatable Binding etc. Broken atm..
Browse files Browse the repository at this point in the history
  • Loading branch information
Sophos-Elias-Vasylenko committed Nov 29, 2015
1 parent 7819319 commit 8d0dd53
Show file tree
Hide file tree
Showing 15 changed files with 281 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.osgi.service.component.annotations.ReferenceCardinality;

import uk.co.strangeskies.modabi.SchemaManager;
import uk.co.strangeskies.modabi.io.structured.NavigableStructuredDataSource;
import uk.co.strangeskies.modabi.io.structured.StructuredDataBuffer;
import uk.co.strangeskies.utilities.ContextClassLoaderRunner;
import uk.co.strangeskies.utilities.Log;
Expand Down Expand Up @@ -71,20 +70,19 @@ public void profile(BundleContext context) throws BundleException {
}

private long bindingProfile(int profileRounds) {
NavigableStructuredDataSource buffer = manager
.unbind(manager.getMetaSchema().getSchemaModel(),
manager.getMetaSchema())
.to(StructuredDataBuffer.singleBuffer()).getBuffer();
StructuredDataBuffer.Navigable buffer = StructuredDataBuffer.singleBuffer();
manager.unbind(manager.getMetaSchema().getSchemaModel(),
manager.getMetaSchema()).to(buffer);

log(Level.INFO, "Binding Profiling");
long startTime = System.currentTimeMillis();
for (int i = 0; i < profileRounds; i++) {
if (i % 10 == 0)
log(Level.INFO, " working... (" + i + " / " + profileRounds + ")");

buffer.reset();
manager.bind(manager.getMetaSchema().getSchemaModel()).from(buffer)
.resolve();
buffer.getBuffer().reset();
manager.bind(manager.getMetaSchema().getSchemaModel())
.from(buffer.getBuffer()).resolve();
}
return System.currentTimeMillis() - startTime;
}
Expand All @@ -108,14 +106,14 @@ private void warmUpProfiling(int warmupRounds) {
if (i % 10 == 0)
log(Level.INFO, " working... (" + i + " / " + warmupRounds + ")");

NavigableStructuredDataSource buffer = manager
.unbind(manager.getMetaSchema().getSchemaModel(),
manager.getMetaSchema())
.to(StructuredDataBuffer.singleBuffer()).getBuffer();
StructuredDataBuffer.Navigable buffer = StructuredDataBuffer
.singleBuffer();
manager.unbind(manager.getMetaSchema().getSchemaModel(),
manager.getMetaSchema()).to(buffer);

buffer.reset();
manager.bind(manager.getMetaSchema().getSchemaModel()).from(buffer)
.resolve();
buffer.getBuffer().reset();
manager.bind(manager.getMetaSchema().getSchemaModel())
.from(buffer.getBuffer()).resolve();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
import uk.co.strangeskies.modabi.Schema;
import uk.co.strangeskies.modabi.SchemaException;
import uk.co.strangeskies.modabi.SchemaManager;
import uk.co.strangeskies.modabi.io.structured.DataInterface;
import uk.co.strangeskies.modabi.io.structured.StructuredDataFormat;
import uk.co.strangeskies.utilities.ContextClassLoaderRunner;

public abstract class ModabiRegistration implements AnalyzerPlugin {
private final DataInterface handler;
private final StructuredDataFormat handler;
private final SchemaManager manager;

public ModabiRegistration(SchemaManager manager, DataInterface handler) {
public ModabiRegistration(SchemaManager manager, StructuredDataFormat handler) {
this.handler = handler;
this.manager = manager;
manager.registerDataInterface(handler);
Expand Down Expand Up @@ -87,7 +87,7 @@ private void registerSchemata(Jar jar, Analyzer analyzer) {
if (newCapabilities != null) {
appendProperties(analyzer, Constants.REQUIRE_CAPABILITY,
"osgi.service;" + "filter:=\"(&(objectClass="
+ DataInterface.class.getTypeName() + ")(formatId="
+ StructuredDataFormat.class.getTypeName() + ")(formatId="
+ handler.getFormatId()
+ "))\";resolution:=mandatory;effective:=active");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,17 @@
*/
package uk.co.strangeskies.modabi;

import uk.co.strangeskies.modabi.io.structured.NavigableStructuredDataSource;
import uk.co.strangeskies.modabi.schema.Model;

public class Binding<T> {
private final Model<T> model;
private final T data;
public interface Binding<T> {
public Model<T> getModel();

public Binding(Model<T> model, T data) {
this.model = model;
this.data = data;
}
public T getData();

public Model<T> getModel() {
return model;
}
public void updateBinding();

public T getData() {
return data;
}
public NavigableStructuredDataSource getSource();

public void updateSource();
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,21 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

import uk.co.strangeskies.modabi.io.DataTarget;
import uk.co.strangeskies.modabi.io.structured.DataInterface;
import uk.co.strangeskies.modabi.io.structured.DiscardingStructuredDataTarget;
import uk.co.strangeskies.modabi.io.structured.StructuredDataFormat;
import uk.co.strangeskies.modabi.io.structured.StructuredDataSource;
import uk.co.strangeskies.modabi.io.structured.StructuredDataTarget;
import uk.co.strangeskies.modabi.io.structured.StructuredDataTargetImpl;
import uk.co.strangeskies.modabi.processing.BindingFuture;
import uk.co.strangeskies.modabi.processing.BindingState;
import uk.co.strangeskies.modabi.processing.UnbindingState;
import uk.co.strangeskies.modabi.schema.ComplexNode;
import uk.co.strangeskies.modabi.schema.DataNode;
import uk.co.strangeskies.modabi.schema.BindingNode;
import uk.co.strangeskies.modabi.schema.Model;
import uk.co.strangeskies.modabi.schema.SchemaNode;
import uk.co.strangeskies.reflection.Reified;
import uk.co.strangeskies.reflection.TypeToken;
import uk.co.strangeskies.reflection.TypedObject;
Expand Down Expand Up @@ -99,11 +95,21 @@ default BindingFuture<T> from(URL input) {

BindingFuture<T> from(String extension, InputStream input);

<U> Binder<T> supply(TypeToken<U> type,
Supplier<TypedObject<? extends U>> action);
default <U> Binder<T> supply(TypeToken<U> type,
Supplier<TypedObject<? extends U>> action) {
return supply(type, s -> action.get());
}

default <U> Binder<T> supply(TypeToken<U> type,
Function<BindingState, TypedObject<? extends U>> action) {
return supply(s -> {
return (s instanceof BindingNode<?, ?, ?> && type.isAssignableFrom(
((BindingNode.Effective<?, ?, ?>) s.bindingNode()).getDataType()))
? action.apply(s) : null;
});
}

<U> Binder<T> supply(TypeToken<U> type,
Function<BindingState, TypedObject<? extends U>> action);
Binder<T> supply(Function<BindingState, TypedObject<?>> action);

/*
* Errors which are rethrown will be passed to the next error handler if
Expand All @@ -113,22 +119,22 @@ <U> Binder<T> supply(TypeToken<U> type,
Binder<T> with(Consumer<Exception> errorHandler);
}

interface Unbinder {
<U extends StructuredDataTarget> U to(U output);
interface Unbinder<T> {
BindingFuture<T> to(StructuredDataTarget output);

default void to(File output) {
to(output.toURI());
default BindingFuture<T> to(File output) {
return to(output.toURI());
}

default void to(URI output) {
default BindingFuture<T> to(URI output) {
try {
to(output.toURL());
return to(output.toURL());
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
}
}

default void to(URL output) {
default BindingFuture<T> to(URL output) {
String extension = output.getQuery();
int lastDot = extension.lastIndexOf('.');
if (lastDot > 0) {
Expand All @@ -140,35 +146,43 @@ default void to(URL output) {

try (
OutputStream fileStream = output.openConnection().getOutputStream()) {
to(extension, fileStream).flush();
BindingFuture<T> binding = to(extension, fileStream);
fileStream.flush();
return binding;
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}

<U extends OutputStream> U to(String extension, U output);
BindingFuture<T> to(String extension, OutputStream output);

Unbinder consume(Predicate<UnbindingState> filter);
Unbinder<T> consume(Predicate<UnbindingState> filter);

default <U> Unbinder<T> consume(TypeToken<U> type,
Predicate<TypedObject<? extends U>> filter) {
return consume(type, (s, o) -> filter.test(o));
}

Unbinder consume(BiPredicate<UnbindingState, TypedObject<?>> filter);
<U> Unbinder<T> consume(TypeToken<U> type,
BiPredicate<UnbindingState, TypedObject<? extends U>> filter);

/*
* Errors which are rethrown will be passed to the next error handler if
* present, or dealt with as normal. Otherwise, a best effort is made at
* unbinding.
* unbinding, and the exception information will be serialised as a comment.
*/
Unbinder with(Consumer<Exception> errorHandler);
Unbinder<T> with(Consumer<Exception> errorHandler);
}

void registerDataInterface(DataInterface handler);
void registerDataInterface(StructuredDataFormat handler);

void unregisterDataInterface(DataInterface handler);
void unregisterDataInterface(StructuredDataFormat handler);

Set<DataInterface> getRegisteredDataInterfaces();
Set<StructuredDataFormat> getRegisteredDataInterfaces();

DataInterface getDataInterface(String id);
StructuredDataFormat getDataInterface(String id);

Set<DataInterface> getDataInterfaces(String extension);
Set<StructuredDataFormat> getDataInterfaces(String extension);

default GeneratedSchema generateSchema(QualifiedName name) {
return generateSchema(name, Collections.emptySet());
Expand All @@ -191,13 +205,10 @@ default <T> void registerProvider(Class<T> providedClass,
registerProvider(TypeToken.over(providedClass), provider);
}

void registerSchema(Schema schema);
boolean registerSchema(Schema schema);

// So we can import from manually added data.
void registerBinding(Binding<?> binding);

default <T> void registerBinding(Model<T> model, T data) {
registerBinding(new Binding<T>(model, data));
default <T> BindingFuture<T> registerBinding(Model<T> model, T data) {
return unbind(model, data).to(new DiscardingStructuredDataTarget());
}

// Blocks until all possible processing is done other than waiting imports:
Expand All @@ -214,53 +225,21 @@ default <T> Binder<T> bind(Class<T> dataClass) {

<T> Set<BindingFuture<T>> bindingFutures(Model<T> model);

default Binder<Schema> bindSchema() {
Binder<Schema> binder = bind(getMetaSchema().getSchemaModel());

return new Binder<Schema>() {
@Override
public BindingFuture<Schema> from(StructuredDataSource input) {
return registerFuture(binder.from(input));
}

@Override
public BindingFuture<Schema> from(URL input) {
return registerFuture(binder.from(input));
}

@Override
public BindingFuture<Schema> from(InputStream input) {
return registerFuture(binder.from(input));
}

@Override
public BindingFuture<Schema> from(String extension, InputStream input) {
return registerFuture(binder.from(extension, input));
}

private BindingFuture<Schema> registerFuture(BindingFuture<Schema> from) {
new Thread(() -> {
registerSchema(from.resolve());
}).start();

return from;
}
};
}
Binder<Schema> bindSchema();

<T> Unbinder unbind(Model<T> model, T data);
<T> Unbinder<T> unbind(Model<T> model, T data);

<T> Unbinder unbind(TypeToken<T> dataClass, T data);
<T> Unbinder<T> unbind(TypeToken<T> dataClass, T data);

default <T> Unbinder unbind(Class<T> dataClass, T data) {
default <T> Unbinder<T> unbind(Class<T> dataClass, T data) {
return unbind(TypeToken.over(dataClass), data);
}

default <T extends Reified<T>> Unbinder unbind(T data) {
default <T extends Reified<T>> Unbinder<T> unbind(T data) {
return unbind(data.getThisType(), data);
}

Unbinder unbind(Object data);
<T> Unbinder<T> unbind(T data);

MetaSchema getMetaSchema();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.io.OutputStream;
import java.util.Set;

public interface DataInterface {
public interface StructuredDataFormat {
String getFormatId();

Set<String> getFileExtensions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ default void accept(ChoiceNode.Effective node) {
}

default <U> void accept(BindingChildNode.Effective<U, ?, ?> node) {
accept(node);
accept((InputNode.Effective<?, ?>) node);
}

default void accept(InputNode.Effective<?, ?> node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ public <U> List<Model.Effective<U>> getMatchingModels(
.map(n -> n.effective()).collect(Collectors.toList());
}

public SchemaManager getSchemaManager() {
return manager;
}

@SuppressWarnings("unchecked")
public <T> ComputingMap<DataType<? extends T>, DataNode.Effective<? extends T>> getDataNodeOverrides(
DataNode<T> node) {
Expand Down
Loading

0 comments on commit 8d0dd53

Please sign in to comment.