Commits

Vladimir Mihailenco  committed 1bfe32a

Updates

  • Participants
  • Parent commits 1333ab0

Comments (0)

Files changed (3)

 class DatabaseFeatures(NonrelDatabaseFeatures):
     allows_primary_key_0 = True
     supports_dicts = True
-    supports_batches = True
+    supports_batching = True
+    batch_pool_size = 500
+    batch_size = 100
 
 class DatabaseOperations(NonrelDatabaseOperations):
     compiler_module = __name__.rsplit('.', 1)[0] + '.compiler'

File db/compiler.py

 
 
 class SQLBatchCompiler(SQLCompiler):
-    def __init__(self, query, connection, using, raw=False, datastore_deadline=15, **kwargs):
+    def __init__(self, query, connection, using, datastore_deadline=None, **kwargs):
         super(SQLBatchCompiler, self).__init__(query, connection, using)
-        self.raw = raw
         self.datastore_deadline = datastore_deadline
 
     def _create_rpc(self):
         return datastore.CreateRPC(deadline=self.datastore_deadline)
 
     @safe_call
-    def batch_save(self, instances):
+    def batch_save(self, instance_cls_values_raw_list):
         items = []
-        for instance in instances:
-            data = self.get_instance_data(instance, raw=self.raw)
-            items.append(self.create_entity(instance.__class__, data))
+        for instance, cls, values, raw in instance_cls_values_raw_list:
+            data = {}
+            for field, value in values:
+                db_type = field.db_type(connection=self.connection)
+                value = self.convert_value_for_db(db_type, value)
+
+                data[field.column] = value
+            items.append(self.create_entity(cls, data))
 
         keys = datastore.Put(items, rpc=self._create_rpc())
         pk_vals = [key.id_or_name() for key in keys]
         return pk_vals
 
     @safe_call
-    def batch_delete(self, instances):
+    def batch_delete(self, instance_cls_list):
         keys = []
-        for instance in instances:
-            keys.append(create_key(instance._meta.db_table, instance.pk))
+        for instance, cls in instance_cls_list:
+            keys.append(create_key(cls._meta.db_table, instance._get_pk_val(cls._meta)))
 
         datastore.Delete(keys, rpc=self._create_rpc())
 

File tests/batch.py

 from django.db.models.signals import post_save
 from django.db.models.signals import pre_delete
 from django.db.models.signals import post_delete
-from django.db.models import batch_query
+from django.db.models import BatchOperation
 from django.test import TestCase
 
 from google.appengine.api import datastore
         datastore.Delete = Delete
 
     def test_batch_save(self):
-        with batch_query() as q:
+        with BatchOperation() as q:
             categories = []
             for name in ['appengine', 'django']:
                 c = BatchCategory(name=name)
         self.assertEqual(BatchCategory.objects.count(), 2)
         self.assertEqual(mem.put_count, 1)
 
-        with batch_query() as q:
+        with BatchOperation() as q:
             categories = BatchCategory.objects.all()
             for c in categories:
                 q.delete(c)
         self.assertEqual(mem.delete_count, 1)
 
     def test_plain_operation_in_batch(self):
-        with batch_query() as q:
+        with BatchOperation() as q:
             appengine = BatchCategory(name='appengine')
             q.save(appengine)
             self.assertEqual(BatchCategory.objects.count(), 0)
 
         appengine = BatchCategory.objects.get(name='appengine')
         django = BatchCategory.objects.get(name='django')
-        with batch_query() as q:
+        with BatchOperation() as q:
             q.delete(appengine)
             self.assertEqual(BatchCategory.objects.count(), 2)