Skip to content

Commit eb13b71

Browse files
committed
[feature] Adds in memory cache for query results
- Use in memory caching for resuls smaller 4 MB adding new CachedContentFile and companion test case - Extended XmlRpcTest to improve coverage of RpcConnection - Added new QueryResultCacheTest Signed-off-by: Patrick Reinhart <[email protected]>
1 parent 1e6737e commit eb13b71

12 files changed

+942
-352
lines changed

exist-core/src/main/java/org/exist/util/io/MemoryContentsInputStream.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import java.io.IOException;
2828
import java.io.InputStream;
29-
import java.io.OutputStream;
3029
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
3130

3231
/**
@@ -84,7 +83,7 @@ public int read() throws IOException {
8483
public int read(byte[] b, int off, int len) throws IOException {
8584
boolean success = false;
8685
int read = 0;
87-
while (!success) {
86+
while (!success && len > 0) {
8887
long positionBefore = POSITION_UPDATER.get(this);
8988
read = this.memoryContents.read(b, positionBefore, off, len);
9089
if (read < 1) {
@@ -115,13 +114,4 @@ public long skip(long n) throws IOException {
115114
}
116115
return skipped;
117116
}
118-
119-
// Java 9 method, has to compile under Java 1.7 so no @Override
120-
public long transferTo(OutputStream out) throws IOException {
121-
long positionBefore = POSITION_UPDATER.get(this);
122-
long written = this.memoryContents.transferTo(out, positionBefore);
123-
POSITION_UPDATER.set(this, this.memoryContents.size());
124-
return written;
125-
}
126-
127117
}

exist-core/src/main/java/org/exist/util/io/VirtualTempPath.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ public final class VirtualTempPath implements ContentFile {
5252
@GuardedBy("lock")
5353
private Path contentFile;
5454

55+
public VirtualTempPath(TemporaryFileManager tempFileManager) {
56+
this(DEFAULT_IN_MEMORY_SIZE, tempFileManager);
57+
}
58+
5559
public VirtualTempPath(int inMemorySize, TemporaryFileManager tempFileManager) {
5660
this.inMemorySize = inMemorySize;
5761
this.lock = new StampedLock();
@@ -97,6 +101,7 @@ public OutputStream newOutputStream() throws IOException {
97101
}
98102
}
99103

104+
@Override
100105
public InputStream newInputStream() throws IOException {
101106
long stamp = lock.readLock();
102107
try {
@@ -129,6 +134,7 @@ public void close() {
129134
}
130135
}
131136

137+
@Override
132138
public long size() {
133139
long stamp = lock.readLock();
134140
try {
@@ -141,6 +147,7 @@ public long size() {
141147
}
142148
}
143149

150+
@Override
144151
public byte[] getBytes() {
145152
long stamp = lock.readLock();
146153
try {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.xmlrpc;
23+
24+
import org.exist.util.io.ContentFile;
25+
26+
/**
27+
* @author <a href="mailto:[email protected]">Patrick Reinhart</a>
28+
*/
29+
public final class CachedContentFile extends AbstractCachedResult {
30+
private final ContentFile result;
31+
32+
public CachedContentFile(final ContentFile result) {
33+
super(0);
34+
this.result = result;
35+
}
36+
37+
@Override
38+
public ContentFile getResult() {
39+
return result;
40+
}
41+
42+
@Override
43+
protected void doClose() {
44+
if (result != null) {
45+
result.close();
46+
}
47+
}
48+
}

exist-core/src/main/java/org/exist/xmlrpc/QueryResultCache.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ public SerializedResult getSerializedResult(final int cacheId) {
8181
return (acr != null && acr instanceof SerializedResult) ? (SerializedResult) acr : null;
8282
}
8383

84+
public CachedContentFile getCachedContentFile(final int cacheId) {
85+
final AbstractCachedResult acr = get(cacheId);
86+
return (acr != null && acr instanceof CachedContentFile) ? (CachedContentFile) acr : null;
87+
}
88+
8489
public void remove(final int cacheId) {
8590
if (cacheId < 0 || cacheId >= cacheIdCounter.get()) {
8691
return; // out of scope

exist-core/src/main/java/org/exist/xmlrpc/RpcAPI.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public interface RpcAPI {
112112
/**
113113
* Retrieve document by name. XML content is indented if prettyPrint is set
114114
* to &gt;=0. Use supplied encoding for output.
115-
*
115+
* <p>
116116
* This method is provided to retrieve a document with encodings other than
117117
* UTF-8. Since the data is handled as binary data, character encodings are
118118
* preserved. byte[]-values are automatically BASE64-encoded by the XMLRPC
@@ -132,7 +132,7 @@ byte[] getDocument(String name, String encoding, int prettyPrint)
132132
* Retrieve document by name. XML content is indented if prettyPrint is set
133133
* to &gt;=0. Use supplied encoding for output and apply the specified
134134
* stylesheet.
135-
*
135+
* <p>
136136
* This method is provided to retrieve a document with encodings other than
137137
* UTF-8. Since the data is handled as binary data, character encodings are
138138
* preserved. byte[]-values are automatically BASE64-encoded by the XMLRPC
@@ -152,7 +152,7 @@ byte[] getDocument(String name, String encoding, int prettyPrint, String stylesh
152152
/**
153153
* Retrieve document by name. All optional output parameters are passed as
154154
* key/value pairs int the <code>parameters</code>.
155-
*
155+
* <p>
156156
* Valid keys may either be taken from
157157
* {@link javax.xml.transform.OutputKeys} or
158158
* {@link org.exist.storage.serializers.EXistOutputKeys}. For example, the
@@ -247,10 +247,10 @@ List<String> getCollectionListing(final String collection)
247247
List<String> getDocumentListing(String collection)
248248
throws EXistException, PermissionDeniedException, URISyntaxException;
249249

250-
Map<String, List> listDocumentPermissions(String name)
250+
Map<String, List<Object>> listDocumentPermissions(String name)
251251
throws EXistException, PermissionDeniedException, URISyntaxException;
252252

253-
Map<XmldbURI, List> listCollectionPermissions(String name)
253+
Map<String, List<Object>> listCollectionPermissions(String name)
254254
throws EXistException, PermissionDeniedException, URISyntaxException;
255255

256256
/**
@@ -497,6 +497,7 @@ byte[] query(
497497
* @throws PermissionDeniedException If the current user is not allowed to perform this action
498498
* @deprecated use List query() or int executeQuery() instead
499499
*/
500+
@Deprecated
500501
Map<String, Object> querySummary(String xquery)
501502
throws EXistException, PermissionDeniedException;
502503

@@ -612,7 +613,7 @@ String uploadCompressed(String file, byte[] data, int length)
612613

613614
/**
614615
* Parse a file previously uploaded with upload.
615-
*
616+
* <p>
616617
* The temporary file will be removed.
617618
*
618619
* @param localFile temporary file name
@@ -749,6 +750,7 @@ Map<String, Object> execute(String path, Map<String, Object> parameters)
749750
*
750751
* @deprecated Use {@link #executeT(String, Map)} instead.
751752
*/
753+
@Deprecated
752754
Map<String, Object> executeT(String path, Map<String, Object> parameters)
753755
throws EXistException, PermissionDeniedException;
754756

@@ -926,7 +928,7 @@ String hasUserLock(String path)
926928

927929
List<String> getGroups() throws EXistException, PermissionDeniedException;
928930

929-
List<List> getIndexedElements(String collectionName, boolean inclusive)
931+
List<List<Object>> getIndexedElements(String collectionName, boolean inclusive)
930932
throws EXistException, PermissionDeniedException, URISyntaxException;
931933

932934
boolean releaseQueryResult(int handle);

0 commit comments

Comments
 (0)