Skip to content

Commit 6b938ae

Browse files
committed
more javadocs
1 parent 899e6d1 commit 6b938ae

File tree

4 files changed

+434
-28
lines changed

4 files changed

+434
-28
lines changed

src/main/java/com/marklogic/client/eval/EvalResult.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,64 @@
1818
import com.marklogic.client.io.Format;
1919
import com.marklogic.client.io.marker.AbstractReadHandle;
2020

21+
/** Represents the content and metadata of a result from a call to
22+
* {@link ServerEvaluationCall#eval() eval}.
23+
* Also includes some convenience methods for deserializing the result
24+
* as a String, Number, Boolean, AbstractReadHandle, or an object of
25+
* any Class which is registered by a ContentHandle.
26+
*/
2127
public interface EvalResult {
28+
/** Convenience enum useful for strongly-typed comparison to ensure
29+
* the result is of the type expected. This list captures most of
30+
* the types expected from the X-Primitive header.
31+
* @see <a href="http://docs.marklogic.com/guide/rest-dev">REST GUIDE</a> -&gt; eval endpoint
32+
*/
2233
enum Type {
2334
XML, JSON,
2435
STRING, BOOLEAN, NULL, OTHER,
2536
ANYURI, BASE64BINARY, DATE, DATETIME, DECIMAL, DOUBLE, DURATION,
2637
FLOAT, GDAY, GMONTH, GMONTHDAY, GYEAR, GYEARMONTH, HEXBINARY, INTEGER, QNAME, TIME,
2738
ATTRIBUTE, BINARY, COMMENT, PROCESSINGINSTRUCTION, TEXTNODE
2839
};
40+
/** The {@link Type} of this result from the X-Primitive header
41+
* provided by the REST API eval endpoint.
42+
* @see <a href="http://docs.marklogic.com/guide/rest-dev">REST GUIDE</a> -&gt; eval endpoint
43+
* @return the type of this result
44+
*/
2945
public Type getType();
46+
47+
/** The format of this result from the Content-Type header
48+
* provided by the REST API eval endpoint.
49+
* @see <a href="http://docs.marklogic.com/guide/rest-dev">REST GUIDE</a> -&gt; eval endpoint
50+
* @return the format of this result
51+
*/
3052
public Format getFormat();
53+
54+
/** Use a handle to retrieve the contents of this result.
55+
* @param handle the handle to populate with the contents of this result
56+
* @return the handle populated with the contents of this result
57+
*/
3158
public <H extends AbstractReadHandle> H get(H handle);
59+
60+
/** An io shortcut method to get the contents as an object
61+
* of the specified type, which must be a registered type.
62+
* @param clazz the Class (type). This type must be registered by an io handle.
63+
* @return an instance of the requested type populated with the contents of this result
64+
*/
3265
public <T> T getAs(Class<T> clazz);
66+
67+
/** This result directly as a string.
68+
* @return this result as a string
69+
*/
3370
public String getString();
71+
72+
/** This result parsed to a Number.
73+
* @return this result as a number
74+
*/
3475
public Number getNumber();
76+
77+
/** This result parsed to a Boolean.
78+
* @return a boolean as generated by new Boolean({@link #getString getString()})
79+
*/
3580
public Boolean getBoolean();
3681
}

src/main/java/com/marklogic/client/eval/EvalResultIterator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
import java.util.Iterator;
1919

20+
/** An Iterator to walk through all results returned from calls to
21+
* {@link ServerEvaluationCall#eval()}.
22+
*/
2023
public interface EvalResultIterator extends Iterable<EvalResult>, Iterator<EvalResult> {
2124
public Iterator<EvalResult> iterator();
2225
public boolean hasNext();

src/main/java/com/marklogic/client/eval/ServerEvaluationCall.java

Lines changed: 167 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.marklogic.client.eval;
1717

18+
import com.marklogic.client.DatabaseClientFactory;
1819
import com.marklogic.client.FailedRequestException;
1920
import com.marklogic.client.ForbiddenUserException;
2021
import com.marklogic.client.Transaction;
@@ -23,26 +24,189 @@
2324
import com.marklogic.client.io.marker.TextWriteHandle;
2425
import com.marklogic.client.util.EditableNamespaceContext;
2526

27+
/**
28+
* ServerEvaluationCall uses a fluent builder-style API to collect the parameters
29+
* for a server-side {@link #xquery xquery} or {@link #javascript javascript} eval or
30+
* invoke ({@link #modulePath modulePath}) call. ServerEvaluationCall also
31+
* conveniently has the eval* methods which execute those calls and return the
32+
* results. You must call one and only one of the following methods: xquery,
33+
* javascript, or modulePath. The xquery
34+
* and javascript methods initialize this call for server-side eval and accept
35+
* source code as a String or a TextWriteHandle (in case you are streaming the
36+
* source code from the file system, a URL, or other source that is most easily
37+
* accessed via io handles). The modulePath method initializes this call for server-
38+
* side invoke given the path to a module previously installed on the server.
39+
*
40+
* Here is a simple "hello world" junit example: <pre>{@code
41+
*String javascript = "'hello world'";
42+
*String response = client.newServerEval()
43+
* .javascript(javascript)
44+
* .evalAs(String.class);
45+
*assertEquals("hello world", response);
46+
*}</pre>
47+
* or in xquery: <pre>{@code
48+
*String xquery = "'hello world'";
49+
*String response = client.newServerEval()
50+
* .xquery(xquery)
51+
* .evalAs(String.class);
52+
*assertEquals("hello world", response);
53+
*}</pre>
54+
*
55+
* Variables can be added with the addVariable methods.
56+
* {@link #addVariable(String, AbstractWriteHandle) addVariable(String, AbstractWriteHandle)}
57+
* allows you to pass complex JSON or XML values directly from io handles.
58+
* {@link #addVariableAs(String, Object) addVariableAs(String, Object)}
59+
* follows the <a href="http://www.marklogic.com/blog/io-shortcut-marklogic-java-client-api/">
60+
* shortcut pattern</a> which maps objects by type to the appropriate handle.
61+
* For simpler atomic values, convenience addVariable methods are provided for
62+
* String, Number, and Boolean types.
63+
*
64+
* Here is a simple "hello solar system" example with a variable: <pre>{@code
65+
*String javascript = "var planet;'hello solar system from ' + planet";
66+
*String response = client.newServerEval()
67+
* .javascript(javascript)
68+
* .addVariable("planet", "Mars)
69+
* .evalAs(String.class);
70+
*assertEquals( "hello solar system from Mars", response);
71+
*}</pre>
72+
* or in xquery: <pre>{@code
73+
*String xquery = "declare variable $planet external;'hello solar system from ' || $planet";
74+
*String response = client.newServerEval()
75+
* .xquery(xquery)
76+
* .addVariable("planet", "Mars)
77+
* .evalAs(String.class);
78+
*assertEquals( "hello solar system from Mars", response);
79+
* }</pre>
80+
*
81+
* Each call can be executed within a {@link #transaction transaction}, within a
82+
* {@link DatabaseClientFactory#newClient(String, int, String) particular database},
83+
* and with particular {@link #namespaceContext namespaces} available for expansion
84+
* of prefixed variable names.
85+
*
86+
* Each call can be executed with only one expected response of a particular
87+
* {@link #evalAs type} or {@link #eval(AbstractReadHandle) handle type}. Or calls can be executed
88+
* with {@link #eval() multiple responses expected}.
89+
*/
2690
public interface ServerEvaluationCall {
91+
/** Initialize this server-side eval with xquery-syntax source code.
92+
* @param xquery the xquery-syntax source code to eval on the server
93+
* @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder
94+
*/
2795
public ServerEvaluationCall xquery(String xquery);
96+
97+
/** Initialize this server-side eval with xquery-syntax source code.
98+
* @param xquery a handle containing the xquery-syntax source code to eval on the server
99+
* @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder
100+
*/
28101
public ServerEvaluationCall xquery(TextWriteHandle xquery);
102+
103+
/** Initialize this server-side eval with javascript-syntax source code.
104+
* @param javascript the javascript-syntax source code to eval on the server
105+
* @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder
106+
*/
29107
public ServerEvaluationCall javascript(String javascript);
30-
public ServerEvaluationCall javascript(TextWriteHandle xquery);
108+
109+
/** Initialize this server-side eval call with javascript-syntax source code.
110+
* @param javascript a handle containing the javascript-syntax source code to eval on the server
111+
* @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder
112+
*/
113+
public ServerEvaluationCall javascript(TextWriteHandle javascript);
114+
115+
/** Initialize this server-side invoke call with a path to the module to invoke.
116+
* @param modulePath a path to a module previously installed in the server
117+
* @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder
118+
*/
31119
public ServerEvaluationCall modulePath(String modulePath);
120+
121+
/** Set a variable name-value pair to pass to the code executing server-side.
122+
* @param name the variable name, including a namespace prefix if the prefix is
123+
* mapped to a uri in the {@link #namespaceContext namespace context}
124+
* @param value the atomic variable value
125+
* @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder
126+
*/
32127
public ServerEvaluationCall addVariable(String name, String value);
128+
129+
/** Set a variable name-value pair to pass to the code executing server-side.
130+
* @param name the variable name, including a namespace prefix if the prefix is
131+
* mapped to a uri in the {@link #namespaceContext namespace context}
132+
* @param value the atomic variable value
133+
* @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder
134+
*/
33135
public ServerEvaluationCall addVariable(String name, Number value);
136+
137+
/** Set a variable name-value pair to pass to the code executing server-side.
138+
* @param name the variable name, including a namespace prefix if the prefix is
139+
* mapped to a uri in the {@link #namespaceContext namespace context}
140+
* @param value the atomic variable value
141+
* @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder
142+
*/
34143
public ServerEvaluationCall addVariable(String name, Boolean value);
144+
145+
/** Set a variable name-value pair to pass to the code executing server-side.
146+
* @param name the variable name, including a namespace prefix if the prefix is
147+
* mapped to a uri in the {@link #namespaceContext namespace context}
148+
* @param value the handle containing the variable value, most likely XML or JSON
149+
* @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder
150+
*/
35151
public ServerEvaluationCall addVariable(String name, AbstractWriteHandle value);
36-
/** Like other *As convenience methods throughout the API, the Object value
37-
* is managed by the Handle registered for that Class. */
152+
153+
/** Convenience method to set a variable of a type mapped to an io handle. Like other
154+
* <a href="http://www.marklogic.com/blog/io-shortcut-marklogic-java-client-api/">
155+
* *As convenience methods</a> throughout the API, the Object value
156+
* is provided by the handle registered for that Class.
157+
*
158+
* @param name the variable name, including a namespace prefix if the prefix is
159+
* mapped to a uri in the {@link #namespaceContext namespace context}
160+
* @param value the handle containing the variable value
161+
* @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder
162+
*/
38163
public ServerEvaluationCall addVariableAs(String name, Object value);
164+
165+
/** Initialize this call with a transaction under which server-side execution should occur.
166+
* @param transaction the open transaction under which to run this call in the server
167+
* @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder
168+
*/
39169
public ServerEvaluationCall transaction(Transaction transaction);
170+
171+
/** Add a single namespace prefix-to-uri mapping to the namespace context.
172+
* @param prefix the prefix for this mapping
173+
* @param namespaceURI the uri for this mapping
174+
* @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder
175+
*/
40176
public ServerEvaluationCall addNamespace(String prefix, String namespaceURI);
177+
178+
/** Initialize this call with namespaces so variables with prefixes can be sent with
179+
* their prefixes translated to uris that will match the uris in the code to be
180+
* executed on the server.
181+
* @param namespaces a namespace context specifying the mapping from prefixes to namespaces
182+
* @return a reference to this ServerEvaluationCall instance for use as a fluent-style builder
183+
*/
41184
public ServerEvaluationCall namespaceContext(EditableNamespaceContext namespaces);
185+
186+
/** Conveneince method to get the response serialized to a particular type by an io handle.
187+
* Like other <a href="http://www.marklogic.com/blog/io-shortcut-marklogic-java-client-api/">
188+
* *As convenience methods</a> throughout the API, the return value
189+
* is provided by the handle registered for the provided responseType.
190+
*
191+
* @param responseType the type desired for the response. Must be a Class regiestered
192+
* to a handle.
193+
* @return the result deserialized by the implicit handle mapped to this responseType
194+
*/
42195
public <T> T evalAs(Class<T> responseType)
43196
throws ForbiddenUserException, FailedRequestException;
197+
198+
/** Provides the single result of the server-side eval or invoke call, wrapped in an io
199+
* handle.
200+
* @param responseHandle the type of handle appropriate for the expected single result
201+
* @return the handle which wraps the response
202+
*/
44203
public <H extends AbstractReadHandle> H eval(H responseHandle)
45204
throws ForbiddenUserException, FailedRequestException;
205+
206+
/** Provides all results returned by the server-side eval or invoke call.
207+
* @return an EvalResultIterator which provides access to all the results returned by
208+
* the server-side eval or invoke call.
209+
*/
46210
public EvalResultIterator eval()
47211
throws ForbiddenUserException, FailedRequestException;
48212
}

0 commit comments

Comments
 (0)