Question

I am building an inverted index file for my search engine. I have written the class however, when I try to test it, the program gets stuck in this part:

for (final HashObject value: list) {

   if(url.equals(value.getUrl()))
       value.setFrequency();

   if(!url.equals(value.getUrl())){       
       list.add(new HashObject(title, term, url, 1, 1));           
   }
}

Whenever I delete the block of code placed above, the whole search engine keeps crawling correctly. It seems that it gets stuck as the program takes no further actions... You can see the whole code here:

/*
 * Classname: InvertedFile
 * Version: 1.1
 * Date: 15/04/2014
 * Copyright by Mateusz Michalski

 * Description: This class implements an inverted builder for storing results of crawling.
 */

package searchengine;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class InvertedFile
{
    ConcurrentHashMap<String, List<HashObject>> myMap = new ConcurrentHashMap<>();
    public static int docCollection = 0;

    SearchEngine searchEngine = new SearchEngine();

    public void insertValues(String title, String term, String url)
    {
        if (!myMap.containsKey(term)){
            List<HashObject> list = new ArrayList<>();
            list.add(new HashObject(title, term, url, 1, 1));
            myMap.put(term, list);
        }

        if(myMap.containsKey(term)){            
            List<HashObject> list = myMap.get(term);

            for (final HashObject value: list) {          
                 if(url.equals(value.getUrl()))
                     value.setFrequency();

                if(!url.equals(value.getUrl())){          
                    list.add(new HashObject(title, term, url, 1, 1));

                //for (HashObject val: itemList)
                    //val.setDoc();
                }
            }              
            myMap.put(term, list);
        }
    }
}

I have no clue why it gets stuck in that for loop. I might be too tired already and hopefully not seeing a minor mistakes... Can anyone give me their opinions on this code?

Was it helpful?

Solution

Not sure what the problematic loop is intended to do as it seems that you are checking for the url passed in as a parameter to exist in the HashObject of the current iteration through the list, and if not found in there insert a new HashObject in the list which has its url value as that of the passed in url. If none of the existing HashObjects have the same value as the passed in url, then you will be insterting multiple HashObjects in the list with the same value for title, term, url etc.

If this is indeed what you need then use a ListIterator for adding while iterating through the list. You can read more about the add method of ListIterator.

ListIterator listIter = list.listIterator();
while (listIter.hasNext()) { 
    HashObject value = listIter.next();       
    if(url.equals(value.getUrl())){
        value.setFrequency();
    }

    if(!url.equals(value.getUrl())){          
        listIter.add(new HashObject(title, term, url, 1, 1));
    }
}

But maybe on discovering that none of your HashObjects in the list have the passed in url, you want to insert a new HashObject with that url only once. In that case the code above will have to change.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top