Mercurial > hg > Members > shoshi > TreeCMSv1
changeset 48:ecba122dd3de
added CassEditor,CassLink,CassNode
implementation finished.
but debugging has not done yet.
author | shoshi |
---|---|
date | Sun, 30 Jan 2011 02:53:39 +0900 |
parents | 5b36891db5ca |
children | d12e1f578c9a |
files | src/treecms/proto/cassandra/CassBrowser.java src/treecms/proto/cassandra/CassEditor.java src/treecms/proto/cassandra/CassLink.java src/treecms/proto/cassandra/CassNode.java src/treecms/proto/cassandra/test/CassGetSliceTest.java src/treecms/proto/simple/SimpleEditor.java |
diffstat | 6 files changed, 282 insertions(+), 87 deletions(-) [+] |
line wrap: on
line diff
--- a/src/treecms/proto/cassandra/CassBrowser.java Sat Jan 29 03:05:20 2011 +0900 +++ b/src/treecms/proto/cassandra/CassBrowser.java Sun Jan 30 02:53:39 2011 +0900 @@ -38,7 +38,7 @@ private static final String ROOTNODE = "RootNode"; private static final String CLASSNAME = "ClassName"; - private static final String TITLE = "title"; + private static final String TITLE = "Title"; private static final String CHILDREN = "Children"; private static final String CHILDREN_DELIM = ","; @@ -96,7 +96,7 @@ set.put(st.nextToken(),Boolean.TRUE); } - StringBuffer buf = new StringBuffer(); + StringBuffer buf = new StringBuffer(children); if(!set.isEmpty()){ buf.append(CHILDREN_DELIM); @@ -118,12 +118,12 @@ buf.append(key); - setColumn(_id.toString(),CHILDREN,buf.toString(),false); + setColumn(_id.toString(),CHILDREN,buf.toString()); } public void clearChildren(NodeID _id) { - setColumn(_id.toString(),CHILDREN,"",false); + setColumn(_id.toString(),CHILDREN,""); } public boolean removeChild(NodeID _id,Node _child) @@ -157,7 +157,7 @@ } if(changed){ - setColumn(_id.toString(),CHILDREN,buf.toString(),false); + setColumn(_id.toString(),CHILDREN,buf.toString()); } return changed; @@ -186,7 +186,7 @@ } if(flagSuccess == 2){ - setColumn(_id.toString(),CHILDREN,buf.toString(),false); + setColumn(_id.toString(),CHILDREN,buf.toString()); } } @@ -208,7 +208,7 @@ buf.append(prefix+token); } - setColumn(_id.toString(),CHILDREN,buf.toString(),false); + setColumn(_id.toString(),CHILDREN,buf.toString()); } public void up(NodeID _id,Node _child) @@ -240,7 +240,7 @@ public void setClassName(NodeID _id,String _className) { - setColumn(_id.toString(),CLASSNAME,_className,false); + setColumn(_id.toString(),CLASSNAME,_className); } public Node createNode() @@ -271,13 +271,29 @@ { NodeID newID = _id.update(); + SliceRange sr = new SliceRange(); + sr.setStart(new byte[0]); + sr.setFinish(new byte[0]); - return null; + SlicePredicate sp = new SlicePredicate(); + sp.setSlice_range(sr); + + try{ + List<ColumnOrSuperColumn> columns = m_cassandra.get_slice(m_keySpace,_id.toString(),new ColumnParent(m_colFamily),sp,ConsistencyLevel.ONE); + Map<String,List<ColumnOrSuperColumn>> cfmap = new HashMap<String,List<ColumnOrSuperColumn>>(); + cfmap.put(m_colFamily,columns); + + m_cassandra.batch_insert(m_keySpace,newID.toString(),cfmap,ConsistencyLevel.ONE); + }catch(Exception _e){ + _e.printStackTrace(); + } + + return new CassNode(this,newID); } public CassLink createLink(NodeID _id) { - return null; + return new CassLink(new CassDecNodeID(this),_id,this); } public boolean isChild(NodeID _id,Node _child) @@ -291,32 +307,32 @@ public String getClassName(NodeID _id) { - return getColumn(_id.toString(),CLASSNAME,false); + return getColumn(_id.toString(),CLASSNAME); } public String getChildrenRaw(NodeID _id) { - return getColumn(_id.toString(),CHILDREN,false); + return getColumn(_id.toString(),CHILDREN); } public String getTitle(NodeID _id) { - return getColumn(_id.toString(),TITLE,false); + return getColumn(_id.toString(),TITLE); } public void setTitle(NodeID _id,String _title) { - setColumn(_id.toString(),TITLE,_title,false); + setColumn(_id.toString(),TITLE,_title); } public String getAttribute(NodeID _id,String _attr) { - return getColumn(_id.toString(),ATTR_PREFIX+_attr,false); + return getColumn(_id.toString(),ATTR_PREFIX+_attr); } public void setAttribute(NodeID _id,String _attr,String _value) { - setColumn(_id.toString(),ATTR_PREFIX+_attr,_value,false); + setColumn(_id.toString(),ATTR_PREFIX+_attr,_value); } public Set<String> getAttributeKeys(NodeID _id) @@ -367,7 +383,7 @@ return null; } - private String getColumn(String _row,String _colName,boolean _retry) + private String getColumn(String _row,String _colName) { ColumnPath path = new ColumnPath(); path.column_family = m_colFamily; @@ -385,7 +401,7 @@ return value; } - private boolean setColumn(String _row,String _colName,String _value,boolean _retry) + private boolean setColumn(String _row,String _colName,String _value) { ColumnPath path = new ColumnPath(); path.column_family = m_colFamily; @@ -401,15 +417,15 @@ return false; } - public CassNode createNode(String _id) + public Node getNodeByID(NodeID _id) { - return null; + return new CassNode(this,_id); } @Override public Node useContents() { - String uuid = getColumn(TREEINFO,ROOTNODE,false); + String uuid = getColumn(TREEINFO,ROOTNODE); NodeID rootID = getTipIDFromUUID(uuid); return new CassNode(this,rootID); }
--- a/src/treecms/proto/cassandra/CassEditor.java Sat Jan 29 03:05:20 2011 +0900 +++ b/src/treecms/proto/cassandra/CassEditor.java Sun Jan 30 02:53:39 2011 +0900 @@ -1,36 +1,81 @@ package treecms.proto.cassandra; import treecms.proto.api.Editor; +import java.util.List; + +import java.util.LinkedList; + +import treecms.proto.api.Browser; import treecms.proto.api.Node; +import treecms.proto.api.NodeID; +import treecms.proto.marge.Marger; +import treecms.proto.marge.ReplaceMarger; public class CassEditor implements Editor { + private Node m_local; + private Node m_remote; + + private Browser m_browser; + + public CassEditor(Browser _browser) + { + update(); + } + + public void copyNode(Node _from,Node _to) + { + _to.setClassName(_from.getClassName()); + _to.setTitle(_from.getTitle()); + + for(String _key : _from.getAttributeKeys()){ + _to.setAttribute(_key,_from.getAttribute(_key)); + } + + _to.addChildren(_from.getChildren()); + } + @Override public boolean check() { + NodeID remoteID = m_remote.getID(); + if(remoteID.equals(remoteID.getTip())){ + return false; + } + return true; + } + + public boolean commit(boolean _force) + { + if(_force || !check()){ + Node target = m_remote.cloneNode(); + target.clearChildren(); + copyNode(m_local,target); + return true; + } + return false; } + + public void merge() + { + Marger marger = new ReplaceMarger(); + m_local = marger.merge(m_remote,m_local); + } + + public void discard() + { + copyNode(m_remote,m_local); + } @Override - public boolean commit(boolean _force) + public Node useContents() { - return false; + return m_local; } @Override - public Node edit(Node _target) - { - return null; - } - - @Override - public Node getUncommited() - { - return null; - } - - @Override - public void login(String _user, String _pass) + public void login(String user, String pass) { } @@ -40,20 +85,85 @@ } @Override - public void merge() + public Node edit(Node _target) + { + LinkedList<Node> path = findPath(m_local,_target); + if(path.isEmpty()){ + return null; + } + + LinkedList<Node> change = new LinkedList<Node>(); + Node root = path.poll().cloneNode(); + change.add(root); + cloneTree(path,root,change); + + m_local = root; + + Node clone = change.peekLast(); + return clone; + } + + private void cloneTree(LinkedList<Node> _path,Node _parent,LinkedList<Node> _change) { + List<Node> children = _parent.getChildren(); + Node target = _path.poll(); + if(target == null){ + return; + } + for(int i = 0;i < children.size();i ++){ + Node _child = children.get(i); + if(_child.getID().isFamily(target.getID())){ + //clone node + Node clone = _child.cloneNode(); + _change.add(clone); + + //remove old node from clonedTree + _parent.replace(_child,clone); + + cloneTree(_path,clone,_change); + break; + } + } + } + + private LinkedList<Node> findPath(Node _root,Node _child) + { + LinkedList<Node> list = new LinkedList<Node>(); + if(findPath(_root,_child,list)){ + list.addFirst(_root); + } + return list; + } + + private boolean findPath(Node _root,Node _child,LinkedList<Node> _list) + { + if(_root.getID().isFamily(_child.getID())){ + return true; + } + + for(Node child : _root.getChildren()){ + if(findPath(child,_child,_list)){ + _list.addFirst(child); + return true; + } + } + return false; + } + + @Override + public Node getUncommited() + { + return m_local; } @Override public boolean update() { - return false; + m_remote = m_browser.useContents(); + m_local = m_remote.createNode(); + + copyNode(m_remote,m_local); + return true; } +} - @Override - public Node useContents() - { - return null; - } - -}
--- a/src/treecms/proto/cassandra/CassLink.java Sat Jan 29 03:05:20 2011 +0900 +++ b/src/treecms/proto/cassandra/CassLink.java Sun Jan 30 02:53:39 2011 +0900 @@ -10,138 +10,154 @@ public class CassLink implements Link { - + private NodeID m_id; + private NodeID m_target; + + private CassBrowser m_browser; + + public CassLink(NodeID _id,NodeID _target,CassBrowser _browser) + { + m_id = _id; + m_target = _target; + + m_browser = _browser; + } + @Override public Node getNode() { - return null; + return m_browser.getNodeByID(m_target.getTip()); } @Override - public void setNode(Node link) + public void setNode(Node _link) { + m_target = _link.getID(); } @Override - public Node addChild(Node child) + public Node addChild(Node _child) { - return null; + return getNode().addChild(_child); } @Override - public void addChildren(List<Node> child) + public void addChildren(List<Node> _children) { + getNode().addChildren(_children); } @Override public void clearChildren() { + getNode().clearChildren(); } @Override public Node cloneNode() { - return null; + return getNode().cloneNode(); } @Override public Link createLink() { - return null; + return getNode().createLink(); } @Override public Node createNode() { - return null; + return m_browser.createNode(); } @Override public void down(Node _child) { + getNode().down(_child); } @Override public String getAttribute(String _attr) { - return null; + return getNode().getAttribute(_attr); } @Override public Set<String> getAttributeKeys() { - return null; + return getNode().getAttributeKeys(); } @Override public List<Node> getChildren() { - return null; + return getNode().getChildren(); } @Override public String getClassName() { - return null; + return getNode().getClassName(); } @Override public NodeID getID() { - return null; + return m_id; } @Override public String getTitle() { - return null; + return getNode().getTitle(); } @Override public boolean isChild(Node _child) { - return false; + return getNode().isChild(_child); } @Override public Iterator<Node> iterator() { - return null; + return getNode().iterator(); } @Override public boolean removeChild(Node _child) { - // TODO Auto-generated method stub - return false; + return getNode().removeChild(_child); } @Override - public void replace(Node target, Node newChild) + public void replace(Node _target,Node _newChild) { + getNode().replace(_target,_newChild); } @Override - public void setAttribute(String attr, String value) + public void setAttribute(String _attr,String _value) { + getNode().setAttribute(_attr,_value); } @Override - public void setClassName(String class1) { - // TODO Auto-generated method stub - + public void setClassName(String _className) + { + getNode().setClassName(_className); } @Override - public void setTitle(String title) { - // TODO Auto-generated method stub - + public void setTitle(String _title) + { + getNode().setTitle(_title); } @Override - public void up(Node child) { - // TODO Auto-generated method stub - + public void up(Node _child) + { + getNode().up(_child); } - }
--- a/src/treecms/proto/cassandra/CassNode.java Sat Jan 29 03:05:20 2011 +0900 +++ b/src/treecms/proto/cassandra/CassNode.java Sun Jan 30 02:53:39 2011 +0900 @@ -1,11 +1,9 @@ package treecms.proto.cassandra; -import java.nio.CharBuffer; + import java.util.Iterator; -import java.util.LinkedList; import java.util.List; import java.util.Set; -import java.util.StringTokenizer; import treecms.proto.api.Link; import treecms.proto.api.Node;
--- a/src/treecms/proto/cassandra/test/CassGetSliceTest.java Sat Jan 29 03:05:20 2011 +0900 +++ b/src/treecms/proto/cassandra/test/CassGetSliceTest.java Sun Jan 30 02:53:39 2011 +0900 @@ -6,8 +6,13 @@ * 2011/01/19 Shoshi TAMAKI */ +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.ColumnParent; import org.apache.cassandra.thrift.ColumnPath; @@ -41,11 +46,58 @@ tr.open(); System.out.println("ClusterName = "+client.describe_cluster_name()); - testGetRangeSlice(client); + //testGetRangeSlice(client); + testBatchInsert(client); tr.close(); } + public static void testBatchInsert(Cassandra.Client _client) throws InvalidRequestException, UnavailableException, TimedOutException, TException + { + List<ColumnOrSuperColumn> list = new LinkedList<ColumnOrSuperColumn>(); + + ColumnOrSuperColumn c1 = new ColumnOrSuperColumn(); + c1.column = new Column(); + c1.column.name = "hogehoge".getBytes(); + c1.column.value = "fugafuga".getBytes(); + c1.column.timestamp = System.currentTimeMillis()/1000; + + ColumnOrSuperColumn c2 = new ColumnOrSuperColumn(); + c2.column = new Column(); + c2.column.name = "abc".getBytes(); + c2.column.value = "abcabc".getBytes(); + c2.column.timestamp = System.currentTimeMillis()/1000; + + ColumnOrSuperColumn c3 = new ColumnOrSuperColumn(); + c3.column = new Column(); + c3.column.name = "def".getBytes(); + c3.column.value = "defdef".getBytes(); + c3.column.timestamp = System.currentTimeMillis()/1000; + + list.add(c1); + list.add(c2); + list.add(c3); + + Map<String,List<ColumnOrSuperColumn>> map = new HashMap<String,List<ColumnOrSuperColumn>>(); + map.put("TreeCMSCF01",list); + + _client.batch_insert("TreeCMSKS","hoge",map,ConsistencyLevel.ONE); + + SliceRange sr = new SliceRange(); + sr.setStart(new byte[0]); + sr.setFinish(new byte[0]); + + SlicePredicate sp = new SlicePredicate(); + sp.setSlice_range(sr); + + List<ColumnOrSuperColumn> result = _client.get_slice("TreeCMSKS","hoge",new ColumnParent("TreeCMSCF01"),sp,ConsistencyLevel.ONE); + + for(ColumnOrSuperColumn val : result){ + System.out.print("name:"+new String(val.column.name)); + System.out.println("\tvalue:"+new String(val.column.value)); + } + } + public static void testGet(Cassandra.Client _client) throws InvalidRequestException, NotFoundException, UnavailableException, TimedOutException, TException { ColumnPath path = new ColumnPath();
--- a/src/treecms/proto/simple/SimpleEditor.java Sat Jan 29 03:05:20 2011 +0900 +++ b/src/treecms/proto/simple/SimpleEditor.java Sun Jan 30 02:53:39 2011 +0900 @@ -1,7 +1,10 @@ package treecms.proto.simple; import java.util.List; + import java.util.LinkedList; + +import treecms.proto.api.Browser; import treecms.proto.api.Editor; import treecms.proto.api.Node; import treecms.proto.api.NodeID; @@ -10,10 +13,10 @@ public class SimpleEditor implements Editor { - private SimpleNode m_local; - private SimpleNode m_remote; + private Node m_local; + private Node m_remote; - private static final SimpleBrowser m_browser = SimpleBrowser.getSingleton(); + private static final Browser m_browser = SimpleBrowser.getSingleton(); public SimpleEditor() { @@ -57,7 +60,7 @@ public void merge() { Marger marger = new ReplaceMarger(); - m_local = (SimpleNode)marger.merge(m_remote,m_local); + m_local = marger.merge(m_remote,m_local); } public void discard() @@ -90,7 +93,7 @@ } LinkedList<Node> change = new LinkedList<Node>(); - SimpleNode root = (SimpleNode)path.poll().cloneNode(); + Node root = path.poll().cloneNode(); change.add(root); cloneTree(path,root,change); @@ -156,8 +159,8 @@ @Override public boolean update() { - m_remote = (SimpleNode)m_browser.useContents(); - m_local = m_browser.createNode(); + m_remote = m_browser.useContents(); + m_local = m_remote.createNode(); copyNode(m_remote,m_local); return true;