[Zodb-checkins] CVS: Zope/lib/python/BTrees - BTreeTemplate.c:1.29

Jim Fulton jim@zope.com
Thu, 30 May 2002 17:47:40 -0400


Update of /cvs-repository/Zope/lib/python/BTrees
In directory cvs.zope.org:/tmp/cvs-serv7658

Modified Files:
	BTreeTemplate.c 
Log Message:
Backed off fix made in 1.27, which seems to cause intermittent core
dumps. Copied source from 1.26.


=== Zope/lib/python/BTrees/BTreeTemplate.c 1.28 => 1.29 ===
     }
 
-  /* Binary search to find insertion point.
-     min will be set to the index of the child that might have the key. 
-  */
+  /* Binary search to find insertion point */
   for (min=0, max=self->len, i=max/2; max-min > 1; i=(max+min)/2)
     {
       d=self->data+i;
@@ -372,26 +370,20 @@
       else max=i;
     }
 
-  d=self->data+min;             /* Get item */
-  if (SameType_Check(self, d->value)) /* Child is a BTree */
-    grew = _BTree_set( BTREE(d->value), keyarg, value, unique, noval);
-  else                          /* Child is a bucket */
-    grew = _bucket_set(BUCKET(d->value), keyarg, value, unique, noval, 
-                       &bchanged);
-
+  d=self->data+min;
+  if (SameType_Check(self, d->value))
+    grew= _BTree_set( BTREE(d->value), keyarg, value, unique, noval);
+  else
+    grew=_bucket_set(BUCKET(d->value), keyarg, value, unique, noval, 
+                     &bchanged);
   if (grew < 0) goto err;
 
-  /* grew >0 if we changed the length of the BTree.
-     If we got smaller and a bucket got deleted, then
-     grew might be >1 to indicate that we need to adjust previous
-     bucket pointers, if we can.
-  */
   if (grew)
     {
-      bchanged=1;  /* A bucket changed size */
-      if (value)   /* got bigger, check for max size exceeded */
+      bchanged=1;               /* A bucket changed size */
+      if (value)			/* got bigger */
 	{
-          if (SameType_Check(self, d->value)) /* BTree */
+          if (SameType_Check(self, d->value))
             {
               if (BTREE(d->value)->len > MAX_BTREE_SIZE(d->value))
                 {
@@ -399,7 +391,7 @@
                   changed=1;
                 }
             }          
-          else                  /* Bucket */
+          else
             {
               if (BUCKET(d->value)->len > MAX_BUCKET_SIZE(d->value))
                 {
@@ -449,46 +441,38 @@
               if (min < self->len)
                 memmove(d, d+1, (self->len-min)*sizeof(BTreeItem));
 
-              changed=1;
-            }
-          
-          if (min == 0 && grew > 1) 
-            /* The first bucket got deleted by us or a child.
-               If grew is > 1 then either we deleted the first one and
-               incremented grew ourself (just above) or
-               a child deleted it's first bucket and nobody could
-               adjust the pervious bucket's pointer, because there
-               were no previous buckets. Thus, it *must* be the first!
-            */
-            {
-              if (self->len)
-                { /* We just deleted our first child, so we need to
-                     adjust our first bucket. */
-                  if (SameType_Check(self, self->data->value))
-                    {
-                      UNLESS (PER_USE(BTREE(self->data->value))) goto err;
-                      ASSIGNB(self->firstbucket, 
-                              BTREE(self->data->value)->firstbucket);
-                      Py_XINCREF(self->firstbucket);
-                      PER_ALLOW_DEACTIVATION(BTREE(self->data->value));
-                      PER_ACCESSED(BTREE(self->data->value));
+              if (! min) 
+                {
+                  if (self->len)
+                    { /* We just deleted our first child, so we need to
+                         adjust our first bucket. */
+                      if (SameType_Check(self, self->data->value))
+                        {
+                          UNLESS (PER_USE(BTREE(self->data->value))) goto err;
+                          ASSIGNB(self->firstbucket, 
+                                  BTREE(self->data->value)->firstbucket);
+                          Py_XINCREF(self->firstbucket);
+                          PER_ALLOW_DEACTIVATION(BTREE(self->data->value));
+                          PER_ACCESSED(BTREE(self->data->value));
+                        }
+                      else
+                        {
+                          ASSIGNB(self->firstbucket, 
+                                  BUCKET(self->data->value));
+                          Py_INCREF(self->firstbucket);
+                        }
+                      /* We can toss our first key now */
+                      DECREF_KEY(self->data->key);
                     }
-                  else
+                  else 
                     {
-                      ASSIGNB(self->firstbucket, 
-                              BUCKET(self->data->value));
-                      Py_INCREF(self->firstbucket);
+                      Py_XDECREF(self->firstbucket);
+                      self->firstbucket = 0;
                     }
-                  /* We can toss our first key now */
-                  DECREF_KEY(self->data->key);
-                }
-              else 
-                {
-                  Py_XDECREF(self->firstbucket);
-                  self->firstbucket = 0;
                 }
+
+              changed=1;
             }
-          
         }
     }