//package nl.surfsara.warcexamples.hadoop.warc;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.*;

class TorrentWebReducer extends Reducer<Text, Text, Text, Text> {

    @Override
    public void reduce(Text pKey, Iterable<Text> pValues, Context pContext)
            throws IOException, InterruptedException {

        List<String> torrents = new ArrayList<String>();
        String value;
        int iter = 0;
        boolean hasTorrentFile = false;

        for (Text t : pValues) {
            value = t.toString();

            if (!value.startsWith("magnet:?"))
            {
                torrents.add(value);
                hasTorrentFile = true;
            } else if(iter < 15){
                torrents.add(value);
            }

            iter ++;

            if (hasTorrentFile && iter > 10)
                break;

        }

        StringBuilder out = new StringBuilder();
        for (Object o : torrents)
        {
            out.append(o.toString());
            out.append("\t");
        }

        pContext.write(pKey, new Text(out.toString()));
    }

}
@@ -395,79 +395,196 @@
     
-List
+Map
 %3CString
-%3E torrents = new ArrayList%3CString%3E();%0A        String value;%0A
+, Integer%3E hm = new HashMap%3CString, Integer%3E();%0A        //String content;%0A        int iValue;%0A        String value;%0A%0A//        List%3CString%3E torrents = new ArrayList%3CString%3E();%0A//
     
@@ -601,16 +601,18 @@
 er = 0;%0A
+//
         
@@ -712,16 +712,122 @@
 ing();%0A%0A
+            iValue = hm.containsKey(value) ? hm.get(value) : 0;%0A            hm.put(value, iValue + 1);%0A%0A//
         
@@ -865,16 +865,18 @@
 et:?%22))%0A
+//
         
@@ -873,32 +873,34 @@
 //            %7B%0A
+//
                 
@@ -912,32 +912,34 @@
 nts.add(value);%0A
+//
                 
@@ -957,24 +957,26 @@
 ile = true;%0A
+//
             
@@ -997,16 +997,18 @@
  %3C 15)%7B%0A
+//
         
@@ -1036,16 +1036,18 @@
 value);%0A
+//
         
@@ -1044,33 +1044,37 @@
 //            %7D%0A
-%0A
+//%0A//
             iter
@@ -1078,17 +1078,21 @@
 ter ++;%0A
-%0A
+//%0A//
         
@@ -1128,16 +1128,18 @@
 r %3E 10)%0A
+//
         
@@ -1153,17 +1153,16 @@
  break;%0A
-%0A
         
@@ -1154,32 +1154,79 @@
 break;%0A        %7D
+%0A%0A//        System.out.println(%22num: %22 + iter);
 %0A%0A        String
@@ -1272,77 +1272,244 @@
     
-for (Object o : torrents)%0A        %7B%0A            out.append(o.toString
+out.append(%22%5Cn%22);%0A        Iterator iterator = hm.entrySet().iterator();%0A        while (iterator.hasNext())%0A        %7B%0A            Map.Entry pairs = (Map.Entry)iterator.next();%0A            out.append(pairs.getValue() + %22%5Ct%22 + pairs.getKey
 ());
@@ -1534,17 +1534,17 @@
 ppend(%22%5C
-t
+n
 %22);%0A    

add torrent source file