public static void loadFromDbThenSerializeToJsonThenSaveToDir(IMongoDatabase db, IExportImportPathInfoProvider pathInfo, string collectionNamePrefix, ISimmpleProgressReporter progressReporter, CodeApproachType codeApproach = CodeApproachType.AsyncFunctional) { if (db == null) { return; } var dirName = $@"{pathInfo.RootPath}\{pathInfo.WorkingDirName} {collectionNamePrefix}"; progressReporter?.Start($"Start exporting data from {db.DatabaseNamespace.DatabaseName} to {dirName}"); Directory.CreateDirectory(dirName); progressReporter?.Report($"{dirName} was created"); var filter = Builders <BsonDocument> .Filter.Regex("name", $"^{collectionNamePrefix}"); var localFunctions = new Dictionary <CodeApproachType, Action>() { { CodeApproachType.AsyncFunctional, doItAsync }, { CodeApproachType.SyncFunctional, doItSync }, { CodeApproachType.SyncNotFunctional, doItSyncAndNotFuncional } }; localFunctions[codeApproach].Invoke(); progressReporter?.End("Export complete"); #region Local functions IMongoCollection <BsonDocument> getCollectionByName(string name) => db.GetCollection <BsonDocument>(name); void reportProgressAndSaveSyncFunc(Tuple <string, IEnumerable <string> > tuple) { progressReporter?.Report($"Saving {tuple.Item1}"); saveSyncFunc(tuple.Item1, tuple.Item2); progressReporter?.Report($"Complete {tuple.Item1}"); } void saveSyncFunc(string fileName, IEnumerable <string> linesToWrite) { using (var streamWriter = new StreamWriter($"{dirName}/{fileName}.{pathInfo.FileExtension}")) linesToWrite .ToList() .ForEach(line => streamWriter.WriteLine(line)); } void doItAsync() => db.ListCollectionNames(new ListCollectionNamesOptions { Filter = filter }) .ToEnumerable() .AsParallel() .Select(getCollectionByName) .Select(col => Tuple.Create(col.CollectionNamespace.CollectionName, serializeCollectionParallel(col))) .ForAll(reportProgressAndSaveSyncFunc); void doItSync() => db.ListCollectionNames(new ListCollectionNamesOptions { Filter = filter }) .ToEnumerable() .Select(getCollectionByName) .Select(col => Tuple.Create(col.CollectionNamespace.CollectionName, serializeCollection(col))) .ToList() .ForEach(reportProgressAndSaveSyncFunc); void doItSyncAndNotFuncional() { using (IAsyncCursor <string> cursor = db.ListCollectionNames(new ListCollectionNamesOptions { Filter = filter })) { while (cursor.MoveNext()) { foreach (var collectionName in cursor.Current) { var collection = getCollectionByName(collectionName); var colName = collection.CollectionNamespace.CollectionName; progressReporter?.Report($"Saving {colName}"); using (var fluentCursor = collection.Find(new BsonDocument()).ToCursor()) while (fluentCursor.MoveNext()) { foreach (var document in fluentCursor.Current) { using (var streamWriter = new StreamWriter($"{dirName}/{colName}.{pathInfo.FileExtension}")) using (var stringWriter = new StringWriter()) using (var jsonWriter = new JsonWriter(stringWriter)) { var serializationContext = BsonSerializationContext.CreateRoot(jsonWriter); collection.DocumentSerializer.Serialize(serializationContext, document); var serializedLine = stringWriter.ToString(); streamWriter.WriteLine(serializedLine); } } } progressReporter?.Report($"Complete"); } } } } #endregion }
public static void loadFromDirThenDeserializeFromJsonThenSaveToDb(IMongoDatabase db, IExportImportPathInfoProvider pathInfoProvider, ISimmpleProgressReporter progressReporter = null, CodeApproachType codeApproach = CodeApproachType.AsyncFunctional) { var dirPath = $@"{pathInfoProvider.RootPath}\{pathInfoProvider.WorkingDirName}"; if (Directory.Exists(dirPath) && db != null) { var localFunctions = new Dictionary <CodeApproachType, Action>() { { CodeApproachType.AsyncFunctional, doItAsync }, { CodeApproachType.SyncFunctional, doItSync }, { CodeApproachType.SyncNotFunctional, doItSyncAndNotFuncional } }; progressReporter?.Start($"Start importing data from {dirPath} to {db.DatabaseNamespace.DatabaseName}"); localFunctions[codeApproach].Invoke(); progressReporter?.End($"All data from {dirPath} was loaded successfully"); } #region Local functions void doItAsync() => Directory.EnumerateFiles(dirPath, $"*.{pathInfoProvider.FileExtension}", SearchOption.TopDirectoryOnly) .AsParallel() .Select(fullPath => new { Name = Path.GetFileNameWithoutExtension(fullPath), Lines = readLinesFromFile(fullPath) }) .Select(file => new { Collection = dropThenCreateCollection(db, file.Name), Documents = file.Lines.Select(deserializeFunc) }) .ForAll(async deserialized => { var name = deserialized.Collection.CollectionNamespace.CollectionName; progressReporter?.Report($"Loading {name}"); await deserialized.Collection.InsertManyAsync(deserialized.Documents); progressReporter?.Report($"Complete {name}"); }); void doItSync() => Directory.EnumerateFiles(dirPath, $"*.{pathInfoProvider.FileExtension}", SearchOption.TopDirectoryOnly) .Select(fullPath => new { Name = Path.GetFileNameWithoutExtension(fullPath), Lines = readLinesFromFile(fullPath) }) .Select(file => new { Collection = dropThenCreateCollection(db, file.Name), Documents = file.Lines.Select(deserializeFunc) }) .ToList() .ForEach(deserialized => { var name = deserialized.Collection.CollectionNamespace.CollectionName; progressReporter?.Report($"Loading {name}"); deserialized.Collection.InsertMany(deserialized.Documents); progressReporter?.Report($"Complete {name}"); }); void doItSyncAndNotFuncional() { foreach (var fullPath in Directory.EnumerateFiles(dirPath, $"*.{pathInfoProvider.FileExtension}", SearchOption.TopDirectoryOnly)) { var collectionName = Path.GetFileNameWithoutExtension(fullPath); var collection = dropThenCreateCollection(db, collectionName); progressReporter?.Report($"Loading {collectionName}"); using (var streamReader = new StreamReader(fullPath)) { string line; while ((line = streamReader.ReadLine()) != null) { var document = BsonSerializer.Deserialize <BsonDocument>(line); collection.InsertOne(document); } } progressReporter?.Report($"Complete {collectionName}"); } } BsonDocument deserializeFunc(string line) => BsonSerializer.Deserialize <BsonDocument>(line); #endregion }
public void Export(string namePrefix, CodeApproachType testType = CodeApproachType.AsyncFunctional) => loadFromDbThenSerializeToJsonThenSaveToDir(this.MongoDb, this.PathInfo, namePrefix, this.ProgressReporter, testType);
public void Export(CodeApproachType codeApproach = CodeApproachType.AsyncFunctional) => loadFromDirThenDeserializeFromJsonThenSaveToDb(MongoDb, PathInfo, ProgressReporter, codeApproach);