Skip to content

Commit 59e0680

Browse files
committed
feat: add support for map:size function
1 parent a5b73e0 commit 59e0680

File tree

3 files changed

+75
-6
lines changed

3 files changed

+75
-6
lines changed

src/main/java/org/rumbledb/context/BuiltinFunctionCatalogue.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
import org.rumbledb.runtime.functions.maps.MapGetFunctionIterator;
9797
import org.rumbledb.runtime.functions.maps.MapEntryFunctionIterator;
9898
import org.rumbledb.runtime.functions.maps.MapKeysFunctionIterator;
99+
import org.rumbledb.runtime.functions.maps.MapSizeFunctionIterator;
99100
import org.rumbledb.runtime.functions.object.ObjectAccumulateFunctionIterator;
100101
import org.rumbledb.runtime.functions.object.ObjectDescendantFunctionIterator;
101102
import org.rumbledb.runtime.functions.object.ObjectDescendantPairsFunctionIterator;
@@ -2786,6 +2787,21 @@ private static BuiltinFunction createBuiltinFunction(
27862787
MapKeysFunctionIterator.class,
27872788
BuiltinFunction.BuiltinFunctionExecutionMode.LOCAL
27882789
);
2790+
2791+
/**
2792+
* W3C map:size — F&O 3.1: map:size($map as map(*)) as xs:integer.
2793+
*/
2794+
static final BuiltinFunction map_size = createBuiltinFunction(
2795+
new Name(
2796+
Name.MAP_NS,
2797+
"map",
2798+
"size"
2799+
),
2800+
"map",
2801+
"integer",
2802+
MapSizeFunctionIterator.class,
2803+
BuiltinFunction.BuiltinFunctionExecutionMode.LOCAL
2804+
);
27892805
/**
27902806
* function that returns the JSON null
27912807
*/
@@ -3769,8 +3785,9 @@ private static BuiltinFunction createBuiltinFunction(
37693785
builtinFunctions.put(keys.getIdentifier(), keys);
37703786
builtinFunctions.put(members.getIdentifier(), members);
37713787
builtinFunctions.put(map_get.getIdentifier(), map_get);
3772-
builtinFunctions.put(map_entry.getIdentifier(), map_entry);
3788+
builtinFunctions.put(map_entry.getIdentifier(), map_entry);
37733789
builtinFunctions.put(map_keys.getIdentifier(), map_keys);
3790+
builtinFunctions.put(map_size.getIdentifier(), map_size);
37743791
builtinFunctions.put(null_function.getIdentifier(), null_function);
37753792
builtinFunctions.put(size.getIdentifier(), size);
37763793
builtinFunctions.put(array_size.getIdentifier(), array_size);

src/main/java/org/rumbledb/runtime/functions/maps/MapEntryFunctionIterator.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
/**
1818
* W3C XPath/XQuery {@code map:entry}:
1919
* <ul>
20-
* <li>atomizes a single key into exactly one atomic value</li>
21-
* <li>materializes the value into a general sequence (possibly empty)</li>
22-
* <li>returns a map containing a single key/value binding</li>
20+
* <li>atomizes a single key into exactly one atomic value</li>
21+
* <li>materializes the value into a general sequence (possibly empty)</li>
22+
* <li>returns a map containing a single key/value binding</li>
2323
* </ul>
2424
*/
2525
public class MapEntryFunctionIterator extends AtMostOneItemLocalRuntimeIterator {
@@ -79,12 +79,13 @@ public Item materializeFirstItemOrNull(DynamicContext dynamicContext) {
7979
List<Item> valueSequence = new ArrayList<>();
8080
this.valueIterator.materialize(dynamicContext, valueSequence);
8181

82-
return ItemFactory.getInstance().createMapItem(
82+
return ItemFactory.getInstance()
83+
.createMapItem(
8384
Collections.singletonList(key),
8485
Collections.singletonList(valueSequence),
8586
getMetadata(),
8687
false
87-
);
88+
);
8889
}
8990
}
9091

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.rumbledb.runtime.functions.maps;
18+
19+
import org.rumbledb.api.Item;
20+
import org.rumbledb.context.DynamicContext;
21+
import org.rumbledb.context.RuntimeStaticContext;
22+
import org.rumbledb.items.ItemFactory;
23+
import org.rumbledb.runtime.AtMostOneItemLocalRuntimeIterator;
24+
import org.rumbledb.runtime.RuntimeIterator;
25+
26+
import java.util.List;
27+
28+
/**
29+
* W3C map:size function.
30+
*/
31+
public class MapSizeFunctionIterator extends AtMostOneItemLocalRuntimeIterator {
32+
33+
private static final long serialVersionUID = 1L;
34+
35+
public MapSizeFunctionIterator(
36+
List<RuntimeIterator> arguments,
37+
RuntimeStaticContext staticContext
38+
) {
39+
super(arguments, staticContext);
40+
}
41+
42+
@Override
43+
public Item materializeFirstItemOrNull(DynamicContext context) {
44+
Item map = this.children.get(0).materializeFirstItemOrNull(context);
45+
if (map == null) {
46+
return null;
47+
}
48+
return ItemFactory.getInstance().createIntItem(map.getItemKeys().size());
49+
}
50+
}
51+

0 commit comments

Comments
 (0)