Skip to content

Commit 6f1682d

Browse files
author
ehennum
committed
Bug:26056 interface to new update policy property of the REST server
git-svn-id: svn+ssh://svn.marklogic.com/project/engsvn/client-api/java/branches/b2_0@163636 62cac252-8da6-4816-9e9d-6dc37b19578c
1 parent 9dcc916 commit 6f1682d

File tree

3 files changed

+105
-15
lines changed

3 files changed

+105
-15
lines changed

src/main/java/com/marklogic/client/admin/ServerConfigurationManager.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,36 @@
2626
* of the server.
2727
*/
2828
public interface ServerConfigurationManager {
29+
/**
30+
* Specifies the policy for updating documents in the database.
31+
*/
32+
public enum UpdatePolicy {
33+
/**
34+
* A document can be updated or deleted only if its version number
35+
* is supplied and matches; a document can be created only if no
36+
* version number is supplied and the document doesn't exist. This
37+
* update policy requires optimistic locking.
38+
*/
39+
VERSION_REQUIRED,
40+
/**
41+
* A document can be updated or deleted if its version number is
42+
* not supplied or if the version number matches. This update policy
43+
* provides for optional optimistic locking.
44+
*/
45+
VERSION_OPTIONAL,
46+
/**
47+
* If a document exists, the supplied metadata is merged with the
48+
* persisted document metadata. This update policy is the default
49+
* (equivalent to content versions of Policy.NONE).
50+
*/
51+
MERGE_METADATA,
52+
/**
53+
* The document is written without testing whether it exists. Only
54+
* the supplied metadata is persisted. This update policy is slightly
55+
* faster than other update policies.
56+
*/
57+
OVERWRITE_METADATA
58+
}
2959
/**
3060
* Specifies the policy for use of a capability.
3161
*/
@@ -116,15 +146,28 @@ public void writeConfiguration()
116146
public void setServerRequestLogging(Boolean on);
117147

118148
/**
119-
* Returns whether the server requires, allows, or ignores document versionss
149+
* Returns the policy for updating or deleting documents in the database.
150+
* @return the policy controlling updates or deletes
151+
*/
152+
public UpdatePolicy getUpdatePolicy();
153+
/**
154+
* Specifies the policy for updating or deleting documents in the database.
155+
* @param policy the policy controlling updates or deletes
156+
*/
157+
public void setUpdatePolicy(UpdatePolicy policy);
158+
159+
/**
160+
* Returns whether the server requires, allows, or ignores document versions
120161
* on document read, write, and delete requests.
121162
* @return the policy as required, optional, or none
163+
* @deprecated use {@link #getUpdatePolicy()}, which provides more alternatives
122164
*/
123165
public Policy getContentVersionRequests();
124166
/**
125-
* Specifies whether the server requires, allows, or ignores document versionss
167+
* Specifies whether the server requires, allows, or ignores document versions
126168
* on document read, write, and delete requests.
127169
* @param policy required, optional, or none for document versions
170+
* @deprecated use {@link #setUpdatePolicy(UpdatePolicy)}, which provides more alternatives
128171
*/
129172
public void setContentVersionRequests(Policy policy);
130173

src/main/java/com/marklogic/client/impl/ServerConfigurationManagerImpl.java

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ class ServerConfigurationManagerImpl
5151

5252
static final private String REST_API_NS = "http://marklogic.com/rest-api";
5353

54-
private Boolean validatingQueries;
55-
private Boolean validatingQueryOptions;
56-
private String defaultDocumentReadTransform;
57-
private Boolean defaultDocumentReadTransformAll;
58-
private Boolean serverRequestLogging;
59-
private Policy contentVersions;
60-
private Format errorFormat;
54+
private Boolean validatingQueries;
55+
private Boolean validatingQueryOptions;
56+
private String defaultDocumentReadTransform;
57+
private Boolean defaultDocumentReadTransformAll;
58+
private Boolean serverRequestLogging;
59+
private Policy contentVersions;
60+
private UpdatePolicy updatePolicy;
61+
private Format errorFormat;
6162

6263
private RESTServices services;
6364
private HandleFactoryRegistry handleRegistry;
@@ -97,6 +98,7 @@ public void readConfiguration()
9798
defaultDocumentReadTransformAll = null;
9899
serverRequestLogging = null;
99100
contentVersions = null;
101+
updatePolicy = null;
100102

101103
while (reader.hasNext()) {
102104
if (reader.next() != XMLStreamReader.START_ELEMENT)
@@ -115,12 +117,21 @@ public void readConfiguration()
115117
serverRequestLogging = Boolean.valueOf(reader.getElementText());
116118
} else if ("content-versions".equals(localName)) {
117119
contentVersions = Enum.valueOf(Policy.class, reader.getElementText().toUpperCase());
120+
} else if ("update-policy".equals(localName)) {
121+
updatePolicy = Enum.valueOf(UpdatePolicy.class,
122+
reader.getElementText().toUpperCase().replace("-", "_"));
118123
} else if ("error-format".equals(localName)) {
119124
errorFormat = Format.valueOf(reader.getElementText().toUpperCase());
120125
}
121126
}
122127

123128
reader.close();
129+
130+
if (contentVersions == null) {
131+
updatePolicyToContentVersion();
132+
} else if (updatePolicy == null) {
133+
contentVersionToUpdatePolicy();
134+
}
124135
} catch (XMLStreamException e) {
125136
logger.error("Failed to read server configuration stream",e);
126137
throw new MarkLogicInternalException(e);
@@ -178,6 +189,11 @@ public void write(OutputStream out) throws IOException {
178189
serializer.writeCharacters(contentVersions.name().toLowerCase());
179190
serializer.writeEndElement();
180191
}
192+
if (updatePolicy != null) {
193+
serializer.writeStartElement(REST_API_NS, "update-policy");
194+
serializer.writeCharacters(updatePolicy.name().toLowerCase().replace("_", "-"));
195+
serializer.writeEndElement();
196+
}
181197
if (errorFormat != null) {
182198
serializer.writeStartElement(REST_API_NS, "error-format");
183199
serializer.writeCharacters(errorFormat.name().toLowerCase());
@@ -238,13 +254,44 @@ public void setServerRequestLogging(Boolean on) {
238254
serverRequestLogging = on;
239255
}
240256

257+
@Override
258+
public UpdatePolicy getUpdatePolicy() {
259+
return updatePolicy;
260+
}
261+
@Override
262+
public void setUpdatePolicy(UpdatePolicy updatePolicy) {
263+
this.updatePolicy = updatePolicy;
264+
updatePolicyToContentVersion();
265+
}
266+
private void updatePolicyToContentVersion() {
267+
if (updatePolicy == null) {
268+
return;
269+
}
270+
switch (updatePolicy) {
271+
case VERSION_REQUIRED: contentVersions = Policy.REQUIRED; break;
272+
case VERSION_OPTIONAL: contentVersions = Policy.OPTIONAL; break;
273+
case MERGE_METADATA: contentVersions = Policy.NONE; break;
274+
}
275+
}
276+
241277
@Override
242278
public Policy getContentVersionRequests() {
243279
return contentVersions;
244280
}
245281
@Override
246282
public void setContentVersionRequests(Policy policy) {
247283
contentVersions = policy;
284+
contentVersionToUpdatePolicy();
285+
}
286+
private void contentVersionToUpdatePolicy() {
287+
if (contentVersions == null) {
288+
return;
289+
}
290+
switch (contentVersions) {
291+
case REQUIRED: updatePolicy = UpdatePolicy.VERSION_REQUIRED; break;
292+
case OPTIONAL: updatePolicy = UpdatePolicy.VERSION_OPTIONAL; break;
293+
case NONE: updatePolicy = UpdatePolicy.MERGE_METADATA; break;
294+
}
248295
}
249296

250297
@Override

src/test/java/com/marklogic/client/test/ConditionalDocumentTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@
2727
import org.xml.sax.SAXException;
2828

2929
import com.marklogic.client.DatabaseClient;
30+
import com.marklogic.client.FailedRequestException;
3031
import com.marklogic.client.ForbiddenUserException;
3132
import com.marklogic.client.ResourceNotFoundException;
3233
import com.marklogic.client.ResourceNotResendableException;
33-
import com.marklogic.client.document.DocumentDescriptor;
34-
import com.marklogic.client.FailedRequestException;
35-
import com.marklogic.client.io.Format;
3634
import com.marklogic.client.admin.ServerConfigurationManager;
37-
import com.marklogic.client.admin.ServerConfigurationManager.Policy;
35+
import com.marklogic.client.admin.ServerConfigurationManager.UpdatePolicy;
36+
import com.marklogic.client.document.DocumentDescriptor;
3837
import com.marklogic.client.document.XMLDocumentManager;
3938
import com.marklogic.client.impl.FailedRequest;
39+
import com.marklogic.client.io.Format;
4040
import com.marklogic.client.io.StringHandle;
4141

4242
public class ConditionalDocumentTest {
@@ -49,7 +49,7 @@ public static void beforeClass()
4949
Common.connectAdmin();
5050
serverConfig = Common.client.newServerConfigManager();
5151
serverConfig.readConfiguration();
52-
serverConfig.setContentVersionRequests(Policy.REQUIRED);
52+
serverConfig.setUpdatePolicy(UpdatePolicy.VERSION_REQUIRED);
5353
serverConfig.writeConfiguration();
5454
adminClient = Common.client;
5555
Common.release();
@@ -60,7 +60,7 @@ public static void beforeClass()
6060
public static void afterClass()
6161
throws FailedRequestException, ResourceNotFoundException, ResourceNotResendableException, ForbiddenUserException {
6262
Common.release();
63-
serverConfig.setContentVersionRequests(Policy.NONE);
63+
serverConfig.setUpdatePolicy(UpdatePolicy.MERGE_METADATA);
6464
serverConfig.writeConfiguration();
6565
adminClient.release();
6666
}

0 commit comments

Comments
 (0)