Skip to content

Commit af32312

Browse files
committed
Merge pull request #59 from paxtonhare/5_spec_for_plugins
more changes to the plugin signature
2 parents da3b6d5 + c61ff5a commit af32312

File tree

14 files changed

+109
-92
lines changed

14 files changed

+109
-92
lines changed

data-hub/src/main/java/com/marklogic/hub/Mlcp.java

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,23 @@ public class Mlcp {
3030
private static final Logger LOGGER = LoggerFactory.getLogger(Mlcp.class);
3131

3232
private List<MlcpSource> sources = new ArrayList<>();
33-
33+
3434
private String mlcpPath;
35-
35+
3636
private String host;
37-
37+
3838
private String port;
39-
39+
4040
private String user;
41-
41+
4242
private String password;
43-
43+
4444
public Mlcp(String mlcpHome, String host, String port, String user, String password) {
4545
this.host = host;
4646
this.port = port;
4747
this.user = user;
4848
this.password = password;
49-
49+
5050
// set the mlcp executable path based on OS
5151
this.mlcpPath = mlcpHome;
5252
String osName = System.getProperty("os.name");
@@ -57,19 +57,19 @@ public Mlcp(String mlcpHome, String host, String port, String user, String passw
5757
mlcpPath += "/bin/mlcp.sh";
5858
}
5959
}
60-
60+
6161
public void addSourceDirectory(String directoryPath, SourceOptions options) {
6262
MlcpSource source = new MlcpSource(directoryPath, options);
6363
sources.add(source);
6464
}
65-
65+
6666
public void loadContent() {
6767
for (MlcpSource source : sources) {
6868
Thread inputThread = null;
6969
Thread errorThread = null;
7070
try {
7171
List<String> arguments = new ArrayList<>();
72-
72+
7373
arguments.add(mlcpPath);
7474
arguments.add("import");
7575
arguments.add("-mode");
@@ -82,20 +82,20 @@ public void loadContent() {
8282
arguments.add(user);
8383
arguments.add("-password");
8484
arguments.add(password);
85-
85+
8686
// add arguments related to the source
8787
List<String> sourceArguments = source.getMlcpArguments();
8888
arguments.addAll(sourceArguments);
89-
89+
9090
ProcessBuilder pb = new ProcessBuilder(arguments.toArray(new String[0]));
9191
Process process = pb.start();
92-
92+
9393
inputThread = IOUtil.createInputStreamSink(process.getInputStream(), LOGGER, LogLevel.DEBUG);
9494
errorThread = IOUtil.createInputStreamSink(process.getErrorStream(), LOGGER, LogLevel.ERROR);
95-
95+
9696
inputThread.start();
9797
errorThread.start();
98-
98+
9999
process.waitFor();
100100
}
101101
catch (Exception e) {
@@ -111,24 +111,24 @@ public void loadContent() {
111111
}
112112
}
113113
}
114-
114+
115115
private static class MlcpSource {
116116
private String sourcePath;
117117
private SourceOptions sourceOptions;
118-
118+
119119
public MlcpSource(String sourcePath, SourceOptions sourceOptions) {
120120
this.sourcePath = sourcePath;
121121
this.sourceOptions = sourceOptions;
122122
}
123-
123+
124124
public String getSourcePath() {
125125
return sourcePath;
126126
}
127-
127+
128128
public List<String> getMlcpArguments() throws IOException {
129129
File file = new File(sourcePath);
130130
String canonicalPath = file.getCanonicalPath();
131-
131+
132132
List<String> arguments = new ArrayList<>();
133133
arguments.add("-input_file_path");
134134
arguments.add(canonicalPath);
@@ -139,71 +139,70 @@ public List<String> getMlcpArguments() throws IOException {
139139
else {
140140
arguments.add(sourceOptions.getInputFileType());
141141
}
142-
142+
143143
if (sourceOptions.getInputFilePattern() != null) {
144144
arguments.add("-input_file_pattern");
145145
arguments.add(sourceOptions.getInputFilePattern());
146146
}
147-
147+
148148
// by default, cut the source directory path to make URIs shorter
149-
String uriReplace = "/" + canonicalPath + ",''";
149+
String uriReplace = canonicalPath + ",''";
150150
uriReplace = uriReplace.replaceAll("\\\\", "/");
151-
151+
152152
arguments.add("-output_uri_replace");
153-
arguments.add(uriReplace);
154-
153+
arguments.add("\"" + uriReplace + "\"");
154+
155155
arguments.add("-transform_module");
156156
arguments.add("/com.marklogic.hub/mlcp-flow-transform.xqy");
157157
arguments.add("-transform_namespace");
158158
arguments.add("http://marklogic.com/hub-in-a-box/mlcp-flow-transform");
159159
arguments.add("-transform_param");
160160
arguments.add("\"" + sourceOptions.getTransformParams() + "\"");
161-
162161
return arguments;
163162
}
164163
}
165-
164+
166165
public static class SourceOptions {
167166
private String domainName;
168167
private String flowName;
169168
private String flowType;
170169
private String inputFileType;
171170
private String inputFilePattern;
172-
171+
173172
public SourceOptions(String domainName, String flowName, String flowType) {
174173
this.domainName = domainName;
175174
this.flowName = flowName;
176175
this.flowType = flowType;
177176
}
178-
177+
179178
public String getDomainName() {
180179
return domainName;
181180
}
182-
181+
183182
public String getFlowName() {
184183
return flowName;
185184
}
186-
185+
187186
public String getFlowType() {
188187
return flowType;
189188
}
190-
189+
191190
public String getInputFileType() {
192191
return inputFileType;
193192
}
194-
193+
195194
public void setInputFileType(String inputFileType) {
196195
this.inputFileType = inputFileType;
197196
}
198-
197+
199198
public String getInputFilePattern() {
200199
return inputFilePattern;
201200
}
202-
201+
203202
public void setInputFilePattern(String inputFilePattern) {
204203
this.inputFilePattern = inputFilePattern;
205204
}
206-
205+
207206
protected String getTransformParams() {
208207
return String.format("<params><domain-name>%s</domain-name><flow-name>%s</flow-name><flow-type>%s</flow-type></params>", domainName, flowName, flowType);
209208
}

data-hub/src/main/resources/ml-modules/root/com.marklogic.hub/lib/flow-lib.xqy

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,23 @@ declare function flow:run-flow(
387387
$flow as element(hub:flow),
388388
$identifier as xs:string,
389389
$options as map:map) as empty-sequence()
390+
{
391+
flow:run-flow($flow, $identifier, (), $options)
392+
};
393+
394+
declare function flow:run-plugins(
395+
$flow as element(hub:flow),
396+
$identifier as xs:string,
397+
$content as node()?,
398+
$options as map:map
399+
) as element(envelope:envelope)
390400
{
391401
let $content :=
392402
map:new((
393-
map:entry("identifier", $identifier)
403+
map:entry("identifier", $identifier),
404+
if ($content) then
405+
map:entry("content", $content)
406+
else ()
394407
))
395408
let $_ :=
396409
xdmp:invoke-function(function() {
@@ -413,7 +426,17 @@ declare function flow:run-flow(
413426
map:entry("isolation", "different-transaction"),
414427
map:entry("transactionMode", "query")
415428
)))
416-
let $envelope := flow:make-envelope($content)
429+
return
430+
flow:make-envelope($content)
431+
};
432+
433+
declare function flow:run-flow(
434+
$flow as element(hub:flow),
435+
$identifier as xs:string,
436+
$content as node()?,
437+
$options as map:map) as empty-sequence()
438+
{
439+
let $envelope := flow:run-plugins($flow, $identifier, $content, $options)
417440
let $_ :=
418441
for $writer in $flow/hub:writer
419442
return
@@ -452,9 +475,9 @@ declare function flow:make-envelope($map as map:map)
452475
declare function flow:run-plugin(
453476
$plugin as element(hub:plugin),
454477
$identifier as xs:string,
455-
$content as element()?,
456-
$headers as element()*,
457-
$triples as element(sem:triple)*,
478+
$content as node()?,
479+
$headers as node()*,
480+
$triples as sem:triple*,
458481
$options as map:map)
459482
{
460483
let $module-uri := $plugin/@module
@@ -483,7 +506,7 @@ declare function flow:run-plugin(
483506
declare function flow:run-writer(
484507
$writer as element(hub:writer),
485508
$identifier as xs:string,
486-
$envelope as element(),
509+
$envelope as element(envelope:envelope),
487510
$options as map:map)
488511
{
489512
let $module-uri as xs:string := $writer/@module

data-hub/src/main/resources/ml-modules/root/com.marklogic.hub/mlcp-flow-transform.xqy

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,26 @@ xquery version "1.0-ml";
22

33
module namespace mlcpFlow = "http://marklogic.com/hub-in-a-box/mlcp-flow-transform";
44

5-
import module namespace flowLib = "http://marklogic.com/hub-in-a-box/flow-lib" at "/com.marklogic.hub/lib/flow-lib.xqy";
5+
import module namespace flow = "http://marklogic.com/hub-in-a-box/flow-lib"
6+
at "/com.marklogic.hub/lib/flow-lib.xqy";
67

78
declare function mlcpFlow:transform(
89
$content as map:map,
910
$context as map:map
1011
) as map:map*
1112
{
1213
let $uri := map:get($content, "uri")
13-
let $_ := xdmp:log('mlcp-flow-transform received: ' || $uri)
14-
14+
1515
let $paramNodes := xdmp:unquote(map:get($context, 'transform_param'))/node()/*
1616
let $paramMap := map:new()
1717
let $_ := $paramNodes ! map:put($paramMap, fn:local-name(.), ./string())
18-
19-
let $_ := xdmp:log($paramMap)
20-
21-
let $flow := flowLib:get-flow(map:get($paramMap, 'domain-name'), map:get($paramMap, 'flow-name'), map:get($paramMap, 'flow-type'))
22-
let $_ := xdmp:log('Flow:')
23-
let $_ := xdmp:log($flow)
24-
25-
let $_ := xdmp:log('Running flow with: ' || $uri)
26-
27-
let $flowResult := flowLib:run-flow($flow, $uri, $paramMap)
28-
let $_ := xdmp:log('Flow Result:')
29-
let $_ := xdmp:log($flowResult)
30-
31-
return ()
32-
};
18+
19+
let $flow := flow:get-flow(
20+
map:get($paramMap, 'domain-name'),
21+
map:get($paramMap, 'flow-name'),
22+
map:get($paramMap, 'flow-type'))
23+
24+
let $envelope := flow:run-plugins($flow, $uri, map:get($content, "value"), $paramMap)
25+
return
26+
$content
27+
};

data-hub/src/main/resources/ml-modules/root/com.marklogic.hub/plugins/raw.xqy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ declare option xdmp:mapping "false";
3232
:)
3333
declare function plugin:create-content(
3434
$id as xs:string,
35-
$content as element()?,
36-
$headers as element()*,
35+
$content as node()?,
36+
$headers as node()*,
3737
$triples as element()*,
38-
$options as map:map) as element()?
38+
$options as map:map) as node()?
3939
{
4040
fn:doc($id)/*
4141
};

data-hub/src/main/resources/ml-modules/root/com.marklogic.hub/writers/default.xqy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ declare option xdmp:mapping "false";
2121

2222
declare function plugin:write(
2323
$id as xs:string,
24-
$content as element(),
24+
$content as node(),
2525
$context as map:map)
2626
{
2727
xdmp:document-insert("/conformed" || $id, $content)

data-hub/src/test/resources/flow-manager-test/my-test-flow-with-all/content/content.xqy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ declare option xdmp:mapping "false";
1717
:)
1818
declare function plugin:create-content(
1919
$id as xs:string,
20-
$content as element()?,
21-
$headers as element()*,
20+
$content as node()?,
21+
$headers as node()*,
2222
$triples as element()*,
23-
$options as map:map) as element()?
23+
$options as map:map) as node()?
2424
{
2525
let $source := fn:doc($id)
2626
return

data-hub/src/test/resources/flow-manager-test/my-test-flow-with-all/headers/headers.xqy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ declare option xdmp:mapping "false";
1717
:)
1818
declare function plugin:create-headers(
1919
$id as xs:string,
20-
$content as element()?,
21-
$headers as element()*,
20+
$content as node()?,
21+
$headers as node()*,
2222
$triples as element()*,
23-
$options as map:map) as element()*
23+
$options as map:map) as node()*
2424
{
2525
<testing>123</testing>
2626
};

data-hub/src/test/resources/flow-manager-test/my-test-flow-with-all/triples/triples.xqy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ declare option xdmp:mapping "false";
1717
:)
1818
declare function plugin:create-triples(
1919
$id as xs:string,
20-
$content as element()?,
21-
$headers as element()*,
20+
$content as node()?,
21+
$headers as node()*,
2222
$triples as element()*,
23-
$options as map:map) as element(sem:triple)*
23+
$options as map:map) as sem:triple*
2424
{
2525
document { sem:triple("this-test", "is", "awesome") }/*
2626
};

data-hub/src/test/resources/flow-manager-test/my-test-flow-with-header/content/content.xqy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ declare option xdmp:mapping "false";
1717
:)
1818
declare function plugin:create-content(
1919
$id as xs:string,
20-
$content as element()?,
21-
$headers as element()*,
20+
$content as node()?,
21+
$headers as node()*,
2222
$triples as element()*,
23-
$options as map:map) as element()?
23+
$options as map:map) as node()?
2424
{
2525
fn:doc($id)/*
2626
};

0 commit comments

Comments
 (0)