Ejemplo n.º 1
0
        public static IEnumerable <T> FastSpool <T>(this MongoCursor <RawBsonDocument> cursor, int threads = 2)
        {
            var binaryReaderSettings = new BsonBinaryReaderSettings();

            binaryReaderSettings.MaxDocumentSize = int.MaxValue;
            var serializer = BsonSerializer.LookupSerializer(typeof(T));

            return(cursor
                   .AsParallel().AsOrdered()
                   .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
                   .WithDegreeOfParallelism(Math.Max(1, threads))
                   .Select(doc =>
            {
                using (doc)
                {
                    return (T)serializer.Deserialize(new BsonBinaryReader(new BsonBuffer(doc.Slice, false), binaryReaderSettings),
                                                     typeof(T), null);
                }
            }));
        }