Mercurial > hg > Members > shoshi > TreeCMSv1
changeset 49:d12e1f578c9a
added CassTreeImporter (import tree structure from others)
author | shoshi |
---|---|
date | Sun, 30 Jan 2011 21:22:27 +0900 |
parents | ecba122dd3de |
children | a72718a0bccf |
files | src/treecms/proto/cassandra/CassBrowser.java src/treecms/proto/cassandra/CassTreeImporter.java |
diffstat | 2 files changed, 107 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/src/treecms/proto/cassandra/CassBrowser.java Sun Jan 30 02:53:39 2011 +0900 +++ b/src/treecms/proto/cassandra/CassBrowser.java Sun Jan 30 21:22:27 2011 +0900 @@ -34,13 +34,13 @@ private String m_host,m_port; private String m_keySpace,m_colFamily; - private static final String TREEINFO = "TreeInfo"; - private static final String ROOTNODE = "RootNode"; + public static final String TREEINFO = "TreeInfo"; + public static final String ROOTNODE = "RootNode"; - private static final String CLASSNAME = "ClassName"; - private static final String TITLE = "Title"; - private static final String CHILDREN = "Children"; - private static final String CHILDREN_DELIM = ","; + public static final String CLASSNAME = "ClassName"; + public static final String TITLE = "Title"; + public static final String CHILDREN = "Children"; + public static final String CHILDREN_DELIM = ","; private static final String ATTR_PREFIX = "_"; @@ -53,6 +53,21 @@ connect(); } + public Cassandra.Client getClient() + { + return m_cassandra; + } + + public String getKeySpace() + { + return m_keySpace; + } + + public String getColumnFamily() + { + return m_colFamily; + } + public synchronized void connect() throws TTransportException { if(m_cassandra != null && m_cassandra.getOutputProtocol().getTransport().isOpen()){
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/cassandra/CassTreeImporter.java Sun Jan 30 21:22:27 2011 +0900 @@ -0,0 +1,86 @@ +package treecms.proto.cassandra; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import org.apache.cassandra.thrift.Cassandra; +import org.apache.cassandra.thrift.Column; +import org.apache.cassandra.thrift.ColumnOrSuperColumn; +import org.apache.cassandra.thrift.ConsistencyLevel; +import org.apache.cassandra.thrift.Mutation; + +import treecms.proto.api.Node; + +public class CassTreeImporter +{ + public CassBrowser m_browser; + + public CassTreeImporter(CassBrowser _browser) + { + m_browser = _browser; + } + + public void importTree(Node _root) + { + List<Node> list = listNode(_root); + + Cassandra.Client client = m_browser.getClient(); + + Map<String,Map<String,List<Mutation>>> mutation = new HashMap<String,Map<String,List<Mutation>>>(); + Map<String,List<Mutation>> map = new HashMap<String,List<Mutation>>(); + for(Node node : list){ + LinkedList<Mutation> columns = new LinkedList<Mutation>(); + + Column className = new Column(); + className.name = CassBrowser.CLASSNAME.getBytes(); + className.value = node.getClassName().getBytes(); + columns.add(packColumn(className)); + + Column title = new Column(); + title.name = CassBrowser.TITLE.getBytes(); + title.value = node.getTitle().getBytes(); + columns.add(packColumn(title)); + + for(String key : node.getAttributeKeys()){ + Column attr = new Column(); + attr.name = key.getBytes(); + attr.value = node.getAttribute(key).getBytes(); + columns.add(packColumn(attr)); + } + + map.put(node.getID().toString(),columns); + } + + mutation.put(m_browser.getColumnFamily(),map); + + try{ + client.batch_mutate(m_browser.getKeySpace(),mutation,ConsistencyLevel.ONE); + }catch(Exception _e){ + _e.printStackTrace(); + } + } + + public Mutation packColumn(Column _col) + { + ColumnOrSuperColumn cors = new ColumnOrSuperColumn(); + + cors.column = _col; + + Mutation mut = new Mutation(); + mut.column_or_supercolumn = cors; + + return mut; + } + + private List<Node> listNode(Node _root) + { + LinkedList<Node> list = new LinkedList<Node>(); + list.addAll(_root.getChildren()); + for(Node child : list){ + list.addAll(listNode(child)); + } + return list; + } +}