aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/schema/DocumentGraphValidator.java
blob: 648cdf18c5b2bdff67d11eeb4eb36db9da9f0e7d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema;

import com.yahoo.schema.document.SDDocumentType;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.List;

import static java.util.stream.Collectors.joining;

/**
 * Validates that there are no cycles between document types (exception: self-reference is allowed).
 * Example: if document B inherits A, then A cannot have a document reference to B.
 *
 * @author bjorncs
 */
public class DocumentGraphValidator {

    public void validateDocumentGraph(List<SDDocumentType> documents) {
        for (SDDocumentType document : documents) {
            validateRoot(document);
        }
    }

    private static void validateRoot(SDDocumentType root) {
        validateChildren(root, root);
    }

    private static void validateChildren(SDDocumentType root, SDDocumentType currentDocument) {
        try {
            currentDocument.getDocumentReferences().get()
                    .forEach(entry -> {
                        SDDocumentType referencedDocument = entry.getValue().targetSearch().getDocument();
                        validateDocument(root, referencedDocument);
                    });
            currentDocument.getInheritedTypes()
                    .forEach(inheritedDocument -> {
                        if (!isRootDocument(inheritedDocument)) {
                            validateDocument(root, inheritedDocument);
                        }
                    });
        } catch (DocumentGraphException e) {
            e.addParentDocument(currentDocument);
            throw e;
        }
    }

    private static void validateDocument(SDDocumentType root, SDDocumentType currentDocument) {
        if (root.equals(currentDocument)) {
            throw new DocumentGraphException(currentDocument);
        }
        validateChildren(root, currentDocument);
    }

    private static boolean isRootDocument(SDDocumentType doc) {
        return doc.getName().equals("document");
    }

    public static class DocumentGraphException extends IllegalArgumentException {
        private final Deque<SDDocumentType> deque = new ArrayDeque<>();

        public DocumentGraphException(SDDocumentType document) {
            deque.addLast(document);
        }

        public void addParentDocument(SDDocumentType document) {
            deque.addFirst(document);
        }

        @Override
        public String getMessage() {
            return deque.stream()
                    .map(SDDocumentType::getName)
                    .collect(joining("->", "Document dependency cycle detected: ", "."));
        }
    }

}