diff --git a/lib/tmengine.jar b/lib/tmengine.jar index ece564b..3c97b83 100644 Binary files a/lib/tmengine.jar and b/lib/tmengine.jar differ diff --git a/src/com/maxprograms/tmengine/Constants.java b/src/com/maxprograms/tmengine/Constants.java index b671d80..e9c788f 100644 --- a/src/com/maxprograms/tmengine/Constants.java +++ b/src/com/maxprograms/tmengine/Constants.java @@ -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"; diff --git a/src/com/maxprograms/tmengine/MapDbEngine.java b/src/com/maxprograms/tmengine/MapDbEngine.java index ae0032d..3874e13 100644 --- a/src/com/maxprograms/tmengine/MapDbEngine.java +++ b/src/com/maxprograms/tmengine/MapDbEngine.java @@ -18,9 +18,8 @@ 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; @@ -28,6 +27,7 @@ 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; @@ -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" }; @@ -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(); @@ -203,7 +203,7 @@ public Set getAllSubjects() { public List searchTranslation(String searchStr, String srcLang, String tgtLang, int similarity, boolean caseSensitive) throws IOException, SAXException, ParserConfigurationException { - List result = new ArrayList<>(); + List result = new Vector<>(); if (similarity == 100) { // check for perfect matches @@ -241,7 +241,7 @@ public List searchTranslation(String searchStr, String srcLang, String tg int min = size * similarity / 100; int max = size * (200 - similarity) / 100; - Map candidates = new HashMap<>(); + Map candidates = new Hashtable<>(); String lowerSearch = searchStr.toLowerCase(); NavigableSet> index = fuzzyIndex.getIndex(srcLang); @@ -322,7 +322,7 @@ private Element buildElement(Map properties) @Override public List concordanceSearch(String searchStr, String srcLang, int limit, boolean isRegexp, boolean caseSensitive) throws IOException, SAXException, ParserConfigurationException { - List result = new ArrayList<>(); + List result = new Vector<>(); Pattern pattern = null; if (isRegexp) { try { @@ -379,7 +379,7 @@ public void storeTu(Element tu) throws IOException { if (tu.getAttributeValue("creationid").isEmpty()) { tu.setAttribute("creationid", System.getProperty("user.name")); } - Map tuProperties = new HashMap<>(); + Map tuProperties = new Hashtable<>(); List atts = tu.getAttributes(); Iterator at = atts.iterator(); @@ -403,7 +403,7 @@ public void storeTu(Element tu) throws IOException { tuProperties.put("project", currProject); } List tuvs = tu.getChildren("tuv"); - Set tuLangs = new TreeSet<>(); + Set tuLangs = Collections.synchronizedSortedSet(new TreeSet<>()); Iterator it = tuvs.iterator(); while (it.hasNext()) { @@ -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(); diff --git a/src/com/maxprograms/tmengine/NGrams.java b/src/com/maxprograms/tmengine/NGrams.java index 3a0ceac..c1ed458 100644 --- a/src/com/maxprograms/tmengine/NGrams.java +++ b/src/com/maxprograms/tmengine/NGrams.java @@ -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 { @@ -32,7 +33,7 @@ private NGrams() { public static int[] getNGrams(String string) { String src = string.toLowerCase(); List words = buildWordList(src); - Set set = new HashSet<>(); + Set set = Collections.synchronizedSortedSet(new TreeSet<>()); Iterator it = words.iterator(); while (it.hasNext()) { @@ -64,7 +65,7 @@ public static int[] getNGrams(String string) { } private static List buildWordList(String src) { - List result = new ArrayList<>(); + List result = new Vector<>(); StringTokenizer tokenizer = new StringTokenizer(src, SEPARATORS); while (tokenizer.hasMoreElements()) { result.add(tokenizer.nextToken()); diff --git a/src/com/maxprograms/tmengine/SQLEngine.java b/src/com/maxprograms/tmengine/SQLEngine.java index 4fb5e17..4322c07 100644 --- a/src/com/maxprograms/tmengine/SQLEngine.java +++ b/src/com/maxprograms/tmengine/SQLEngine.java @@ -23,8 +23,8 @@ 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; @@ -32,17 +32,18 @@ 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()); @@ -61,7 +62,7 @@ public class SQLEngine implements ITmEngine { private long next; - private TreeSet languages; + private Set languages; private PreparedStatement insertProperties; private PreparedStatement removeProperties; @@ -282,7 +283,7 @@ public void flag(String tuid) throws SQLException { @Override public Set getAllClients() throws SQLException { - Set result = new TreeSet<>(); + Set result = Collections.synchronizedSortedSet(new TreeSet<>()); try (Statement stmt = conn.createStatement()) { try (ResultSet rs = stmt .executeQuery("SELECT DISTINCT content FROM `" + dbName + "`.tuprop WHERE propType='customer'")) { @@ -297,7 +298,7 @@ public Set getAllClients() throws SQLException { @Override public Set 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()) { @@ -311,7 +312,7 @@ public Set getAllLanguages() throws SQLException { @Override public Set getAllProjects() throws SQLException { - Set result = new TreeSet<>(); + Set result = Collections.synchronizedSortedSet(new TreeSet<>()); try (Statement stmt = conn.createStatement()) { try (ResultSet rs = stmt .executeQuery("SELECT DISTINCT content FROM `" + dbName + "`.tuprop WHERE propType='project'")) { @@ -325,7 +326,7 @@ public Set getAllProjects() throws SQLException { @Override public Set getAllSubjects() throws SQLException { - Set result = new TreeSet<>(); + Set result = Collections.synchronizedSortedSet(new TreeSet<>()); try (Statement stmt = conn.createStatement()) { try (ResultSet rs = stmt .executeQuery("SELECT DISTINCT content FROM `" + dbName + "`.tuprop WHERE propType='subject'")) { @@ -340,7 +341,7 @@ public Set getAllSubjects() throws SQLException { @Override public List searchTranslation(String searchStr, String srcLang, String tgtLang, int similarity, boolean caseSensitive) throws IOException, SAXException, ParserConfigurationException, SQLException { - List result = new ArrayList<>(); + List result = new Vector<>(); int[] ngrams = NGrams.getNGrams(searchStr); int size = ngrams.length; @@ -357,7 +358,7 @@ public List searchTranslation(String searchStr, String srcLang, String tg set.append("," + ngrams[i]); } - Set candidates = new TreeSet<>(); + Set candidates = Collections.synchronizedSortedSet(new TreeSet<>()); String lowerSearch = searchStr.toLowerCase(); PreparedStatement stmt = selectNgram.get(srcLang); @@ -420,7 +421,7 @@ private String getPureText(String lang, String tuid) throws SQLException { @Override public List concordanceSearch(String searchStr, String srcLang, int limit, boolean isRegexp, boolean caseSensitive) throws IOException, SAXException, ParserConfigurationException, SQLException { - Set candidates = new TreeSet<>(); + Set candidates = Collections.synchronizedSortedSet(new TreeSet<>()); if (isRegexp) { try (PreparedStatement stmt = conn.prepareStatement( "SELECT tuid, pureText FROM `" + dbName + "`.tuv WHERE lang=? AND pureText REGEXP ? LIMIT ?")) { @@ -460,7 +461,7 @@ public List concordanceSearch(String searchStr, String srcLang, int lim } } } - List result = new ArrayList<>(); + List result = new Vector<>(); Iterator it = candidates.iterator(); while (it.hasNext()) { Element tu = getTu(it.next()); @@ -511,7 +512,7 @@ public void storeTu(Element tu) throws IOException, SQLException { tuProperties.put("project", currProject); } List tuvs = tu.getChildren("tuv"); - Set tuLangs = new TreeSet<>(); + Set tuLangs = Collections.synchronizedSortedSet(new TreeSet<>()); Iterator it = tuvs.iterator(); while (it.hasNext()) { @@ -659,7 +660,7 @@ public void commit() throws SQLException { private Element getTu(String tuid, Set 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" }; @@ -699,7 +700,7 @@ private Element getTu(String tuid, Set 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 { diff --git a/src/com/maxprograms/tmserver/TmHandler.java b/src/com/maxprograms/tmserver/TmHandler.java index c682544..6a57407 100644 --- a/src/com/maxprograms/tmserver/TmHandler.java +++ b/src/com/maxprograms/tmserver/TmHandler.java @@ -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; @@ -388,7 +388,7 @@ private JSONObject exportMemory(String request) { } Set 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)); @@ -396,7 +396,7 @@ private JSONObject exportMemory(String request) { } Map properties = null; if (json.has("properties")) { - properties = new HashMap<>(); + properties = new ConcurrentHashMap<>(); JSONObject props = json.getJSONObject("properties"); Iterator keys = props.keys(); while (keys.hasNext()) { @@ -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 {