Skip to content

Commit 4aeb678

Browse files
author
ehennum
committed
#1294 client interface for generate view and redaction on rows
1 parent 883f179 commit 4aeb678

File tree

8 files changed

+693
-5
lines changed

8 files changed

+693
-5
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/expression/PlanBuilder.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*/
2727
public abstract class PlanBuilder implements PlanBuilderBase {
2828
protected PlanBuilder(
29-
CtsExpr cts, FnExpr fn, GeoExpr geo, JsonExpr json, MapExpr map, MathExpr math, RdfExpr rdf, SemExpr sem, SpellExpr spell, SqlExpr sql, XdmpExpr xdmp, XsExpr xs
29+
CtsExpr cts, FnExpr fn, GeoExpr geo, JsonExpr json, MapExpr map, MathExpr math, RdfExpr rdf, SemExpr sem, SpellExpr spell, SqlExpr sql, XdmpExpr xdmp, XsExpr xs, RdtExpr rdt
3030
) {
3131
this.cts = cts;
3232
this.fn = fn;
@@ -40,7 +40,7 @@ protected PlanBuilder(
4040
this.sql = sql;
4141
this.xdmp = xdmp;
4242
this.xs = xs;
43-
43+
this.rdt = rdt;
4444
}
4545
/**
4646
* Builds expressions with cts server functions.
@@ -90,7 +90,8 @@ protected PlanBuilder(
9090
* Builds expressions with xs server functions.
9191
*/
9292
public final XsExpr xs;
93-
/**
93+
public final RdtExpr rdt;
94+
/**
9495
* This function returns the sum of the specified numeric expressions. In expressions, the call should pass the result from an op:col function to identify a column.
9596
* <p>
9697
* Provides a client interface to the <a href="http://docs.marklogic.com/op:add" target="mlserverdoc">op:add</a> server function.
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
/*
2+
* Copyright (c) 2021 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.marklogic.client.expression;
17+
18+
import com.marklogic.client.type.PlanColumn;
19+
import com.marklogic.client.type.PlanExprCol;
20+
import com.marklogic.client.type.ServerExpression;
21+
22+
import java.util.Map;
23+
24+
/**
25+
* The RdtExpr instance provides functions that build expressions
26+
* for redacting the values of a column.
27+
* <p>In addition to using the provided functions,
28+
* you can redact any column by using {@link PlanBuilder#as(PlanColumn, ServerExpression)}
29+
* to rebind the column to an expression that replaces the existing value
30+
* with an altered or randomly generated value.
31+
* You can also hide a column by binding the column to the null value
32+
* or by projecting other columns.
33+
* </p>
34+
*/
35+
public interface RdtExpr {
36+
/**
37+
* Redacts a column with string values by replacing each value with deterministic masking text.
38+
* That is, a specific value generates the same masked value every time the value is redacted.
39+
* <p>Provides a client interface to the
40+
* <a href="http://docs.marklogic.com/ordt:mask-deterministic" target="mlserverdoc">ordt:mask-deterministic</a>
41+
* server function.</p>
42+
* @param column the column to be redacted
43+
* @return a PlanExprCol object
44+
*/
45+
PlanExprCol maskDeterministic(PlanColumn column);
46+
/**
47+
* Redacts a column with string values by replacing each value with deterministic masking text.
48+
* That is, a specific value generates the same masked value every time the value is redacted.
49+
* <p>Provides a client interface to the
50+
* <a href="http://docs.marklogic.com/ordt:mask-deterministic" target="mlserverdoc">ordt:mask-deterministic</a>
51+
* server function.</p>
52+
* @param column the column to be redacted
53+
* @param options the options for redacting the column
54+
* @return a PlanExprCol object
55+
*/
56+
PlanExprCol maskDeterministic(PlanColumn column, Map<String,?> options);
57+
/**
58+
* Redacts a column with string values by replacing each value with random masking text.
59+
* The same value may produce a different masked value every time the value is redacted.
60+
* <p>Provides a client interface to the
61+
* <a href="http://docs.marklogic.com/ordt:mask-random" target="mlserverdoc">ordt:mask-random</a>
62+
* server function.</p>
63+
* @param column the column to be redacted
64+
* @return a PlanExprCol object for the redacted column
65+
*/
66+
PlanExprCol maskRandom(PlanColumn column);
67+
/**
68+
* Redacts a column with string values by replacing each value with random masking text.
69+
* The same value may produce a different masked value every time the value is redacted.
70+
* <p>Provides a client interface to the
71+
* <a href="http://docs.marklogic.com/ordt:mask-random" target="mlserverdoc">ordt:mask-random</a>
72+
* server function.</p>
73+
* @param column the column to be redacted
74+
* @param options the options for redacting the column
75+
* @return a PlanExprCol object for the redacted column
76+
*/
77+
PlanExprCol maskRandom(PlanColumn column, Map<String,?> options);
78+
/**
79+
* Redacts a column with date or datetime values either by masking part
80+
* of the existing value or by generating a random value.
81+
* <p>Provides a client interface to the
82+
* <a href="http://docs.marklogic.com/ordt:redact-datetime" target="mlserverdoc">ordt:redact-datetime</a>
83+
* server function.</p>
84+
* @param column the column to be redacted
85+
* @param options the options for redacting the column
86+
* @return a PlanExprCol object for the redacted column
87+
*/
88+
PlanExprCol redactDatetime(PlanColumn column, Map<String,?> options);
89+
/**
90+
* Redacts a column with email address string
91+
* that conforms to the pattern <code>name@domain</code>.
92+
* <p>Provides a client interface to the
93+
* <a href="http://docs.marklogic.com/ordt:redact-email" target="mlserverdoc">ordt:redact-email</a>
94+
* server function.</p>
95+
* @param column the column to be redacted
96+
* @return a PlanExprCol object for the redacted column
97+
*/
98+
PlanExprCol redactEmail(PlanColumn column);
99+
/**
100+
* Redacts a column with email address string
101+
* that conforms to the pattern <code>name@domain</code>.
102+
* <p>Provides a client interface to the
103+
* <a href="http://docs.marklogic.com/ordt:redact-email" target="mlserverdoc">ordt:redact-email</a>
104+
* server function.</p>
105+
* @param column the column to be redacted
106+
* @param options the options for redacting the column
107+
* @return a PlanExprCol object for the redacted column
108+
*/
109+
PlanExprCol redactEmail(PlanColumn column, Map<String,?> options);
110+
/**
111+
* Redacts a column with IPv4 address string that conforms to a pattern with
112+
* four blocks of 1-3 decimal digits separated by period (.) where the value of each block
113+
* of digits is less than or equal to 255 as in <code>123.201.098.112</code> and
114+
* <code>123.45.678.0</code>.
115+
* <p>Provides a client interface to the
116+
* <a href="http://docs.marklogic.com/ordt:redact-ipv4" target="mlserverdoc">ordt:redact-ipv4</a>
117+
* server function.</p>
118+
* @param column the column to be redacted
119+
* @return a PlanExprCol object for the redacted column
120+
*/
121+
PlanExprCol redactIpv4(PlanColumn column);
122+
/**
123+
* Redacts a column with IPv4 address string that conforms to a pattern with
124+
* four blocks of 1-3 decimal digits separated by period (.) where the value of each block
125+
* of digits is less than or equal to 255 as in <code>123.201.098.112</code> and
126+
* <code>123.45.678.0</code>.
127+
* <p>Provides a client interface to the
128+
* <a href="http://docs.marklogic.com/ordt:redact-ipv4" target="mlserverdoc">ordt:redact-ipv4</a>
129+
* server function.</p>
130+
* @param column the column to be redacted
131+
* @param options the options for redacting the column
132+
* @return a PlanExprCol object for the redacted column
133+
*/
134+
PlanExprCol redactIpv4(PlanColumn column, Map<String,?> options);
135+
/**
136+
* Redacts a column by generating a random number within a configurable range
137+
* either as a numeric data type or as a formatted string.
138+
* <p>Provides a client interface to the
139+
* <a href="http://docs.marklogic.com/ordt:redact-number" target="mlserverdoc">ordt:redact-number</a>
140+
* server function.</p>
141+
* @param column the column to be redacted
142+
* @return a PlanExprCol object for the redacted column
143+
*/
144+
PlanExprCol redactNumber(PlanColumn column);
145+
/**
146+
* Redacts a column by generating a random number within a configurable range
147+
* either as a numeric data type or as a formatted string.
148+
* <p>Provides a client interface to the
149+
* <a href="http://docs.marklogic.com/ordt:redact-number" target="mlserverdoc">ordt:redact-number</a>
150+
* server function.</p>
151+
* @param column the column to be redacted
152+
* @param options the options for redacting the column
153+
* @return a PlanExprCol object for the redacted column
154+
*/
155+
PlanExprCol redactNumber(PlanColumn column, Map<String,?> options);
156+
/**
157+
* Redacts a string column by applying a regular expression.
158+
* <p>Provides a client interface to the
159+
* <a href="http://docs.marklogic.com/ordt:redact-regex" target="mlserverdoc">ordt:redact-regex</a>
160+
* server function.</p>
161+
* @param column the column to be redacted
162+
* @param options the options for redacting the column
163+
* @return a PlanExprCol object for the redacted column
164+
*/
165+
PlanExprCol redactRegex(PlanColumn column, Map<String,?> options);
166+
/**
167+
* Redacts a column with a 10-digit US phone number string
168+
* by generating random numbers or replacing numbers with a masking character.
169+
* <p>Provides a client interface to the
170+
* <a href="http://docs.marklogic.com/ordt:redact-us-phone" target="mlserverdoc">ordt:redact-us-phone</a>
171+
* server function.</p>
172+
* @param column the column to be redacted
173+
* @return a PlanExprCol object for the redacted column
174+
*/
175+
PlanExprCol redactUsPhone(PlanColumn column);
176+
/**
177+
* Redacts a column with a 10-digit US phone number string
178+
* by generating random numbers or replacing numbers with a masking character.
179+
* <p>Provides a client interface to the
180+
* <a href="http://docs.marklogic.com/ordt:redact-us-phone" target="mlserverdoc">ordt:redact-us-phone</a>
181+
* server function.</p>
182+
* @param column the column to be redacted
183+
* @param options the options for redacting the column
184+
* @return a PlanExprCol object for the redacted column
185+
*/
186+
PlanExprCol redactUsPhone(PlanColumn column, Map<String,?> options);
187+
/**
188+
* Redacts a column with a 9-digit US SSN (Social Security Number) string
189+
* by generating random numbers or replacing numbers with a masking character.
190+
* <p>Provides a client interface to the
191+
* <a href="http://docs.marklogic.com/ordt:redact-us-ssn" target="mlserverdoc">ordt:redact-us-ssn</a>
192+
* server function.</p>
193+
* @param column the column to be redacted
194+
* @return a PlanExprCol object for the redacted column
195+
*/
196+
PlanExprCol redactUsSsn(PlanColumn column);
197+
/**
198+
* Redacts a column with a 9-digit US SSN (Social Security Number) string
199+
* by generating random numbers or replacing numbers with a masking character.
200+
* <p>Provides a client interface to the
201+
* <a href="http://docs.marklogic.com/ordt:redact-us-ssn" target="mlserverdoc">ordt:redact-us-ssn</a>
202+
* server function.</p>
203+
* @param column the column to be redacted
204+
* @param options the options for redacting the column
205+
* @return a PlanExprCol object for the redacted column
206+
*/
207+
PlanExprCol redactUsSsn(PlanColumn column, Map<String,?> options);
208+
}

marklogic-client-api/src/main/java/com/marklogic/client/impl/PlanBuilderBaseImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ abstract class PlanBuilderBaseImpl extends PlanBuilder {
3737
super(
3838
CtsExprImpl.cts, FnExprImpl.fn, GeoExprImpl.geo, JsonExprImpl.json, MapExprImpl.map,
3939
MathExprImpl.math, RdfExprImpl.rdf, SemExprImpl.sem, SpellExprImpl.spell,
40-
SqlExprImpl.sql, XdmpExprImpl.xdmp, XsExprImpl.xs
40+
SqlExprImpl.sql, XdmpExprImpl.xdmp, XsExprImpl.xs, RdtExprImpl.rdt
4141
);
4242
}
4343

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright (c) 2021 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.marklogic.client.impl;
17+
18+
import com.marklogic.client.expression.RdtExpr;
19+
import com.marklogic.client.type.PlanColumn;
20+
import com.marklogic.client.type.PlanExprCol;
21+
22+
import java.util.Map;
23+
24+
public class RdtExprImpl implements RdtExpr {
25+
final static RdtExprImpl rdt = new RdtExprImpl();
26+
27+
@Override
28+
public PlanExprCol maskDeterministic(PlanColumn column) {
29+
return maskDeterministic(column, null);
30+
}
31+
@Override
32+
public PlanExprCol maskDeterministic(PlanColumn column, Map<String,?> options) {
33+
return redactImpl("maskDeterministic", "mask-deterministic", column, options);
34+
}
35+
@Override
36+
public PlanExprCol maskRandom(PlanColumn column) {
37+
return maskRandom(column, null);
38+
}
39+
@Override
40+
public PlanExprCol maskRandom(PlanColumn column, Map<String,?> options) {
41+
return redactImpl("maskRandom", "mask-random", column, options);
42+
}
43+
@Override
44+
public PlanExprCol redactDatetime(PlanColumn column, Map<String,?> options) {
45+
if (column == null) {
46+
throw new IllegalArgumentException("must provide options for redactDatetime()");
47+
}
48+
return redactImpl("redactDatetime", "redact-datetime", column, options);
49+
}
50+
@Override
51+
public PlanExprCol redactEmail(PlanColumn column) {
52+
return redactEmail(column, null);
53+
}
54+
@Override
55+
public PlanExprCol redactEmail(PlanColumn column, Map<String,?> options) {
56+
return redactImpl("redactEmail", "redact-email", column, options);
57+
}
58+
@Override
59+
public PlanExprCol redactIpv4(PlanColumn column) {
60+
return redactIpv4(column, null);
61+
}
62+
@Override
63+
public PlanExprCol redactIpv4(PlanColumn column, Map<String,?> options) {
64+
return redactImpl("redactIpv4", "redact-ipv4", column, options);
65+
}
66+
@Override
67+
public PlanExprCol redactNumber(PlanColumn column) {
68+
return redactNumber(column, null);
69+
}
70+
@Override
71+
public PlanExprCol redactNumber(PlanColumn column, Map<String,?> options) {
72+
return redactImpl("redactNumber", "redact-number", column, options);
73+
}
74+
@Override
75+
public PlanExprCol redactRegex(PlanColumn column, Map<String,?> options) {
76+
if (column == null) {
77+
throw new IllegalArgumentException("must provide options for redactRegex()");
78+
}
79+
return redactImpl("redactRegex", "redact-regex", column, options);
80+
}
81+
@Override
82+
public PlanExprCol redactUsPhone(PlanColumn column) {
83+
return redactUsPhone(column, null);
84+
}
85+
@Override
86+
public PlanExprCol redactUsPhone(PlanColumn column, Map<String,?> options) {
87+
return redactImpl("redactUsPhone", "redact-us-phone", column, options);
88+
}
89+
@Override
90+
public PlanExprCol redactUsSsn(PlanColumn column) {
91+
return redactUsSsn(column, null);
92+
}
93+
@Override
94+
public PlanExprCol redactUsSsn(PlanColumn column, Map<String,?> options) {
95+
return redactImpl("redactUsSsn", "redact-us-ssn", column, options);
96+
}
97+
private PlanExprCol redactImpl(String clientFn, String enodeFn, PlanColumn column, Map<String,?> options) {
98+
if (column == null) {
99+
throw new IllegalArgumentException("must provide column to redact for "+clientFn+"()");
100+
}
101+
return new RedactCallImpl(enodeFn, new Object[]{column, (options == null) ? null : new BaseTypeImpl.BaseMapImpl(options)});
102+
}
103+
static class RedactCallImpl extends PlanBuilderSubImpl.ExprColCallImpl {
104+
RedactCallImpl(String fnName, Object[] fnArgs) {
105+
super("ordt", fnName, fnArgs);
106+
}
107+
}
108+
}

marklogic-client-api/src/main/java/com/marklogic/client/impl/RowManagerImpl.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,36 @@ public <T> T explainAs(Plan plan, Class<T> as) {
255255
return handle.get();
256256
}
257257

258+
@Override
259+
public <T extends XMLReadHandle> T generateView(Plan plan, String schema, String view, T resultsHandle) {
260+
if (resultsHandle == null) {
261+
throw new IllegalArgumentException("Must specify a handle to generate a view for the plan");
262+
} else if (schema == null || schema.length() == 0) {
263+
throw new IllegalArgumentException("Must specify a schema name to generate a view for the plan");
264+
} else if (view == null || view.length() == 0) {
265+
throw new IllegalArgumentException("Must specify a view name to generate a view for the plan");
266+
}
267+
268+
PlanBuilderBaseImpl.RequestPlan requestPlan = checkPlan(plan);
269+
AbstractWriteHandle astHandle = requestPlan.getHandle();
270+
271+
RequestParameters params = new RequestParameters();
272+
params.add("output", "generateView");
273+
params.add("schemaName", "schema");
274+
params.add("viewName", "view");
275+
276+
return services.postResource(requestLogger, "rows", null, params, astHandle, resultsHandle);
277+
}
278+
@Override
279+
public <T> T generateViewAs(Plan plan, String schema, String view, Class<T> as) {
280+
ContentHandle<T> handle = handleFor(as);
281+
if (generateView(plan, schema, view, (XMLReadHandle) handle) == null) {
282+
return null;
283+
}
284+
285+
return handle.get();
286+
}
287+
258288
private void addDatatypeStyleParam(RequestParameters params, RowSetPart datatypeStyle) {
259289
if (datatypeStyle != null) {
260290
switch (datatypeStyle) {

0 commit comments

Comments
 (0)