You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by be...@apache.org on 2013/07/08 11:51:46 UTC

[7/9] git commit: HBase storage: add private data persistence

HBase storage: add private data persistence


Project: http://git-wip-us.apache.org/repos/asf/mina-vysper/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-vysper/commit/5317a44a
Tree: http://git-wip-us.apache.org/repos/asf/mina-vysper/tree/5317a44a
Diff: http://git-wip-us.apache.org/repos/asf/mina-vysper/diff/5317a44a

Branch: refs/heads/master
Commit: 5317a44a772927034bcc87021b3f662b2d40820e
Parents: 472093c
Author: Bernd Fondermann <be...@brainlounge.de>
Authored: Mon Jul 8 10:28:36 2013 +0200
Committer: Bernd Fondermann <be...@brainlounge.de>
Committed: Mon Jul 8 10:28:36 2013 +0200

----------------------------------------------------------------------
 .../vysper/storage/hbase/HBaseStorage.java      |  9 +-
 .../HBasePrivateDataPersistenceManager.java     | 95 ++++++++++++++++++++
 2 files changed, 103 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-vysper/blob/5317a44a/server/storage/hbase/src/main/java/org/apache/vysper/storage/hbase/HBaseStorage.java
----------------------------------------------------------------------
diff --git a/server/storage/hbase/src/main/java/org/apache/vysper/storage/hbase/HBaseStorage.java b/server/storage/hbase/src/main/java/org/apache/vysper/storage/hbase/HBaseStorage.java
index b2faeb9..2e0b16f 100644
--- a/server/storage/hbase/src/main/java/org/apache/vysper/storage/hbase/HBaseStorage.java
+++ b/server/storage/hbase/src/main/java/org/apache/vysper/storage/hbase/HBaseStorage.java
@@ -39,6 +39,10 @@ import static org.apache.vysper.storage.hbase.HBaseUtils.*;
 
 /**
  * back-end adaptor for HBase
+ * 
+ * prepare HBase by creating table vysper_user:
+ * create 'vysper_user', {NAME => 'bsc', VERSIONS => 1}, {NAME => 'cct', VERSIONS => 1}, {NAME => 'rst', VERSIONS => 1}, {NAME => 'xep', VERSIONS => 5}
+ * 
  * @author The Apache MINA Project (dev@mina.apache.org)
  */
 public class HBaseStorage {
@@ -51,6 +55,8 @@ public class HBaseStorage {
     public static final byte[] COLUMN_FAMILY_NAME_CONTACT_BYTES = COLUMN_FAMILY_NAME_CONTACT.getBytes();
     public static final String COLUMN_FAMILY_NAME_ROSTER = "rst";
     public static final byte[] COLUMN_FAMILY_NAME_ROSTER_BYTES = COLUMN_FAMILY_NAME_ROSTER.getBytes();
+    public static final String COLUMN_FAMILY_NAME_XEP = "xep";
+    public static final byte[] COLUMN_FAMILY_NAME_XEP_BYTES = COLUMN_FAMILY_NAME_XEP.getBytes();
     
     protected static HBaseStorage hbaseStorageSingleton;
 
@@ -111,8 +117,9 @@ public class HBaseStorage {
             columnFamilyNames = new String[]{COLUMN_FAMILY_NAME_CONTACT};
         }
 
-        final HTableInterface userTable = getTable(TABLE_NAME_USER);
+        HTableInterface userTable = null;
         try {
+            userTable = getTable(TABLE_NAME_USER);
             final Get get = new Get(entityAsBytes(entity.getBareJID()));
             for (String columnFamilyName : columnFamilyNames) {
                 get.addFamily(asBytes(columnFamilyName));

http://git-wip-us.apache.org/repos/asf/mina-vysper/blob/5317a44a/server/storage/hbase/src/main/java/org/apache/vysper/storage/hbase/privatedata/HBasePrivateDataPersistenceManager.java
----------------------------------------------------------------------
diff --git a/server/storage/hbase/src/main/java/org/apache/vysper/storage/hbase/privatedata/HBasePrivateDataPersistenceManager.java b/server/storage/hbase/src/main/java/org/apache/vysper/storage/hbase/privatedata/HBasePrivateDataPersistenceManager.java
new file mode 100644
index 0000000..1f80063
--- /dev/null
+++ b/server/storage/hbase/src/main/java/org/apache/vysper/storage/hbase/privatedata/HBasePrivateDataPersistenceManager.java
@@ -0,0 +1,95 @@
+/*
+ *  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.vysper.storage.hbase.privatedata;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.hadoop.hbase.client.HTableInterface;
+import org.apache.hadoop.hbase.client.Put;
+import org.apache.hadoop.hbase.client.Result;
+import org.apache.vysper.storage.hbase.HBaseStorage;
+import org.apache.vysper.xmpp.addressing.Entity;
+import org.apache.vysper.xmpp.modules.extension.xep0049_privatedata.PrivateDataPersistenceManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+
+import static org.apache.vysper.storage.hbase.HBaseStorage.*;
+import static org.apache.vysper.storage.hbase.HBaseUtils.*;
+
+/**
+ * @author The Apache MINA Project (dev@mina.apache.org)
+ */
+public class HBasePrivateDataPersistenceManager implements PrivateDataPersistenceManager {
+
+    final Logger logger = LoggerFactory.getLogger(HBasePrivateDataPersistenceManager.class);
+
+    public static final String COLUMN_PREFIX_NAME = "xep_priv_data:";
+    
+    protected HBaseStorage hbaseStorage;
+
+    public HBasePrivateDataPersistenceManager(HBaseStorage hbaseStorage) {
+        this.hbaseStorage = hbaseStorage;
+    }
+
+    public boolean isAvailable() {
+        HTableInterface table = null;
+        try {
+            table = hbaseStorage.getTable(TABLE_NAME_USER);
+            return table != null;
+        } finally {
+            hbaseStorage.putTable(table);
+        }
+    }
+
+    public String getPrivateData(Entity entity, String key) {
+        final Result entityRow = hbaseStorage.getEntityRow(entity, COLUMN_FAMILY_NAME_XEP);
+
+        String column = COLUMN_PREFIX_NAME + key;
+        String value = toStr(entityRow.getValue(COLUMN_FAMILY_NAME_XEP_BYTES, asBytes(column)));
+        
+        return value;
+    }
+
+    public boolean setPrivateData(Entity entity, String key, String xml) {
+
+        if (key == null || StringUtils.isBlank(key)) {
+            throw new IllegalArgumentException("private data key must not be blank");
+        }
+        String column = COLUMN_PREFIX_NAME + key;
+        
+        final Put put = new Put(entityAsBytes(entity.getBareJID()));
+        put.add(COLUMN_FAMILY_NAME_XEP_BYTES, asBytes(column), asBytes(xml));
+
+        HTableInterface table = null;
+        try {
+            table = hbaseStorage.getTable(TABLE_NAME_USER);
+            table.put(put);
+            logger.debug("stored private data for {} with key {}", entity, key);
+            return true;
+        } catch (IOException e) {
+            logger.warn("failed to save private data for {} with key {}", entity, key);
+            return false;
+        } finally {
+            hbaseStorage.putTable(table);
+        }
+    }
+
+}