@@ -38,75 +38,83 @@ public Class<GivenJongoCollection> getGivenClass() {
3838 }
3939
4040 public GivenCleaner run (final GivenJongoCollection given , final ImmutableMap <String , String > params ) {
41- MongoClientURI mongoClientURI = new MongoClientURI (
42- checkNotNull (params .get (DB_URI ),
43- DB_URI + " param is required" ));
44- Jongo jongo = new Jongo (new MongoClient (mongoClientURI ).getDB (mongoClientURI .getDatabase ()));
4541 try {
46- Stopwatch stopwatch = Stopwatch .createStarted ();
47- MongoCollection collection = jongo .getCollection (given .getCollection ());
48- Iterable <String > items = Splitter .on ("\n " ).trimResults ().omitEmptyStrings ().split (given .getData ());
49- int count = 0 ;
50- for (String item : items ) {
51- collection .insert (item );
52- count ++;
42+ MongoClientURI mongoClientURI = new MongoClientURI (
43+ checkNotNull (params .get (DB_URI ),
44+ DB_URI + " param is required" ));
45+ Jongo jongo = new Jongo (new MongoClient (mongoClientURI ).getDB (mongoClientURI .getDatabase ()));
46+ try {
47+ Stopwatch stopwatch = Stopwatch .createStarted ();
48+ MongoCollection collection = jongo .getCollection (given .getCollection ());
49+ Iterable <String > items = Splitter .on ("\n " ).trimResults ().omitEmptyStrings ().split (given .getData ());
50+ int count = 0 ;
51+ for (String item : items ) {
52+ collection .insert (item );
53+ count ++;
54+ }
55+ System .out .printf ("imported %s[%d] -- %s%n" , given .getCollection (), count , stopwatch .stop ().toString ());
56+ } finally {
57+ jongo .getDatabase ().getMongo ().close ();
5358 }
54- System .out .printf ("imported %s[%d] -- %s%n" , given .getCollection (), count , stopwatch .stop ().toString ());
55- } finally {
56- jongo .getDatabase ().getMongo ().close ();
57- }
5859
59- final UnmodifiableIterator <String > it = given .getSequence ().iterator ();
60- final CollectionSequence iteratingSequence = new CollectionSequence () {
61- @ Override
62- public Optional <String > next () {
63- return it .hasNext () ? Optional .of (it .next ()) : Optional .<String >absent ();
64- }
65- };
66-
67- final SingleNameFactoryMachine <ComponentCustomizerEngine > customizerMachine =
68- new SingleNameFactoryMachine <>(0 , new StdMachineEngine <ComponentCustomizerEngine >(
69- Name .of (ComponentCustomizerEngine .class , "JongoCollectionSequenceSupplierOf"
70- + given .getCollection ()),
71- BoundlessComponentBox .FACTORY ) {
72- @ Override
73- public BillOfMaterials getBillOfMaterial () {
74- return BillOfMaterials .of ();
75- }
60+ final UnmodifiableIterator <String > it = given .getSequence ().iterator ();
61+ final CollectionSequence iteratingSequence = new CollectionSequence () {
62+ @ Override
63+ public Optional <String > next () {
64+ return it .hasNext () ? Optional .of (it .next ()) : Optional .<String >absent ();
65+ }
66+ };
67+
68+ final SingleNameFactoryMachine <ComponentCustomizerEngine > customizerMachine =
69+ new SingleNameFactoryMachine <>(0 , new StdMachineEngine <ComponentCustomizerEngine >(
70+ Name .of (ComponentCustomizerEngine .class , "JongoCollectionSequenceSupplierOf"
71+ + given .getCollection ()),
72+ BoundlessComponentBox .FACTORY ) {
73+ @ Override
74+ public BillOfMaterials getBillOfMaterial () {
75+ return BillOfMaterials .of ();
76+ }
77+
78+ @ Override
79+ protected ComponentCustomizerEngine doNewComponent (final SatisfiedBOM satisfiedBOM ) {
80+ return new SingleComponentNameCustomizerEngine <JongoCollection >(
81+ 0 , Name .of (JongoCollection .class , given .getCollection ())) {
82+ @ Override
83+ public NamedComponent <JongoCollection > customize (NamedComponent <JongoCollection > namedComponent ) {
84+ if (namedComponent .getName ().getName ().equals (given .getCollection ())) {
85+ return new NamedComponent <>(namedComponent .getName (),
86+ new SequencedJongoCollection (namedComponent .getComponent (),iteratingSequence ));
87+ } else {
88+ return namedComponent ;
89+ }
7690
77- @ Override
78- protected ComponentCustomizerEngine doNewComponent (final SatisfiedBOM satisfiedBOM ) {
79- return new SingleComponentNameCustomizerEngine <JongoCollection >(
80- 0 , Name .of (JongoCollection .class , given .getCollection ())) {
81- @ Override
82- public NamedComponent <JongoCollection > customize (NamedComponent <JongoCollection > namedComponent ) {
83- if (namedComponent .getName ().getName ().equals (given .getCollection ())) {
84- return new NamedComponent <>(namedComponent .getName (),
85- new SequencedJongoCollection (namedComponent .getComponent (),iteratingSequence ));
86- } else {
87- return namedComponent ;
8891 }
89-
92+ };
93+ }
94+ });
95+ final Factory .LocalMachines localMachines = threadLocal ();
96+ localMachines .addMachine (customizerMachine );
97+
98+ return new GivenCleaner () {
99+ @ Override
100+ public void cleanUp () {
101+ try {
102+ localMachines .removeMachine (customizerMachine );
103+ MongoClientURI mongoClientURI = new MongoClientURI (
104+ checkNotNull (params .get (DB_URI ),
105+ DB_URI + " param is required" ));
106+ Jongo jongo = new Jongo (new MongoClient (mongoClientURI ).getDB (mongoClientURI .getDatabase ()));
107+ Stopwatch stopwatch = Stopwatch .createStarted ();
108+ jongo .getCollection (given .getCollection ()).drop ();
109+ System .out .printf ("dropped %s -- %s%n" , given .getCollection (), stopwatch .stop ().toString ());
110+ } catch (UnknownHostException e ) {
111+ throw new RuntimeException (e );
90112 }
91- };
92- }
93- });
94- final Factory .LocalMachines localMachines = threadLocal ();
95- localMachines .addMachine (customizerMachine );
96-
97- return new GivenCleaner () {
98- @ Override
99- public void cleanUp () {
100- localMachines .removeMachine (customizerMachine );
101- MongoClientURI mongoClientURI = new MongoClientURI (
102- checkNotNull (params .get (DB_URI ),
103- DB_URI + " param is required" ));
104- Jongo jongo = new Jongo (new MongoClient (mongoClientURI ).getDB (mongoClientURI .getDatabase ()));
105- Stopwatch stopwatch = Stopwatch .createStarted ();
106- jongo .getCollection (given .getCollection ()).drop ();
107- System .out .printf ("dropped %s -- %s%n" , given .getCollection (), stopwatch .stop ().toString ());
108- }
109- };
113+ }
114+ };
115+ } catch (UnknownHostException e ) {
116+ throw new RuntimeException (e );
117+ }
110118 }
111119
112120 public static interface CollectionSequence {
0 commit comments