You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:56:21 UTC

[sling-org-apache-sling-nosql-generic] 18/23: SLING-5024 Sling NoSQL Resource Provider for MongoDB (based on nosql.generic)

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.nosql.generic-1.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-nosql-generic.git

commit a125fa96deb797da42a1146f8273cd77d2604059
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Mon Sep 14 21:19:29 2015 +0000

    SLING-5024 Sling NoSQL Resource Provider for MongoDB (based on nosql.generic)
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/nosql/generic@1703061 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  8 +-
 .../sling/nosql/generic/adapter/MapConverter.java  | 87 ++++++++++++++++++++++
 .../{NoSqlData.java => MultiValueMode.java}        | 31 +++-----
 .../sling/nosql/generic/adapter/NoSqlData.java     | 28 ++++++-
 .../nosql/generic/adapter/MapConverterTest.java    | 79 ++++++++++++++++++++
 5 files changed, 209 insertions(+), 24 deletions(-)

diff --git a/pom.xml b/pom.xml
index a0fdc71..4e82433 100644
--- a/pom.xml
+++ b/pom.xml
@@ -88,6 +88,12 @@
             <scope>provided</scope>
         </dependency>
         <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-lang3</artifactId>
+            <version>3.3.2</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
             <groupId>org.osgi</groupId>
             <artifactId>org.osgi.compendium</artifactId>
             <scope>provided</scope>
@@ -128,7 +134,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.testing.sling-mock</artifactId>
-            <version>1.3.0</version>
+            <version>1.5.0</version>
             <scope>test</scope>
         </dependency>
         <dependency>
diff --git a/src/main/java/org/apache/sling/nosql/generic/adapter/MapConverter.java b/src/main/java/org/apache/sling/nosql/generic/adapter/MapConverter.java
new file mode 100644
index 0000000..a04c8f6
--- /dev/null
+++ b/src/main/java/org/apache/sling/nosql/generic/adapter/MapConverter.java
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.nosql.generic.adapter;
+
+import java.lang.reflect.Array;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.lang3.ArrayUtils;
+
+/**
+ * Transforms NoSqlData maps to a valid form for couchbase JSON document.
+ * All arrays have to be transformed to lists.
+ */
+final class MapConverter {
+
+    private MapConverter() {
+        // static methods only
+    }
+
+    /**
+     * @param map Map with multi-valued arrays
+     * @return Map with multi-valued lists
+     */
+    public static Map<String, Object> mapArrayToList(Map<String, Object> map) {
+        for (Map.Entry<String, Object> entry : map.entrySet()) {
+            if (entry.getValue().getClass().isArray()) {
+                Class componentType = entry.getValue().getClass().getComponentType();
+                if (componentType == int.class) {
+                    entry.setValue(Arrays.asList(ArrayUtils.toObject((int[]) entry.getValue())));
+                }
+                else if (componentType == long.class) {
+                    entry.setValue(Arrays.asList(ArrayUtils.toObject((long[]) entry.getValue())));
+                }
+                else if (componentType == double.class) {
+                    entry.setValue(Arrays.asList(ArrayUtils.toObject((double[]) entry.getValue())));
+                }
+                else if (componentType == boolean.class) {
+                    entry.setValue(Arrays.asList(ArrayUtils.toObject((boolean[]) entry.getValue())));
+                }
+                else {
+                    entry.setValue(Arrays.asList((Object[]) entry.getValue()));
+                }
+            }
+        }
+        return map;
+    }
+
+    /**
+     * @param map Map with multi-valued lists
+     * @return Map with multi-valued arrays
+     */
+    @SuppressWarnings("unchecked")
+    public static Map<String, Object> mapListToArray(Map<String, Object> map) {
+        for (Map.Entry<String, Object> entry : map.entrySet()) {
+            if (entry.getValue() instanceof List) {
+                List list = (List) entry.getValue();
+                if (list.size() == 0) {
+                    entry.setValue(null);
+                }
+                else {
+                    Class type = list.get(0).getClass();
+                    entry.setValue(list.toArray((Object[]) Array.newInstance(type, list.size())));
+                }
+            }
+        }
+        return map;
+    }
+
+}
diff --git a/src/main/java/org/apache/sling/nosql/generic/adapter/NoSqlData.java b/src/main/java/org/apache/sling/nosql/generic/adapter/MultiValueMode.java
similarity index 62%
copy from src/main/java/org/apache/sling/nosql/generic/adapter/NoSqlData.java
copy to src/main/java/org/apache/sling/nosql/generic/adapter/MultiValueMode.java
index 2aef8b4..770c93a 100644
--- a/src/main/java/org/apache/sling/nosql/generic/adapter/NoSqlData.java
+++ b/src/main/java/org/apache/sling/nosql/generic/adapter/MultiValueMode.java
@@ -18,30 +18,19 @@
  */
 package org.apache.sling.nosql.generic.adapter;
 
-import java.util.Map;
-
-import aQute.bnd.annotation.ProviderType;
-
 /**
- * Wrapper for properties of a NoSQL document for a given path.
+ * Mode for multi-valued field data in {@link NoSqlData properties).
  */
-@ProviderType
-public final class NoSqlData {
-
-    private final String path;
-    private final Map<String,Object> properties;
-    
-    public NoSqlData(String path, Map<String, Object> properties) {
-        this.path = path;
-        this.properties = properties;
-    }
+public enum MultiValueMode {
 
-    public String getPath() {
-        return path;
-    }
+    /**
+     * Return multi-valued field values as array (default).
+     */
+    ARRAYS,
     
-    public Map<String, Object> getProperties() {
-        return properties;
-    }
+    /**
+     * Return multi-valued field values as lists.
+     */
+    LISTS
     
 }
diff --git a/src/main/java/org/apache/sling/nosql/generic/adapter/NoSqlData.java b/src/main/java/org/apache/sling/nosql/generic/adapter/NoSqlData.java
index 2aef8b4..0b52038 100644
--- a/src/main/java/org/apache/sling/nosql/generic/adapter/NoSqlData.java
+++ b/src/main/java/org/apache/sling/nosql/generic/adapter/NoSqlData.java
@@ -32,8 +32,21 @@ public final class NoSqlData {
     private final Map<String,Object> properties;
     
     public NoSqlData(String path, Map<String, Object> properties) {
+        this(path, properties, MultiValueMode.ARRAYS);
+    }
+
+    public NoSqlData(String path, Map<String, Object> properties, MultiValueMode multiValueMode) {
         this.path = path;
-        this.properties = properties;
+        switch (multiValueMode) {
+            case ARRAYS:
+                this.properties = properties;
+                break;
+            case LISTS:
+                this.properties = MapConverter.mapListToArray(properties);
+                break;
+            default:
+                throw new IllegalArgumentException("Multi value mode not supported: " + multiValueMode);
+        }
     }
 
     public String getPath() {
@@ -41,7 +54,18 @@ public final class NoSqlData {
     }
     
     public Map<String, Object> getProperties() {
-        return properties;
+        return getProperties(MultiValueMode.ARRAYS);
+    }
+    
+    public Map<String, Object> getProperties(MultiValueMode multiValueMode) {
+        switch (multiValueMode) {
+            case ARRAYS:
+                return properties;
+            case LISTS:
+                return MapConverter.mapArrayToList(properties);
+            default:
+                throw new IllegalArgumentException("Multi value mode not supported: " + multiValueMode);
+        }
     }
     
 }
diff --git a/src/test/java/org/apache/sling/nosql/generic/adapter/MapConverterTest.java b/src/test/java/org/apache/sling/nosql/generic/adapter/MapConverterTest.java
new file mode 100644
index 0000000..32aa7cd
--- /dev/null
+++ b/src/test/java/org/apache/sling/nosql/generic/adapter/MapConverterTest.java
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.nosql.generic.adapter;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+import java.util.Map;
+
+import org.apache.sling.nosql.generic.adapter.MapConverter;
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
+
+public class MapConverterTest {
+
+    @Test
+    public void testMapArrayToList() throws Exception {
+        Map<String, Object> result = MapConverter.mapArrayToList(Maps.newHashMap(ImmutableMap.<String, Object>builder()
+                .put("prop1", "value1")
+                .put("prop2", 2)
+                .put("stringArray", new String[] { "value1", "value2" })
+                .put("integerArray", new Integer[] { 1, 2, 3 })
+                .put("integerArray2", new int[] { 1, 2, 3 })
+                .put("longArray", new long[] { 1L, 2L })
+                .put("doubleArray", new double[] { 1.1d, 1.2d })
+                .put("booleanArray", new boolean[] { true, false })
+                .build()));
+
+        assertEquals("prop1", "value1", result.get("prop1"));
+        assertEquals("prop2", 2, result.get("prop2"));
+        assertEquals("stringArray", ImmutableList.of("value1", "value2"), result.get("stringArray"));
+        assertEquals("integerArray", ImmutableList.of(1, 2, 3), result.get("integerArray"));
+        assertEquals("integerArray2", ImmutableList.of(1, 2, 3), result.get("integerArray2"));
+        assertEquals("longArray", ImmutableList.of(1L, 2L), result.get("longArray"));
+        assertEquals("doubleArray", ImmutableList.of(1.1d, 1.2d), result.get("doubleArray"));
+        assertEquals("booleanArray", ImmutableList.of(true, false), result.get("booleanArray"));
+    }
+
+    @Test
+    public void testMapListToArray() throws Exception {
+        Map<String, Object> result = MapConverter.mapListToArray(Maps.newHashMap(ImmutableMap.<String, Object>builder()
+                .put("prop1", "value1")
+                .put("prop2", 2)
+                .put("stringArray", ImmutableList.of("value1", "value2"))
+                .put("integerArray", ImmutableList.of(1, 2, 3))
+                .put("longArray", ImmutableList.of(1L, 2L))
+                .put("doubleArray", ImmutableList.of(1.1d, 1.2d))
+                .put("booleanArray", ImmutableList.of(true, false))
+                .build()));
+
+        assertEquals("prop1", "value1", result.get("prop1"));
+        assertEquals("prop2", 2, result.get("prop2"));
+        assertArrayEquals("stringArray", new String[] { "value1", "value2" }, (String[]) result.get("stringArray"));
+        assertArrayEquals("integerArray", new Integer[] { 1, 2, 3 }, (Integer[]) result.get("integerArray"));
+        assertArrayEquals("longArray", new Long[] { 1L, 2L }, (Long[]) result.get("longArray"));
+        assertArrayEquals("doubleArray", new Double[] { 1.1d, 1.2d }, (Double[]) result.get("doubleArray"));
+        assertArrayEquals("booleanArray", new Boolean[] { true, false }, (Boolean[]) result.get("booleanArray"));
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.