You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by sh...@apache.org on 2009/04/22 07:35:48 UTC

svn commit: r767376 - in /hadoop/avro/trunk: CHANGES.txt src/py/avro/schema.py src/test/py/interoptests.py src/test/py/testio.py

Author: sharad
Date: Wed Apr 22 05:35:47 2009
New Revision: 767376

URL: http://svn.apache.org/viewvc?rev=767376&view=rev
Log:
AVRO-15. Override __eq__() and __hash__() in Schema classes.

Modified:
    hadoop/avro/trunk/CHANGES.txt
    hadoop/avro/trunk/src/py/avro/schema.py
    hadoop/avro/trunk/src/test/py/interoptests.py
    hadoop/avro/trunk/src/test/py/testio.py

Modified: hadoop/avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/CHANGES.txt?rev=767376&r1=767375&r2=767376&view=diff
==============================================================================
--- hadoop/avro/trunk/CHANGES.txt (original)
+++ hadoop/avro/trunk/CHANGES.txt Wed Apr 22 05:35:47 2009
@@ -22,6 +22,9 @@
 
     AVRO-16.  Minor documentation improvements.  (cutting)
 
+    AVRO-15. Override __eq__() and __hash__() in Schema classes.
+    (sharad)
+
   OPTIMIZATIONS
 
   BUG FIXES

Modified: hadoop/avro/trunk/src/py/avro/schema.py
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/py/avro/schema.py?rev=767376&r1=767375&r2=767376&view=diff
==============================================================================
--- hadoop/avro/trunk/src/py/avro/schema.py (original)
+++ hadoop/avro/trunk/src/py/avro/schema.py Wed Apr 22 05:35:47 2009
@@ -44,6 +44,14 @@
   def gettype(self):
     return self.__type
 
+  def __eq__(self, other, seen=None):
+    if self is other:
+      return True
+    return isinstance(other, Schema) and self.__type == other.__type
+
+  def __hash__(self, seen=None):
+    return self.__type.__hash__()
+
 class _StringSchema(Schema):
   def __init__(self):
     Schema.__init__(self, STRING)
@@ -149,6 +157,30 @@
     str.write("}}")
     return str.getvalue()
 
+  def __eq__(self, other, seen={}):
+    if self is other or seen.get(id(self)) is other:
+      return True
+    if isinstance(other, _RecordSchema):
+      size = len(self.__fields)
+      if len(other.__fields) != size:
+        return False
+      seen[id(self)] = other
+      for i in range(0, size):
+        if not self.__fields[i][1].__eq__(other.__fields[i][1], seen):
+          return False
+      return True
+    else:
+      return False
+
+  def __hash__(self, seen=set()):
+    if seen.__contains__(id(self)):
+      return 0
+    seen.add(id(self))
+    hash = self.gettype().__hash__() 
+    for field, fieldschm in self.__fields:
+      hash = hash + fieldschm.__hash__(seen)
+    return hash
+
 class _ArraySchema(Schema):
   def __init__(self, elemtype):
     Schema.__init__(self, ARRAY)
@@ -164,6 +196,19 @@
     str.write("}")
     return str.getvalue()
 
+  def __eq__(self, other, seen={}):
+    if self is other or seen.get(id(self)) is other:
+      return True
+    seen[id(self)]= other
+    return (isinstance(other, _ArraySchema) and 
+            self.__elemtype.__eq__(other.__elemtype, seen))
+
+  def __hash__(self, seen=set()):
+    if seen.__contains__(id(self)):
+      return 0
+    seen.add(id(self))
+    return self.gettype().__hash__() + self.__elemtype.__hash__(seen)
+
 class _MapSchema(Schema):
   def __init__(self, keytype, valuetype):
     Schema.__init__(self, MAP)
@@ -176,7 +221,6 @@
   def getvaluetype(self):
     return self.__vtype
 
-
   def str(self, names):
     str = cStringIO.StringIO()
     str.write("{\"type\": \"map\", \"keys\":  ")
@@ -186,6 +230,22 @@
     str.write("}")
     return str.getvalue()
 
+  def __eq__(self, other, seen={}):
+    if self is other or seen.get(id(self)) is other:
+      return True
+    seen[id(self)]= other
+    return (isinstance(other, _MapSchema) and 
+            self.__ktype.__eq__(other.__ktype, seen) and 
+            self.__vtype.__eq__(other.__vtype), seen)
+
+  def __hash__(self, seen=set()):
+    if seen.__contains__(id(self)):
+      return 0
+    seen.add(id(self))
+    return (self.gettype().__hash__() + 
+            self.__ktype.__hash__(seen) +
+            self.__vtype.__hash__(seen))
+
 class _UnionSchema(Schema):
   def __init__(self, elemtypes):
     Schema.__init__(self, UNION)
@@ -206,6 +266,30 @@
     str.write("]")
     return str.getvalue()
 
+  def __eq__(self, other, seen={}):
+    if self is other or seen.get(id(self)) is other:
+      return True
+    seen[id(self)]= other
+    if isinstance(other, _UnionSchema):
+      size = len(self.__elemtypes)
+      if len(other.__elemtypes) != size:
+        return False
+      for i in range(0, size):
+        if not self.__elemtypes[i].__eq__(other.__elemtypes[i], seen):
+          return False
+      return True
+    else:
+      return False
+
+  def __hash__(self, seen=set()):
+    if seen.__contains__(id(self)):
+      return 0
+    seen.add(id(self))
+    hash = self.gettype().__hash__() 
+    for elem in self.__elemtypes:
+      hash = hash + elem.__hash__(seen)
+    return hash
+
 _PRIMITIVES = {'string':_StringSchema(),
         'bytes':_BytesSchema(),
         'int':_IntSchema(),

Modified: hadoop/avro/trunk/src/test/py/interoptests.py
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/test/py/interoptests.py?rev=767376&r1=767375&r2=767376&view=diff
==============================================================================
--- hadoop/avro/trunk/src/test/py/interoptests.py (original)
+++ hadoop/avro/trunk/src/test/py/interoptests.py Wed Apr 22 05:35:47 2009
@@ -41,8 +41,7 @@
                              self.__datumreader())
       count = int(dr.getmeta("count"))
       decodedSchm = schema.parse(dr.getmeta("schema"))
-      self.assertEquals(schema.stringval(origschm), 
-                        schema.stringval(decodedSchm))
+      self.assertEquals(origschm, decodedSchm)
       for i in range(0,count):
         datum = dr.next()
         self.assertTrue(self.__validator(origschm, datum))

Modified: hadoop/avro/trunk/src/test/py/testio.py
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/test/py/testio.py?rev=767376&r1=767375&r2=767376&view=diff
==============================================================================
--- hadoop/avro/trunk/src/test/py/testio.py (original)
+++ hadoop/avro/trunk/src/test/py/testio.py Wed Apr 22 05:35:47 2009
@@ -150,6 +150,10 @@
     schm = schema.parse(string)
     st = schema.stringval(schm)
     self.assertEquals(string.replace(" ",""), st.replace(" ",""))
+    #test __eq__
+    self.assertEquals(schm, schema.parse(string))
+    #test hashcode doesn't generate infinite recursion
+    schm.__hash__()
     randomdata = self.__random(schm)
     for i in range(1,10):
       self.checkser(schm, randomdata)