-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added delete method to GridOut + more methods
- Loading branch information
Giorgio Franceschetti
committed
Mar 12, 2023
1 parent
34069de
commit 0dec04b
Showing
10 changed files
with
304 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import 'dart:async'; | ||
import 'dart:io'; | ||
|
||
import 'package:mongo_dart/mongo_dart.dart'; | ||
import 'package:path/path.dart'; | ||
|
||
const dbName = 'mongo-dart-example'; | ||
const dbAddress = '127.0.0.1'; | ||
|
||
const defaultUri = 'mongodb://$dbAddress:27017/$dbName'; | ||
|
||
class MockConsumer implements StreamConsumer<List<int>> { | ||
List<int> data = []; | ||
|
||
Future consume(Stream<List<int>> stream) { | ||
var completer = Completer(); | ||
stream.listen(_onData, onDone: () => completer.complete(null)); | ||
return completer.future; | ||
} | ||
|
||
void _onData(List<int> chunk) { | ||
data.addAll(chunk); | ||
} | ||
|
||
@override | ||
Future addStream(Stream<List<int>> stream) { | ||
var completer = Completer(); | ||
stream.listen(_onData, onDone: () => completer.complete(null)); | ||
return completer.future; | ||
} | ||
|
||
@override | ||
Future close() => Future.value(true); | ||
} | ||
|
||
void main() async { | ||
var db = Db(defaultUri); | ||
await db.open(); | ||
|
||
Future cleanupDatabase() async { | ||
await db.close(); | ||
} | ||
|
||
if (!db.masterConnection.serverCapabilities.supportsOpMsg) { | ||
return; | ||
} | ||
|
||
var collectionName = 'delete-gridfs'; | ||
var gridFS = GridFS(db, collectionName); | ||
await gridFS.dropBucket(); | ||
|
||
// **** data preparation | ||
var smallData = <int>[ | ||
0x00, | ||
0x01, | ||
0x10, | ||
0x11, | ||
0x7e, | ||
0x7f, | ||
0x80, | ||
0x81, | ||
0xfe, | ||
0xff | ||
]; | ||
|
||
// Set small chunks | ||
GridFS.defaultChunkSize = 9; | ||
|
||
// assures at least 3 chunks | ||
var target = GridFS.defaultChunkSize * 3; | ||
var data = <int>[]; | ||
while (data.length < target) { | ||
data.addAll(smallData); | ||
} | ||
print('Expected chunks: ${(data.length / GridFS.defaultChunkSize).ceil()}'); | ||
var extraData = <String, dynamic>{ | ||
'test': [1, 2, 3], | ||
'extraData': 'Test', | ||
'map': {'a': 1} | ||
}; | ||
|
||
var inputStream = Stream.fromIterable([data]); | ||
var input = gridFS.createFile(inputStream, 'test'); | ||
input.extraData = extraData; | ||
await input.save(); | ||
|
||
var gridOut = await gridFS.findOne(where.eq('_id', input.id)); | ||
var consumer = MockConsumer(); | ||
var out = IOSink(consumer); | ||
await gridOut?.writeTo(out); | ||
|
||
await gridOut?.delete(); | ||
|
||
print('Out Chunk size: ${gridOut?.chunkSize}'); // 9 | ||
print('Out Chunk lengt: ${gridOut?.length}'); | ||
print('Out Chunk num: ${gridOut?.numChunks()}'); | ||
|
||
await cleanupDatabase(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import 'dart:io'; | ||
import 'package:path/path.dart' as p; | ||
|
||
extension FileExt on File { | ||
String get name => | ||
path.substring(path.lastIndexOf(Platform.pathSeparator) + 1); | ||
|
||
String newPathByName(String newFileName) => | ||
path.substring(0, path.lastIndexOf(Platform.pathSeparator) + 1) + | ||
newFileName; | ||
|
||
Future<String> get safePath async => toSafePath(path); | ||
|
||
Future<String> toSafePath(String newPath) async { | ||
var basename = p.basenameWithoutExtension(newPath); | ||
var dirname = p.dirname(newPath) + Platform.pathSeparator; | ||
var ext = p.extension(newPath); | ||
|
||
String tryPath = newPath; | ||
File newFile = File(tryPath); | ||
var count = 1; | ||
while (await newFile.exists()) { | ||
tryPath = '$dirname$basename($count)$ext'; | ||
count++; | ||
newFile = File(tryPath); | ||
} | ||
return tryPath; | ||
} | ||
|
||
Future<File> changeFileNameOnly(String newFileName) async => | ||
rename(newPathByName(newFileName)); | ||
|
||
Future<File> changeFileNameOnlySafe(String newFileName) async => | ||
renameSafe(newPathByName(newFileName)); | ||
|
||
Future<File> renameSafe(String newPath) async => | ||
rename(await toSafePath(newPath)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:mongo_dart/src/extensions/file_ext.dart'; | ||
import 'package:path/path.dart'; | ||
import 'package:test/test.dart'; | ||
import 'package:uuid/uuid.dart'; | ||
|
||
Future<void> main() async { | ||
var dir = Directory(current); | ||
var filename = Uuid().v4(); | ||
var file = File('${dir.path}${Platform.pathSeparator}$filename'); | ||
await file.writeAsString('Test'); | ||
group('Base methods', () { | ||
test('Name', () { | ||
expect(file.name, filename); | ||
}); | ||
test('New Path by Name', () { | ||
expect(file.newPathByName('prova'), | ||
'${dir.path}${Platform.pathSeparator}prova'); | ||
}); | ||
test('Safe Path', () async { | ||
var newPath = await file.safePath; | ||
expect(newPath, '${dir.path}${Platform.pathSeparator}$filename(1)'); | ||
}); | ||
test('To Safe Path', () async { | ||
var newPath = await file.toSafePath(file.newPathByName('prova')); | ||
expect(newPath, '${dir.path}${Platform.pathSeparator}prova'); | ||
}); | ||
}); | ||
|
||
group('Change File Name', () { | ||
test('Change File Name Only', () async { | ||
var changedFile = await file.copy(file.newPathByName('chgf')); | ||
var newFile = await changedFile.changeFileNameOnly('testo'); | ||
var name = newFile.name; | ||
await newFile.delete(); | ||
expect(name, 'testo'); | ||
}); | ||
|
||
test('Change File Name Only - two tries', () async { | ||
var changedFile = await file.copy(file.newPathByName('chgf2')); | ||
var newFile = await changedFile.changeFileNameOnly('test2'); | ||
changedFile = await file.copy(file.newPathByName('chgf2')); | ||
var newFile2 = await changedFile.changeFileNameOnly('test2'); | ||
var name = newFile2.name; | ||
var exists = await changedFile.exists(); | ||
var exists2 = await newFile2.exists(); | ||
|
||
await newFile2.delete(); | ||
expect(name, 'test2'); | ||
expect(exists, isFalse); | ||
expect(exists2, isTrue); | ||
}); | ||
}); | ||
group('Safe Change File Name', () { | ||
test('Safe Change File Name Only', () async { | ||
var changedFile = await file.copy(file.newPathByName('chgf4')); | ||
var newFile = await changedFile.changeFileNameOnlySafe('test4.ts'); | ||
var name = newFile.name; | ||
await newFile.delete(); | ||
expect(name, 'test4.ts'); | ||
}); | ||
|
||
test('Change File Name Only - two tries', () async { | ||
var changedFile = await file.copy(file.newPathByName('chgf3')); | ||
var newFile = await changedFile.changeFileNameOnlySafe('test3.1.ts'); | ||
changedFile = await file.copy(file.newPathByName('chgf3')); | ||
var newFile2 = await changedFile.changeFileNameOnlySafe('test3.1.ts'); | ||
var name = newFile.name; | ||
var name2 = newFile2.name; | ||
|
||
var exists = await changedFile.exists(); | ||
var exists2 = await newFile.exists(); | ||
var exists3 = await newFile2.exists(); | ||
|
||
await newFile.delete(); | ||
await newFile2.delete(); | ||
|
||
expect(name, 'test3.1.ts'); | ||
expect(name2, 'test3.1(1).ts'); | ||
|
||
expect(exists, isFalse); | ||
expect(exists2, isTrue); | ||
expect(exists3, isTrue); | ||
}); | ||
}); | ||
|
||
tearDownAll(() async { | ||
await file.delete(); | ||
}); | ||
} |