Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
fix thread-safety for word counts; avoid recounts
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed May 18, 2011
1 parent a0190ff commit b58fe96
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 9 deletions.
14 changes: 10 additions & 4 deletions server/zanata-model/src/main/java/org/zanata/model/HTextFlow.java
Expand Up @@ -194,8 +194,11 @@ public HDocument getDocument()

public void setDocument(HDocument document)
{
this.document = document;
updateWordCount();
if (this.document != document)
{
this.document = document;
updateWordCount();
}
}

// TODO use orphanRemoval=true: requires JPA 2.0
Expand Down Expand Up @@ -224,8 +227,11 @@ public String getContent()

public void setContent(String content)
{
this.content = content;
updateWordCount();
if (!this.content.equals(content))
{
this.content = content;
updateWordCount();
}
}

@OneToMany(cascade = CascadeType.REMOVE, mappedBy = "textFlow")
Expand Down
12 changes: 9 additions & 3 deletions server/zanata-model/src/main/java/org/zanata/util/OkapiUtil.java
Expand Up @@ -53,12 +53,18 @@ public static long countWords(String s, String bcp47Locale)
log.error("can't understand '{}' as a BCP-47 locale; defaulting to English", bcp47Locale);
locale = LocaleId.ENGLISH;
}
long count = WordCounter.count(s, locale);
return count;
synchronized (WordCounter.class)
{
long count = WordCounter.count(s, locale);
return count;
}
}
catch (Exception e)
{
log.error("unable to count words in string '{}' for locale '{}'", s, bcp47Locale);
Object[] args = new Object[] {s, bcp47Locale, e};
if (log.isInfoEnabled())
log.info("unable to count words in string '{}' for locale '{}'", args);
log.error("unable to count words in string '{}' for locale '{}': {}", args);
return 0;
}
}
Expand Down
@@ -0,0 +1,52 @@
package org.zanata.util;

import static org.zanata.util.OkapiUtil.*;

import org.testng.Assert;
import org.testng.annotations.Test;

@Test(groups = { "unit-tests" })
public class OkapiUtilTest
{
String[] strings =
{
"<author><firstname>Emmanuel</firstname> <surname>Bernard</surname></author>",
"Graphic Design",
"the object is passed up to the UI tier",

"// in the first session\n" +
"Cat cat = (Cat) firstSession.load(Cat.class, catID);\n" +
"\n" +
"// in a higher tier of the application\n" +
"Cat mate = new Cat();\n" +
"cat.setMate(mate);\n" +
"\n" +
"// later, in a new session\n" +
"secondSession.saveOrUpdate(cat); // update existing state (cat has a non-null id)\n" +
"secondSession.saveOrUpdate(mate); // save the new instance (mate has a null id)"
};
long[] count =
{
8,
2,
9,
// 4+8+7+4+3+5+11+12 = 54 ?
49
};
@Test
public void testCountWords()
{
countWords(null, "en-US");
int i=0;
for (String s : strings)
{
long expected = count[i++];
long n = countWords(s, "en-US");
Assert.assertEquals(n, expected);
// if (n == 0)
// Assert.fail(s + ":" + n);
// else
// System.out.println(n);
}
}
}
Expand Up @@ -62,13 +62,13 @@ public void execute(Database database) throws CustomChangeException, Unsupported
String locale = rs1.getString(2);
docToLocaleMap.put(docId, locale);
}
String countSql = "select count(*) from HTextFlow where wordCount is null";
String countSql = "select count(*) from HTextFlow where wordCount is null or wordCount = 0";
ResultSet rs2 = stmt.executeQuery(countSql);
rs2.next();
long totalRows = rs2.getLong(1);
Logger log = LogFactory.getLogger();
log.info("CountWordsInHTextFlow: updating " + totalRows + " rows");
String textFlowSql = "select id, document_id, content, wordCount from HTextFlow where wordCount is null";
String textFlowSql = "select id, document_id, content, wordCount from HTextFlow where wordCount is null or wordCount = 0";
ResultSet rs3 = stmt.executeQuery(textFlowSql);
long rowsUpdated = 0;
while (rs3.next())
Expand Down
Expand Up @@ -72,4 +72,15 @@
<dropColumn tableName="HProjectIteration" columnName="description" />
</changeSet>

<changeSet author="sflaniga@redhat.com" id="1">
<comment>reset and recalculate word counts</comment>
<!--
<sql>update HTextFlow set wordCount=0</sql>
-->
<update tableName="HTextFlow">
<column name="wordCount" value="0" />
</update>
<customChange class="org.zanata.liquibase.custom.CountWordsInHTextFlow" />
</changeSet>

</databaseChangeLog>

0 comments on commit b58fe96

Please sign in to comment.