Skip to content

Commit

Permalink
Switched to synchronized classes
Browse files Browse the repository at this point in the history
  • Loading branch information
rmraya committed Nov 23, 2020
1 parent 21585dd commit 59bae85
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 38 deletions.
Binary file modified lib/tmengine.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions src/com/maxprograms/tmengine/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ private Constants() {
}

public static final String CREATIONTOOL = "Maxprograms TM Engine";
public static final String VERSION = "5.0.1";
public static final String BUILD = "20191212_0727";
public static final String VERSION = "5.0.2";
public static final String BUILD = "20201123_0623";

public static final String PENDING = "Pending";
public static final String COMPLETED = "Completed";
Expand Down
20 changes: 10 additions & 10 deletions src/com/maxprograms/tmengine/MapDbEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
import java.lang.System.Logger.Level;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NavigableSet;
import java.util.Set;
import java.util.TreeSet;
import java.util.Vector;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

Expand Down Expand Up @@ -64,7 +64,7 @@ public class MapDbEngine implements ITmEngine, AutoCloseable {

public MapDbEngine(String dbname, String workFolder) throws IOException {
this.dbname = dbname;
tuAttributes = new TreeSet<>();
tuAttributes = Collections.synchronizedSortedSet(new TreeSet<>());
String[] array = new String[] { "tuid", "o-encoding", "datatype", "usagecount", "lastusagedate", "creationtool",
"creationtoolversion", "creationdate", "creationid", "changedate", "segtype", "changeid", "o-tmf",
"srclang" };
Expand Down Expand Up @@ -111,7 +111,7 @@ public String getType() {
}

@Override
public void close() throws IOException {
public synchronized void close() throws IOException {
fuzzyIndex.close();
tuDb.close();
tuvDb.close();
Expand Down Expand Up @@ -203,7 +203,7 @@ public Set<String> getAllSubjects() {
public List<Match> searchTranslation(String searchStr, String srcLang, String tgtLang, int similarity,
boolean caseSensitive) throws IOException, SAXException, ParserConfigurationException {

List<Match> result = new ArrayList<>();
List<Match> result = new Vector<>();

if (similarity == 100) {
// check for perfect matches
Expand Down Expand Up @@ -241,7 +241,7 @@ public List<Match> searchTranslation(String searchStr, String srcLang, String tg
int min = size * similarity / 100;
int max = size * (200 - similarity) / 100;

Map<String, Integer> candidates = new HashMap<>();
Map<String, Integer> candidates = new Hashtable<>();
String lowerSearch = searchStr.toLowerCase();

NavigableSet<Fun.Tuple2<Integer, String>> index = fuzzyIndex.getIndex(srcLang);
Expand Down Expand Up @@ -322,7 +322,7 @@ private Element buildElement(Map<String, String> properties)
@Override
public List<Element> concordanceSearch(String searchStr, String srcLang, int limit, boolean isRegexp,
boolean caseSensitive) throws IOException, SAXException, ParserConfigurationException {
List<Element> result = new ArrayList<>();
List<Element> result = new Vector<>();
Pattern pattern = null;
if (isRegexp) {
try {
Expand Down Expand Up @@ -379,7 +379,7 @@ public void storeTu(Element tu) throws IOException {
if (tu.getAttributeValue("creationid").isEmpty()) {
tu.setAttribute("creationid", System.getProperty("user.name"));
}
Map<String, String> tuProperties = new HashMap<>();
Map<String, String> tuProperties = new Hashtable<>();

List<Attribute> atts = tu.getAttributes();
Iterator<Attribute> at = atts.iterator();
Expand All @@ -403,7 +403,7 @@ public void storeTu(Element tu) throws IOException {
tuProperties.put("project", currProject);
}
List<Element> tuvs = tu.getChildren("tuv");
Set<String> tuLangs = new TreeSet<>();
Set<String> tuLangs = Collections.synchronizedSortedSet(new TreeSet<>());

Iterator<Element> it = tuvs.iterator();
while (it.hasNext()) {
Expand Down Expand Up @@ -440,7 +440,7 @@ public void storeTu(Element tu) throws IOException {
}

@Override
public void commit() {
public synchronized void commit() {
fuzzyIndex.commit();
tuDb.commit();
tuvDb.commit();
Expand Down
9 changes: 5 additions & 4 deletions src/com/maxprograms/tmengine/NGrams.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
*******************************************************************************/
package com.maxprograms.tmengine;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.Vector;

public class NGrams {

Expand All @@ -32,7 +33,7 @@ private NGrams() {
public static int[] getNGrams(String string) {
String src = string.toLowerCase();
List<String> words = buildWordList(src);
Set<String> set = new HashSet<>();
Set<String> set = Collections.synchronizedSortedSet(new TreeSet<>());

Iterator<String> it = words.iterator();
while (it.hasNext()) {
Expand Down Expand Up @@ -64,7 +65,7 @@ public static int[] getNGrams(String string) {
}

private static List<String> buildWordList(String src) {
List<String> result = new ArrayList<>();
List<String> result = new Vector<>();
StringTokenizer tokenizer = new StringTokenizer(src, SEPARATORS);
while (tokenizer.hasMoreElements()) {
result.add(tokenizer.nextToken());
Expand Down
33 changes: 17 additions & 16 deletions src/com/maxprograms/tmengine/SQLEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,27 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import java.util.Vector;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import com.maxprograms.tmx.TMXReader;
import com.maxprograms.tmutils.TMUtils;
import com.maxprograms.tmx.TMXReader;
import com.maxprograms.xml.Attribute;
import com.maxprograms.xml.Element;
import com.maxprograms.xml.Indenter;

import org.xml.sax.SAXException;

public class SQLEngine implements ITmEngine {

private static final Logger LOGGER = System.getLogger(SQLEngine.class.getName());
Expand All @@ -61,7 +62,7 @@ public class SQLEngine implements ITmEngine {

private long next;

private TreeSet<String> languages;
private Set<String> languages;

private PreparedStatement insertProperties;
private PreparedStatement removeProperties;
Expand Down Expand Up @@ -282,7 +283,7 @@ public void flag(String tuid) throws SQLException {

@Override
public Set<String> getAllClients() throws SQLException {
Set<String> result = new TreeSet<>();
Set<String> result = Collections.synchronizedSortedSet(new TreeSet<>());
try (Statement stmt = conn.createStatement()) {
try (ResultSet rs = stmt
.executeQuery("SELECT DISTINCT content FROM `" + dbName + "`.tuprop WHERE propType='customer'")) {
Expand All @@ -297,7 +298,7 @@ public Set<String> getAllClients() throws SQLException {
@Override
public Set<String> getAllLanguages() throws SQLException {
if (languages == null) {
languages = new TreeSet<>();
languages = Collections.synchronizedSortedSet(new TreeSet<>());
try (Statement stmt = conn.createStatement()) {
try (ResultSet rs = stmt.executeQuery("SELECT lang FROM `" + dbName + "`.langs")) {
while (rs.next()) {
Expand All @@ -311,7 +312,7 @@ public Set<String> getAllLanguages() throws SQLException {

@Override
public Set<String> getAllProjects() throws SQLException {
Set<String> result = new TreeSet<>();
Set<String> result = Collections.synchronizedSortedSet(new TreeSet<>());
try (Statement stmt = conn.createStatement()) {
try (ResultSet rs = stmt
.executeQuery("SELECT DISTINCT content FROM `" + dbName + "`.tuprop WHERE propType='project'")) {
Expand All @@ -325,7 +326,7 @@ public Set<String> getAllProjects() throws SQLException {

@Override
public Set<String> getAllSubjects() throws SQLException {
Set<String> result = new TreeSet<>();
Set<String> result = Collections.synchronizedSortedSet(new TreeSet<>());
try (Statement stmt = conn.createStatement()) {
try (ResultSet rs = stmt
.executeQuery("SELECT DISTINCT content FROM `" + dbName + "`.tuprop WHERE propType='subject'")) {
Expand All @@ -340,7 +341,7 @@ public Set<String> getAllSubjects() throws SQLException {
@Override
public List<Match> searchTranslation(String searchStr, String srcLang, String tgtLang, int similarity,
boolean caseSensitive) throws IOException, SAXException, ParserConfigurationException, SQLException {
List<Match> result = new ArrayList<>();
List<Match> result = new Vector<>();

int[] ngrams = NGrams.getNGrams(searchStr);
int size = ngrams.length;
Expand All @@ -357,7 +358,7 @@ public List<Match> searchTranslation(String searchStr, String srcLang, String tg
set.append("," + ngrams[i]);
}

Set<String> candidates = new TreeSet<>();
Set<String> candidates = Collections.synchronizedSortedSet(new TreeSet<>());
String lowerSearch = searchStr.toLowerCase();

PreparedStatement stmt = selectNgram.get(srcLang);
Expand Down Expand Up @@ -420,7 +421,7 @@ private String getPureText(String lang, String tuid) throws SQLException {
@Override
public List<Element> concordanceSearch(String searchStr, String srcLang, int limit, boolean isRegexp,
boolean caseSensitive) throws IOException, SAXException, ParserConfigurationException, SQLException {
Set<String> candidates = new TreeSet<>();
Set<String> candidates = Collections.synchronizedSortedSet(new TreeSet<>());
if (isRegexp) {
try (PreparedStatement stmt = conn.prepareStatement(
"SELECT tuid, pureText FROM `" + dbName + "`.tuv WHERE lang=? AND pureText REGEXP ? LIMIT ?")) {
Expand Down Expand Up @@ -460,7 +461,7 @@ public List<Element> concordanceSearch(String searchStr, String srcLang, int lim
}
}
}
List<Element> result = new ArrayList<>();
List<Element> result = new Vector<>();
Iterator<String> it = candidates.iterator();
while (it.hasNext()) {
Element tu = getTu(it.next());
Expand Down Expand Up @@ -511,7 +512,7 @@ public void storeTu(Element tu) throws IOException, SQLException {
tuProperties.put("project", currProject);
}
List<Element> tuvs = tu.getChildren("tuv");
Set<String> tuLangs = new TreeSet<>();
Set<String> tuLangs = Collections.synchronizedSortedSet(new TreeSet<>());

Iterator<Element> it = tuvs.iterator();
while (it.hasNext()) {
Expand Down Expand Up @@ -659,7 +660,7 @@ public void commit() throws SQLException {
private Element getTu(String tuid, Set<String> langs)
throws SQLException, SAXException, IOException, ParserConfigurationException {
if (tuAttributes == null) {
tuAttributes = new TreeSet<>();
tuAttributes = Collections.synchronizedSortedSet(new TreeSet<>());
String[] array = new String[] { "tuid", "o-encoding", "datatype", "usagecount", "lastusagedate",
"creationtool", "creationtoolversion", "creationdate", "creationid", "changedate", "segtype",
"changeid", "o-tmf", "srclang" };
Expand Down Expand Up @@ -699,7 +700,7 @@ private Element getTu(String tuid, Set<String> langs)

@Override
public Element getTu(String tuid) throws IOException, SAXException, ParserConfigurationException, SQLException {
return getTu(tuid, new TreeSet<>());
return getTu(tuid, Collections.synchronizedSortedSet(new TreeSet<>()));
}

private String getSegText(String lang, String tuid) throws SQLException {
Expand Down
10 changes: 4 additions & 6 deletions src/com/maxprograms/tmserver/TmHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import java.nio.file.Files;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -388,15 +388,15 @@ private JSONObject exportMemory(String request) {
}
Set<String> langs = null;
if (json.has("langs")) {
langs = new TreeSet<>();
langs = Collections.synchronizedSortedSet(new TreeSet<>());
JSONArray array = json.getJSONArray("langs");
for (int i = 0; i < array.length(); i++) {
langs.add(array.getString(i));
}
}
Map<String, String> properties = null;
if (json.has("properties")) {
properties = new HashMap<>();
properties = new ConcurrentHashMap<>();
JSONObject props = json.getJSONObject("properties");
Iterator<String> keys = props.keys();
while (keys.hasNext()) {
Expand Down Expand Up @@ -475,9 +475,7 @@ protected void open(String id) throws IOException, SQLException {
} else if ("SQLEngine".equals(mem.getString("type"))) {
openEngines.put(id, new SQLEngine(mem.getString("name"), mem.getString("serverName"), mem.getInt("port"),
mem.getString("userName"), mem.getString("password")));
} else {
throw new IOException("Unknown memory type");
}
}
}

protected void close(String id) throws IOException, SQLException {
Expand Down

0 comments on commit 59bae85

Please sign in to comment.