17
17
18
18
package nextflow.data.cid
19
19
20
- import groovy.json.JsonSlurper
21
20
import groovy.transform.CompileStatic
22
21
import groovy.util.logging.Slf4j
23
22
@@ -27,7 +26,6 @@ import java.nio.file.Files
27
26
import java.nio.file.Path
28
27
import java.nio.file.attribute.BasicFileAttributes
29
28
30
- import nextflow.data.cid.fs.CidPath
31
29
import nextflow.data.cid.serde.CidEncoder
32
30
import nextflow.data.cid.serde.CidSerializable
33
31
import nextflow.data.config.DataConfig
@@ -84,7 +82,7 @@ class DefaultCidStore implements CidStore {
84
82
final path = metaLocation. resolve(" $key /$METADATA_FILE " )
85
83
log. debug(" Loading from path $path " )
86
84
if (path. exists())
87
- return encoder. decode(path. text)
85
+ return encoder. decode(path. text) as CidSerializable
88
86
log. debug(" File for key $key not found" )
89
87
return null
90
88
}
@@ -107,26 +105,18 @@ class DefaultCidStore implements CidStore {
107
105
void close () throws IOException { }
108
106
109
107
@Override
110
- List<Object > query ( URI uri ) {
111
- log . debug( " Query $u ri . query , Path: $u ri . path , Scheme: $u ri . path $u ri . authority $u ri . rawPath $u ri . rawAuthority " )
108
+ List<CidSerializable > search ( String queryString ) {
109
+
112
110
def params = null
113
- if (uri. query) {
114
- params = uri. query. split(' &' ). collectEntries {
115
- it. split(' =' ). collect { URLDecoder . decode(it, ' UTF-8' ) }
116
- } as Map<String , String >
117
- }
118
- String key = uri. authority ? uri. authority + uri. path : uri. path
119
- if (key == CidPath . SEPARATOR ) {
120
- searchAllFiles(params)
121
- } else {
122
- searchPath(key, params)
111
+ if (queryString) {
112
+ params = CidUtils . parseQuery(queryString)
123
113
}
114
+ return searchAllFiles(params)
124
115
125
116
}
126
117
127
- private List<Object > searchAllFiles (Map<String ,String > params ) {
128
- final results = new LinkedList<Object > ()
129
- final slurper = new JsonSlurper ()
118
+ private List<CidSerializable > searchAllFiles (Map<String ,String > params ) {
119
+ final results = new LinkedList<CidSerializable > ()
130
120
131
121
Files . walkFileTree(metaLocation, new FileVisitor<Path > () {
132
122
@@ -137,10 +127,11 @@ class DefaultCidStore implements CidStore {
137
127
138
128
@Override
139
129
FileVisitResult visitFile (Path file , BasicFileAttributes attrs ) throws IOException {
140
-
141
130
if (file. name. startsWith(' .data.json' ) ) {
142
- final cidObject = slurper. parse( file. text. toCharArray() ) as Map
143
- DefaultCidStore . treatObject(cidObject, params, results)
131
+ final cidObject = encoder. decode(file. text)
132
+ if (CidUtils . checkParams(cidObject, params)){
133
+ results. add(cidObject as CidSerializable )
134
+ }
144
135
}
145
136
FileVisitResult . CONTINUE
146
137
}
@@ -158,62 +149,4 @@ class DefaultCidStore implements CidStore {
158
149
159
150
return results
160
151
}
161
-
162
- private List<Object > searchPath ( String path , Map<String ,String > params , String [] childs = []) {
163
- final results = new LinkedList<Object > ()
164
- final slurper = new JsonSlurper ()
165
- final object = load(path)
166
- if ( object ) {
167
- final cidObject = slurper. parse(object. toString(). toCharArray()) as Map
168
- if (childs && childs. size() > 0 ) {
169
- final output = cidObject. navigate(childs. join(' .' ))
170
- if (output) {
171
- treatObject(output, params, results)
172
- } else {
173
- throw new FileNotFoundException (" Cid object $path /${ childs ? childs.join('/') : ''} not found." )
174
- }
175
- } else {
176
- treatObject(cidObject, params, results)
177
- }
178
- } else {
179
- // If there isn't metadata check the parent to check if it is a subfolder of a task/workflow output
180
- final currentPath = Path . of(path)
181
- final parent = currentPath. getParent()
182
- if ( parent) {
183
- ArrayList<String > newChilds = new ArrayList<String > ()
184
- newChilds. add(currentPath. getFileName(). toString())
185
- newChilds. addAll(childs)
186
- return searchPath(parent. toString(), params, newChilds as String [])
187
- } else {
188
- throw new FileNotFoundException (" Cid object $path /${ childs ? childs.join('/') :''} not found." )
189
- }
190
- }
191
- return results
192
- }
193
-
194
- protected static void treatObject (def object , Map<String ,String > params , List<Object > results ) {
195
- if (params) {
196
- if (object instanceof Collection ) {
197
- (object as Collection ). forEach { treatObject(it, params, results) }
198
- } else if (checkParams(object as Map , params)) {
199
- results. add(object)
200
- }
201
- } else {
202
- results. add(object)
203
- }
204
- }
205
-
206
- private static boolean checkParams (Map object , Map<String ,String > params ) {
207
- log. debug(" Checking $object , $params " )
208
- for (final entry : params. entrySet()) {
209
- final value = object. navigate(entry. key)
210
- log. debug(" comparing $value , $entry . value " )
211
- if (! value || value. toString() != entry. value. toString() ) {
212
- return false
213
- }
214
- }
215
- log. debug(" Returning true" )
216
- return true
217
- }
218
-
219
152
}
0 commit comments