Does the work to add a given word to the sparse vector. May not succeed in race conditions when two ore more threads are trying to add a word at the same time. @param word the word to add to the vector @param vec the location to store the word occurrence @param entry the number of times the word oc
(String word, SparseVector vec, Integer value)
| 213 | * the word wasn't added due to a race- and should be tried again |
| 214 | */ |
| 215 | private boolean addWord(String word, SparseVector vec, Integer value) |
| 216 | { |
| 217 | Integer indx = wordIndex.get(word); |
| 218 | if(indx == null)//this word has never been seen before! |
| 219 | { |
| 220 | Integer index_for_new_word; |
| 221 | if((index_for_new_word = wordIndex.putIfAbsent(word, -1)) == null)//I won the race to insert this word into the map |
| 222 | { |
| 223 | /* |
| 224 | * we need to do this increment after words to avoid a race |
| 225 | * condition where two people incrment currentLength for the |
| 226 | * same word, as that will throw off other word additions |
| 227 | * before we can fix the problem |
| 228 | */ |
| 229 | index_for_new_word = currentLength.getAndIncrement(); |
| 230 | wordIndex.put(word, index_for_new_word);//overwrite with correct value |
| 231 | } |
| 232 | if(index_for_new_word < 0) |
| 233 | return false; |
| 234 | |
| 235 | //possible race on tdf as well when two threads found same new word at the same time |
| 236 | AtomicInteger termCount = new AtomicInteger(0), tmp = null; |
| 237 | tmp = termDocumentFrequencys.putIfAbsent(index_for_new_word, termCount); |
| 238 | if(tmp != null) |
| 239 | termCount = tmp; |
| 240 | termCount.incrementAndGet(); |
| 241 | |
| 242 | int newLen = Math.max(index_for_new_word+1, vec.length()); |
| 243 | vec.setLength(newLen); |
| 244 | vec.set(index_for_new_word, value); |
| 245 | } |
| 246 | else//this word has been seen before |
| 247 | { |
| 248 | if(indx < 0) |
| 249 | return false; |
| 250 | |
| 251 | AtomicInteger toInc = termDocumentFrequencys.get(indx); |
| 252 | if (toInc == null) |
| 253 | { |
| 254 | //wordIndex and termDocumnetFrequences are not updated |
| 255 | //atomicly together, so could get index but not have tDF ready |
| 256 | toInc = termDocumentFrequencys.putIfAbsent(indx, new AtomicInteger(1)); |
| 257 | if (toInc == null)//other person finished adding before we "fixed" via putIfAbsent |
| 258 | toInc = termDocumentFrequencys.get(indx); |
| 259 | } |
| 260 | toInc.incrementAndGet(); |
| 261 | |
| 262 | if (vec.length() <= indx)//happens when another thread sees the word first and adds it, then get check and find it- but haven't increased our vector legnth |
| 263 | vec.setLength(indx+1); |
| 264 | vec.set(indx, value); |
| 265 | } |
| 266 | |
| 267 | return true; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Once all original documents have been added, this method is called so |
no test coverage detected