Skip to content
This repository was archived by the owner on Apr 27, 2025. It is now read-only.

Commit 59bae85

Browse files
committed
Switched to synchronized classes
1 parent 21585dd commit 59bae85

File tree

6 files changed

+38
-38
lines changed

6 files changed

+38
-38
lines changed

lib/tmengine.jar

91 Bytes
Binary file not shown.

src/com/maxprograms/tmengine/Constants.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ private Constants() {
1818
}
1919

2020
public static final String CREATIONTOOL = "Maxprograms TM Engine";
21-
public static final String VERSION = "5.0.1";
22-
public static final String BUILD = "20191212_0727";
21+
public static final String VERSION = "5.0.2";
22+
public static final String BUILD = "20201123_0623";
2323

2424
public static final String PENDING = "Pending";
2525
public static final String COMPLETED = "Completed";

src/com/maxprograms/tmengine/MapDbEngine.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
import java.lang.System.Logger.Level;
1919
import java.nio.charset.StandardCharsets;
2020
import java.text.MessageFormat;
21-
import java.util.ArrayList;
2221
import java.util.Calendar;
23-
import java.util.HashMap;
22+
import java.util.Collections;
2423
import java.util.Hashtable;
2524
import java.util.Iterator;
2625
import java.util.List;
2726
import java.util.Map;
2827
import java.util.NavigableSet;
2928
import java.util.Set;
3029
import java.util.TreeSet;
30+
import java.util.Vector;
3131
import java.util.regex.Pattern;
3232
import java.util.regex.PatternSyntaxException;
3333

@@ -64,7 +64,7 @@ public class MapDbEngine implements ITmEngine, AutoCloseable {
6464

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

113113
@Override
114-
public void close() throws IOException {
114+
public synchronized void close() throws IOException {
115115
fuzzyIndex.close();
116116
tuDb.close();
117117
tuvDb.close();
@@ -203,7 +203,7 @@ public Set<String> getAllSubjects() {
203203
public List<Match> searchTranslation(String searchStr, String srcLang, String tgtLang, int similarity,
204204
boolean caseSensitive) throws IOException, SAXException, ParserConfigurationException {
205205

206-
List<Match> result = new ArrayList<>();
206+
List<Match> result = new Vector<>();
207207

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

244-
Map<String, Integer> candidates = new HashMap<>();
244+
Map<String, Integer> candidates = new Hashtable<>();
245245
String lowerSearch = searchStr.toLowerCase();
246246

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

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

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

442442
@Override
443-
public void commit() {
443+
public synchronized void commit() {
444444
fuzzyIndex.commit();
445445
tuDb.commit();
446446
tuvDb.commit();

src/com/maxprograms/tmengine/NGrams.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
*******************************************************************************/
1212
package com.maxprograms.tmengine;
1313

14-
import java.util.ArrayList;
15-
import java.util.HashSet;
14+
import java.util.Collections;
1615
import java.util.Iterator;
1716
import java.util.List;
1817
import java.util.Set;
1918
import java.util.StringTokenizer;
19+
import java.util.TreeSet;
20+
import java.util.Vector;
2021

2122
public class NGrams {
2223

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

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

6667
private static List<String> buildWordList(String src) {
67-
List<String> result = new ArrayList<>();
68+
List<String> result = new Vector<>();
6869
StringTokenizer tokenizer = new StringTokenizer(src, SEPARATORS);
6970
while (tokenizer.hasMoreElements()) {
7071
result.add(tokenizer.nextToken());

src/com/maxprograms/tmengine/SQLEngine.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,27 @@
2323
import java.sql.ResultSet;
2424
import java.sql.SQLException;
2525
import java.sql.Statement;
26-
import java.util.ArrayList;
2726
import java.util.Calendar;
27+
import java.util.Collections;
2828
import java.util.Hashtable;
2929
import java.util.Iterator;
3030
import java.util.List;
3131
import java.util.Map;
3232
import java.util.Properties;
3333
import java.util.Set;
3434
import java.util.TreeSet;
35+
import java.util.Vector;
3536

3637
import javax.xml.parsers.ParserConfigurationException;
3738

38-
import org.xml.sax.SAXException;
39-
40-
import com.maxprograms.tmx.TMXReader;
4139
import com.maxprograms.tmutils.TMUtils;
40+
import com.maxprograms.tmx.TMXReader;
4241
import com.maxprograms.xml.Attribute;
4342
import com.maxprograms.xml.Element;
4443
import com.maxprograms.xml.Indenter;
4544

45+
import org.xml.sax.SAXException;
46+
4647
public class SQLEngine implements ITmEngine {
4748

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

6263
private long next;
6364

64-
private TreeSet<String> languages;
65+
private Set<String> languages;
6566

6667
private PreparedStatement insertProperties;
6768
private PreparedStatement removeProperties;
@@ -282,7 +283,7 @@ public void flag(String tuid) throws SQLException {
282283

283284
@Override
284285
public Set<String> getAllClients() throws SQLException {
285-
Set<String> result = new TreeSet<>();
286+
Set<String> result = Collections.synchronizedSortedSet(new TreeSet<>());
286287
try (Statement stmt = conn.createStatement()) {
287288
try (ResultSet rs = stmt
288289
.executeQuery("SELECT DISTINCT content FROM `" + dbName + "`.tuprop WHERE propType='customer'")) {
@@ -297,7 +298,7 @@ public Set<String> getAllClients() throws SQLException {
297298
@Override
298299
public Set<String> getAllLanguages() throws SQLException {
299300
if (languages == null) {
300-
languages = new TreeSet<>();
301+
languages = Collections.synchronizedSortedSet(new TreeSet<>());
301302
try (Statement stmt = conn.createStatement()) {
302303
try (ResultSet rs = stmt.executeQuery("SELECT lang FROM `" + dbName + "`.langs")) {
303304
while (rs.next()) {
@@ -311,7 +312,7 @@ public Set<String> getAllLanguages() throws SQLException {
311312

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

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

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

360-
Set<String> candidates = new TreeSet<>();
361+
Set<String> candidates = Collections.synchronizedSortedSet(new TreeSet<>());
361362
String lowerSearch = searchStr.toLowerCase();
362363

363364
PreparedStatement stmt = selectNgram.get(srcLang);
@@ -420,7 +421,7 @@ private String getPureText(String lang, String tuid) throws SQLException {
420421
@Override
421422
public List<Element> concordanceSearch(String searchStr, String srcLang, int limit, boolean isRegexp,
422423
boolean caseSensitive) throws IOException, SAXException, ParserConfigurationException, SQLException {
423-
Set<String> candidates = new TreeSet<>();
424+
Set<String> candidates = Collections.synchronizedSortedSet(new TreeSet<>());
424425
if (isRegexp) {
425426
try (PreparedStatement stmt = conn.prepareStatement(
426427
"SELECT tuid, pureText FROM `" + dbName + "`.tuv WHERE lang=? AND pureText REGEXP ? LIMIT ?")) {
@@ -460,7 +461,7 @@ public List<Element> concordanceSearch(String searchStr, String srcLang, int lim
460461
}
461462
}
462463
}
463-
List<Element> result = new ArrayList<>();
464+
List<Element> result = new Vector<>();
464465
Iterator<String> it = candidates.iterator();
465466
while (it.hasNext()) {
466467
Element tu = getTu(it.next());
@@ -511,7 +512,7 @@ public void storeTu(Element tu) throws IOException, SQLException {
511512
tuProperties.put("project", currProject);
512513
}
513514
List<Element> tuvs = tu.getChildren("tuv");
514-
Set<String> tuLangs = new TreeSet<>();
515+
Set<String> tuLangs = Collections.synchronizedSortedSet(new TreeSet<>());
515516

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

700701
@Override
701702
public Element getTu(String tuid) throws IOException, SAXException, ParserConfigurationException, SQLException {
702-
return getTu(tuid, new TreeSet<>());
703+
return getTu(tuid, Collections.synchronizedSortedSet(new TreeSet<>()));
703704
}
704705

705706
private String getSegText(String lang, String tuid) throws SQLException {

src/com/maxprograms/tmserver/TmHandler.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
import java.nio.file.Files;
2828
import java.sql.SQLException;
2929
import java.text.SimpleDateFormat;
30+
import java.util.Collections;
3031
import java.util.Date;
3132
import java.util.Enumeration;
32-
import java.util.HashMap;
3333
import java.util.Iterator;
3434
import java.util.List;
3535
import java.util.Map;
@@ -388,15 +388,15 @@ private JSONObject exportMemory(String request) {
388388
}
389389
Set<String> langs = null;
390390
if (json.has("langs")) {
391-
langs = new TreeSet<>();
391+
langs = Collections.synchronizedSortedSet(new TreeSet<>());
392392
JSONArray array = json.getJSONArray("langs");
393393
for (int i = 0; i < array.length(); i++) {
394394
langs.add(array.getString(i));
395395
}
396396
}
397397
Map<String, String> properties = null;
398398
if (json.has("properties")) {
399-
properties = new HashMap<>();
399+
properties = new ConcurrentHashMap<>();
400400
JSONObject props = json.getJSONObject("properties");
401401
Iterator<String> keys = props.keys();
402402
while (keys.hasNext()) {
@@ -475,9 +475,7 @@ protected void open(String id) throws IOException, SQLException {
475475
} else if ("SQLEngine".equals(mem.getString("type"))) {
476476
openEngines.put(id, new SQLEngine(mem.getString("name"), mem.getString("serverName"), mem.getInt("port"),
477477
mem.getString("userName"), mem.getString("password")));
478-
} else {
479-
throw new IOException("Unknown memory type");
480-
}
478+
}
481479
}
482480

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

0 commit comments

Comments
 (0)