1
+ /*
2
+ * eXist-db Open Source Native XML Database
3
+ * Copyright (C) 2001 The eXist-db Authors
4
+ *
5
+
6
+ * http://www.exist-db.org
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; either
11
+ * version 2.1 of the License, or (at your option) any later version.
12
+ *
13
+ * This library is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
+ * Lesser General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU Lesser General Public
19
+ * License along with this library; if not, write to the Free Software
20
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
+ */
22
+ package org .exist .indexing ;
23
+
24
+ import org .exist .EXistException ;
25
+ import org .exist .collections .Collection ;
26
+ import org .exist .collections .CollectionConfigurationException ;
27
+ import org .exist .collections .CollectionConfigurationManager ;
28
+ import org .exist .collections .triggers .TriggerException ;
29
+ import org .exist .dom .persistent .DefaultDocumentSet ;
30
+ import org .exist .dom .persistent .DocumentSet ;
31
+ import org .exist .dom .persistent .MutableDocumentSet ;
32
+ import org .exist .security .PermissionDeniedException ;
33
+ import org .exist .storage .BrokerPool ;
34
+ import org .exist .storage .DBBroker ;
35
+ import org .exist .storage .txn .TransactionManager ;
36
+ import org .exist .storage .txn .Txn ;
37
+ import org .exist .test .ExistEmbeddedServer ;
38
+ import org .exist .test .TestConstants ;
39
+ import org .exist .util .LockException ;
40
+ import org .exist .util .MimeType ;
41
+ import org .exist .util .StringInputSource ;
42
+ import org .exist .xmldb .XmldbURI ;
43
+ import org .exist .xquery .XPathException ;
44
+ import org .exist .xquery .XQuery ;
45
+ import org .exist .xquery .XQueryContext ;
46
+ import org .exist .xquery .value .Sequence ;
47
+ import org .junit .*;
48
+ import org .junit .runner .RunWith ;
49
+ import org .junit .runners .Parameterized ;
50
+ import org .xml .sax .SAXException ;
51
+
52
+ import java .io .IOException ;
53
+ import java .net .URISyntaxException ;
54
+ import java .util .Arrays ;
55
+ import java .util .Optional ;
56
+
57
+ import static org .exist .util .PropertiesBuilder .propertiesBuilder ;
58
+ import static org .junit .Assert .assertEquals ;
59
+
60
+ @ RunWith (Parameterized .class )
61
+ public class EnforceIndexUseTest {
62
+ @ Parameterized .Parameters (name = "{0}" )
63
+ public static java .util .Collection <Object []> data () {
64
+ return Arrays .asList (new Object [][] {
65
+ { "always" , 1 },
66
+ { "strict" , 3 }
67
+ });
68
+ }
69
+
70
+ @ Parameterized .Parameter
71
+ public String enforceIndexUseValue ;
72
+ // always means the database will only get results from the collections indexed by range index it will ignore un-indexed collections
73
+ // strict means it will get results from all the collections and will not ignore un-indexed
74
+
75
+ @ Parameterized .Parameter (value = 1 )
76
+ public int expectedSearchCount ;
77
+
78
+ private ExistEmbeddedServer existEmbeddedServer ;
79
+ private static final String OLD_RANGE_COLLECTION_CONFIG =
80
+ "<collection xmlns=\" http://exist-db.org/collection-config/1.0\" >\n " +
81
+ " <index xmlns:xs=\" http://www.w3.org/2001/XMLSchema\" >\n " +
82
+ " <create qname=\" @bar\" type=\" xs:string\" />\n " +
83
+ " </index>\n " +
84
+ "</collection>" ;
85
+ private static final String NEW_RANGE_COLLECTION_CONFIG =
86
+ "<collection xmlns=\" http://exist-db.org/collection-config/1.0\" >\n " +
87
+ " <index xmlns:xs=\" http://www.w3.org/2001/XMLSchema\" >\n " +
88
+ " <range>\n " +
89
+ " <create qname=\" @bar\" type=\" xs:string\" />\n " +
90
+ " </range>\n " +
91
+ " </index>\n " +
92
+ "</collection>" ;
93
+
94
+ private static final String XML =
95
+ "<root>\n " +
96
+ "<foo bar=\" baz\" />\n " +
97
+ "</root>" ;
98
+
99
+ @ Test
100
+ public void matchesWithDiffrentIndexStyles () throws PermissionDeniedException , EXistException , XPathException {
101
+ //query and expand
102
+ final String query = "for $hit in collection(\" " + TestConstants .TEST_COLLECTION_URI .toString () + "\" )//foo[matches(@bar, \" ^b\" )]\n " +
103
+ "return $hit" ;
104
+
105
+ final BrokerPool pool = existEmbeddedServer .getBrokerPool ();
106
+ try (final DBBroker broker = pool .get (Optional .of (pool .getSecurityManager ().getSystemSubject ()))) {
107
+ final XQuery xquery = pool .getXQueryService ();
108
+ final Sequence seq = xquery .execute (broker , query , null );
109
+ assertEquals (expectedSearchCount , seq .getItemCount ());
110
+ }
111
+ }
112
+
113
+ private DocumentSet configureAndStore (final String configuration , final String data , final String docName , String path ) throws EXistException , CollectionConfigurationException , PermissionDeniedException , SAXException , LockException , IOException , URISyntaxException {
114
+ final MutableDocumentSet docs = new DefaultDocumentSet ();
115
+ final BrokerPool pool = existEmbeddedServer .getBrokerPool ();
116
+ final TransactionManager transact = pool .getTransactionManager ();
117
+ try (final DBBroker broker = pool .get (Optional .of (pool .getSecurityManager ().getSystemSubject ()));
118
+ final Txn transaction = transact .beginTransaction ();
119
+ final Collection collection = broker .getOrCreateCollection (transaction , XmldbURI .xmldbUriFor (TestConstants .TEST_COLLECTION_URI + path ))) {
120
+
121
+
122
+ if (configuration != null ) {
123
+ final CollectionConfigurationManager mgr = pool .getConfigurationManager ();
124
+ mgr .addConfiguration (transaction , broker , collection , configuration );
125
+ }
126
+ broker .storeDocument (transaction , XmldbURI .create (docName ), new StringInputSource (data ), MimeType .XML_TYPE , collection );
127
+
128
+ docs .add (collection .getDocument (broker , XmldbURI .create (docName )));
129
+ transaction .commit ();
130
+ }
131
+
132
+ return docs ;
133
+ }
134
+
135
+
136
+ @ Before
137
+ public void setup () throws Throwable {
138
+ existEmbeddedServer = new ExistEmbeddedServer (
139
+ propertiesBuilder ()
140
+ .put (XQueryContext .PROPERTY_ENFORCE_INDEX_USE , enforceIndexUseValue ).build ()
141
+ ,true
142
+ ,true );
143
+
144
+ existEmbeddedServer .startDb ();
145
+
146
+ final BrokerPool pool = existEmbeddedServer .getBrokerPool ();
147
+
148
+ final TransactionManager transact = pool .getTransactionManager ();
149
+ try (final DBBroker broker = pool .get (Optional .of (pool .getSecurityManager ().getSystemSubject ()));
150
+ final Txn transaction = transact .beginTransaction ()) {
151
+
152
+ try (final Collection test = broker .getOrCreateCollection (transaction , TestConstants .TEST_COLLECTION_URI );
153
+ final Collection newRange = broker .getOrCreateCollection (transaction , XmldbURI .xmldbUriFor (TestConstants .TEST_COLLECTION_URI + "new-range-index" ));
154
+ final Collection oldRange = broker .getOrCreateCollection (transaction , XmldbURI .xmldbUriFor (TestConstants .TEST_COLLECTION_URI + "old-range-index" ));
155
+ final Collection noRange = broker .getOrCreateCollection (transaction , XmldbURI .xmldbUriFor (TestConstants .TEST_COLLECTION_URI + "no-range-index" ))){
156
+
157
+ broker .saveCollection (transaction , newRange );
158
+ broker .saveCollection (transaction , test );
159
+ broker .saveCollection (transaction , oldRange );
160
+ broker .saveCollection (transaction , noRange );
161
+ }
162
+
163
+ transaction .commit ();
164
+ }
165
+
166
+ // store the xml data and configure it
167
+ configureAndStore (NEW_RANGE_COLLECTION_CONFIG , XML , "test.xml" , "/new-range-index" );
168
+ configureAndStore (OLD_RANGE_COLLECTION_CONFIG , XML , "test.xml" , "/old-range-index" );
169
+ configureAndStore (null , XML , "test.xml" , "/no-range-index" );
170
+
171
+ }
172
+
173
+ @ After
174
+ public void cleanup () throws EXistException , PermissionDeniedException , IOException , TriggerException {
175
+ final BrokerPool pool = existEmbeddedServer .getBrokerPool ();
176
+ final TransactionManager transact = pool .getTransactionManager ();
177
+ try (final DBBroker broker = pool .get (Optional .of (pool .getSecurityManager ().getSystemSubject ()));
178
+ final Txn transaction = transact .beginTransaction ();
179
+ final Collection collConfig = broker .getOrCreateCollection (transaction , XmldbURI .create (XmldbURI .CONFIG_COLLECTION + "/db" ))) {
180
+
181
+ broker .removeCollection (transaction , collConfig );
182
+ try (Collection test = broker .getOrCreateCollection (transaction , TestConstants .TEST_COLLECTION_URI )) {
183
+ if (test != null ) {
184
+ broker .removeCollection (transaction , test );
185
+ }
186
+ }
187
+
188
+ transaction .commit ();
189
+ }
190
+
191
+ existEmbeddedServer .stopDb (true );
192
+ existEmbeddedServer = null ;
193
+ }
194
+ }
0 commit comments