|
1 | 1 | /* |
| 2 | + * Elemental |
| 3 | + * Copyright (C) 2024, Evolved Binary Ltd |
| 4 | + * |
| 5 | + |
| 6 | + * https://www.evolvedbinary.com | https://www.elemental.xyz |
| 7 | + * |
| 8 | + * This library is free software; you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU Lesser General Public |
| 10 | + * License as published by the Free Software Foundation; version 2.1. |
| 11 | + * |
| 12 | + * This library is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + * Lesser General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public |
| 18 | + * License along with this library; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | + * |
| 21 | + * NOTE: Parts of this file contain code from 'The eXist-db Authors'. |
| 22 | + * The original license header is included below. |
| 23 | + * |
| 24 | + * ===================================================================== |
| 25 | + * |
2 | 26 | * eXist-db Open Source Native XML Database |
3 | 27 | * Copyright (C) 2001 The eXist-db Authors |
4 | 28 | * |
|
41 | 65 | import org.w3c.dom.Node; |
42 | 66 |
|
43 | 67 | import javax.annotation.Nullable; |
44 | | -import java.util.*; |
| 68 | +import java.lang.ref.WeakReference; |
| 69 | +import java.util.Arrays; |
| 70 | +import java.util.HashSet; |
| 71 | +import java.util.Iterator; |
| 72 | +import java.util.NoSuchElementException; |
| 73 | +import java.util.Set; |
45 | 74 |
|
46 | 75 | /** |
47 | 76 | * A fast node set implementation, based on arrays to store nodes and documents. |
|
67 | 96 | */ |
68 | 97 | public class NewArrayNodeSet extends AbstractArrayNodeSet implements ExtNodeSet, DocumentSet { |
69 | 98 |
|
70 | | - private Set<Collection> cachedCollections = null; |
| 99 | + @Nullable private WeakReference<Set<Collection>> cachedCollectionsRef = null; |
71 | 100 |
|
72 | 101 | private int documentCount = 0; |
73 | 102 |
|
@@ -1014,16 +1043,27 @@ public boolean equalDocs(final DocumentSet other) { |
1014 | 1043 |
|
1015 | 1044 | @Override |
1016 | 1045 | public Iterator<Collection> getCollectionIterator() { |
1017 | | - sort(); |
1018 | | - if(cachedCollections == null) { |
1019 | | - cachedCollections = new HashSet<>(); |
1020 | | - for(int i = 0; i < documentCount; i++) { |
1021 | | - final DocumentImpl doc = nodes[documentNodesOffset[i]].getOwnerDocument(); |
1022 | | - if(!cachedCollections.contains(doc.getCollection())) { |
1023 | | - cachedCollections.add(doc.getCollection()); |
1024 | | - } |
| 1046 | + // First, try and retrieve from Cache |
| 1047 | + Set<Collection> cachedCollections; |
| 1048 | + if (this.cachedCollectionsRef != null) { |
| 1049 | + cachedCollections = this.cachedCollectionsRef.get(); |
| 1050 | + if (cachedCollections != null) { |
| 1051 | + return cachedCollections.iterator(); |
1025 | 1052 | } |
1026 | 1053 | } |
| 1054 | + |
| 1055 | + sort(); |
| 1056 | + |
| 1057 | + // Second, Cache is empty, so create a Cache and return |
| 1058 | + cachedCollections = new HashSet<>(); |
| 1059 | + for (int i = 0; i < documentCount; i++) { |
| 1060 | + final DocumentImpl doc = nodes[documentNodesOffset[i]].getOwnerDocument(); |
| 1061 | + final Collection collection = doc.getCollection(); |
| 1062 | + cachedCollections.add(collection); |
| 1063 | + } |
| 1064 | + |
| 1065 | + this.cachedCollectionsRef = new WeakReference<>(cachedCollections); |
| 1066 | + |
1027 | 1067 | return cachedCollections.iterator(); |
1028 | 1068 | } |
1029 | 1069 |
|
|
0 commit comments