You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by er...@apache.org on 2011/08/20 12:27:51 UTC

svn commit: r1159864 - in /james/server/trunk/hbase/src: main/java/org/apache/james/rrt/hbase/ main/java/org/apache/james/rrt/hbase/def/ test/java/org/apache/james/ test/java/org/apache/james/rrt/hbase/

Author: eric
Date: Sat Aug 20 10:27:50 2011
New Revision: 1159864

URL: http://svn.apache.org/viewvc?rev=1159864&view=rev
Log:
Fix RRT on HBase (JAMES-1273)

Modified:
    james/server/trunk/hbase/src/main/java/org/apache/james/rrt/hbase/HBaseRecipientRewriteTable.java
    james/server/trunk/hbase/src/main/java/org/apache/james/rrt/hbase/def/HRecipientRewriteTable.java
    james/server/trunk/hbase/src/test/java/org/apache/james/JamesServerHBaseIntegrationTest.java
    james/server/trunk/hbase/src/test/java/org/apache/james/rrt/hbase/HBaseRecipientRewriteTableTest.java

Modified: james/server/trunk/hbase/src/main/java/org/apache/james/rrt/hbase/HBaseRecipientRewriteTable.java
URL: http://svn.apache.org/viewvc/james/server/trunk/hbase/src/main/java/org/apache/james/rrt/hbase/HBaseRecipientRewriteTable.java?rev=1159864&r1=1159863&r2=1159864&view=diff
==============================================================================
--- james/server/trunk/hbase/src/main/java/org/apache/james/rrt/hbase/HBaseRecipientRewriteTable.java (original)
+++ james/server/trunk/hbase/src/main/java/org/apache/james/rrt/hbase/HBaseRecipientRewriteTable.java Sat Aug 20 10:27:50 2011
@@ -27,15 +27,12 @@ import java.util.Map;
 
 import org.apache.hadoop.hbase.KeyValue;
 import org.apache.hadoop.hbase.client.Delete;
+import org.apache.hadoop.hbase.client.Get;
 import org.apache.hadoop.hbase.client.HTable;
 import org.apache.hadoop.hbase.client.Put;
 import org.apache.hadoop.hbase.client.Result;
 import org.apache.hadoop.hbase.client.ResultScanner;
 import org.apache.hadoop.hbase.client.Scan;
-import org.apache.hadoop.hbase.filter.BinaryComparator;
-import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
-import org.apache.hadoop.hbase.filter.Filter;
-import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
 import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.james.rrt.api.RecipientRewriteTableException;
 import org.apache.james.rrt.hbase.def.HRecipientRewriteTable;
@@ -47,8 +44,6 @@ import org.slf4j.LoggerFactory;
 
 /**
  * Implementation of the RecipientRewriteTable for a HBase persistence.
- * 
- * TODO Fix wildcard and recursive mapping.
  */
 public class HBaseRecipientRewriteTable extends AbstractRecipientRewriteTable {
 
@@ -57,19 +52,21 @@ public class HBaseRecipientRewriteTable 
      */
     private static Logger log = LoggerFactory.getLogger(HBaseRecipientRewriteTable.class.getName());
     
+    private static final String ROW_SEPARATOR = "@";
+    
     /* (non-Javadoc)
      * @see org.apache.james.rrt.lib.AbstractRecipientRewriteTable#addMappingInternal(String, String, String)
      */
     @Override
     protected void addMappingInternal(String user, String domain, String mapping) throws RecipientRewriteTableException {
-        String newUser = getUserString(user);
-        String newDomain = getDomainString(domain);
-        Collection<String> map = getUserDomainMappings(newUser, newDomain);
+        String fixedUser = getFixedUser(user);
+        String fixedDomain = getFixedDomain(domain);
+        Collection<String> map = getUserDomainMappings(fixedUser, fixedDomain);
         if (map != null && map.size() != 0) {
             map.add(mapping);
-            doUpdateMapping(newUser, newDomain, RecipientRewriteTableUtil.CollectionToMapping(map));
+            doUpdateMapping(fixedUser, fixedDomain, RecipientRewriteTableUtil.CollectionToMapping(map));
         } else {
-            doAddMapping(newUser, newDomain, mapping);
+            doAddMapping(fixedUser, fixedDomain, mapping);
         }
     }
 
@@ -83,16 +80,8 @@ public class HBaseRecipientRewriteTable 
         ResultScanner resultScanner = null;
         try {
             table = TablePool.getInstance().getRecipientRewriteTable();
-            Scan scan = new Scan();
-            scan.addFamily(HRecipientRewriteTable.COLUMN_FAMILY_NAME);
-            scan.setCaching(table.getScannerCaching() * 2);
-            Filter filter = new SingleColumnValueFilter(HRecipientRewriteTable.COLUMN_FAMILY_NAME, Bytes.toBytes(user), CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(user)));
-            scan.setFilter(filter);
-            resultScanner = table.getScanner(scan);
-            Result result = null;
-            while ((result = resultScanner.next()) != null) {
-                list.add(Bytes.toString(result.getRow()));
-            }
+            // Optimize this to only make one call.
+            feedUserDomainMappingsList(table, user, domain, list);
         } catch (IOException e) {
             log.error("Error while getting user domain mapping in HBase", e);
             throw new RecipientRewriteTableException("Error while getting user domain mapping in HBase", e);
@@ -110,6 +99,15 @@ public class HBaseRecipientRewriteTable 
         }
         return list;
     }
+    
+    private void feedUserDomainMappingsList(HTable table, String user, String domain, Collection<String> list) throws IOException {
+        Get get = new Get(Bytes.toBytes(getRowKey(user, domain)));
+        Result result = table.get(get);
+        List<KeyValue> keyValues = result.getColumn(HRecipientRewriteTable.COLUMN_FAMILY_NAME, HRecipientRewriteTable.COLUMN.MAPPING);
+        if (keyValues.size() > 0) {
+            list.addAll(RecipientRewriteTableUtil.mappingToCollection(Bytes.toString(keyValues.get(0).getValue())));
+        }
+    }
 
     /* (non-Javadoc)
      * @see org.apache.james.rrt.lib.AbstractRecipientRewriteTable#getAllMappingsInternal()
@@ -130,9 +128,7 @@ public class HBaseRecipientRewriteTable 
                 List<KeyValue> keyValues = result.list();
                 if (keyValues != null) {
                     for (KeyValue keyValue: keyValues) {
-                        byte[] user = keyValue.getQualifier();
-                        byte[] domain = keyValue.getValue();
-                        String email = Bytes.toString(user) + '@' + Bytes.toString(domain);
+                        String email = Bytes.toString(keyValue.getRow());
                         if (map == null) {
                             map = new HashMap<String, Collection<String>>();
                         }
@@ -170,18 +166,15 @@ public class HBaseRecipientRewriteTable 
     protected String mapAddressInternal(String user, String domain) throws RecipientRewriteTableException {
         HTable table = null;
         ResultScanner resultScanner = null;
-        StringBuffer mappingBuffer = new StringBuffer();
+        String mappings = null;
         try {
             table = TablePool.getInstance().getRecipientRewriteTable();
-            Scan scan = new Scan();
-            scan.addFamily(HRecipientRewriteTable.COLUMN_FAMILY_NAME);
-            scan.setCaching(table.getScannerCaching() * 2);
-            Filter filter = new SingleColumnValueFilter(HRecipientRewriteTable.COLUMN_FAMILY_NAME, Bytes.toBytes(user), CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(domain)));
-            scan.setFilter(filter);
-            resultScanner = table.getScanner(scan);
-            Result result = null;
-            while ((result = resultScanner.next()) != null) {
-                mappingBuffer.append(Bytes.toString(result.getRow()) + ";");
+            mappings = getMapping(table, user, domain);
+            if (mappings == null) {
+                mappings = getMapping(table, WILDCARD, domain);
+            }
+            if (mappings == null) {
+                mappings = getMapping(table, user, WILDCARD);
             }
         } catch (IOException e) {
             log.error("Error while mapping address in HBase", e);
@@ -198,11 +191,17 @@ public class HBaseRecipientRewriteTable 
                 }
             }
         }
-        if (mappingBuffer.length() == 0) {
-            return null;
+        return mappings;
+    }
+    
+    private String getMapping(HTable table, String user, String domain) throws IOException {
+        Get get = new Get(Bytes.toBytes(getRowKey(user, domain)));
+        Result result = table.get(get);
+        List<KeyValue> keyValues = result.getColumn(HRecipientRewriteTable.COLUMN_FAMILY_NAME, HRecipientRewriteTable.COLUMN.MAPPING);
+        if (keyValues.size() > 0) {
+            return Bytes.toString(keyValues.get(0).getValue());
         }
-        String mapping = mappingBuffer.toString();
-        return mapping.substring(0, mapping.length() - 1);
+        return null;
     }
     
     /* (non-Javadoc)
@@ -210,19 +209,21 @@ public class HBaseRecipientRewriteTable 
      */
     @Override
     protected void removeMappingInternal(String user, String domain, String mapping) throws RecipientRewriteTableException {
-        String newUser = getUserString(user);
-        String newDomain = getDomainString(domain);
-        Collection<String> map = getUserDomainMappings(newUser, newDomain);
+        String fixedUser = getFixedUser(user);
+        String fixedDomain = getFixedDomain(domain);
+        Collection<String> map = getUserDomainMappings(fixedUser, fixedDomain);
         if (map != null && map.size() > 1) {
             map.remove(mapping);
-            doUpdateMapping(newUser, newDomain, RecipientRewriteTableUtil.CollectionToMapping(map));
+            doUpdateMapping(fixedUser, fixedDomain, RecipientRewriteTableUtil.CollectionToMapping(map));
         } else {
-            doRemoveMapping(newUser, newDomain, mapping);
+            doRemoveMapping(fixedUser, fixedDomain, mapping);
         }
     }
 
     /**
-     * Update the mapping for the given user and domain
+     * Update the mapping for the given user and domain.
+     * For HBase, this is simply achieved delegating
+     * the work to the doAddMapping method.
      * 
      * @param user the user
      * @param domain the domain
@@ -234,7 +235,7 @@ public class HBaseRecipientRewriteTable 
     }
 
     /**
-     * Remove a mapping for the given user and domain
+     * Remove a mapping for the given user and domain.
      * 
      * @param user the user
      * @param domain the domain
@@ -245,8 +246,7 @@ public class HBaseRecipientRewriteTable 
         HTable table = null;
         try {
             table = TablePool.getInstance().getRecipientRewriteTable();
-            Delete delete = new Delete(Bytes.toBytes(mapping));
-            delete.deleteColumn(HRecipientRewriteTable.COLUMN_FAMILY_NAME, Bytes.toBytes(user));
+            Delete delete = new Delete(Bytes.toBytes(getRowKey(user, domain)));
             table.delete(delete);
             table.flushCommits();
         } catch (IOException e) {
@@ -275,8 +275,8 @@ public class HBaseRecipientRewriteTable 
         HTable table = null;
         try {
             table = TablePool.getInstance().getRecipientRewriteTable();
-            Put put = new Put(Bytes.toBytes(mapping));
-            put.add(HRecipientRewriteTable.COLUMN_FAMILY_NAME, Bytes.toBytes(user), Bytes.toBytes(domain));
+            Put put = new Put(Bytes.toBytes(getRowKey(user, domain)));
+            put.add(HRecipientRewriteTable.COLUMN_FAMILY_NAME, HRecipientRewriteTable.COLUMN.MAPPING, Bytes.toBytes(mapping));
             table.put(put);
             table.flushCommits();
         } catch (IOException e) {
@@ -292,5 +292,16 @@ public class HBaseRecipientRewriteTable 
             }
         }
     }
+    
+    /**
+     * Constructs a Key based on the user and domain.
+     * 
+     * @param user
+     * @param domain
+     * @return the key
+     */
+    private String getRowKey(String user, String domain) {
+        return user + ROW_SEPARATOR + domain;
+    }
 
 }

Modified: james/server/trunk/hbase/src/main/java/org/apache/james/rrt/hbase/def/HRecipientRewriteTable.java
URL: http://svn.apache.org/viewvc/james/server/trunk/hbase/src/main/java/org/apache/james/rrt/hbase/def/HRecipientRewriteTable.java?rev=1159864&r1=1159863&r2=1159864&view=diff
==============================================================================
--- james/server/trunk/hbase/src/main/java/org/apache/james/rrt/hbase/def/HRecipientRewriteTable.java (original)
+++ james/server/trunk/hbase/src/main/java/org/apache/james/rrt/hbase/def/HRecipientRewriteTable.java Sat Aug 20 10:27:50 2011
@@ -31,4 +31,8 @@ public interface HRecipientRewriteTable 
     byte[] TABLE_NAME = Bytes.toBytes("JAMES_RRT");
     byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("JAMES_RRT");
 
+    public interface COLUMN {
+        byte [] MAPPING = Bytes.toBytes("map");
+    }
+
 }

Modified: james/server/trunk/hbase/src/test/java/org/apache/james/JamesServerHBaseIntegrationTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/hbase/src/test/java/org/apache/james/JamesServerHBaseIntegrationTest.java?rev=1159864&r1=1159863&r2=1159864&view=diff
==============================================================================
--- james/server/trunk/hbase/src/test/java/org/apache/james/JamesServerHBaseIntegrationTest.java (original)
+++ james/server/trunk/hbase/src/test/java/org/apache/james/JamesServerHBaseIntegrationTest.java Sat Aug 20 10:27:50 2011
@@ -22,6 +22,7 @@ import org.apache.hadoop.conf.Configurat
 import org.apache.hadoop.hbase.HBaseTestingUtility;
 import org.apache.hadoop.hbase.MiniHBaseCluster;
 import org.apache.james.domainlist.hbase.HBaseDomainListTest;
+import org.apache.james.rrt.hbase.HBaseRecipientRewriteTableTest;
 import org.apache.james.system.hbase.TablePool;
 import org.apache.james.system.hbase.TablePoolTest;
 import org.apache.james.user.hbase.HBaseUsersRepositoryTest;
@@ -44,7 +45,7 @@ import org.junit.runners.Suite.SuiteClas
 @SuiteClasses({
     TablePoolTest.class,
     HBaseDomainListTest.class,
-//    HBaseRecipientRewriteTableTest.class,
+    HBaseRecipientRewriteTableTest.class,
     HBaseUsersRepositoryTest.class
   })
 public class JamesServerHBaseIntegrationTest {

Modified: james/server/trunk/hbase/src/test/java/org/apache/james/rrt/hbase/HBaseRecipientRewriteTableTest.java
URL: http://svn.apache.org/viewvc/james/server/trunk/hbase/src/test/java/org/apache/james/rrt/hbase/HBaseRecipientRewriteTableTest.java?rev=1159864&r1=1159863&r2=1159864&view=diff
==============================================================================
--- james/server/trunk/hbase/src/test/java/org/apache/james/rrt/hbase/HBaseRecipientRewriteTableTest.java (original)
+++ james/server/trunk/hbase/src/test/java/org/apache/james/rrt/hbase/HBaseRecipientRewriteTableTest.java Sat Aug 20 10:27:50 2011
@@ -34,8 +34,6 @@ import org.slf4j.LoggerFactory;
  * 
  * Simply create the needed HBaseRecipientRewriteTable instance, and let the
  * AbstractRecipientRewriteTableTest run the tests.
- * 
- * TODO Fix wildcards, recursive and alias tests.
  */
 public class HBaseRecipientRewriteTableTest extends AbstractRecipientRewriteTableTest {
 
@@ -44,38 +42,6 @@ public class HBaseRecipientRewriteTableT
      */
     private static Logger logger = Logger.getLogger(HBaseRecipientRewriteTableTest.class);
     
-    /**
-     * Mini Hbase Cluster
-     * 
-     * TODO Remove this when RRT 
-     */
-    private static MiniHBaseCluster hbaseCluster;
-    
-    /* (non-Javadoc)
-     * @see org.apache.james.rrt.lib.AbstractRecipientRewriteTableTest#setUp()
-     */
-    public void setUp() throws Exception {
-        super.setUp();
-        if (hbaseCluster == null) {
-            HBaseTestingUtility htu = new HBaseTestingUtility();
-            htu.getConfiguration().setBoolean("dfs.support.append", true);
-            try {
-                hbaseCluster = htu.startMiniCluster();
-            } 
-            catch (Exception e) {
-                logger.error("Exception when starting HBase Mini Cluster", e);
-            }
-            TablePool.getInstance(getConfiguration());
-        }
-    }
-    
-    /* (non-Javadoc)
-     * @see org.apache.james.rrt.lib.AbstractRecipientRewriteTableTest#tearDown()
-     */
-    public void tearDown() throws Exception {
-        super.tearDown();
-    }
-    
     /* (non-Javadoc)
      * @see org.apache.james.rrt.lib.AbstractRecipientRewriteTableTest#getRecipientRewriteTable()
      */



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org