Commits

jasonjwwilliams committed 7f6e6f3

Added RiakBucket.purge_keys() convenience function.

* Deletes all keys in the bucket. Leverages the
new RiakBucket.list_keys() function to retrieve
the current key list and then deletes each key.

Comments (0)

Files changed (2)

             keys = keys + temp_keys
         
         defer.returnValue(keys)
+    
+    @defer.inlineCallbacks
+    def purge_keys(self):
+        """
+        Purge all keys from the bucket.
+        
+        :returns: None
+        
+        This is a convenience function that lists all of the keys
+        in the bucket and then deletes them.
+        
+        NB: This is a VERY resource-intensive operation, and is
+            IRREVERSIBLE. Be careful.
+        """
+        
+        # Get the current key list
+        keys = yield self.list_keys()
+        
+        # Major key-killing action
+        for key in keys:
+            obj = yield self.get(key)
+            yield obj.delete()
+        
+        return
 
 class RiakObject(object):
     """
         yield obj1.store()
         
         keys = yield bucket.list_keys()
-        self.assertEquals(sorted([u"foo1", u"foo2"]), sorted(keys))
+        self.assertEqual(sorted([u"foo1", u"foo2"]), sorted(keys))
+    
+    @defer.inlineCallbacks
+    def test_purge_keys(self):
+        """Test purging all keys in a bucket."""
+        log.msg("*** purge_keys")
+        client = riak.RiakClient(client_id=RIAK_CLIENT_ID)
+        bucket = client.bucket("bucket")
+        obj = bucket.new("foo1", "test1")
+        yield obj.store()
+        obj1 = bucket.new("foo2", "test2")
+        yield obj1.store()
+        
+        yield bucket.purge_keys()
+        keys = yield bucket.list_keys()
+        self.assertEqual([], keys)
     
     @defer.inlineCallbacks
     def test_list_buckets(self):