private void bulkInsertDocuments <T>(IReadOnlyCollection <T> documents, int batchSize, NpgsqlConnection conn, BulkInsertMode mode) { var loader = _tenant.BulkLoaderFor <T>(); if (mode != BulkInsertMode.InsertsOnly) { var sql = loader.CreateTempTableForCopying(); conn.RunSql(sql); } var writer = _writerPool.Lease(); try { if (documents.Count <= batchSize) { loadDocuments(documents, loader, mode, conn, writer); } else { var batch = new List <T>(batchSize); foreach (var document in documents) { batch.Add(document); if (batch.Count < batchSize) { continue; } loadDocuments(batch, loader, mode, conn, writer); batch.Clear(); } loadDocuments(batch, loader, mode, conn, writer); } } finally { if (writer != null) { _writerPool.Release(writer); } } if (mode == BulkInsertMode.IgnoreDuplicates) { var copy = loader.CopyNewDocumentsFromTempTable(); conn.RunSql(copy); } else if (mode == BulkInsertMode.OverwriteExisting) { var overwrite = loader.OverwriteDuplicatesFromTempTable(); var copy = loader.CopyNewDocumentsFromTempTable(); conn.RunSql(overwrite, copy); } }
private void bulkInsertDocuments <T>(T[] documents, int batchSize, NpgsqlConnection conn, BulkInsertMode mode) { var loader = _tenant.BulkLoaderFor <T>(); if (mode != BulkInsertMode.InsertsOnly) { var sql = loader.CreateTempTableForCopying(); conn.RunSql(sql); } var writer = _writerPool.Lease(); try { if (documents.Length <= batchSize) { if (mode == BulkInsertMode.InsertsOnly) { loader.Load(_tenant, Serializer, conn, documents, writer); } else { loader.LoadIntoTempTable(_tenant, Serializer, conn, documents, writer); } } else { var total = 0; var page = 0; while (total < documents.Length) { var batch = documents.Skip(page * batchSize).Take(batchSize).ToArray(); if (mode == BulkInsertMode.InsertsOnly) { loader.Load(_tenant, Serializer, conn, batch, writer); } else { loader.LoadIntoTempTable(_tenant, Serializer, conn, batch, writer); } page++; total += batch.Length; } } } finally { if (writer != null) { _writerPool.Release(writer); } } if (mode == BulkInsertMode.IgnoreDuplicates) { var copy = loader.CopyNewDocumentsFromTempTable(); conn.RunSql(copy); } else if (mode == BulkInsertMode.OverwriteExisting) { var overwrite = loader.OverwriteDuplicatesFromTempTable(); var copy = loader.CopyNewDocumentsFromTempTable(); conn.RunSql(overwrite, copy); } }
public IBulkLoader <T> BulkLoaderFor <T>() { return(_inner.BulkLoaderFor <T>()); }