Mercurial > hg > Members > shoshi > TreeCMS > TreeCMSPrototype1
changeset 45:347feeca4728
added CassDecNodeID
author | shoshi |
---|---|
date | Sat, 22 Jan 2011 00:33:46 +0900 |
parents | cc66b6933d44 |
children | a529aad2ac92 |
files | src/treecms/proto/cassandra/CassBrowser.java src/treecms/proto/cassandra/CassDecNodeID.java src/treecms/proto/id/DecrementalNodeID.java src/treecms/proto/simple/SimpleDecNodeID.java |
diffstat | 4 files changed, 99 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/src/treecms/proto/cassandra/CassBrowser.java Fri Jan 21 16:35:57 2011 +0900 +++ b/src/treecms/proto/cassandra/CassBrowser.java Sat Jan 22 00:33:46 2011 +0900 @@ -1,8 +1,11 @@ package treecms.proto.cassandra; +import java.util.List; import treecms.proto.api.Browser; import treecms.proto.api.Node; import treecms.proto.api.NodeID; + +import org.apache.thrift.TException; import org.apache.thrift.transport.TTransport; import org.apache.thrift.transport.TFramedTransport; import org.apache.thrift.transport.TSocket; @@ -11,8 +14,15 @@ import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.cassandra.thrift.Cassandra; import org.apache.cassandra.thrift.ColumnOrSuperColumn; +import org.apache.cassandra.thrift.ColumnParent; import org.apache.cassandra.thrift.ColumnPath; import org.apache.cassandra.thrift.ConsistencyLevel; +import org.apache.cassandra.thrift.InvalidRequestException; +import org.apache.cassandra.thrift.KeySlice; +import org.apache.cassandra.thrift.SlicePredicate; +import org.apache.cassandra.thrift.SliceRange; +import org.apache.cassandra.thrift.TimedOutException; +import org.apache.cassandra.thrift.UnavailableException; public class CassBrowser implements Browser { @@ -64,6 +74,34 @@ return getColumn(_id.toString(),_colName,true); } + public NodeID getTipIDFromUUID(String _uuid) + { + SliceRange sliceRange = new SliceRange(); + sliceRange.setStart(_uuid.getBytes()); + sliceRange.setFinish(new byte[0]); + + SlicePredicate slicePredicate = new SlicePredicate(); + slicePredicate.setSlice_range(sliceRange); + + try { + List<KeySlice> values = m_cassandra.get_range_slice(m_keySpace,new ColumnParent(m_colFamily),slicePredicate,"hogehoge","hogehogea",100,ConsistencyLevel.ONE); + String key = values.get(0).getKey(); + + String tip = key.replaceFirst((_uuid+"@").intern(),""); + return new CassDecNodeID(this,_uuid,Long.parseLong(tip)); + }catch(InvalidRequestException _e){ + _e.printStackTrace(); + }catch(UnavailableException _e){ + _e.printStackTrace(); + }catch(TimedOutException _e){ + _e.printStackTrace(); + }catch(TException _e){ + _e.printStackTrace(); + } + + return null; + } + private String getColumn(String _row,String _colName,boolean _retry) { ColumnPath path = new ColumnPath();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/cassandra/CassDecNodeID.java Sat Jan 22 00:33:46 2011 +0900 @@ -0,0 +1,58 @@ +package treecms.proto.cassandra; + +import java.util.UUID; + +import treecms.proto.api.NodeID; +import treecms.proto.id.DecrementalNodeID; + +public class CassDecNodeID extends DecrementalNodeID +{ + private String m_uuid; + private long m_version; + + private CassBrowser m_cassandra; + + public CassDecNodeID(CassBrowser _browser) + { + m_uuid = UUID.randomUUID().toString(); + m_version = START_VALUE; + m_cassandra = _browser; + } + + public CassDecNodeID(CassBrowser _browser,String _uuid,long _version) + { + m_cassandra = _browser; + m_uuid = _uuid; + m_version = _version; + } + + @Override + public NodeID create() + { + return new CassDecNodeID(m_cassandra); + } + + @Override + public NodeID getTip() + { + return m_cassandra.getTipIDFromUUID(m_uuid); + } + + @Override + public String getUUID() + { + return m_uuid; + } + + @Override + public String getVersion() + { + return Long.toString(m_version); + } + + @Override + public NodeID update() + { + return null; + } +}
--- a/src/treecms/proto/id/DecrementalNodeID.java Fri Jan 21 16:35:57 2011 +0900 +++ b/src/treecms/proto/id/DecrementalNodeID.java Sat Jan 22 00:33:46 2011 +0900 @@ -4,6 +4,8 @@ public abstract class DecrementalNodeID implements NodeID { + public static final long START_VALUE = 100000; + @Override public abstract NodeID create();
--- a/src/treecms/proto/simple/SimpleDecNodeID.java Fri Jan 21 16:35:57 2011 +0900 +++ b/src/treecms/proto/simple/SimpleDecNodeID.java Sat Jan 22 00:33:46 2011 +0900 @@ -16,7 +16,7 @@ { m_uuid = UUID.randomUUID().toString(); m_tip = new AtomicLong(); - m_tip.set(10000); + m_tip.set(START_VALUE); m_version = m_tip.getAndDecrement(); }