You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by ty...@apache.org on 2014/08/21 21:00:39 UTC

git commit: Use python driver for UDTs in DESCRIBE output

Repository: cassandra
Updated Branches:
  refs/heads/cassandra-2.1 d586ea898 -> d088f0299


Use python driver for UDTs in DESCRIBE output

Patch by Tyler Hobbs; review by Aleksey Yeschenko for CASSANDRA-7659


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/d088f029
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/d088f029
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/d088f029

Branch: refs/heads/cassandra-2.1
Commit: d088f02993e44089d6bd7711aae549e7f2bc37f5
Parents: d586ea8
Author: Tyler Hobbs <ty...@datastax.com>
Authored: Thu Aug 21 13:59:26 2014 -0500
Committer: Tyler Hobbs <ty...@datastax.com>
Committed: Thu Aug 21 13:59:26 2014 -0500

----------------------------------------------------------------------
 CHANGES.txt                    |  2 ++
 bin/cqlsh                      | 57 ++++++++++++++-----------------------
 pylib/cqlshlib/cql3handling.py | 48 ++++---------------------------
 3 files changed, 30 insertions(+), 77 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/d088f029/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 11ec7ff..701fd38 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,6 @@
 2.1.1
+ * (cqlsh) Order UDTs according to cross-type dependencies in DESCRIBE
+   output (CASSANDRA-7659)
  * (cqlsh) Fix handling of CAS statement results (CASSANDRA-7671)
  * (cqlsh) COPY TO/FROM improvements (CASSANDRA-7405)
  * Support list index operations with conditions (CASSANDRA-7499)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/d088f029/bin/cqlsh
----------------------------------------------------------------------
diff --git a/bin/cqlsh b/bin/cqlsh
index b633e93..3fb972a 100755
--- a/bin/cqlsh
+++ b/bin/cqlsh
@@ -652,16 +652,21 @@ class Shell(cmd.Cmd):
         if ksname is None:
             ksname = self.current_keyspace
 
-        return self.get_usertypes_meta().get_usertypes_names(ksname)
+        return self.get_keyspace_meta(ksname).user_types.keys()
 
     def get_usertype_layout(self, ksname, typename):
         if ksname is None:
             ksname = self.current_keyspace
-        layout = self.get_usertypes_meta().get_fields_with_types(ksname, typename)
 
-        if not layout:
+        ks_meta = self.get_keyspace_meta(ksname)
+
+        try:
+            user_type = ks_meta.user_types[typename]
+        except KeyError:
             raise UserTypeNotFound("User type %r not found" % typename)
-        return layout
+
+        return [(field_name, field_type.cql_parameterized_type())
+                for field_name, field_type in zip(user_type.field_names, user_type.field_types)]
 
     def get_cluster_name(self):
         return self.conn.metadata.cluster_name
@@ -1070,32 +1075,8 @@ class Shell(cmd.Cmd):
             return cqlruleset.dequote_value(valstr)
 
     def print_recreate_keyspace(self, ksdef, out):
-        ksname = protect_name(ksdef.name)
-        out.write(ksdef.as_cql_query())
-        out.write("\n\n")
-
-        uts = self.get_usertypes_meta().get_usertypes_names(ksdef.name)
-        if uts:
-            out.write("USE %s;\n" % ksname)
-            for ut in uts:
-                out.write('\n')
-                self.print_recreate_usertype(ksdef.name, ut, out)
-
-        cfs = self.get_columnfamily_names(ksdef.name)
-        if cfs:
-            for cf in cfs:
-                out.write('\n')
-                self.print_recreate_columnfamily(ksdef.name, cf, out)
-
-    def print_recreate_usertype(self, ksname, utname, out):
-        layout = self.get_usertype_layout(ksname, utname)
-        out.write("CREATE TYPE %s (\n" % utname)
-        for (index,(tname,ttype)) in enumerate(layout, 1):
-            out.write("  %s %s" % (tname, ttype))
-            if index < len(layout):
-                out.write(",\n")
-            else:
-                out.write("\n); \n")
+        out.write(ksdef.export_as_string())
+        out.write("\n")
 
     def print_recreate_columnfamily(self, ksname, cfname, out):
         """
@@ -1140,21 +1121,27 @@ class Shell(cmd.Cmd):
     def describe_usertypes(self, ksname):
         print
         if ksname is None:
-            for k in self.get_keyspaces():
-                name = protect_name(k.name)
+            for ksmeta in self.get_keyspaces():
+                name = protect_name(ksmeta.name)
                 print 'Keyspace %s' % (name,)
                 print '---------%s' % ('-' * len(name))
-                cmd.Cmd.columnize(self, protect_names(self.get_usertype_names(k.name)))
+                cmd.Cmd.columnize(self, protect_names(ksmeta.user_types.keys()))
                 print
         else:
-            cmd.Cmd.columnize(self, protect_names(self.get_usertype_names(ksname)))
+            ksmeta = self.get_keyspace_meta(ksname)
+            cmd.Cmd.columnize(self, protect_names(ksmeta.user_types.keys()))
             print
 
     def describe_usertype(self, ksname, typename):
         if ksname is None:
             ksname = self.current_keyspace
         print
-        self.print_recreate_usertype(ksname, typename, sys.stdout)
+        ksmeta = self.get_keyspace_meta(ksname)
+        try:
+            usertype = ksmeta.user_types[typename]
+        except KeyError:
+            raise UserTypeNotFound("User type %r not found" % typename)
+        print usertype.as_cql_query(formatted=True)
         print
 
     def describe_cluster(self):

http://git-wip-us.apache.org/repos/asf/cassandra/blob/d088f029/pylib/cqlshlib/cql3handling.py
----------------------------------------------------------------------
diff --git a/pylib/cqlshlib/cql3handling.py b/pylib/cqlshlib/cql3handling.py
index e4ef67b..7655413 100644
--- a/pylib/cqlshlib/cql3handling.py
+++ b/pylib/cqlshlib/cql3handling.py
@@ -26,11 +26,6 @@ simple_cql_types.difference_update(('set', 'map', 'list'))
 from . import helptopics
 cqldocs = helptopics.CQL3HelpTopics()
 
-try:
-    import json
-except ImportError:
-    import simplejson as json
-
 class UnexpectedTableStructure(UserWarning):
     def __init__(self, msg):
         self.msg = msg
@@ -131,13 +126,12 @@ class Cql3ParsingRuleSet(CqlParsingRuleSet):
 CqlRuleSet = Cql3ParsingRuleSet()
 
 # convenience for remainder of module
-shorthands = ('completer_for', 'explain_completion',
-              'dequote_value', 'dequote_name',
-              'escape_value',
-              'maybe_escape_name')
-
-for shorthand in shorthands:
-    globals()[shorthand] = getattr(CqlRuleSet, shorthand)
+completer_for = CqlRuleSet.completer_for
+explain_completion = CqlRuleSet.explain_completion
+dequote_value = CqlRuleSet.dequote_value
+dequote_name = CqlRuleSet.dequote_name
+escape_value = CqlRuleSet.escape_value
+maybe_escape_name = CqlRuleSet.maybe_escape_name
 
 
 # BEGIN SYNTAX/COMPLETION RULE DEFINITIONS
@@ -1162,33 +1156,3 @@ def username_name_completer(ctxt, cass):
 # END SYNTAX/COMPLETION RULE DEFINITIONS
 
 CqlRuleSet.append_rules(syntax_rules)
-
-from cassandra.cqltypes import lookup_casstype
-
-class UserTypesMeta(object):
-    _meta = {}
-
-    def __init__(self, meta):
-        self._meta = meta
-
-    @classmethod
-    def from_layout(cls, layout):
-        result = {}
-        for row in layout:
-            ksname = row['keyspace_name']
-            if ksname not in result:
-                result[ksname] = {}
-            utname = row['type_name']
-
-            result[ksname][utname] = zip(row['field_names'], row['field_types'])
-        return cls(meta=result)
-
-    def get_usertypes_names(self, keyspace):
-        return map(str, self._meta.get(keyspace, {}).keys())
-
-    def get_field_names(self, keyspace, type):
-        return [row[0] for row in self._meta.get(keyspace, {}).get(type, [])]
-
-    def get_fields_with_types(self, ksname, typename):
-        return [(field[0], lookup_casstype(field[1]).cql_parameterized_type()) for field in
-                self._meta.get(ksname, {}).get(typename, [])]