com.yahoo.vespa.configmodel.producers.DocumentManager Maven / Gradle / Ivy
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.configmodel.producers;
import com.yahoo.document.ArrayDataType;
import com.yahoo.document.DataType;
import com.yahoo.document.DocumentType;
import com.yahoo.document.MapDataType;
import com.yahoo.document.PrimitiveDataType;
import com.yahoo.document.StructDataType;
import com.yahoo.document.TensorDataType;
import com.yahoo.document.WeightedSetDataType;
import com.yahoo.document.config.DocumentmanagerConfig;
import com.yahoo.document.annotation.AnnotationReferenceDataType;
import com.yahoo.document.annotation.AnnotationType;
import com.yahoo.documentmodel.NewDocumentReferenceDataType;
import com.yahoo.documentmodel.NewDocumentType;
import com.yahoo.documentmodel.OwnedTemporaryType;
import com.yahoo.documentmodel.TemporaryUnknownType;
import com.yahoo.documentmodel.VespaDocumentType;
import com.yahoo.schema.document.FieldSet;
import com.yahoo.vespa.documentmodel.DocumentModel;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author baldersheim
* @author arnej
*/
public class DocumentManager {
private boolean useV8GeoPositions = false;
public DocumentManager useV8GeoPositions(boolean useV8GeoPositions) {
this.useV8GeoPositions = useV8GeoPositions;
return this;
}
public DocumentmanagerConfig.Builder produce(DocumentModel model,
DocumentmanagerConfig.Builder documentConfigBuilder) {
return produceDocTypes(model, documentConfigBuilder);
}
// Alternate (new) way to build config:
public DocumentmanagerConfig.Builder produceDocTypes(DocumentModel model, DocumentmanagerConfig.Builder builder) {
builder.usev8geopositions(this.useV8GeoPositions);
Map produced = new HashMap<>();
var indexMap = new IdxMap();
for (NewDocumentType documentType : model.getDocumentManager().getTypes()) {
docTypeInheritOrder(documentType, builder, produced, indexMap);
}
indexMap.verifyAllDone();
return builder;
}
private void docTypeInheritOrder(NewDocumentType documentType,
DocumentmanagerConfig.Builder builder,
Map produced,
IdxMap indexMap)
{
if (! produced.containsKey(documentType.getFullName())) {
for (NewDocumentType inherited : documentType.getInherited()) {
docTypeInheritOrder(inherited, builder, produced, indexMap);
}
docTypeBuild(documentType, builder, indexMap);
produced.put(documentType.getFullName(), documentType);
}
}
static private class IdxMap {
private final Map doneMap = new HashMap<>();
private final Map map = new HashMap<>();
private final DataTypeRecognizer recognizer = new DataTypeRecognizer();
private void add(String name) {
// the adding of "10000" here is mostly to make it more
// unique to grep for when debugging
int nextIdx = 10000 + map.size();
map.computeIfAbsent(name, k -> nextIdx);
}
int idxOf(Object someType) {
if (someType instanceof DocumentType) {
var dt = (DocumentType) someType;
if (dt.getId() == 8) {
return idxOf(VespaDocumentType.INSTANCE);
}
}
String name = recognizer.nameOf(someType);
add(name);
return map.get(name);
}
private boolean isDoneIdx(int idx) {
return doneMap.computeIfAbsent(idx, k -> false);
}
boolean isDone(Object someType) {
return isDoneIdx(idxOf(someType));
}
void setDone(Object someType) {
assert(! isDone(someType));
doneMap.put(idxOf(someType), true);
}
void verifyAllDone() {
for (var entry : map.entrySet()) {
String needed = entry.getKey();
int idxOfNeed = entry.getValue();
if (! isDoneIdx(idxOfNeed)) {
throw new IllegalArgumentException("Could not generate config for all needed types, missing: " + needed);
}
}
}
}
static private List sortedList(Collection unsorted, Comparator cmp) {
var list = new ArrayList<>(unsorted);
list.sort(cmp);
return list;
}
private void docTypeBuild(NewDocumentType documentType, DocumentmanagerConfig.Builder builder, IdxMap indexMap) {
DocumentmanagerConfig.Doctype.Builder db = new DocumentmanagerConfig.Doctype.Builder();
db.
idx(indexMap.idxOf(documentType)).
name(documentType.getName()).
contentstruct(indexMap.idxOf(documentType.getContentStruct()));
docTypeBuildFieldSets(documentType.getFieldSets(), db);
docTypeBuildImportedFields(documentType.getImportedFieldNames(), db);
for (NewDocumentType inherited : documentType.getInherited()) {
db.inherits(b -> b.idx(indexMap.idxOf(inherited)));
}
docTypeBuildAnyType(documentType.getContentStruct(), db, indexMap);
for (DataType dt : sortedList(documentType.getAllTypes().getTypes(),
(a,b) -> a.getName().compareTo(b.getName()))) {
docTypeBuildAnyType(dt, db, indexMap);
}
for (AnnotationType ann : sortedList(documentType.getAnnotations(),
(a,b) -> a.getName().compareTo(b.getName()))) {
docTypeBuildAnnotationType(ann, db, indexMap);
}
builder.doctype(db);
indexMap.setDone(documentType);
}
private void docTypeBuildFieldSets(Set
© 2015 - 2024 Weber Informatics LLC | Privacy Policy