use of nl.knaw.huygens.timbuctoo.v5.datastores.prefixstore.TypeNameStore in project timbuctoo by HuygensING.
the class RootQuery method rebuildSchema.
public synchronized GraphQLSchema rebuildSchema() {
final TypeDefinitionRegistry staticQuery = schemaParser.parse(this.staticQuery);
if (archetypes != null && !archetypes.isEmpty()) {
staticQuery.merge(schemaParser.parse(archetypes + "extend type DataSetMetadata {\n" + " archetypes: Archetypes! @passThrough\n" + "}\n" + "\n"));
}
TypeDefinitionRegistry registry = new TypeDefinitionRegistry();
registry.merge(staticQuery);
final RuntimeWiring.Builder wiring = RuntimeWiring.newRuntimeWiring();
wiring.type("Query", builder -> builder.dataFetcher("promotedDataSets", env -> dataSetRepository.getPromotedDataSets().stream().map(DataSetWithDatabase::new).collect(Collectors.toList())).dataFetcher("allDataSets", env -> dataSetRepository.getDataSets().stream().map(DataSetWithDatabase::new).filter(x -> {
if (x.isPublished()) {
return true;
} else {
ContextData contextData = env.getContext();
UserPermissionCheck userPermissionCheck = contextData.getUserPermissionCheck();
return userPermissionCheck.getPermissions(x.getDataSet().getMetadata()).contains(Permission.READ);
}
}).collect(Collectors.toList())).dataFetcher("dataSetMetadata", env -> {
final String dataSetId = env.getArgument("dataSetId");
ContextData context = env.getContext();
final User user = context.getUser().orElse(null);
Tuple<String, String> splitCombinedId = DataSetMetaData.splitCombinedId(dataSetId);
return dataSetRepository.getDataSet(user, splitCombinedId.getLeft(), splitCombinedId.getRight()).map(DataSetWithDatabase::new);
}).dataFetcher("dataSetMetadataList", env -> {
Stream<DataSetWithDatabase> dataSets = dataSetRepository.getDataSets().stream().map(DataSetWithDatabase::new);
if (env.getArgument("promotedOnly")) {
dataSets = dataSets.filter(DataSetWithDatabase::isPromoted);
}
if (env.getArgument("publishedOnly")) {
dataSets = dataSets.filter(DataSetWithDatabase::isPublished);
}
return dataSets.filter(x -> {
ContextData contextData = env.getContext();
UserPermissionCheck userPermissionCheck = contextData.getUserPermissionCheck();
return userPermissionCheck.getPermissions(x.getDataSet().getMetadata()).contains(Permission.READ);
}).collect(Collectors.toList());
}).dataFetcher("aboutMe", env -> ((RootData) env.getRoot()).getCurrentUser().orElse(null)).dataFetcher("availableExportMimetypes", env -> supportedFormats.getSupportedMimeTypes().stream().map(MimeTypeDescription::create).collect(Collectors.toList())));
wiring.type("DataSetMetadata", builder -> builder.dataFetcher("currentImportStatus", env -> {
DataSetMetaData input = env.getSource();
Optional<User> currentUser = ((RootData) env.getRoot()).getCurrentUser();
if (!currentUser.isPresent()) {
throw new RuntimeException("User is not provided");
}
return dataSetRepository.getDataSet(currentUser.get(), input.getOwnerId(), input.getDataSetId()).map(dataSet -> dataSet.getImportManager().getImportStatus());
}).dataFetcher("dataSetImportStatus", env -> {
Optional<User> currentUser = ((RootData) env.getRoot()).getCurrentUser();
if (!currentUser.isPresent()) {
throw new RuntimeException("User is not provided");
}
DataSetMetaData input = env.getSource();
return dataSetRepository.getDataSet(currentUser.get(), input.getOwnerId(), input.getDataSetId()).map(dataSet -> dataSet.getImportManager().getDataSetImportStatus());
}).dataFetcher("collectionList", env -> getCollections(env.getSource(), ((ContextData) env.getContext()).getUser())).dataFetcher("collection", env -> {
String collectionId = (String) env.getArguments().get("collectionId");
if (collectionId != null && collectionId.endsWith("List")) {
collectionId = collectionId.substring(0, collectionId.length() - "List".length());
}
DataSetMetaData input = env.getSource();
ContextData context = env.getContext();
final User user = context.getUser().orElse(null);
final DataSet dataSet = dataSetRepository.getDataSet(user, input.getOwnerId(), input.getDataSetId()).get();
final TypeNameStore typeNameStore = dataSet.getTypeNameStore();
String collectionUri = typeNameStore.makeUri(collectionId);
if (dataSet.getSchemaStore().getStableTypes() == null || dataSet.getSchemaStore().getStableTypes().get(collectionUri) == null) {
return null;
} else {
return getCollection(dataSet, typeNameStore, dataSet.getSchemaStore().getStableTypes().get(collectionUri));
}
}).dataFetcher("dataSetId", env -> ((DataSetMetaData) env.getSource()).getCombinedId()).dataFetcher("dataSetName", env -> ((DataSetMetaData) env.getSource()).getDataSetId()).dataFetcher("ownerId", env -> ((DataSetMetaData) env.getSource()).getOwnerId()));
wiring.type("CurrentImportStatus", builder -> builder.dataFetcher("elapsedTime", env -> {
final String timeUnit = env.getArgument("unit");
return ((ImportStatus) env.getSource()).getElapsedTime(timeUnit);
}));
wiring.type("DataSetImportStatus", builder -> builder.dataFetcher("lastImportDuration", env -> {
final String timeUnit = env.getArgument("unit");
return ((DataSetImportStatus) env.getSource()).getLastImportDuration(timeUnit);
}));
wiring.type("EntryImportStatus", builder -> builder.dataFetcher("elapsedTime", env -> {
final String timeUnit = env.getArgument("unit");
return ((EntryImportStatus) env.getSource()).getElapsedTime(timeUnit);
}));
wiring.type("CollectionMetadata", builder -> builder.dataFetcher("indexConfig", env -> {
SubjectReference source = env.getSource();
final QuadStore qs = source.getDataSet().getQuadStore();
try (Stream<CursorQuad> quads = qs.getQuads(source.getSubjectUri(), TIM_HASINDEXERCONFIG, Direction.OUT, "")) {
final Map result = quads.findFirst().map(q -> {
try {
return objectMapper.readValue(q.getObject(), Map.class);
} catch (IOException e) {
LOG.error("Value not a Map", e);
return new HashMap<>();
}
}).orElse(new HashMap());
if (!result.containsKey("facet") || !(result.get("facet") instanceof List)) {
result.put("facet", new ArrayList<>());
}
if (!result.containsKey("fullText") || !(result.get("fullText") instanceof List)) {
result.put("fullText", new ArrayList<>());
}
return result;
}
}).dataFetcher("viewConfig", new ViewConfigFetcher(objectMapper)));
wiring.type("AboutMe", builder -> builder.dataFetcher("dataSets", env -> (Iterable) () -> dataSetRepository.getDataSetsWithWriteAccess(env.getSource()).stream().map(DataSetWithDatabase::new).iterator()).dataFetcher("dataSetMetadataList", env -> (Iterable) () -> {
Stream<DataSetWithDatabase> dataSets = dataSetRepository.getDataSets().stream().map(DataSetWithDatabase::new);
if (env.getArgument("ownOnly")) {
String userId = ((ContextData) env.getContext()).getUser().map(u -> "u" + u.getPersistentId()).orElse(null);
dataSets = dataSets.filter(d -> d.getOwnerId().equals(userId));
}
Permission permission = Permission.valueOf(env.getArgument("permission"));
if (permission != Permission.READ) {
// Read is implied
UserPermissionCheck check = ((ContextData) env.getContext()).getUserPermissionCheck();
dataSets = dataSets.filter(d -> check.getPermissions(d).contains(permission));
}
return dataSets.iterator();
}).dataFetcher("id", env -> ((User) env.getSource()).getPersistentId()).dataFetcher("name", env -> ((User) env.getSource()).getDisplayName()).dataFetcher("personalInfo", env -> "http://example.com").dataFetcher("canCreateDataSet", env -> true));
wiring.type("Mutation", builder -> builder.dataFetcher("setViewConfig", new ViewConfigMutation(dataSetRepository)).dataFetcher("setSummaryProperties", new SummaryPropsMutation(dataSetRepository)).dataFetcher("setIndexConfig", new IndexConfigMutation(dataSetRepository)).dataFetcher("createDataSet", new CreateDataSetMutation(dataSetRepository)).dataFetcher("deleteDataSet", new DeleteDataSetMutation(dataSetRepository)).dataFetcher("publish", new MakePublicMutation(dataSetRepository)).dataFetcher("extendSchema", new ExtendSchemaMutation(dataSetRepository)).dataFetcher("setDataSetMetadata", new DataSetMetadataMutation(dataSetRepository)).dataFetcher("setCollectionMetadata", new CollectionMetadataMutation(dataSetRepository)));
wiring.wiringFactory(wiringFactory);
StringBuilder root = new StringBuilder("type DataSets {\n sillyWorkaroundWhenNoDataSetsAreVisible: Boolean\n");
boolean[] dataSetAvailable = new boolean[] { false };
dataSetRepository.getDataSets().forEach(dataSet -> {
final DataSetMetaData dataSetMetaData = dataSet.getMetadata();
final String name = dataSetMetaData.getCombinedId();
Map<String, Type> types = dataSet.getSchemaStore().getStableTypes();
Map<String, List<ExplicitField>> customSchema = dataSet.getCustomSchema();
final Map<String, Type> customTypes = new HashMap<>();
for (Map.Entry<String, List<ExplicitField>> entry : customSchema.entrySet()) {
ExplicitType explicitType = new ExplicitType(entry.getKey(), entry.getValue());
customTypes.put(entry.getKey(), explicitType.convertToType());
}
Map<String, Type> mergedTypes;
MergeSchemas mergeSchemas = new MergeSchemas();
mergedTypes = mergeSchemas.mergeSchema(types, customTypes);
types = mergedTypes;
if (types != null) {
dataSetAvailable[0] = true;
root.append(" ").append(name).append(":").append(name).append(" @dataSet(userId:\"").append(dataSetMetaData.getOwnerId()).append("\", dataSetId:\"").append(dataSetMetaData.getDataSetId()).append("\")\n");
wiring.type(name, c -> c.dataFetcher("metadata", env -> new DataSetWithDatabase(dataSet)));
final String schema = typeGenerator.makeGraphQlTypes(name, types, dataSet.getTypeNameStore());
staticQuery.merge(schemaParser.parse(schema));
}
});
root.append("}\n\nextend type Query {\n #The actual dataSets\n dataSets: DataSets @passThrough\n}\n\n");
if (dataSetAvailable[0]) {
staticQuery.merge(schemaParser.parse(root.toString()));
}
SchemaGenerator schemaGenerator = new SchemaGenerator();
return schemaGenerator.makeExecutableSchema(staticQuery, wiring.build());
}
use of nl.knaw.huygens.timbuctoo.v5.datastores.prefixstore.TypeNameStore in project timbuctoo by HuygensING.
the class ViewConfigFetcher method makeDefaultViewConfig.
private ArrayList<Map> makeDefaultViewConfig(String collectionUri, Map<String, Type> schema, TypeNameStore typeNameStore) {
ArrayList<Map> result = new ArrayList<>();
Type collectionType = schema.get(collectionUri);
if (collectionType == null) {
LOG.error("The collectionUri " + collectionUri + " does not exist in the schema! (it does contain: [ " + schema.keySet().stream().collect(Collectors.joining(", ")) + " ]");
} else {
result.add(title(path(jsnA(jsnA(jsn("Entity"), jsn("title")), jsnA(jsn("Value"), jsn("value"))))));
final String collectionGraphqlTypeWithoutDataSet = typeNameStore.makeGraphQlname(collectionUri);
for (Predicate predicate : collectionType.getPredicates()) {
final String predicateAsGraphqlProp = typeNameStore.makeGraphQlnameForPredicate(predicate.getName(), predicate.getDirection(), predicate.isList());
ArrayNode predicateReference = jsnA(jsnA(jsn(collectionGraphqlTypeWithoutDataSet), jsn(predicateAsGraphqlProp)));
if (predicate.isList()) {
predicateReference.add(jsnA(jsn("items"), jsn("items")));
}
String title = "";
if (predicate.getDirection() == Direction.IN) {
title = "⬅︎ ";
}
title += typeNameStore.shorten(predicate.getName());
if (predicate.getReferenceTypes().values().stream().anyMatch(x -> x > 0)) {
// it's at least sometimes a link
result.add(keyValue(title, internalLink(path(pushImm(predicateReference, jsnA(jsn("Entity"), jsn("uri")))), path(pushImm(predicateReference, jsnA(jsn("Entity"), jsn("title")), jsnA(jsn("Value"), jsn("value")))))));
}
if (predicate.getValueTypes().values().stream().anyMatch(x -> x > 0)) {
// it's at least sometimes a normal value
result.add(keyValue(title, path(pushImm(predicateReference, jsnA(jsn("Value"), jsn("value"))))));
}
}
}
return result;
}
use of nl.knaw.huygens.timbuctoo.v5.datastores.prefixstore.TypeNameStore in project timbuctoo by HuygensING.
the class ViewConfigFetcher method get.
@Override
public Object get(DataFetchingEnvironment env) {
SubjectReference source = env.getSource();
final DataSet dataSet = source.getDataSet();
final QuadStore qs = dataSet.getQuadStore();
final Map<String, Type> schema = dataSet.getSchemaStore().getStableTypes();
final TypeNameStore typeNameStore = dataSet.getTypeNameStore();
try (Stream<CursorQuad> quads = qs.getQuads(source.getSubjectUri(), HAS_VIEW_CONFIG, Direction.OUT, "")) {
return quads.findFirst().flatMap(q -> {
try {
return Optional.ofNullable(objectMapper.readValue(q.getObject(), List.class));
} catch (IOException e) {
LOG.error("view config is not a valid JSON object", e);
return Optional.empty();
}
}).orElseGet(() -> makeDefaultViewConfig(source.getSubjectUri(), schema, typeNameStore));
}
}
use of nl.knaw.huygens.timbuctoo.v5.datastores.prefixstore.TypeNameStore in project timbuctoo by HuygensING.
the class DerivedSchemaTypeGenerator method makeGraphQlTypes.
public String makeGraphQlTypes(String rootType, Map<String, Type> types, TypeNameStore nameStore) {
DerivedSchemaContainer typesContainer = new DerivedSchemaContainer(rootType, nameStore, this.argumentsHelper);
// FIXME find a better way to register standard types to the schema of a data set
typesContainer.valueType(RdfConstants.MARKDOWN);
typesContainer.valueType(RdfConstants.STRING);
typesContainer.valueType(RdfConstants.URI);
for (Type type : types.values()) {
typesContainer.openObjectType(type.getName());
for (Predicate predicate : type.getPredicates()) {
fieldForDerivedType(predicate, typesContainer);
}
typesContainer.closeObjectType(type.getName());
}
return typesContainer.getSchema();
}
use of nl.knaw.huygens.timbuctoo.v5.datastores.prefixstore.TypeNameStore in project timbuctoo by HuygensING.
the class DataSet method dataSet.
public static DataSet dataSet(DataSetMetaData metadata, ExecutorService executorService, String rdfPrefix, BdbEnvironmentCreator dataStoreFactory, Runnable onUpdated, DataSetStorage dataSetStorage) throws IOException, DataStoreCreationException {
String userId = metadata.getOwnerId();
String dataSetId = metadata.getDataSetId();
File descriptionFile = dataSetStorage.getResourceSyncDescriptionFile();
FileStorage fileStorage = dataSetStorage.getFileStorage();
ImportManager importManager = new ImportManager(dataSetStorage.getLogList(), fileStorage, fileStorage, dataSetStorage.getLogStorage(), executorService, dataSetStorage.getRdfIo(), onUpdated);
try {
importManager.subscribeToRdf(new RdfDescriptionSaver(descriptionFile, metadata.getBaseUri(), importManager.getImportStatus()));
} catch (ParserConfigurationException | SAXException e) {
LOG.error("Could not construct import manager of data set", e);
}
final TupleBinding<String> stringBinding = TupleBinding.getPrimitiveBinding(String.class);
try {
StringStringIsCleanHandler stringStringIsCleanHandler = new StringStringIsCleanHandler();
BdbTripleStore quadStore = new BdbTripleStore(dataStoreFactory.getDatabase(userId, dataSetId, "rdfData", true, stringBinding, stringBinding, stringStringIsCleanHandler));
final BdbTypeNameStore typeNameStore = new BdbTypeNameStore(new BdbBackedData(dataStoreFactory.getDatabase(userId, dataSetId, "typenames", false, stringBinding, stringBinding, stringStringIsCleanHandler)), rdfPrefix);
final BdbSchemaStore schema = new BdbSchemaStore(new BdbBackedData(dataStoreFactory.getDatabase(userId, dataSetId, "schema", false, stringBinding, stringBinding, stringStringIsCleanHandler)), importManager.getImportStatus());
final BdbTruePatchStore truePatchStore = new BdbTruePatchStore(dataStoreFactory.getDatabase(userId, dataSetId, "truePatch", true, stringBinding, stringBinding, stringStringIsCleanHandler));
final TupleBinding<Integer> integerBinding = TupleBinding.getPrimitiveBinding(Integer.class);
final UpdatedPerPatchStore updatedPerPatchStore = new UpdatedPerPatchStore(dataStoreFactory.getDatabase(userId, dataSetId, "updatedPerPatch", true, integerBinding, stringBinding, new IsCleanHandler<Integer, String>() {
@Override
public Integer getKey() {
return Integer.MAX_VALUE;
}
@Override
public String getValue() {
return "isClean";
}
}));
final BdbRmlDataSourceStore rmlDataSourceStore = new BdbRmlDataSourceStore(dataStoreFactory.getDatabase(userId, dataSetId, "rmlSource", true, stringBinding, stringBinding, stringStringIsCleanHandler), importManager.getImportStatus());
VersionStore versionStore = new VersionStore(dataStoreFactory.getDatabase(userId, dataSetId, "versions", false, stringBinding, integerBinding, new IsCleanHandler<String, Integer>() {
@Override
public String getKey() {
return "isClean";
}
@Override
public Integer getValue() {
return Integer.MAX_VALUE;
}
}));
final StoreUpdater storeUpdater = new StoreUpdater(dataStoreFactory, quadStore, typeNameStore, truePatchStore, updatedPerPatchStore, Lists.newArrayList(schema, rmlDataSourceStore), versionStore, importManager.getImportStatus());
importManager.subscribeToRdf(storeUpdater);
ImmutableDataSet dataSet = ImmutableDataSet.builder().ownerId(userId).dataSetName(dataSetId).bdbEnvironmentCreator(dataStoreFactory).metadata(metadata).quadStore(quadStore).typeNameStore(typeNameStore).schemaStore(schema).dataSource(new RdfDataSourceFactory(rmlDataSourceStore)).schemaStore(schema).importManager(importManager).dataSetStorage(dataSetStorage).build();
importManager.init(dataSet);
if (!quadStore.isClean() || !typeNameStore.isClean() || !schema.isClean() || !truePatchStore.isClean() || !updatedPerPatchStore.isClean() || !rmlDataSourceStore.isClean() || !versionStore.isClean()) {
LOG.error("Data set '{}__{}' data is corrupted, starting to reimport.", userId, dataSetId);
quadStore.empty();
typeNameStore.empty();
schema.empty();
truePatchStore.empty();
updatedPerPatchStore.empty();
rmlDataSourceStore.empty();
versionStore.empty();
importManager.reprocessLogs();
} else {
// process unprocessed logs
importManager.processLogs();
}
return dataSet;
} catch (BdbDbCreationException e) {
throw new DataStoreCreationException(e.getCause());
}
}
Aggregations