aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/annotation/SpanTree.java
blob: f785cf3b3ec51da7b327cead9fd2b8f3664ca234 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.annotation;

import com.google.common.collect.ImmutableMultiset;
import com.yahoo.document.CollectionDataType;
import com.yahoo.document.Field;
import com.yahoo.document.MapDataType;
import com.yahoo.document.StructuredDataType;
import com.yahoo.document.datatypes.CollectionFieldValue;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.MapFieldValue;
import com.yahoo.document.datatypes.StringFieldValue;
import com.yahoo.document.datatypes.StructuredFieldValue;

import java.util.ArrayList;
import java.util.Collection;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * A SpanTree holds a root node of a tree of SpanNodes, and a List of Annotations pointing to these nodes
 * or each other. It also has a name.
 *
 * @author Einar M R Rosenvinge
 * @see com.yahoo.document.annotation.SpanNode
 * @see com.yahoo.document.annotation.Annotation
 */
public class SpanTree implements Iterable<Annotation>, SpanNodeParent, Comparable<SpanTree> {

    private String name;
    private SpanNode root;
    private AnnotationContainer annotations = new ListAnnotationContainer();
    private StringFieldValue stringFieldValue;

    /**
     * WARNING! Only to be used by deserializers! Creates an empty SpanTree instance.
     */
    public SpanTree() { }

    /**
     * Creates a new SpanTree with a given root node.
     *
     * @param name the name of the span tree
     * @param root the root node of the span tree
     * @throws IllegalStateException if the root node is invalid
     */
    public SpanTree(String name, SpanNode root) {
        this.name = name;
        setRoot(root);
    }

    /**
     * Creates a new SpanTree with the given name and an empty SpanList as its root node.
     *
     * @param name the name of the span tree
     */
    public SpanTree(String name) {
        this.name = name;
        setRoot(new SpanList());
    }

    public SpanTree(SpanTree otherToCopy) {
        name = otherToCopy.name;
        setRoot(copySpan(otherToCopy.root));
        List<Annotation> annotationsToCopy = new ArrayList<>(otherToCopy.getAnnotations());
        List<Annotation> newAnnotations = new ArrayList<>(annotationsToCopy.size());

        for (Annotation otherAnnotationToCopy : annotationsToCopy) {
            newAnnotations.add(new Annotation(otherAnnotationToCopy));
        }

        IdentityHashMap<SpanNode, Integer> originalSpanNodes = getSpanNodes(otherToCopy);
        List<SpanNode> copySpanNodes = getSpanNodes();

        for (int i = 0; i < annotationsToCopy.size(); i++) {
            Annotation originalAnnotation = annotationsToCopy.get(i);
            if (!originalAnnotation.isSpanNodeValid()) {  //returns false also if spanNode is null!
                continue;
            }
            Integer indexOfOriginalSpanNode = originalSpanNodes.get(originalAnnotation.getSpanNode());
            if (indexOfOriginalSpanNode == null) {
                throw new IllegalStateException("Could not clone tree, SpanNode of " + originalAnnotation + " not found.");
            }
            newAnnotations.get(i).setSpanNode(copySpanNodes.get(indexOfOriginalSpanNode));
        }

        IdentityHashMap<Annotation, Integer> originalAnnotations = getAnnotations(annotationsToCopy);

        for (Annotation a : newAnnotations) {
            if (!a.hasFieldValue()) {
                continue;
            }
            setCorrectAnnotationReference(a.getFieldValue(), originalAnnotations, newAnnotations);
        }

        for (Annotation a : newAnnotations) {
            annotate(a);
        }
        for (IndexKey key : otherToCopy.getCurrentIndexes()) {
            createIndex(key);
        }
    }

    private void setCorrectAnnotationReference(FieldValue value, IdentityHashMap<Annotation, Integer> originalAnnotations, List<Annotation> newAnnotations) {
        if (value == null) {
            return;
        }

        if (value.getDataType() instanceof AnnotationReferenceDataType) {
            AnnotationReference ref = (AnnotationReference) value;
            if (ref.getReference() == null) {
                return;
            }
            Integer referenceIndex = originalAnnotations.get(ref.getReference());
            if (referenceIndex == null) {
                throw new IllegalStateException("Cannot find Annotation pointed to by " + ref);
            }
            try {
                Annotation newReference = newAnnotations.get(referenceIndex);
                ref.setReference(newReference);
            } catch (IndexOutOfBoundsException ioobe) {
                throw new IllegalStateException("Cannot find Annotation pointed to by " + ref, ioobe);
            }
        } else if (value.getDataType() instanceof StructuredDataType) {
            setCorrectAnnotationReference((StructuredFieldValue) value, originalAnnotations, newAnnotations);
        } else if (value.getDataType() instanceof CollectionDataType) {
            setCorrectAnnotationReference((CollectionFieldValue) value, originalAnnotations, newAnnotations);
        } else if (value.getDataType() instanceof MapDataType) {
            setCorrectAnnotationReference((MapFieldValue) value, originalAnnotations, newAnnotations);
        }
    }

    private void setCorrectAnnotationReference(StructuredFieldValue struct, IdentityHashMap<Annotation, Integer> originalAnnotations, List<Annotation> newAnnotations) {
        for (Field f : struct.getDataType().getFields()) {
            setCorrectAnnotationReference(struct.getFieldValue(f), originalAnnotations, newAnnotations);
        }
    }

    private void setCorrectAnnotationReference(CollectionFieldValue collection, IdentityHashMap<Annotation, Integer> originalAnnotations, List<Annotation> newAnnotations) {
        Iterator it = collection.fieldValueIterator();
        while (it.hasNext()) {
            setCorrectAnnotationReference((FieldValue) it.next(), originalAnnotations, newAnnotations);
        }
    }

    private void setCorrectAnnotationReference(MapFieldValue map, IdentityHashMap<Annotation, Integer> originalAnnotations, List<Annotation> newAnnotations) {
        for (Object o : map.values()) {
            setCorrectAnnotationReference((FieldValue) o, originalAnnotations, newAnnotations);
        }
    }

    private IdentityHashMap<Annotation, Integer> getAnnotations(List<Annotation> annotationsToCopy) {
        IdentityHashMap<Annotation, Integer> map = new IdentityHashMap<>();
        for (int i = 0; i < annotationsToCopy.size(); i++) {
            map.put(annotationsToCopy.get(i), i);
        }
        return map;
    }


    private List<SpanNode> getSpanNodes() {
        ArrayList<SpanNode> nodes = new ArrayList<>();
        nodes.add(root);
        Iterator<SpanNode> it = root.childIteratorRecursive();
        while (it.hasNext()) {
            nodes.add(it.next());
        }
        return nodes;
    }

    private static IdentityHashMap<SpanNode, Integer> getSpanNodes(SpanTree otherToCopy) {
        IdentityHashMap<SpanNode, Integer> map = new IdentityHashMap<>();
        int spanNodeCounter = 0;
        map.put(otherToCopy.getRoot(), spanNodeCounter++);
        Iterator<SpanNode> it = otherToCopy.getRoot().childIteratorRecursive();
        while (it.hasNext()) {
            map.put(it.next(), spanNodeCounter++);
        }
        return map;
    }

    private SpanNode copySpan(SpanNode spanTree) {
        if (spanTree instanceof Span) {
            return new Span((Span) spanTree);
        } else if (spanTree instanceof AlternateSpanList) {
            return new AlternateSpanList((AlternateSpanList) spanTree);
        } else if (spanTree instanceof SpanList) {
            return new SpanList((SpanList) spanTree);
        } else if (spanTree instanceof DummySpanNode) {
            return spanTree;  //shouldn't really happen
        } else {
            throw new IllegalStateException("Cannot create copy of " + spanTree + " with class "
            + ((spanTree == null) ? "null" : spanTree.getClass()));
        }
    }

    /**
     * WARNING!&nbsp;Only to be used by deserializers!&nbsp;Sets the name of this SpanTree instance.
     *
     * @param name the name to set for this SpanTree instance.
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * WARNING!&nbsp;Only to be used by deserializers!&nbsp;Sets the root of this SpanTree instance.
     *
     * @param root the root to set for this SpanTree instance.
     */
    public void setRoot(SpanNode root) {
        if (!root.isValid()) {
            throw new IllegalStateException("Cannot use invalid node " + root + " as root node.");
        }
        if (root.getParent() != null) {
            if (root.getParent() != this) {
                throw new IllegalStateException(root + " is already a child of " + root.getParent() + ", cannot be root of " + this);
            }
        }
        this.root = root;
        root.setParent(this);
    }

    /** Returns the name of this span tree. */
    public String getName() {
        return name;
    }

    /** Returns the root node of this span tree. */
    public SpanNode getRoot() {
        return root;
    }

    /**
     * Convenience shorthand for <code>(SpanList)getRoot()</code>.
     * This must of course only be used when it is known that the root in this tree actually is a SpanList.
     */
    public SpanList spanList() {
        return (SpanList)root;
    }

    /**
     * Ensures consistency of the tree in case SpanNodes have been removed, and there are still
     * Annotations pointing to them. This method has a maximum upper bound of O(3nm), where n is the
     * total number of Annotations, and m is the number of SpanNodes that had been removed from the tree.
     * The lower bound is Omega(n), if no SpanNodes had been removed from the tree.
     */
    public void cleanup() {
        Map<Annotation, Annotation> removedAnnotations = removeAnnotationsThatPointToInvalidSpanNodes();

        //here:
        //iterate through all annotations;
        //if any of those have ONLY an annotationreference as its value,
        //    - null reference
        //    - remove value from annotation
        //    - remove annotation and add it to removedAnnotations map
        if (!removedAnnotations.isEmpty()) {
            Iterator<Annotation> annotationIt = iterator();
            while (annotationIt.hasNext()) {
                Annotation a = annotationIt.next();
                if (!a.hasFieldValue()) {
                    continue;
                }
                FieldValue value = a.getFieldValue();

                if (value instanceof AnnotationReference ref) {
                    //the annotation "a" has a reference
                    if (removedAnnotations.get(ref.getReference()) != null) {
                        //this reference refers to a dead annotation
                        ref.setReference(null);
                        a.setFieldValue(null);
                        if (!a.isSpanNodeValid()) {
                            //this annotation has no span node, delete it
                            annotationIt.remove();
                            removedAnnotations.put(a, a);
                        }
                    }
                }
            }
        }

        //there may still be references to removed annotations,
        //inside maps, weighted sets, structs, etc.
        //if any of those have such references,
        //   - null reference
        //   - remove annotationref from struct, map, etc.
        //   - apart from this, keep struct, map etc. and annotation
        if (!removedAnnotations.isEmpty()) {
            for (Annotation a : this) {
                if (!a.hasFieldValue()) {
                    continue;
                }
                removeObsoleteReferencesFromFieldValue(a.getFieldValue(), removedAnnotations, true);
            }
        }
        //was any annotations removed from the global list? do we still have references to those annotations
        //that have been removed? if so, remove the references
        removeAnnotationReferencesThatPointToRemovedAnnotations();
    }

    private boolean removeObsoleteReferencesFromFieldValue(FieldValue value, Map<Annotation, Annotation> selectedAnnotations, boolean removeIfPresent) {
        if (value == null) {
            return false;
        }

        if (value.getDataType() instanceof AnnotationReferenceDataType) {
            AnnotationReference ref = (AnnotationReference) value;
            if (removeIfPresent) {
                if (selectedAnnotations.containsValue(ref.getReference())) {
                    //this reference refers to a dead annotation
                    ref.setReference(null);
                    return true;
                }
            } else {
                if (!selectedAnnotations.containsValue(ref.getReference())) {
                    //this reference refers to a dead annotation
                    ref.setReference(null);
                    return true;
                }
            }
        } else if (value.getDataType() instanceof StructuredDataType) {
            removeObsoleteReferencesFromStructuredType((StructuredFieldValue) value, selectedAnnotations, removeIfPresent);
        } else if (value.getDataType() instanceof CollectionDataType) {
            removeObsoleteReferencesFromCollectionType((CollectionFieldValue) value, selectedAnnotations, removeIfPresent);
        } else if (value.getDataType() instanceof MapDataType) {
            removeObsoleteReferencesFromMapType((MapFieldValue) value, selectedAnnotations, removeIfPresent);
        }
        return false;
    }

    private boolean removeObsoleteReferencesFromStructuredType(StructuredFieldValue struct, Map<Annotation, Annotation> selectedAnnotations, boolean removeIfPresent) {
        for (Field f : struct.getDataType().getFields()) {
            FieldValue fValue = struct.getFieldValue(f);
            if (removeObsoleteReferencesFromFieldValue(fValue, selectedAnnotations, removeIfPresent)) {
                struct.removeFieldValue(f);
            }
        }
        return false;
    }

    private boolean removeObsoleteReferencesFromCollectionType(CollectionFieldValue collection, Map<Annotation, Annotation> selectedAnnotations, boolean removeIfPresent) {
        Iterator it = collection.fieldValueIterator();
        while (it.hasNext()) {
            FieldValue fValue = (FieldValue) it.next();
            if (removeObsoleteReferencesFromFieldValue(fValue, selectedAnnotations, removeIfPresent)) {
                it.remove();
            }
        }
        return false;
    }

    private boolean removeObsoleteReferencesFromMapType(MapFieldValue map, Map<Annotation, Annotation> selectedAnnotations, boolean removeIfPresent) {
        Iterator valueIt = map.values().iterator();
        while (valueIt.hasNext()) {
            FieldValue fValue = (FieldValue) valueIt.next();
            if (removeObsoleteReferencesFromFieldValue(fValue, selectedAnnotations, removeIfPresent)) {
                valueIt.remove();
            }
        }
        return false;
    }

    private Map<Annotation, Annotation> removeAnnotationsThatPointToInvalidSpanNodes() {
        Map<Annotation, Annotation> removedAnnotations = new IdentityHashMap<Annotation, Annotation>();

        Iterator<Annotation> annotationIt = iterator();
        while (annotationIt.hasNext()) {
            Annotation a = annotationIt.next();
            if (a.hasSpanNode() && !a.isSpanNodeValid()) {
                a.setSpanNode(null);
                a.setFieldValue(null);
                removedAnnotations.put(a, a);
                annotationIt.remove();
            }
        }
        return removedAnnotations;
    }

    private boolean hasAnyFieldValues() {
        for (Annotation a : this) {
            if (a.hasFieldValue()) {
                return true;
            }
        }
        return false;
    }
    private void removeAnnotationReferencesThatPointToRemovedAnnotations() {
        if (hasAnyFieldValues()) {
            Map<Annotation, Annotation> annotationsStillPresent = new IdentityHashMap<Annotation, Annotation>();
            for (Annotation a : this) {
                annotationsStillPresent.put(a, a);
            }
            for (Annotation a : this) {
                if (!a.hasFieldValue()) {
                    continue;
                }
                //do we have any references to annotations that are NOT in this global list??
                removeObsoleteReferencesFromFieldValue(a.getFieldValue(), annotationsStillPresent, false);
            }
        }
    }

    private void annotateInternal(SpanNode node, Annotation annotation) {
        annotations.annotate(annotation);
    }

    private Collection<Annotation> getAnnotations() {
        return annotations.annotations();
    }

    /**
     * Adds an Annotation to the internal list of annotations for this SpanTree.&nbsp;Use this when
     * adding an Annotation that uses an AnnotationReference, and does not annotate a SpanNode.
     *
     * @param a the Annotation to add
     * @return this, for chaining
     * @see com.yahoo.document.annotation.Annotation
     * @see com.yahoo.document.annotation.AnnotationReference
     * @see com.yahoo.document.annotation.AnnotationReferenceDataType
     */
    public SpanTree annotate(Annotation a) {
        if (a.getSpanNode() == null) {
            annotateInternal(DummySpanNode.INSTANCE, a);
        } else {
            annotateInternal(a.getSpanNode(), a);
        }
        return this;
    }

    /**
     * Adds an Annotation to the internal list of annotations for this SpanTree.&nbsp;Use this when
     * adding an Annotation that shall annotate a SpanNode. Upon return, Annotation.getSpanNode()
     * returns the given node.
     *
     * @param node the node to annotate
     * @param annotation the Annotation to add
     * @return this, for chaining
     * @see com.yahoo.document.annotation.Annotation
     */
    public SpanTree annotate(SpanNode node, Annotation annotation) {
        annotation.setSpanNode(node);
        return annotate(annotation);
    }

    /**
     * Adds an Annotation to the internal list of annotations for this SpanTree.&nbsp;Use this when
     * adding an Annotation that shall annotate a SpanNode. Upon return, Annotation.getSpanNode()
     * returns the given node. This one is unchecked and assumes that the SpanNode is valid and has
     * already been attached to the Annotation.
     *
     * @param node the node to annotate
     * @param annotation the Annotation to add
     * @return this, for chaining
     * @see com.yahoo.document.annotation.Annotation
     */
    public final SpanTree annotateFast(SpanNode node, Annotation annotation) {
        annotateInternal(node, annotation);
        return this;
    }

    /**
     * Adds an Annotation.
     * Convenience shorthand for <code>annotate(node,new Annotation(type,value)</code>
     *
     * @param node the node to annotate
     * @param type the type of the Annotation to add
     * @param value the value of the Annotation to add
     * @return this, for chaining
     * @see com.yahoo.document.annotation.Annotation
     */
    public SpanTree annotate(SpanNode node, AnnotationType type,FieldValue value) {
        return annotate(node, new Annotation(type, value));
    }

    /**
     * Creates an Annotation based on the given AnnotationType, and adds it to the internal list of
     * annotations for this SpanTree (convenience method).&nbsp;Use this when
     * adding an Annotation (that does not have a FieldValue) that shall annotate a SpanNode.
     * Upon return, Annotation.getSpanNode()
     * returns the given node.
     *
     * @param node the node to annotate
     * @param type the AnnotationType to create an Annotation from
     * @return this, for chaining
     * @see com.yahoo.document.annotation.Annotation
     * @see com.yahoo.document.annotation.AnnotationType
     */
    public SpanTree annotate(SpanNode node, AnnotationType type) {
        Annotation a = new Annotation(type);
        return annotate(node, a);
    }

    /**
     * Removes an Annotation from the internal list of annotations.
     *
     * @param a the annotation to remove
     * @return true if the Annotation was successfully removed, false otherwise
     */
    public boolean remove(Annotation a) {
        return getAnnotations().remove(a);
    }

    /**
     * Returns the total number of annotations in the tree.
     *
     * @return the total number of annotations in the tree.
     */
    public int numAnnotations() {
        return annotations.annotations().size();
    }

    /**
     * Clears all Annotations for a given SpanNode.
     *
     * @param node the SpanNode to clear all Annotations for.
     */
    public void clearAnnotations(SpanNode node) {
        Iterator<Annotation> annIt = iterator(node);
        while (annIt.hasNext()) {
            annIt.next();
            annIt.remove();
        }
    }

    /**
     * Clears all Annotations for a given SpanNode and its child nodes.
     *
     * @param node the SpanNode to clear all Annotations for.
     */
    public void clearAnnotationsRecursive(SpanNode node) {
        Iterator<Annotation> annIt = iteratorRecursive(node);
        while (annIt.hasNext()) {
            annIt.next();
            annIt.remove();
        }
    }

    /**
     * Returns an Iterator over all annotations in this tree.&nbsp;Note that the iteration order is non-deterministic.
     * @return an Iterator over all annotations in this tree.
     */
    public Iterator<Annotation> iterator() {
        return annotations.annotations().iterator();
    }

    /**
     * Returns an Iterator over all annotations that annotate the given node.
     *
     * @param node the node to return annotations for.
     * @return an Iterator over all annotations that annotate the given node.
     */
    public Iterator<Annotation> iterator(SpanNode node) {
        return annotations.iterator(node);
    }

    /**
     * Returns a recursive Iterator over all annotations that annotate the given node and its subnodes.
     *
     * @param node the node to recursively return annotations for.
     * @return a recursive Iterator over all annotations that annotate the given node and its subnodes.
     */
    public Iterator<Annotation> iteratorRecursive(SpanNode node) {
        return annotations.iteratorRecursive(node);
    }

    /**
     * Returns itself.&nbsp;Needed for this class to be able to be a parent of SpanNodes.
     *
     * @return this SpanTree instance.
     */
    @Override
    public SpanTree getSpanTree() {
        return this;
    }

    /**
     * Sets the StringFieldValue that this SpanTree belongs to.&nbsp;This is called by
     * {@link StringFieldValue#setSpanTree(SpanTree)} and there is no need for the user to call this
     * except in unit tests.
     *
     * @param stringFieldValue the StringFieldValue that this SpanTree should belong to (might be null to clear the current value)
     */
    public void setStringFieldValue(StringFieldValue stringFieldValue) {
        this.stringFieldValue = stringFieldValue;
    }

    /**
     * Returns the StringFieldValue that this SpanTree belongs to.
     *
     * @return the StringFieldValue that this SpanTree belongs to, if any, otherwise null.
     */
    @Override
    public StringFieldValue getStringFieldValue() {
        return stringFieldValue;
    }

    public void createIndex(IndexKey key) {
        if (key == IndexKey.SPAN_NODE && annotations instanceof ListAnnotationContainer) {
            AnnotationContainer tmpAnnotations = new SpanNode2AnnotationContainer();
            tmpAnnotations.annotateAll(annotations.annotations());
            annotations = tmpAnnotations;
        } else if (key == IndexKey.ANNOTATION_TYPE && annotations instanceof ListAnnotationContainer) {
            AnnotationContainer tmpAnnotations = new AnnotationType2AnnotationContainer();
            tmpAnnotations.annotateAll(annotations.annotations());
            annotations = tmpAnnotations;
        } else {
            throw new IllegalArgumentException("Multiple indexes not yet supported. Use clearIndex() or clearIndexes() first.");
        }
    }

    public void clearIndex(IndexKey key) {
        if (key == IndexKey.SPAN_NODE && annotations instanceof SpanNode2AnnotationContainer) {
            clearIndex();
        } else if (key == IndexKey.ANNOTATION_TYPE && annotations instanceof AnnotationType2AnnotationContainer) {
            clearIndex();
        }
    }

    public void clearIndexes() {
        if (!(annotations instanceof ListAnnotationContainer)) {
            clearIndex();
        }
    }

    private void clearIndex() {
        AnnotationContainer tmpAnnotations = new ListAnnotationContainer();
        tmpAnnotations.annotateAll(annotations.annotations());
        annotations = tmpAnnotations;
    }

    public Collection<IndexKey> getCurrentIndexes() {
        if (annotations instanceof AnnotationType2AnnotationContainer)
            return List.of(IndexKey.ANNOTATION_TYPE);
        if (annotations instanceof SpanNode2AnnotationContainer)
            return List.of(IndexKey.SPAN_NODE);
        return List.of();
    }

    @Override
    public String toString() {
        return "SpanTree '" + name + "'";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof SpanTree tree)) return false;

        if (!annotationsEquals(tree)) return false;
        if (!name.equals(tree.name)) return false;
        if (!root.equals(tree.root)) return false;

        return true;
    }

    private boolean annotationsEquals(SpanTree tree) {
        List<Annotation> annotationCollection = new LinkedList<Annotation>(getAnnotations());
        List<Annotation> otherAnnotations = new LinkedList<Annotation>(tree.getAnnotations());

        return annotationCollection.size() == otherAnnotations.size() &&
                ImmutableMultiset.copyOf(annotationCollection).equals(ImmutableMultiset.copyOf(otherAnnotations));
    }

    @Override
    public int hashCode() {
        int result = name.hashCode();
        result = 31 * result + root.hashCode();
        result = 31 * result + annotations.hashCode();
        return result;
    }

    @Override
    public int compareTo(SpanTree spanTree) {
        int comp = name.compareTo(spanTree.name);
        if (comp != 0) {
            comp = root.compareTo(spanTree.root);
        }
        return comp;
    }

    public enum IndexKey {
        SPAN_NODE,
        ANNOTATION_TYPE
    }
}