camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acosent...@apache.org
Subject camel git commit: CAMEL-8735 Camel-Hazelcast: Add putIfAbsent operation to map producer
Date Sun, 03 May 2015 14:05:37 GMT
Repository: camel
Updated Branches:
  refs/heads/master e100e0fb6 -> c8b617940


CAMEL-8735 Camel-Hazelcast: Add putIfAbsent operation to map producer


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c8b61794
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c8b61794
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c8b61794

Branch: refs/heads/master
Commit: c8b6179407c6b88bb937540859092ab3a2ab4594
Parents: e100e0f
Author: Andrea Cosentino <ancosen@gmail.com>
Authored: Sun May 3 16:03:23 2015 +0200
Committer: Andrea Cosentino <ancosen@gmail.com>
Committed: Sun May 3 16:03:23 2015 +0200

----------------------------------------------------------------------
 .../component/hazelcast/HazelcastConstants.java |  1 +
 .../hazelcast/map/HazelcastMapProducer.java     | 28 ++++++++++++++++++--
 .../hazelcast/HazelcastMapProducerTest.java     | 21 +++++++++++++++
 3 files changed, 48 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/c8b61794/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastConstants.java
----------------------------------------------------------------------
diff --git a/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastConstants.java
b/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastConstants.java
index ecc63e3..056caa5 100644
--- a/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastConstants.java
+++ b/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastConstants.java
@@ -60,6 +60,7 @@ public final class HazelcastConstants {
     public static final int QUERY_OPERATION = 5;
     public static final int GET_ALL_OPERATION = 6;
     public static final int CLEAR_OPERATION = 7;
+    public static final int PUT_IF_ABSENT_OPERATION = 8;
 
     // multimap
     public static final int REMOVEVALUE_OPERATION = 10;

http://git-wip-us.apache.org/repos/asf/camel/blob/c8b61794/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/map/HazelcastMapProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/map/HazelcastMapProducer.java
b/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/map/HazelcastMapProducer.java
index d0911ff..75de1d2 100644
--- a/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/map/HazelcastMapProducer.java
+++ b/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/map/HazelcastMapProducer.java
@@ -64,7 +64,7 @@ public class HazelcastMapProducer extends HazelcastDefaultProducer {
         }
 
         if (headers.containsKey(HazelcastConstants.TTL_UNIT)) {
-        	ttlUnit = headers.get(HazelcastConstants.TTL_UNIT);
+            ttlUnit = headers.get(HazelcastConstants.TTL_UNIT);
         }
         
         if (headers.containsKey(HazelcastConstants.QUERY)) {
@@ -78,7 +78,15 @@ public class HazelcastMapProducer extends HazelcastDefaultProducer {
             if (ObjectHelper.isEmpty(ttl) && ObjectHelper.isEmpty(ttlUnit)) {
                 this.put(oid, exchange);
             } else {
-                this.put(oid, ttl, ttlUnit, exchange);            	
+                this.put(oid, ttl, ttlUnit, exchange);
+            }
+            break;
+            
+        case HazelcastConstants.PUT_IF_ABSENT_OPERATION:
+            if (ObjectHelper.isEmpty(ttl) && ObjectHelper.isEmpty(ttlUnit)) {
+                this.putIfAbsent(oid, exchange);
+            } else {
+                this.putIfAbsent(oid, ttl, ttlUnit, exchange);
             }
             break;
 
@@ -190,6 +198,22 @@ public class HazelcastMapProducer extends HazelcastDefaultProducer {
     }
     
     /**
+     * if the specified key is not already associated with a value, associate it with the
given value.
+     */
+    private void putIfAbsent(Object oid, Exchange exchange) {
+        Object body = exchange.getIn().getBody();
+        this.cache.putIfAbsent(oid, body);
+    }
+    
+    /**
+     * Puts an entry into this map with a given ttl (time to live) value if the specified
key is not already associated with a value.
+     */
+    private void putIfAbsent(Object oid, Object ttl, Object ttlUnit, Exchange exchange) {
+        Object body = exchange.getIn().getBody();
+        this.cache.putIfAbsent(oid, body, (long) ttl, (TimeUnit) ttlUnit);
+    }
+    
+    /**
      * Clear all the entries
      */
     private void clear(Exchange exchange) {

http://git-wip-us.apache.org/repos/asf/camel/blob/c8b61794/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastMapProducerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastMapProducerTest.java
b/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastMapProducerTest.java
index 2879843..a821303 100644
--- a/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastMapProducerTest.java
+++ b/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastMapProducerTest.java
@@ -180,6 +180,24 @@ public class HazelcastMapProducerTest extends HazelcastCamelTestSupport
implemen
     }
     
     @Test
+    public void testPutIfAbsent() throws InterruptedException {
+        Map<String, Object> headers = new HashMap<String, Object>();
+        headers.put(HazelcastConstants.OBJECT_ID, "4711");
+        template.sendBodyAndHeaders("direct:putIfAbsent", "replaced", headers);
+        verify(map).putIfAbsent("4711", "replaced");
+    }
+    
+    @Test
+    public void testPutIfAbsentWithTtl() throws InterruptedException {
+        Map<String, Object> headers = new HashMap<String, Object>();
+        headers.put(HazelcastConstants.OBJECT_ID, "4711");
+        headers.put(HazelcastConstants.TTL_VALUE, new Long(1));
+        headers.put(HazelcastConstants.TTL_UNIT, TimeUnit.MINUTES);
+        template.sendBodyAndHeaders("direct:putIfAbsent", "replaced", headers);
+        verify(map).putIfAbsent("4711", "replaced", new Long(1), TimeUnit.MINUTES);
+    }
+    
+    @Test
     public void testClear() throws InterruptedException {
         template.sendBody("direct:clear", "test");
         verify(map).clear();
@@ -194,6 +212,9 @@ public class HazelcastMapProducerTest extends HazelcastCamelTestSupport
implemen
                 from("direct:putInvalid").setHeader(HazelcastConstants.OPERATION, constant("bogus")).to(String.format("hazelcast:%sfoo",
HazelcastConstants.MAP_PREFIX));
 
                 from("direct:put").setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.PUT_OPERATION)).to(String.format("hazelcast:%sfoo",
HazelcastConstants.MAP_PREFIX));
+                
+                from("direct:putIfAbsent").setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.PUT_IF_ABSENT_OPERATION))
+                         .to(String.format("hazelcast:%sfoo", HazelcastConstants.MAP_PREFIX));
 
                 from("direct:update").setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.UPDATE_OPERATION)).to(String.format("hazelcast:%sfoo",
HazelcastConstants.MAP_PREFIX));
 


Mime
View raw message