2525import org .HdrHistogram .DoubleHistogram ;
2626import org .neo4j .graphalgo .core .ProcedureConfiguration ;
2727import org .neo4j .graphalgo .core .ProcedureConstants ;
28- import org .neo4j .graphalgo .core .utils .ParallelUtil ;
29- import org .neo4j .graphalgo .core .utils .Pools ;
30- import org .neo4j .graphalgo .core .utils .QueueBasedSpliterator ;
31- import org .neo4j .graphalgo .core .utils .TerminationFlag ;
28+ import org .neo4j .graphalgo .core .utils .*;
3229import org .neo4j .graphalgo .impl .util .TopKConsumer ;
3330import org .neo4j .graphdb .Result ;
3431import org .neo4j .kernel .api .KernelTransaction ;
@@ -95,7 +92,76 @@ Long getWriteBatchSize(ProcedureConfiguration configuration) {
9592 return configuration .get ("writeBatchSize" , 10000L );
9693 }
9794
95+
96+ public class SimilarityResultBuilder {
97+ protected long writeDuration = -1 ;
98+ protected boolean write = false ;
99+ private int nodes ;
100+ private String writeRelationshipType ;
101+ private String writeProperty ;
102+ private AtomicLong similarityPairs ;
103+ private DoubleHistogram histogram ;
104+
105+
106+ public SimilarityResultBuilder withWriteDuration (long writeDuration ) {
107+ this .writeDuration = writeDuration ;
108+ return this ;
109+ }
110+
111+ public SimilarityResultBuilder withWrite (boolean write ) {
112+ this .write = write ;
113+ return this ;
114+ }
115+
116+ /**
117+ * returns an AutoClosable which measures the time
118+ * until it gets closed. Saves the duration as writeMillis
119+ *
120+ * @return
121+ */
122+ public ProgressTimer timeWrite () {
123+ return ProgressTimer .start (this ::withWriteDuration );
124+ }
125+
126+ public SimilaritySummaryResult build () {
127+ return SimilaritySummaryResult .from (nodes , similarityPairs , writeRelationshipType , writeProperty , write , histogram , writeDuration );
128+ }
129+
130+ public SimilarityResultBuilder nodes (int nodes ) {
131+ this .nodes = nodes ;
132+ return this ;
133+ }
134+
135+ public SimilarityResultBuilder write (boolean write ) {
136+ this .write = write ;
137+ return this ;
138+ }
139+
140+ public SimilarityResultBuilder writeRelationshipType (String writeRelationshipType ) {
141+ this .writeRelationshipType = writeRelationshipType ;
142+ return this ;
143+ }
144+
145+ public SimilarityResultBuilder writeProperty (String writeProperty ) {
146+ this .writeProperty = writeProperty ;
147+ return this ;
148+ }
149+
150+ public SimilarityResultBuilder similarityPairs (AtomicLong similarityPairs ) {
151+ this .similarityPairs = similarityPairs ;
152+ return this ;
153+ }
154+
155+ public SimilarityResultBuilder histogram (DoubleHistogram histogram ) {
156+ this .histogram = histogram ;
157+ return this ;
158+ }
159+ }
160+
98161 Stream <SimilaritySummaryResult > writeAndAggregateResults (Stream <SimilarityResult > stream , int length , ProcedureConfiguration configuration , boolean write , String writeRelationshipType , String writeProperty , boolean writeParallel ) {
162+ SimilarityResultBuilder builder = new SimilarityResultBuilder ();
163+ builder .nodes (length ).write (write ).writeRelationshipType (writeRelationshipType ).writeProperty (writeProperty );
164+
99165 long writeBatchSize = getWriteBatchSize (configuration );
100166 AtomicLong similarityPairs = new AtomicLong ();
101167 DoubleHistogram histogram = new DoubleHistogram (5 );
@@ -106,24 +172,31 @@ Stream<SimilaritySummaryResult> writeAndAggregateResults(Stream<SimilarityResult
106172
107173 if (write ) {
108174 if (writeParallel ) {
109- ParallelSimilarityExporter parallelSimilarityExporter = new ParallelSimilarityExporter (api , log , writeRelationshipType , writeProperty , length );
110- parallelSimilarityExporter .export (stream .peek (recorder ), writeBatchSize );
175+ try (ProgressTimer timer = builder .timeWrite ()) {
176+ ParallelSimilarityExporter parallelSimilarityExporter = new ParallelSimilarityExporter (api , log , writeRelationshipType , writeProperty , length );
177+ parallelSimilarityExporter .export (stream .peek (recorder ), writeBatchSize );
178+ }
111179
112180 } else {
113- SimilarityExporter similarityExporter = new SimilarityExporter (api , log , writeRelationshipType , writeProperty );
114- similarityExporter .export (stream .peek (recorder ), writeBatchSize );
181+ try (ProgressTimer timer = builder .timeWrite ()) {
182+ SimilarityExporter similarityExporter = new SimilarityExporter (api , log , writeRelationshipType , writeProperty );
183+ similarityExporter .export (stream .peek (recorder ), writeBatchSize );
184+ }
115185 }
116186
117187 } else {
118188 stream .forEach (recorder );
119189 }
120190
121- return Stream .of (SimilaritySummaryResult .from (length , similarityPairs , writeRelationshipType , writeProperty , write , histogram ));
191+ builder .similarityPairs (similarityPairs ).histogram (histogram );
192+ return Stream .of (builder .build ());
193+
194+ // return Stream.of(SimilaritySummaryResult.from(length, similarityPairs, writeRelationshipType, writeProperty, write, histogram));
122195 }
123196
124197 Stream <SimilaritySummaryResult > emptyStream (String writeRelationshipType , String writeProperty ) {
125198 return Stream .of (SimilaritySummaryResult .from (0 , new AtomicLong (0 ), writeRelationshipType ,
126- writeProperty , false , new DoubleHistogram (5 )));
199+ writeProperty , false , new DoubleHistogram (5 ), - 1 ));
127200 }
128201
129202 Double getSimilarityCutoff (ProcedureConfiguration configuration ) {
0 commit comments