Developer

Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

   

Table of Contents

API

API documentation and openapi.yaml file may be found at https://translate5.github.io/t5memory

End points with additional explanation of inside logic


Tables

PurposeGet author, lang, langGroup(virtual- created in runtime and not saved on the disk), tagtable(deprecated - only one record), document(long filename, long filename caseignore-virtual, short filename- part deprecated)
RequestGET /%service%/%tm_name%/tables
Params

-

tables are provided in format - name(internal key): "position) idInTable:value;"
When adding new entry, for new Id t5memory uses basicaly size of the table before adding+1, so Id should start from id=1. 
When doing fuzzy search, some field could also be saved to the table, to generate then id(or find matching) to compare with saved data.
There are 2 types of entries- older, fixed size, and long filename. For older, every entry has the same size, 39 bytes+endOfLine. You can have up to 389 entries saved in that way in each of the tables. Longname table manage size dynamically, so it's not clear what's the maximum size, but for filenames there are duplicates in old style docname table and in longname(which are used only for this type of data). For filename search, first used longname table, then generated on tm loading caseignore longname table, then short name table. Usually entries in tables are caseignore. 

In the record t5memory saves only id to the table. Maybe it make sense to implement endpoint to edit that table, if something is missing or corrupted. Exception for saving id are context and additional info, some data, that could be saved as int(segId, time), and source and target.
To clear or fix tag tables, you need to do reorganize, but if some entry is missing from the table, you would not restore it on export or segment exctraction via id. So tables could be used to check health of the tm. And for some ways for concordance search or some other checks. 


Code Block
languagejs
titleResponse
collapsetrue
Response example:
{
    "tableName(additional info)(INTERNAL_KEY_OF_THE_TABLE)": "position)idInTable: value;",
    "tagTablesTable(4)": "0)0: \"\"; ",
    "langTable(1)": "0)2: \"DE-DE\"; 1)1: \"FR-FR\"; 2)0: \"\"; ",
    "langGroupTable(virtual)(8)":"0)1: \"F\"; 1)2: \"G\", "authors(3)": "0)36: \"ALINA GEDGAUDIENE\"; 1)7: \"ANAIS KLUCZKA\"; 2)4: \"ANNA MAYER\"; 3)17: \"ARTHUR LE PIVERT\"; 4)35: \"CHARLOTTE FOGLIA\"; 5)10: \"CLAUDIA RICHLING\"; 6)18: \"DANIEL THURSFIELD\"; 7)39: \"DANIELA BARSCH\"; 8)8: \"DANIELA NOEBEL\"; 9)15: \"DUYGU ZORBA\"; 10)48: \"EDITA KAZLAUSKAITE\"; 11)40: \"FRANK ERVEN\"; 12)1: \"FRéDéRIC MOULIN\"; 13)14: \"GIL DéNIEL (DE6U3268)\"; 14)31: \"HEIDRUN LUDOLF\"; 15)55: \"HEIKE REINER\"; 16)30: \"INDRE BARTKIENE\"; 17)23: \"ISABELLE OUELLET\"; 18)19: \"JP COSTEROUSSE (DE6U3269)\"; 19)41: \"JüRGEN KUKLINSKI\"; 20)46: \"LINA PAULAUSKIENE\"; 21)53: \"LINDA PORRECA HEIMROTH\"; 22)58: \"LUCIE BOYN\"; 23)26: \"MARIUS EITMINAVICIUS\"; 24)50: \"MARLIES VAN DEN HURK\"; 25)43: \"MARTIN HOLTMANN\"; 26)54: \"NADINE SCHAELE\"; 27)22: \"NELSON RUSK\"; 28)49: \"RAIMONDA JAZUKEVICIENE\"; 29)25: \"RITA DANZè\"; 30)6: \"UNBEKANNT\"; 31)13: \"UNBEKANNT 1\"; 32)33: \"UNKNOWN 1\"; 33)38: \"VILMA JURGELIONYTE\"; 34)32: \"VILMA JURGELIONYTE2\"; 35)12: \"VINCENT HENRY\"; 36)47: \"VOLKER ZEYER\"; 37)29: \"VéLINA GABROVSKA\"; 38)28: \"W W\"; 39)11: \"[AMPLEXOR]\"; 40)42: \"[BUEHLER, IHRE ABTEILUNG (AUF VS-BUEHLE\"; 41)57: \"[BüHLER AG, TECHNISCHE DOKUMENTATION (\"; 42)24: \"[CLS]\"; 43)56: \"[ERVEN.BIZ - TRAINING, CONSULTING & SUP\"; 44)44: \"[ERVEN]\"; 45)20: \"[EUROSCRIPT DEUTSCHLAND GMBH, IHRE ABTE\"; 46)2: \"[ITL]\"; 47)51: \"[KAVO DENTAL GMBH, IT (AUF DENBIBSVAC00\"; 48)45: \"[LIONBRIDGE]\"; 49)9: \"[OLD - EUROSCRIPT]\"; 50)16: \"[OLD_AMPLEXOR]\"; 51)21: \"[RAPTRAD IMAGINE]\"; 52)37: \"[STO SE & CO.KGAA, (ON DEWEAT06)]\"; 53)52: \"[STOQUART SA, (ON THERIVER)]\"; 54)3: \"[TRANSLINE EUROPE, (ON TEU-SERVER)]\"; 55)5: \"[TRANSLINE]\"; 56)27: \"[WEINOR, MARKETING (AUF NTS-ACROSS)]\"; 57)34: \"[WIENERS]\"; 58)0: \"\"; ",
"filenames(long)(5)": "0)1: \"none\"; 1)0: \"\"; ",
"filenames(long, caseign, virtual)(5)": "0)1: \"NONE\"; 1)0: \"\"; ",
"filenames(short)(2)": "0)1: \"none\"; 1)0: \"\"; "}



Reorganize TM

PurposeReorganizes tm and fixing issues.
RequestGET /%service%/%tm_name%/reorganize
Headers

Accept - applicaton/json

Async, so you can check status of reorganize similar to how you can check status for importTMX

Under the hood it creates new tm with $Org- prefix, then reimport all segments one-by-one, and then deletes original TM and rename reorganized TM to replace original. 
This request should flush tm(from RAM to the disk) before reorganizing


reorganize would check this condition

if (fValidXmlInSrc && fValidXmlInTrg && (pProposal->getSourceLen() != 0) && (pProposal->getTargetLen() != 0) &&
(szTargetLanguage[0] != EOS) && (szTagTable[0] != EOS) )

, and in case if this condition is true and then it passes segment to putProposal function, which is also used by UpdateRequest and ImportTmx request, so other 
issues could be connected to updating new tm.


In 0.4.48 reorganize responce would look like this

{
"too_long_reorg_0_4":"reorganized",
"time":"37 sec",
"reorganizedSegmentCount":"9424",
"invalidSegmentCount":"0"
}
so if there is invalid segments, inspect t5memory log

in 0.7 you can provide body to the request(optional)
Body(optional): 

{

  ["newEntryIfOnlyContextAndTimestampDifferent"]: 0,

     ["saveDifferentTargetsForSameSource"]: 0,

}


  • both newEntryIfOnlyContextAndTimestampDifferent and saveDifferentTargetsForSameSource impact how similar translation would be saved. In case if source and other field(doc, author, lang) are the same, but target different, and newEntryIfOnlyContextAndTimestampDifferent is set to true, you would have 2 translation, otherwise you would have 1 translation saved - with higher updateTime. but if you have different attributes, like document, you would have 2 translation anyway. 
    newEntryIfOnlyContextAndTimestampDifferent would also increase number of saved segments. If values was not provided, defaule values from flags would be used.





Get the status of TM

RequestGET /%service%/%tm_name%/status
Params

-

Would return status of TM. It could be 'not found', 'available' if it's on the disk but not loaded into the RAM yet, and 'open' with additional info. In case if there was at least one try to import tmx or reorganize tm since it was loaded into the RAM, additional fields would appear and stay in the statistics till memory would be unloaded. 

Code Block

In this document the translate5 TM service REST interface is described.

The translate5 TM service is build by using the OpenTM2 Translation Memory Engine.

It provides the following functionality:

  • import new openTM2-TMs
  • delete openTM2-TMs
  • create new empty openTM2-TM
  • import TMX
  • open TM and close TM: not possible see extra section in this document.
  • query TM for Matches: one query per TM, not quering multiple TMs at once.
  • query TM for concordance search
  • save new entry to TM

This can be achieved by the following specification of a RESTful HTTP Serive, the specification is given in the following form:

  1. URL of the HTTP Resource, where servername and an optional path prefix is configurable.
  2. HTTP Method with affected functionality
  3. Brief Description
  4. Sent and returned Body.

Request Data Format:

The transferred data in the requests is JSON and is directly done in the request body.

URL Format:

In this document, the OpenTM2 is always assumed under http://opentm2/.

To rely on full networking features (proxying etc.) the URL is configurable in Translate5 so that the OpenTM2 instance can also reside under http://xyz/foo/bar/.

Errors

For each request, the possible errors are listed below for each resource. In case of an error, the body should contain at least the following JSON, if it is senseful the attributes of the original representation can be added.

{

errors: [{errorMsg: 'Given tmxData is no TMX.'}]

}

Endpoints1Get the list of TMsReturns JSON list of TMs2Create TM

Creates TM with the provided name

3Create/Import TM in internal formatImport and unpack base64 encoded archive of .TMD, .TMI, .MEM files. Rename it to provided name4Delete TMDeletes .TMD, .TMI, .MEM files 5Import TMX into TMImport provided base64 encoded TMX file into TM6Export TMX from TMCreates TMX from tm. Encoded in base647Export in Internal formatCreates and exports archive with .TMD, .TMI, .MEM files of TM8

Status of TM 

Returns status\import status of TM9Fuzzy searchReturns enrties\translations with small differences from requested10Concordance searchReturns entries\translations that contain requested segment11Entry updateUpdates entry\translation 12Entry deleteDeletes entry\translation13Save all TMsFlushes all filebuffers(TMD, TMI files) into the filesystem14Shutdown serviceFlushes all filebuffers into the filesystem and shutting down the service

Values

%service%Name of service(default - t5memory, could be changed in t5m3mory.conf file%tm_name%

Name of Translation Memory

  1. List of TMs
PurposeReturns JSON list of TMsRequestGET /%service%/Params

-

Code Block
languagejs
titleResponse
collapsetrue
Response example:{
    [
        {name:examle_tm
        },
        {name:mem_gt_issue
        }
    ]
}

2. Create TM

PurposeCreates TM with the provided nameRequestPost /%service%/%tm_name%/Params

Required: name, sourceLang

Code Block
languagejs
titleResponse
collapsetrue
Request example 
{    "name": "examle_tm",    
     "sourceLang": "bg-BG" 
   ["data": "base64_encoded_archive_see_import_in_internal_format"]
   ["loggingThreshold": 0]
}

Response example:Success:{
"name": "examle_tm",
}
TM already exists:
{
  "ReturnValue": 1,
  "ErrorMsg": ""
}

3. Create/Import TM in internal format

PurposeImport and unpack base64 encoded archive of .TMD, .TMI, .MEM files. Rename it to provided nameRequestPOST /%service%/%tm_name%/Params

{    "name": "examle_tm",    "sourceLang": "bg-BG" , "data":"base64EncodedArchive" }

Code Block
languagejs
titleResponse
collapsetrue
Request example:{ "name": "mem_internal_format", "sourceLang": "bg-BG", "data":"UEsDBBQACAgIAPmrhVQAAAAAAAAAAAAAAAAWAAQAT1RNXy1JRDE3NS0wXzJfNV9iLk1FTQEAAADtzqEKgDAQgOFTEHwNWZ5swrAO0SBys6wfWxFBDILv6uOI2WZQw33lr38GbvRIsm91baSiigzFEjuEb6XHEK\/myX0PXtXsyxS2OazwhLDWeVTaWgEFMMYYY\/9wAlBLBwhEWTaSXAAAAAAAAAAACAAAAAAAAFBLAwQUAAgICAD5q4VUAAAAAAAAAAAAAAAAFgAEAE9UTV8tSUQxNzUtMF8yXzVfYi5UTUQBAAAA7d3Pa5JxHMDxz+Ns09phDAYdPfaDyQqWRcYjS9nGpoYZhBeZMCISW2v2g5o6VkqQONk\/0KVzh4IoKAovnboUo1PHbuuwU8dSn8c9Pk2yTbc53y+R5\/P9fL7P1wf5Ps9zep5vIOy3iMiSiPLn0yPrQ7In+rStTQARi\/bV9chEyHcxGPIKAGDnPonl21SsHNmUYNgfHZ70nnKNDo9ET0dHozFn2L+Ll9uxZPzazPz1mYQAAAAAAAAAAAAAAAAAAAAAAAAAANDtBkXRoj5Zk7OqSFZ9q35Vn6khNa6W2wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAdBKbKHK4Em1omT5DxV6J7FrmkKFypBKt9FczvYaKtr+2DLpiqPTWVayGiq2uYjFUpC7VI6aElN8F8JPn\/QEAAAAAAAAAAAAAAAAAAAAAAAAAAAAA2ANW7U0Ag9Iv60MnT4j8uLBZ\/X5+7dxn1ztX6Uy5AgAAAAAAAAAAAAAAAAAAgA6nL1qFjmc1rAO2IwNN9bL9u4ulVUeEfcQqQAfxSNtltshZaytB7jalZZ2a5KhFGT3Qr\/ztv1pkzAnP1v06+F7UxL22tRzSNf6aFq08MdoiY078\/znmkTZo5Qm2YdoOSLSyDdbaVUop\/Cj3cDm14I6\/uqf++nDUN1u4lS+k9MbKXL4QK72+775U+phOpp8sucdK728X5nK5hVT+weJqbTiHjMiNzWG1yNxWvI8rvxZ9cTfycj71NH1nsZgbf54uJlKryWy6GFlueBT6xHrzJRupDqkPXc9eyyduJmbLkf6\/mlYRDgQDPtO++3\/uYvsazANfYHx68vLEsSvOKedxqa\/hAGowD4Jh\/1X\/dH1X5sEBZpoH6E6\/AVBLBwj3gRyzjAIAAAAAAAAAAAEAAAAAAFBLAwQUAAgICAD5q4VUAAAAAAAAAAAAAAAAFgAEAE9UTV8tSUQxNzUtMF8yXzVfYi5UTUkBAAAA7d3PS9NhHMDxz\/Y1nbp0zfw2Vw6CEjooJkkFPs9DZZaFCiIRHRxKoJUIFXk06iB0kS5Fvw6dhDp28FDgOSqiIKQ\/ICQMhIIuYVnJt2f7eK2M2Ps1xp49b8Y+fP6ArXegJy4iV0RiPx6BNAXyT6ysrKhXlLZ49PwlkKP9hw\/19XcKAOD3PZX42+PDP0+JWN9AT765u3P33vbm1nxbvj0\/3DLQ0y3r5uClsZGhC2eGxgUAAAAAAAAAAAAAAAAAAAAAAAAAgFKXllh0ahQbLHeInDb3Xc6NWrF77Jibcr22zC2YY6bVLNoX5qp97Pa5SbPc8ci8sqHpd1k7a2+ZN+6eFQAAAAAAAAAAAAAAAAAAAAAAAAAAAAD4YxISk8bVUyq6eVa905dtqtxO3fBlqyqnkrW+ZFVZCGp8aVDl9ZeELxlVjhRNsEWVa+UffAlVuf78rC\/1eoK20JfNqnzt3OhLnSp1DZW+bFJl\/467vqRUuVxV5UutKts\/JX2pUWUyXvie9OopE5U7QWEHSfWZXdmPvlSr8i75xJcqVT7fPOdLpSqj5+t9Sahy8UBhOxWqLEph6nJVHhZNvUFPXbS3MlXyYWFvgSon3xf2FldlpGiCmCoPiiYQVbLR3or\/ZT0tS04AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMC6K4t+ZSAtOWkKQpOSeTfnZty0m3CDrsu1uNB9swv2pZ21IlN23J6w1uZsuV0y82bOzJhpM2EGTZdpMaERAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPjrUmteK0RypXifid5n1tyX6j7+9\/vvUEsHCGo104BhAgAAAAAAAAAAAQAAAAAAUEsBAgAAFAAICAgA912FVERZNpJcAAAAAAgAABYABAAAAAAAAAAAALSBAAAAAE9UTV8tSUQxNzUtMF8yXzVfYi5NRU0BAAAAUEsBAgAAFAAICAgA\/F2FVPeBHLOMAgAAAAABABYABAAAAAAAAAAAALSBrAAAAE9UTV8tSUQxNzUtMF8yXzVfYi5UTUQBAAAAUEsBAgAAFAAICAgA\/F2FVGo104BhAgAAAAABABYABAAAAAAAAAAAALSBiAMAAE9UTV8tSUQxNzUtMF8yXzVfYi5UTUkBAAAAUEsGBiwAAAAAAAAAHgMtAAAAAAAAAAAAAwAAAAAAAAADAAAAAAAAANgAAAAAAAAAOQYAAAAAAABQSwYHAAAAABEHAAAAAAAAAQAAAFBLBQYAAAAAAwADANgAAAA5BgAAAAA=" }
Response example:{
"name": "examle_tm"
}

TM already exists:
{
  "ReturnValue": 65535,
  "ErrorMsg": ""
}

4. Delete TM

PurposeDeletes .TMD, .TMI, .MEM files RequestDelete /%service%/%tm_name%/Params

-

Code Block
languagejs
titleResponse
collapsetrue
Response example:%empty_anyway%
Code Block
languagejs
titleResponse
collapsetrue
Response example:%empty_anyway%

5. Import provided base64 encoded TMX file into TM

PurposeImport provided base64 encoded TMX file into TM. Starts another thead for import. For checking import status use status callRequestPOST /%service%/%tm_name%/importParams

{"tmxData": "base64EncodedTmxFile" }

Code Block
languagejs
titleResponse
collapsetrue
Request example:{
  "tmxData":   "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPHRteCB2ZXJzaW9uPSIxLjQiPgogIDxoZWFkZXIgY3JlYXRpb250b29sPSJTREwgTGFuZ3VhZ2UgUGxhdGZvcm0iIGNyZWF0aW9udG9vbHZlcnNpb249IjguMCIgby10bWY9IlNETCBUTTggRm9ybWF0IiBkYXRhdHlwZT0ieG1sIiBzZWd0eXBlPSJzZW50ZW5jZSIgYWRtaW5sYW5nPSJlbi1HQiIgc3JjbGFuZz0iYmctQkciIGNyZWF0aW9uZGF0ZT0iMjAxNTA4MjFUMDkyNjE0WiIgY3JlYXRpb25pZD0idGVzdCIvPgogIDxib2R5PgoJPHR1IGNyZWF0aW9uZGF0ZT0iMjAxODAyMTZUMTU1MTA1WiIgY3JlYXRpb25pZD0iREVTS1RPUC1SNTlCT0tCXFBDMiIgY2hhbmdlZGF0ZT0iMjAxODAyMTZUMTU1MTA4WiIgY2hhbmdlaWQ9IkRFU0tUT1AtUjU5Qk9LQlxQQzIiIGxhc3R1c2FnZWRhdGU9IjIwMTgwMjE2VDE2MTMwNVoiIHVzYWdlY291bnQ9IjEiPgogICAgICA8dHV2IHhtbDpsYW5nPSJiZy1CRyI+CiAgICAgICAgPHNlZz5UaGUgPHBoIC8+IGVuZDwvc2VnPgogICAgICA8L3R1dj4KICAgICAgPHR1diB4bWw6bGFuZz0iZW4tR0IiPgogICAgICAgIDxzZWc+RXRoIDxwaCAvPiBkbmU8L3NlZz4KICAgICAgPC90dXY+CiAgICA8L3R1PgogIDwvYm9keT4KPC90bXg+Cg=="
}Response example:Error in case of errorFrom v0_2_15
{ "%tm_name%":"deleted"} in case of success
Check status of import using status call

6. Export TMX from TM

PurposeCreates TMX from tm.
RequestGET /%service%/%tm_name%/
Headers

Accept - applicaton/xml

Code Block
languagejs
titleResponse
collapsetrue
Response example:<?xml version="1.0" encoding="UTF-8" ?>
<tmx version="1.4">
<header creationtoolversion="0.2.14" gitCommit="60784cf * refactoring and cleanup" segtype="sentence" adminlang="en-us" srclang="en-GB" o-tmf="t5memory" creationtool="t5memory" datatype="xml" />
<body>
  <tu tuid="1" datatype="xml" creationdate="20190401T084052Z">
     <prop type="tmgr:segNum">10906825</prop>
     <prop type="tmgr:markup">OTMXML</prop>
     <prop type="tmgr:docname">none</prop>
     <tuv xml:lang="en-GB">
          <prop type="tmgr:language">English(U.K.)</prop>
          <seg>For > 100 setups.</seg>
     </tuv>
     <tuv xml:lang="de-DE">
          <prop type="tmgr:language">GERMAN(REFORM)</prop>
     <seg>Für > 100 Aufstellungen.</seg>
     </tuv>
     </tu>
   </body>
</tmx>

7. Export in internal format

PurposeCreates and exports archive with .TMD, .TMI, .MEM files of TMRequestGET /%service%/%tm_name%/Headers

application/zip

Code Block
languagejs
titleResponse
collapsetrue
Response example:%binary_data%

8. Get the status of TM

RequestGET /%service%/%tm_name%/statusParams

-

Code Block
languagejs
titleResponse
collapsetrue
Response example:{
    "tmxImportStatus": "available"
}
The status could be "available", "import" or "failed" if the import had errors. If it's "failed" ErrorMsg would be added

9. Fuzzy search

Purpose Returns enrties\translations with small differences from requestedRequestPOST /%service%/%tm_name%/fuzzysearchParams

Required: source, sourceLang, targetLang

iNumOfProposal -  limit of found proposals - max is 20, if 0 → use default value '5' 
Code Block
languagejs
titleResponse
collapsetrue
RequestResponse example:


Request example:
{  "sourceLang":"en-GB",   "targetLang":"de",   "source":"For > 100 setups.",   ["documentName":"OBJ_DCL-0000000845-004_pt-br.xml"],   ["segmentNumber":""],   ["markupTable":"OTMXUXLF"],   ["context":"395_408"],   ["numOfProposals":20],   ["loggingThreshold": 0]}





Response example:

Success:
{
  "ReturnValue": 0,
  "ErrorMsg": "",
  "NumOfFoundProposals": 1, 
  "results": [
  {
     "source": "For > 100 setups.",
     "target": "Für > 100 Aufstellungen.",
     "segmentNumber": 10906825,
     "id{//just opened tm, without import\reorganize called
    "status": "open",
    "lastAccessTime": "",
    "creationTime": "20230703T122212Z",
    "tmCreatedInT5M_version": "0:5:1"
}

{// after reorgainize was called 
    "status": "open",
    "reorganizeStatus": "available",
    "reorganizeTime": 100,
    "reorganizeTime": "Overall reorganize time is      : 0:00:02\n",
    "segmentsReorganized": 1112,
    "invalidSegments": 10,
    "invalidSegmentsRCs": "5005:10; ",
    "firstInvalidSegments": "123; 432; 554; 623; 659; 675; 741; 742; 753; 755; ",
    "invalidSymbolErrors": -1,
    "reorganizeErrorMsg": "",
       "documentNamelastAccessTime": "none",
      "documentShortNamecreationTime": "NONE20230810T095233Z",
      "sourceLangtmCreatedInT5M_version": "en-GB",
    "targetLang": "de-DE",
    "type": "Manual",
    "matchType0:5:10"
}   {//not opened but available on the disk 
	"status": "Exactavailable",
    "author": "",
    "timestamp
}
{//not found tm {
    "status": "20190401T084052Znot found",
      "matchRateres": 100,
    "markupTable": "OTMXML",
    "context": "",
   "additionalInfo": ""
   }
 ]
}

Not found:

{
"ReturnValue": 133,
"ErrorMsg": "OtmMemoryServiceWorker::concordanceSearch::"
}

10. Concordance search

PurposeReturns entries\translations that contain requested segmentRequestPOST /%service%/%tm_name%/concordancesearchParams

Required: searchString - what we are looking for , searchType ["Source"|"Target"|"SourceAndTarget"] - where to look

iNumOfProposal -  limit of found proposals - max is 20, if 0 → use default value '5' 

Code Block
languagejs
titleResponse
collapsetrue
Request example:
{
    "searchString": "The",
    "searchType": "source",
    ["searchPosition": "",]
    ["numResults": 20,]
    ["msSearchAfterNumResults": 250,]
	["loggingThreshold": 0]
}
Response example:Success:
{
  "ReturnValue": 0,
  "NewSearchPosition": null,
  "results": [
  {
     "source": "For > 100 setups.",
     "target": "Für > 100 Aufstellungen.",
     "segmentNumber": 10906825,
     "id": "",
     "documentName": "none",
     "documentShortName": "NONE",
     "sourceLang": "en-GB",← rfc5646     
     "targetLang": "de-DE",← rfc5646
     "type": "Manual",
     "matchType": "undefined",
     "author": "",
     "timestamp": "20190401T084052Z",
     "matchRate": 0,
     "markupTable": "OTMXML",
     "context": "",
     "additionalInfo": ""
   }
  ],
 "ErrorMsg": ""
}

Success, but with NewSearchPosition - not all TM was checked, use this position to repeat search:
{
  "ReturnValue": 0,
  "NewSearchPosition": "8:1",
  "results": [
  {
     "source": "For > 100 setups.",
     "target": "Für > 100 Aufstellungen.",
     "segmentNumber": 10906825,
     "id": "",
    "documentName": "none",
    "documentShortName": "NONE",
    "sourceLang": "en-GB",
    "targetLang": "de-DE",
    "type": "Manual",
     "matchType": "undefined",
     "author": "",
     "timestamp": "20190401T084052Z",
     "matchRate": 0,
     "markupTable": "OTMXML",
     "context": "",
     "additionalInfo": ""
   }
  ],
 "ErrorMsg": ""
}
SearchPosition / NewSearchPositionFormat: "7:1"
First is segmeng\record number, second is target number
The NextSearchposition is an internal key of the memory for the next position on sequential access. Since it is an internal key, maintained and understood by the underlying memory plug-in (for EqfMemoryPlugin is it the record number and the position in one record),
no assumptions should be made regarding the content. It is just a string that, should be sent back to OpenTM2 on the next request, so that the search starts from there.
So is the implementation in Translate5: The first request to OpenTM2 contains SearchPosition with an empty string, OpenTM2 returns than a string in NewSearchPosition, which is just resent to OpenTM2 in the next request.

Not found:{
"ReturnValue": 0,
"NewSearchPosition": null,
"ErrorMsg": ""
}TM not found:{
"ReturnValue": 133,
"ErrorMsg": "OtmMemoryServiceWorker::concordanceSearch::"
}

11. Update entry

PurposeUpdates entry\translation RequestPOST /%service%/%tm_name%/entryParams

Only sourceLang, targetLang, source and target are required

Code Block
languagejs
titleResponse
collapsetrue
Request example:
{
    "source": "The end",
    "target": "The target",
    "sourceLang": "en",
    "targetLang": "de", 
    ["documentName": "Translate5 Demo Text-en-de.xlf"],
    ["segmentNumber": 8,]
    ["author": "Thomas Lauria"],
    ["timeStamp": "20210621T071042Z"],
    ["context": "2_2"],
    ["addInfo": "2_2"], 
    ["type": "Manual"],
    ["markupTable": "OTMXUXLF"],
	["loggingThreshold": 0] 
}
Response example:

12. Delete entry

PurposeDeletes entry\translation RequestPOST /%service%/%tm_name%/entrydeleteParams

Only sourceLang, targetLang, source, and target are required

Deleting based on strict match(including tags and whitespaces) of target and source

Code Block
languagejs
titleResponse
collapsetrue

Request example:
{
  "sourceLang": "bg",
  "targetLang": "en",
  "source": "The end",
  "target": "Eth dne"
  ["documentName": "my file.sdlxliff",]
  ["segmentNumber": 1,]
  ["markupTable": "translate5",]
  ["author": "Thomas Lauria",]
  ["type": "",]
  ["timeStamp": ""],
  ["context": "",]
   ["addInfo": ""] ,  ["loggingThreshold": 0] 
}

13. Save all TMs

Purpose

Flushes all filebuffers(TMD, TMI files) into the filesystem. Reset 'Modified' flags for file buffers. 

Filebuffer is a file instance of .TMD or .TMI loaded into RAM. It provides better speed and safety when working with files.

RequestGET /%service%/%tm_name%_service/savetmsParams

-

Code Block
languagejs
titleResponse
collapsetrue
Response example:{
'saved files': '/home/or/.t5memory/MEM/bg_internal_format.TMD; /home/or/.t5memory/MEM/bg_internal_format.TMI; /home/or/.t5memory/MEM/mem_gt_issue.TMD; /home/or/.t5memory/MEM/mem_gt_issue.TMI; EQFSYSW.PRP; '
}
List of saved files
 48 // 48- both tmi and tmd files are no found, 16- only TMD file not found, 32 - only TMI file not found
}
  The tmxImportStatus could be "available", "import" or "failed" if the import had errors. If there were at least one import to that tm, new fields would appear
{//tm in process of import
    "status": "open",
    "tmxImportStatus": "import",    
	"importProgress" : 56,    
	"importTime": "00:00:13",    
	"segmentsImported": 1356,    
	"invalidSegments": 23,    
	"invalidSymbolErrors": 2,    
	"importErrorMsg": "", 
    "lastAccessTime":  "%lastAccessTime",
    "creationTime": "20230703T122212Z",
    "tmCreatedInT5M_version": "0:5:1" 
}// in case if internal error happened, like t5memory would have error 5034 or 5035 which indicates, that tm size is reached it's limit and you should create new one to save new segments or part that left from tmx that you tried to import, status would look like this
{
"status": "open",
"tmxImportStatus": "failed",
"importProgress": 100,
"importTime": "Overall import time is : 0:00:19\n",
"segmentsImported": 445,
"invalidSegments": 1,
"invalidSymbolErrors": 0,
"importErrorMsg": "Warning: encoding 'UTF-16' from XML declaration or manually set contradicts the auto-sensed encoding; ignoring at column 40 in line 1; \n Fatal internal Error at column 6 in line 9605, import stopped at progress = 0%, errorMsg: TM is reached it's size limit, please create another one and import segments there, rc = 5034; aciveSegment = 1834\n\nSegment 1834 not imported\r\n\nReason = \nDocument = none\nSourceLanguage = de-DE\nTargetLanguage = en-GB\nMarkup = OTMXUXLF\nSource = in Verbindung mit Befestigungswinkel MS-...-WPE-B zur Wandmontage eines Einzelgeräts\nTarget = In combination with mounting bracket MS-...-WPE-B for wall mounting an individual component ",
"lastAccessTime": "",
"ErrorMsg": " Fatal internal Error at column 6 in line 9605, import stopped at progress = 0%, errorMsg: TM is reached it's size limit, please create another one and import segments there, rc = 5034; aciveSegment = 1834\n\nSegment 1834 not imported\r\n\nReason = \nDoc"
}

So you would have info about last segment which interrupted tm import.
In 0.6.44 and up you would have also optimized way of loading tm, to prevent blocking tm list for a long time, t5memory is using lazy loading, so new statuses are added. First tm is added to the tm list, then it's loading and it could be in new states: waiting to be loaded, loading, failed to load(and old open if it's loaded). In case if tm is failed to load, new request would try to load it again. Here are responces for status request - take attention that info in fields which are taken from binary data is set to 0 or empty till loading would be successful. Also in this version new fields are added: sizeInRAM(in bytes) - precalculated size of tm in RAM, activeRequest - would have info about which blocking requests is running now. 
You don't need to wait till tm would be loaded, because t5memory should manage that. So you can send few, for example, fuzzy requests to not open memory, first would try to open tm, and second would wait for the first one. If it would fail, second would also try to open tm. 

a)
{
"status": "available"
}

b)
{
"status": "waiting for loading",
"sizeInRAM": 48304,
"activeRequest": "",
"lastAccessTime": "20240930T093759Z",
"creationTime": "",
"tmCreatedInT5M_version": "0:0:0",
"segmentIndex": 0,
"sourceLang": "",
"internalDescription": ""
}

c)
{
"status": "failed to open",
"sizeInRAM": 48304,
"activeRequest": "",
"lastAccessTime": "20240930T094715Z",
"creationTime": "",
"tmCreatedInT5M_version": "0:0:0",
"segmentIndex": 0,
"sourceLang": "",
"internalDescription": ""
}

d)
{
"status": "loading",
"sizeInRAM": 48304,
"activeRequest": "",
"lastAccessTime": "20240930T094118Z",
"creationTime": "",
"tmCreatedInT5M_version": "0:0:0",
"segmentIndex": 0,
"sourceLang": "",
"internalDescription": ""
}

e)
{
"status": "open",
"sizeInRAM": 2686128,
"activeRequest": "",
"lastAccessTime": "20240930T093859Z",
"creationTime": "20240926T105408Z",
"tmCreatedInT5M_version": "0:6:39",
"segmentIndex": 1750,
"sourceLang": "de-DE",
"internalDescription": ""
}




src, trg, req

Fuzzy search

Purpose Returns enrties\translations with small differences from requested
RequestPOST /%service%/%tm_name%/fuzzysearch
Params

Required: source, sourceLang, targetLang

iNumOfProposal -  limit of found proposals - max is 20, if 0 → use default value '5' 

14. Shutdown service

PurposeSafely shutting down the service with\without saving all loaded tm files to the disk
RequestGET /%service%/%tm_name%_service/shutdown?dontsave=1
Params

dontsave=1(optional in address) - skips saving tms, for now value doesn't matter, only presence


Code Block
languagejs
titleResponse
collapsetrue
ResponseRequest example:%Empty%

15. Test tag replacement call

PurposeUpdates entry\translation 
RequestPUT /%service%/%tm_name%/entry
Params
Code Block
languagejs
titleResponse
collapsetrue
Fuzzy search tag replacement test:
Request


Request example:
{
 // required fields
  "srcsourceLang": "Tap <ph x='1'/>View <ph x='2' />o<bpt i='1' x='3'/> get <ph x='4'>strong</ph>displayed<ph x='5'>View</ph> two strong<ept i='1' x='6'/>US patents.",
    "trg": "View <ph x='1'/> tap <ph x='2' />to<bpt i='1' x='3'/> got <ph x='4'>strong</ph>dosplayd<ph x='5'>Veiw</ph> two strong<ept i='1' x='6'/>US patents.",
    "req": "Tap <x id='123'/>View <x id='222' />o<g> get <x id='44'>strong</x>displayed<x id='51'>View</x> two strong</g>US patents."
} 

Response example:
//'1' - request result 
//'2' - src result 
//'3' - trg result
{
 '1' :'Tap <x id="123"/>View <x id="222"/>o<bx/> get <x id="44"/>displayed<x id="51"/> two strong<ex/>US patents.',
 '2' :'Tap <x id="123"/>View <x id="222"/>o<g> get <x id="44"/>displayed<x id="51"/> two strong</g>US patents.',
 '3' :'View <x id="123"/> tap <x id="222"/>to<g> got <x id="44"/>dosplayd<x id="51"/> two strong</g>US patents.',
};

Import tag replacement test:
Request example:
{
    "src": "Tap <ph/>View <ph/>o<bpt/> get <ph>strong</ph>displayed<ph>View</ph> two strong<ept/>US patents.",
    "trg": "View <ph/> tap <ph/>to<bpt/> got <ph>strong</ph>dosplayd<ph>Veiw</ph> two strong<ept/>US patents.",
}
Response example:
{
 '1' :'Tap <ph x="1"/>View <ph x="2"/>o<bpt x="3" i="1"/> get <ph x="4"/>displayed<ph x="5"/> two strong<ept x="6" i="1"/>US patents.',
 '2' :'View <ph x="1"/> tap <ph x="2"/>to<bpt x="3" i="1"/> got <ph x="4"/>dosplayd<ph x="5"/> two strong<ept x="6" i="1"/>US patents.',
};

...

"en-GB",    // langs would be checked with languages.xml 
  "targetLang":"de",   
  "source":"For > 100 setups.", 

 // optional fields  
  ["documentName":"OBJ_DCL-0000000845-004_pt-br.xml"],   
  ["segmentNumber":15],   
  ["markupTable":"OTMXUXLF"],    //if there is no markup, default OTMXUXLF would be used. 
								 //Markup tables should be located inside ~/.t5memory/TABLE/%markup$.TBL 
  ["context":"395_408"],  
  ["numOfProposals":20],   // num of expected segments in output. By default it's 5
  ["loggingThreshold": 0]
}





Response example:

Success:
{
    "ReturnValue": 0,
    "ErrorMsg": "",
    "NumOfFoundProposals": 1,
    "results": [
        {
            "source": "The end",
            "target": "The target",
            "segmentNumber": 0,
            "id": "",
            "documentName": "Te2.xlf",
            "sourceLang": "de-DE",
            "targetLang": "EN-GB",
            "type": "Manual",
            "author": "THOMAS LAURIA",
            "timestamp": "20231228T171821Z",
            "markupTable": "OTMXUXLF",
            "context": "2_3",
            "additionalInfo": "",
            "internalKey": "7:1",
            "matchType": "Fuzzy",
            "matchRate": 50,
            "fuzzyWords": 0,
            "fuzzyDiffs": 0
        }
    ]
} 

example 2
{
  "ReturnValue": 0,
  "ErrorMsg": "",
  "NumOfFoundProposals": 1, 
  "results": [
  {
     "source": "For > 100 setups.",
     "target": "Für > 100 Aufstellungen.",
     "segmentNumber": 10906825,
     "id": "",
     "documentName": "none",
    "documentShortName": "NONE",
    "sourceLang": "en-GB",
    "targetLang": "de-DE",
    "type": "Manual",
    "matchType": "Exact", // could be exact or fuzzy
    "author": "",
    "timestamp": "20190401T084052Z",
    "matchRate": 100,
     "fuzzyWords": -1, // for exact match it would be -1 here and in diffs
     "fuzzyDiffs": -1, // otherwise here would be amount of parsed words and diffs that was 
					   // used in fuzzy matchrate calculation    
	 "markupTable": "OTMXML",
     "context": "",
     "additionalInfo": ""
   }
 ]
}

Not found:

{
"ReturnValue": 133,
"ErrorMsg": "OtmMemoryServiceWorker::concordanceSearch::"
}
For exact match used function that's comparing strings ignoring whitespaces. First normalized strings(without tags).
If it's the same string, then t5memory is checking string with tags and could return 100 or 97 match rate depending on result.

Then it's checking context match rate and if document name is the same(non case sensitive)

Then it's checking and modifying exactMatchRate according to code in code block below.
After that it would store exact matches only with usMatchLevel>=100. If there would be no exact matches, fuzzy match calculations would begin.
In case if there is at least one exact match, any fuzzy matches would be skipped.
In case if we have only one exact exact match, it's rate would be set to 102


For equal matches with 100% word matches but different whitespaces/newlines, each whitespace/newline diffs would be count as -1%. For punctuation, at least for 0.4.50, each punctuation would count as word token. This would be changed in future to count punctuation as whitespaces. 

For fuzzy calculation tags would be removed from text, except t5:np tags, which would be replaced with their "r" attribute to be counted as 1 word per tag. 
 

For fuzzy rate calculation we count words and then diffs in normalized string(without tags), using this formula: 
  if (usDiff < usWords )
  {
    *pusFuzzy = (usWords != 0) ? ((usWords - usDiff)*100 / usWords) : 100;
  }
  else
  {
    *pusFuzzy = 0;
  } /* endif */ Regarging Number Protection feature, tags from number protection would be replaced with their regexHashes from their attributes, so they would be count as 1 word each. NP with the same regex would be counted as equal
 To count diffs, t5memory go throuht both segments to find matching tokens, to find something called snake- line of matching tokens. 
 Then It marks unmatched as INSERTED or DELETED tokens, and based on that it calculates diffs.

if it's 100% rate, we add tags and compare it again
if then it's not equal, here is how match rate would be changed - probably this would never happens, because we have exact match test before fuzzy, 
and we do exact test even if triplesHashes is different(which is pre-fuzzy calculation and if it's equal, it could be flag that trigger exact test)

  if ( !fStringEqual )
  {
    if ( usFuzzy > 3 )
    {
      usFuzzy -= 3;
    }
    else
    {
       usFuzzy = 0;
    } /* endif */
    usFuzzy = std::min( (USHORT)99, usFuzzy );
  } /* endif */

then depending on type of translation it could tweak rate
if ( (usModifiedTranslationFlag == TRANSLFLAG_MACHINE) && (usFuzzy < 100) )
{
  // ignore machine fuzzy matches
}
else if ( usFuzzy > TM_FUZZINESS_THRESHOLD )
{
  /********************************************************/
  /* give MT flag a little less fuzziness */
  /********************************************************/
  if ( usModifiedTranslationFlag == TRANSLFLAG_MACHINE )
  {
    if ( usFuzzy > 1 )
    {
      usFuzzy -= 1;
    }
    else
    {
      usFuzzy = 0;
    } /* endif */
  } /* endif */
  if (usFuzzy == 100 && (pGetIn->ulParm & GET_RESPECTCRLF) && !fRespectCRLFStringEqual )
  { // P018279!
    usFuzzy -= 1;
  }
   add to resulting set
} /* endif */
} /* endif */


At the end fuzzy request replaces tags in proposal from TM with tags from request, and if matchRate >= 100, it calculates whitespace diffs and apply matchRate-= wsDiffs


Code Block
languagejs
titleResponse
collapsetrue
ExactMatchRate calculation:so, before usExact is equal to 97 or 100, depending if strings with tags are equal ignoring whitespaces  and then code do some tweaks.
 pClb is struct that have proposals from TM, pGetIn is fuzzy requests data

 // loop over CLBs and look for best matching entry
{
  LONG lLeftClbLen; // left CLB entries in CLB list
  PTMX_TARGET_CLB pClb; // pointer for CLB list processing
  #define SEG_DOC_AND_CONTEXT_MATCH 8
  #define DOC_AND_CONTEXT_MATCH 7
  #define CONTEXT_MATCH 6
  #define SAME_SEG_AND_DOC_MATCH 5
  #define SAME_DOC_MATCH 4
  #define MULT_DOC_MATCH 3
  #define NORMAL_MATCH 2
  #define IGNORE_MATCH 1
  SHORT sCurMatch = 0;

  // loop over all target CLBs
  pClb = pTMXTargetClb;
  lLeftClbLen = RECLEN(pTMXTargetRecord) -
  pTMXTargetRecord->usClb;
  while ( ( lLeftClbLen > 0 ) && (sCurMatch < SAME_SEG_AND_DOC_MATCH) )
  {
    USHORT usTranslationFlag = pClb->bTranslationFlag;
    USHORT usCurContextRanking = 0; // context ranking of this match
    BOOL fIgnoreProposal = FALSE;
    // apply global memory option file on global memory proposals
   if ( pClb->bTranslationFlag == TRANSLFLAG_GLOBMEM ) // pClb it's segment in TM
   {
       if ( (pGetIn->pvGMOptList != NULL) && pClb->usAddDataLen ) // pGetIn it's fuzzy requests segment
       {

           USHORT usAddDataLen = NtmGetAddData( pClb, ADDDATA_ADDINFO_ID, pContextBuffer, MAX_SEGMENT_SIZE );
           if ( usAddDataLen )
           {
               GMMEMOPT GobMemOpt = GlobMemGetFlagForProposal( pGetIn->pvGMOptList, pContextBuffer );
               switch ( GobMemOpt )
               {
                  case GM_SUBSTITUTE_OPT: usTranslationFlag = TRANSLFLAG_NORMAL; break;
                  case GM_HFLAG_OPT : usTranslationFlag = TRANSLFLAG_GLOBMEM; break;
                  case GM_HFLAGSTAR_OPT : usTranslationFlag = TRANSLFLAG_GLOBMEMSTAR; break;
                  case GM_EXCLUDE_OPT : fIgnoreProposal = TRUE; break;
               } /* endswitch */
          } /* endif */
     } /* endif */ 

     if ( pClb == pTMXTargetClb )
    {
       usTargetTranslationFlag = usTranslationFlag;
    } /* endif *
  } /* endif */ 


  // check context strings (if any)
  if ((!fIgnoreProposal)
       && pGetIn->szContext[0]
       && pClb->usAddDataLen )
   {
       USHORT usContextLen = NtmGetAddData( pClb, ADDDATA_CONTEXT_ID, pContextBuffer, MAX_SEGMENT_SIZE );
       if ( usContextLen != 0 )
       {
            usCurContextRanking = NTMCompareContext( pTmClb, pGetIn->szTagTable, pGetIn->szContext, pContextBuffer );
       } /* endif */
    } /* endif */


  // check for matching document names
  if ( pGetIn->ulParm & GET_IGNORE_PATH )
  {
     // we have to compare the real document names rather than comparing the document name IDs
     PSZ pszCLBDocName = NTMFindNameForID( pTmClb, &(pClb->usFileId), (USHORT)FILE_KEY );
     if ( pszCLBDocName != NULL )
     {
        PSZ pszName = UtlGetFnameFromPath( pszCLBDocName );
        if ( pszName == NULL )
        {
           pszName = pszCLBDocName;
         } /* endif */
      fMatchingDocName = stricmp( pszName, pszDocName ) == 0;
    }
    else
    {
       // could not access the document name, we have to compare the document name IDs
      fMatchingDocName = ((pClb->usFileId == usGetFile) || (pClb->usFileId == usAlternateGetFile));
    } /* endif */
  }
  else
  {
     // we can compare the document name IDs
     fMatchingDocName = ((pClb->usFileId == usGetFile) || (pClb->usFileId == usAlternateGetFile));
  } /* endif */


  if ( fIgnoreProposal )
  {
    if ( sCurMatch == 0 )
    {
      sCurMatch = IGNORE_MATCH;
    } /* endif */
  }
  else if ( usCurContextRanking == 100 )
  {
    if ( fMatchingDocName && (pClb->ulSegmId >= (pGetIn->ulSegmentId - 1)) && (pClb->ulSegmId <= (pGetIn->ulSegmentId + 1)) )
    {
      if ( sCurMatch < SEG_DOC_AND_CONTEXT_MATCH )
      {
         sCurMatch = SEG_DOC_AND_CONTEXT_MATCH;
        pTMXTargetClb = pClb; // use this target CLB for match
        usTargetTranslationFlag = usTranslationFlag;
        usContextRanking = usCurContextRanking;
      }
    }
    else if ( fMatchingDocName )
    {
    if ( sCurMatch < DOC_AND_CONTEXT_MATCH )
    {
      sCurMatch = DOC_AND_CONTEXT_MATCH;
      pTMXTargetClb = pClb; // use this target CLB for match
      usTargetTranslationFlag = usTranslationFlag;
      usContextRanking = usCurContextRanking;
     }
     else if ( sCurMatch == DOC_AND_CONTEXT_MATCH )
     {
       // we have already a match of this type so check if context ranking
       if ( usCurContextRanking > usContextRanking )
       {
          pTMXTargetClb = pClb; // use newer target CLB for match
          usTargetTranslationFlag = usTranslationFlag;
          usContextRanking = usCurContextRanking;
       }
       // use time info to ensure that latest match is used
       else if ( usCurContextRanking == usContextRanking )
       {
         // GQ 2015-04-10 New approach: If we have an exact-exact match use this one, otherwise use timestamp for the comparism
         BOOL fExactExactNewCLB = fMatchingDocName && (pClb->ulSegmId >= (pGetIn->ulSegmentId - 1)) && (pClb->ulSegmId <= (pGetIn->ulSegmentId + 1));
         BOOL fExactExactExistingCLB = ((pTMXTargetClb->usFileId == usGetFile) || (pTMXTargetClb->usFileId == usAlternateGetFile)) &&
         (pTMXTargetClb->ulSegmId >= (pGetIn->ulSegmentId - 1)) && (pTMXTargetClb->ulSegmId <= (pGetIn->ulSegmentId + 1));
         if ( fExactExactNewCLB && !fExactExactExistingCLB )
         {
           // use exact-exact CLB for match
           pTMXTargetClb = pClb;
           usTargetTranslationFlag = usTranslationFlag;
           usContextRanking = usCurContextRanking;
         }
         else if ( (fExactExactNewCLB == fExactExactExistingCLB) && (pClb->lTime > pTMXTargetClb->lTime) )
         {
           // use newer target CLB for match
           pTMXTargetClb = pClb;
           usTargetTranslationFlag = usTranslationFlag;
           usContextRanking = usCurContextRanking;
         }
       } /* endif */
     } /* endif */
   }
   else
   {
     if ( sCurMatch < CONTEXT_MATCH )
     {
     sCurMatch = CONTEXT_MATCH;
     pTMXTargetClb = pClb; // use this target CLB for match
     usTargetTranslationFlag = usTranslationFlag;
     usContextRanking = usCurContextRanking;
     }
     else if ( sCurMatch == CONTEXT_MATCH )
     {
       // we have already a match of this type so check if context ranking
      if ( usCurContextRanking > usContextRanking )
      {
        pTMXTargetClb = pClb; // use newer target CLB for match
        usTargetTranslationFlag = usTranslationFlag;
        usContextRanking = usCurContextRanking;
      }
      // use time info to ensure that latest match is used
     else if ( (usCurContextRanking == usContextRanking) && (pClb->lTime > pTMXTargetClb->lTime) )
     {
       pTMXTargetClb = pClb; // use newer target CLB for match
       usTargetTranslationFlag = usTranslationFlag;
       usContextRanking = usCurContextRanking;
      } /* endif */
    } /* endif */
  } /* endif */
 }
 else if ( fMatchingDocName && (pClb->ulSegmId >= (pGetIn->ulSegmentId - 1)) && (pClb->ulSegmId <= (pGetIn->ulSegmentId + 1)) )
 {
   // same segment from same document available
   sCurMatch = SAME_SEG_AND_DOC_MATCH;
   pTMXTargetClb = pClb; // use this target CLB for match
   usContextRanking = usCurContextRanking;
   usTargetTranslationFlag = usTranslationFlag;
 }
 else if ( fMatchingDocName )
 {
    // segment from same document available
    if ( sCurMatch < SAME_DOC_MATCH )
    {
       sCurMatch = SAME_DOC_MATCH;
       pTMXTargetClb = pClb; // use this target CLB for match
       usTargetTranslationFlag = usTranslationFlag;
       usContextRanking = usCurContextRanking;
     }
     else if ( sCurMatch == SAME_DOC_MATCH )
     {
       // we have already a match of this type so
       // use time info to ensure that latest match is used
       if ( pClb->lTime > pTMXTargetClb->lTime )
       {
         pTMXTargetClb = pClb; // use newer target CLB for match
         usTargetTranslationFlag = usTranslationFlag;
         usContextRanking = usCurContextRanking;
       } /* endif */
     } /* endif */
   }
    else if ( pClb->bMultiple )
    {
       // multiple target segment available
       if ( sCurMatch < MULT_DOC_MATCH )
       {
         // no better match yet
         sCurMatch = MULT_DOC_MATCH;
         pTMXTargetClb = pClb; // use this target CLB for match
         usTargetTranslationFlag = usTranslationFlag;
         usContextRanking = usCurContextRanking;
       } /* endif */
     }
     else if ( usTranslationFlag == TRANSLFLAG_NORMAL )
     {
        // a 'normal' memory match is available
        if ( sCurMatch < NORMAL_MATCH )
        {
           // no better match yet
           sCurMatch = NORMAL_MATCH;
           pTMXTargetClb = pClb; // use this target CLB for match
           usTargetTranslationFlag = usTranslationFlag;
           usContextRanking = usCurContextRanking;
         } /* endif */
     } /* endif */

    // continue with next target CLB
    if ( sCurMatch < SAME_SEG_AND_DOC_MATCH )
    {
      lLeftClbLen -= TARGETCLBLEN(pClb);
      if (lLeftClbLen > 0)
      {
        usTgtNum++;
        pClb = NEXTTARGETCLB(pClb);
      }
    } /* endif */
} /* endwhile */


{
  BOOL fNormalMatch = (usTargetTranslationFlag == TRANSLFLAG_NORMAL) ||
  (usTargetTranslationFlag == TRANSLFLAG_GLOBMEM) ||
  (usTargetTranslationFlag == TRANSLFLAG_GLOBMEMSTAR);
  switch ( sCurMatch )
  {
    case IGNORE_MATCH :
      usMatchLevel = 0;
       break;
  case SAME_SEG_AND_DOC_MATCH :
      usMatchLevel = fNormalMatch ? usEqual+2 : usEqual-1;
      break;
   case SEG_DOC_AND_CONTEXT_MATCH :
       usMatchLevel = fNormalMatch ? usEqual+2 : usEqual-1; // exact-exact match with matching context
       break;
    case DOC_AND_CONTEXT_MATCH :
       if ( usContextRanking == 100 )
       {
         // GQ 2015/05/09: treat 100% context matches as normal exact matches
         // usMatchLevel = fNormalMatch ? usEqual+2 : usEqual-1;
         usMatchLevel = fNormalMatch ? usEqual+1 : usEqual-1;
        }
        else
        {
          usMatchLevel = fNormalMatch ? usEqual+1 : usEqual-1;
        } /* endif */
       break;
  case CONTEXT_MATCH :
    if ( usContextRanking == 100 )
    {
      // GQ 2015/05/09: treat 100% context matches as normal exact context matches
      // usMatchLevel = fNormalMatch ? usEqual+2 : usEqual-1;
      // GQ 2016/10/24: treat 100% context matches as normal exact matches
      usMatchLevel = fNormalMatch ? usEqual : usEqual-1;
    }
    else
    {
      usMatchLevel = fNormalMatch ? usEqual : usEqual-1;
     } /* endif */
     break;
  case SAME_DOC_MATCH :
    usMatchLevel = fNormalMatch ? usEqual+1 : usEqual-1;
    break;
  case MULT_DOC_MATCH :
     usMatchLevel = fNormalMatch ? usEqual+1 : usEqual-1;
     break;
  default :
     usMatchLevel = fNormalMatch ? usEqual : usEqual-1;
     break;
  } /* endswitch */
}
}



Here is structure of the segment from responses

{
"source":"in Verbindung 2 fds fdsa amit Befestigungswinkel fdsaf MS-...-WPE-B zur Wandmontage eines sfg Einzelgeräts"// source that was saved
"sourceNPRepl":"in Verbindung 2 fds fdsa amit Befestigungswinkel fdsaf MS-...-WPE-B zur Wandmontage eines sfg Einzelgeräts",// np replaced source - used for fuzzy and triples thresholds - here there are no NP tags, but their hashes
"sourceNorm":"in Verbindung 2 fds fdsa amit Befestigungswinkel fdsaf MS-...-WPE-B zur Wandmontage eines sfg Einzelgeräts",// normalized source - used for fuzzy calclulation - there are no tags at all
"target":"In combinahgfd tion with mounting bracket MS-...-WPE-B for wall mounting an individual component ",// saved target
"segmentNumber":1// previously segmentNumber  - internal Id generated in tm, or provided with update call. Can be used together with internalKey as a primary number in tm 
"id":"", // dummy field, 
"documentName":"Audioscript_Hybrides_Arbeiten.xlsx.sdlxliff",
"sourceLang":"DE-DE",// that langs in requests would be searched in languages.xml, and would be used best match(or preffered) 
"targetLang":"EN-GB",
"type":"Manual",
"author":"PROJECT MANAGER",
"timestamp":"",// if empty, current time would be used. 
"markupTable":"OTMXUXLF"// the same all the time, in the future could be refactored and deleted
"context":"390",// context and addinfo would be saved as additional field in cbl(internal data struct, which saves variants of other variables for the same translation, which have position pointed below - "11:1"
"additionalInfo":"",
"internalKey":"11:1"// internal position of the segment inside tmd file. could be shifted with deleting some other segments. both numbers should not be zero
}



New Concordance search

PurposeReturns entries\translations that fits selected filters. 
RequestPOST /%service%/%tm_name%/search
Params

Required: NONE

iNumOfProposal -  limit of found proposals - max is 200, if 0 → use default value '5' 

Search is made segment-by segment, and it's checking segment if it fits selected filters. You can search for EXACT or CONCORDANCE matches in this fields:
source, target, document, author, addInfo, context
To set filter, use it's SearchMode field, otherwise filter would be disabled. So you have sourceSearchMode, targetSearchMode, documentSearchMode,
authorSearchMode, addInfoSearchMode, contextSearchMode 

Search mode should be set explicitly to CONTAINS/CONCORDANCE or EXACT, otherwise filter would be ignored. But also each searchMode could have additional search parameters "CONTAINS, caseinsensetive, WHITESPACETOLERANT, INVERTED", all that values is not important, as well as delimiter. By default search is case sensetive. If you add Inverted option, check for that filter would be reverted. 
To check how filters would be parsed, check json in responce.  Field with that info could look like this:

"Filters":"
Search filter, field: SOURCE FilterType::CONTAINS SearchStr: 'THE'; Options: SEARCH_FILTERS_NOT|SEARCH_CASEINSENSITIVE_OPT|SEARCH_WHITESPACETOLERANT_OPT|;\n
Search filter, field: TARGET FilterType::EXACT SearchStr: ''; Options: SEARCH_CASEINSENSITIVE_OPT|;\n
Search filter, field: ADDINFO FilterType::CONTAINS SearchStr: 'some add info'; Options: SEARCH_WHITESPACETOLERANT_OPT|;\n
Search filter, field: CONTEXT FilterType::EXACT SearchStr: 'context context'; Options: ;\nSearch filter, field: AUTHOR FilterType::CONTAINS SearchStr: ''; Options: ;\n
Search filter, field: DOCUMENT FilterType::CONTAINS SearchStr: 'evo3_p1137_reports_translation_properties_de_fr_20220720_094902'; Options: SEARCH_FILTERS_NOT|;\n
Search filter, field: TIMESTAMP FilterType::RANGE Range: 20000121T115234Z - 20240121T115234Z Options: ;\n"
,

It's possible to apply filter just with SearchMode, like if you would type "authorSearchMode": "exact",but there would be no "author" field, it would look for segments, where author field is empty.

Also there are  timespan parameter, to set it, use this fields and format:

"timestampSpanStart":"20000121T115234Z",
"timestampSpanEnd":"20240121T115234Z",

You should set both parameters to apply filter, otherwise you would get error as return.  Check output to see how it was parsed and applied.
By default all mentioned filters is applied in logical and combination, but you can change that globaly with adding 

"logicalOr": 1
Then all mentioned filters would be applied in logical or combination(please, use 1 to set this to true, boolean type is not supported by json parser in t5memory). Supported since 0.6.5

"onlyCountSegments":1

Instead of returning segments, just count them and return counter in 

"NumOfFoundSegments":22741

Also there are lang filters, they would always be applied to selection of segments that passed previous filters, so value of  "logicalOr": 1,  wouldn't be applied to that.
To set language filters, use this fields:

"sourceLang":"en-GB",
"targetLang":"de",

Lang filters could be applied with major lang feature, so source lang in this case would be applied as exact filter for source lang, but target lang would check if langs is in the same lang group. That check is done in languages.xml file with isPreferred flag.
Lang filters and if filters is combined in logical or or logical and you can check in  GlobalSearchOptions  field of responce. It could look like this: 

"GlobalSearchOptions":"SEARCH_FILTERS_LOGICAL_OR|SEARCH_EXACT_MATCH_OF_SRC_LANG_OPT, lang = en-GB|SEARCH_GROUP_MATCH_OF_TRG_LANG_OPT, lang = de",

Other that you can send is:

"searchPosition":"8:1",
"numResults":2,
"msSearchAfterNumResults":250
"loggingThreshold": 4 - check other requests,

So search position is position where to start search internaly in btree. This search is limited by num of found segment(set by numResults) or timeout(set by msSearchAfterNumResults), but timeout would be ignored in case if there are no segments in the tm to fit params.  Max  numResults is 200.

You can send empty json and search would work fine, but it would just return first 5 segments in tm
You can go through all segment  with using this 2 fields
"searchPosition":"8:1",
"numResults":200 

and just updating  searchPosition with  NewSearchPosition
from responce.


Code Block
languagejs
titleResponse
collapsetrue
{     
    "logicalOr": 1,
     "source":"the",
    "sourceSearchMode":"CONTAINS, CASEINSENSETIVE, WHITESPACETOLERANT, INVERTED",
    
    "target":"",
    "targetSearchMode":"EXACT, CASEINSENSETIVE",
    
    "document":"evo3_p1137_reports_translation_properties_de_fr_20220720_094902",
    "documentSearchMode":"CONTAINS, INVERTED",
    
     "author":"some author",
     "authorSearchMode":"CONTAINS",

    "timestampSpanStart": "20000121T115234Z",
    "timestampSpanEnd": "20240121T115234Z",

    "addInfo":"some add info",
    "addInfoSearchMode":"CONCORDANCE, WHITESPACETOLERANT",


    "context":"context context",
    "contextSearchMode":"EXACT",
    
    "sourceLang":"en-GB", 
    "targetLang":"SV",  
    "searchPosition": "8:1",
    "numResults": 2,
    "msSearchAfterNumResults": 25,
    "loggingThreshold": 3
}



So here search would be done in logical or way, so if any of source, target, document, context, author, timestamp filters returns true, result would be added to set, which then would be filtered out by sourceLang on exact match check and targetLang on groupLang check.
Search would start from position "8:1"(tm data start at "7:1" but if you wan't to start from the beggining, just avoid that param. 
numResuts:2 - so if there would be 2 segments found, search would end
"msSearchAfterNumResults": 25 - 25ms after first found segment, search would end, even if more segments was found, responce would contain "NewSearchPosition": "10:1", which can be used in searchPosition to continue search

Response example:Success:
example{
"Filters": "Search filter, field: SOURCE FilterType::CONTAINS SearchStr: 'THE'; Options: SEARCH_FILTERS_NOT|SEARCH_CASEINSENSITIVE_OPT|SEARCH_WHITESPACETOLERANT_OPT|;\n
Search filter, field: TARGET FilterType::EXACT SearchStr: ''; Options: SEARCH_CASEINSENSITIVE_OPT|;\n
Search filter, field: ADDINFO FilterType::CONTAINS SearchStr: 'some add info'; Options: SEARCH_WHITESPACETOLERANT_OPT|;\n
Search filter, field: CONTEXT FilterType::EXACT SearchStr: 'context context'; Options: ;\n
Search filter, field: AUTHOR FilterType::CONTAINS SearchStr: ''; Options: ;\n
Search filter, field: DOCUMENT FilterType::CONTAINS SearchStr: 'evo3_p1137_reports_translation_properties_de_fr_20220720_094902'; Options: SEARCH_FILTERS_NOT|;\n
Search filter, field: TIMESTAMP FilterType::RANGE Range: 20000121T115234Z - 20240121T115234Z Options: ;\n",
"GlobalSearchOptions": "SEARCH_FILTERS_LOGICAL_OR|SEARCH_EXACT_MATCH_OF_SRC_LANG_OPT, lang = en-GB|SEARCH_GROUP_MATCH_OF_TRG_LANG_OPT, lang = sv",
"ReturnValue": 0,
"ReturnMessage": "FOUND",
"NewSearchPosition": "10:1",
"results": [
{
"source": "Congratulations on the purchase of a <ph x=\"101\"/> machine control system.",
"target": "Gratulerar till köpet av maskinstyrningsystemet <ph x=\"101\"/>.",
"segmentNumber": 5740419,
"id": "",
"documentName": "none",
"sourceLang": "en-GB",
"targetLang": "SV-SE",
"type": "Manual",
"author": "",
"timestamp": "20170327T091814Z",
"markupTable": "OTMXUXLF",
"context": "",
"additionalInfo": "",
"internalKey": "8:1"
},
{
"source": "The <ph x=\"101\"/> System is an ideal tool for increasing productivity in all aspects of the construction earthmoving industry.",
"target": "Systemet <ph x=\"101\"/> är ett verktyg som lämpar sig perfekt för att öka produktiviteten inom alla delar av bygg- och anläggningsområdet.",
"segmentNumber": 5740420,
"id": "",
"documentName": "none",
"sourceLang": "en-GB",
"targetLang": "SV-SE",
"type": "Manual",
"author": "",
"timestamp": "20170327T091814Z",
"markupTable": "OTMXUXLF",
"context": "",
"additionalInfo": "",
"internalKey": "9:1"
}
]
}
SearchPosition / NewSearchPositionFormat: "7:1"
First is segment\record number, second is target number
The NextSearchposition is an internal key of the memory for the next position on sequential access. Since it is an internal key, maintained and understood by the underlying memory plug-in (for EqfMemoryPlugin is it the record number and the position in one record),
no assumptions should be made regarding the content. It is just a string that, should be sent back to OpenTM2 on the next request, so that the search starts from there.
So is the implementation in Translate5: The first request to OpenTM2 contains SearchPosition with an empty string, OpenTM2 returns than a string in NewSearchPosition, which is just resent to OpenTM2 in the next request.

Not found:{
"ReturnValue": 0,
"NewSearchPosition": null,
"ErrorMsg": ""
}TM not found:{
"ReturnValue": 133,
"ErrorMsg": "OtmMemoryServiceWorker::concordanceSearch::"
}



Here is search request with all possible parameters:
{
"logicalOr": 1, 

  "source":"the",

   "sourceSearchMode":"CONTAINS, CASEINSENSETIVE, WHITESPACETOLERANT, INVERTED",

   "target":"", "targetSearchMode":"EXACT, CASEINSENSETIVE",

   "document":"evo3_p1137_reports_translation_properties_de_fr_20220720_094902",

    "documentSearchMode":"CONTAINS, INVERTED", 

    "author":"some author",
    "authorSearchMode":"CONTAINS",

    "timestampSpanStart": "20000121T115234Z",

    "timestampSpanEnd": "20240121T115234Z",

    "addInfo":"some add info",

    "addInfoSearchMode":"CONCORDANCE, WHITESPACETOLERANT",

    "context":"context context",

    "contextSearchMode":"EXACT",

    "sourceLang":"en-GB",

    "targetLang":"SV",

    "searchPosition": "8:1",

    "numResults": 2,

     "msSearchAfterNumResults": 25,
     "loggingThreshold": 3
}
All fields is optional, but some depends on other, so error should be returned in case of not providing required field

So request with this body would also work:
{
}

ParametervalueTypedefault valuepossible valuesrequireFielddescription
sourceLangstring""langs that can 
be matched to
langs in languages.xml
-

Filter segments on src/trg lang attribute, 

If specified lang is preffered, matching is done based on lang family,
otherwise on exact match

targetLang
searchPositionstring"" (search would
start from "7:1" 
then
"8:1" etcpoint where to start search in tmd file
numResultsint5(0....200]points how many matches return in current request
msSearchAfterNumResults0no checksets how many ms should pass between first found segment and search stop, if it didn't reach the end yet. 
loggingThreshold-1[0...6]additional field to set log level on the run
logicalOrint00 for false, any other number as true, 
example:
"logicalOr": 1, 
"onlyCountSegments": 1
by default source, target, document, author, context, addinfo, timestamp is combined in logical AND, but by sending here "OR" you can switch that to logical OR, any other value would left it in default AND state.
Doesn't apply to sourceLang and targetLang filters, they are always in AND state
onlyCountSegmentsinstead of returning segment, would go in search till the end of tm and return total number of segments, that returns true with selected filters
sourcestring""any string, example 
"source": "data in the segment"
sourceSearchModeSets what to look for in source of the segments, based on type of search, specified in sourceSearchMode(exact, concordance). If sourceSearchMode is not specified, returns an error.
targettargetSearchMode--//–(the same as above but for corresponding fields)
documentdocumentSearchMode
authorauthorSearchMode
contextcontextSearchMode
addInfoaddInfoSearchMode
timestampSpanStartstringstring with date in format
 "20240121T115234Z"
timestampSpanEndSets filter for time.  You need to provid both timestamps, or none, otherwise request would return an error. Could be used in "OR" combination in 
"logicalOr": 1,, but, maybe, it's better to change that behaviour to similar like with langs(Always AND)
timestampSpanEndtimestampSpanStart
sourceSearchModestring""String with required 
EXACT or CONCORDANCE 
(or CONTAINS, what's equal to CONCORDANCE)
words and some optional, like
CASEINSENSETIVE for non case sensetive comparison,
WHITESPACETOLERANT for
modifying whitespaces(result of this actions you can see in filters in responce)
INVERTED  for applying filter in inverted state, so to return false on match and true 
if no match. Logical NOT

Attributes is not case sensetive, 
Separator doen't matters
-

Sets type of search for corresponding field. 
If you set, for example, "authorSearchMode" = "EXACT", but don't provide any
author in request, author field would be "", so request would look for segments, 
where author equals to "". The same is true for other fields
Examples:
1)
"source": "the  text inside"
"sourceSearchMode":"CONTAINS, CASEINSENSETIVE, WHITESPACETOLERANT, INVERTED", - search would be for all segments, which doesn't contains "the text inside" in non case sensetive mode and with normalizing whitespaces.
2) 
"author": "Ed Sheeran",
"authorSearchMode" = "Exact", -search would be done on exact case sensetive matches with "Ed Sheeran" in author field
3) 
"author": "Ed Sheeran",
"authorSearchMode" = "CASEINSENSETIVE", - ERROR, search mode(Exact\Contains) is not selected

4) "author": "Ed Sheeran",  - ERROR, search mode(Exact\Contains) is not selected


5) "authorSearchMode" = "CONTAINS",- OK, filter would check if segment contains "", so every segment would return true then

targetSearchMode
documentSearchMode
authorSearchMode
contextSearchMode
addInfoSearchMode





Concordance search

PurposeReturns entries\translations that contain requested segment
RequestPOST /%service%/%tm_name%/concordancesearch
Params

Required: searchString - what we are looking for , searchType ["Source"|"Target"|"SourceAndTarget"] - where to look

iNumOfProposal -  limit of found proposals - max is 20, if 0 → use default value '5' 


Code Block
languagejs
titleResponse
collapsetrue
Request example:
{
    "searchString": "The",
    "searchType": "source", // could be Source, Target, SourceAndTarget - says where to do search
    ["searchPosition": "",] 
    ["numResults": 20,]
    ["msSearchAfterNumResults": 250,]
	["loggingThreshold": 0]
}
Response example:Success:
example_new{
  "ReturnValue": "ENDREACHED_RC",
  "NewSearchPosition": null,
  "results": [
    {
      "source": "The end",
      "target": "The target",
      "segmentNumber": 0,
      "id": "",
      "documentName": "Te2.xlf",
      "sourceLang": "de-DE",
      "targetLang": "EN-GB",
     "type": "Manual",
     "author": "THOMAS LAURIA",
     "timestamp": "20231228T171821Z",
      "markupTable": "OTMXUXLF",
      "context": "2_3",
      "additionalInfo": "",
      "internalKey": "7:1"
    }
  ]
}

example_old
{
  "ReturnValue": 0,
  "NewSearchPosition": null,
  "results": [
  {
     "source": "For > 100 setups.",
     "target": "Für > 100 Aufstellungen.",
     "segmentNumber": 10906825,
     "id": "",
     "documentName": "none",
     "documentShortName": "NONE",
     "sourceLang": "en-GB",← rfc5646     
     "targetLang": "de-DE",← rfc5646
     "type": "Manual",
     "matchType": "undefined",
     "author": "",
     "timestamp": "20190401T084052Z",
     "matchRate": 0,
     "markupTable": "OTMXML",
     "context": "",
     "additionalInfo": ""
   }
  ],
 "ErrorMsg": ""
}

Success, but with NewSearchPosition - not all TM was checked, use this position to repeat search:
{
  "ReturnValue": 0,
  "NewSearchPosition": "8:1",
  "results": [
  {
     "source": "For > 100 setups.",
     "target": "Für > 100 Aufstellungen.",
     "segmentNumber": 10906825,
     "id": "",
    "documentName": "none",
    "documentShortName": "NONE",
    "sourceLang": "en-GB",
    "targetLang": "de-DE",
    "type": "Manual",
     "matchType": "undefined",
     "author": "",
     "timestamp": "20190401T084052Z",
     "matchRate": 0,
     "markupTable": "OTMXML",
     "context": "",
     "additionalInfo": ""
   }
  ],
 "ErrorMsg": ""
}
SearchPosition / NewSearchPositionFormat: "7:1"
First is segmeng\record number, second is target number
The NextSearchposition is an internal key of the memory for the next position on sequential access. Since it is an internal key, maintained and understood by the underlying memory plug-in (for EqfMemoryPlugin is it the record number and the position in one record),
no assumptions should be made regarding the content. It is just a string that, should be sent back to OpenTM2 on the next request, so that the search starts from there.
So is the implementation in Translate5: The first request to OpenTM2 contains SearchPosition with an empty string, OpenTM2 returns than a string in NewSearchPosition, which is just resent to OpenTM2 in the next request.

Not found:{
"ReturnValue": 0,
"NewSearchPosition": null,
"ErrorMsg": ""
}TM not found:{
"ReturnValue": 133,
"ErrorMsg": "OtmMemoryServiceWorker::concordanceSearch::"
}




Update entry

PurposeUpdates entry\translation 
RequestPOST /%service%/%tm_name%/entry
Params

Only sourceLang, targetLang, source and target are required


This request would made changes only in the filebuffer(so files on disk would not be changed)
To write it to the disk just call request which would flush tm to the disk as part of execution(exportTMX, exportTM, cloneTM) or using SaveAllTms request 

Code Block
languagejs
titleResponse
collapsetrue
Request example:
{
    "source": "The end",
    "target": "The target",
    "sourceLang": "en", // langs would be checked with languages.xml
    "targetLang": "de", 
//additional field
    ["documentName": "Translate5 Demo Text-en-de.xlf"],
    ["segmentNumber": 8,]
    ["author": "Thomas Lauria"],
    ["timeStamp": "20210621T071042Z"], // if there is no timestamp, current time would be used
    ["context": "2_2"], // context and addInfo would be saved in TM in the same field
    ["addInfo": "2_2"], 
    ["type": "Manual"], // could be GlobalMemory, GlobalMemoryStar, MachineTranslation, Manual, by default Undefined         
    ["markupTable": "OTMXUXLF"], //if there is no markup, default OTMXUXLF would be used. 
								 //Markup tables should be located inside ~/.t5memory/TABLE/%markup$.TBL
    ["loggingThreshold": 0],
(in 0.7)  ["newEntryIfOnlyContextAndTimestampDifferent"]: 0,
(in 0.7)["saveDifferentTargetsForSameSource"]: 0, 
	["save2disk": 0]   // flag if we need to flush tm to disk after update. by default is true
}


both newEntryIfOnlyContextAndTimestampDifferent and saveDifferentTargetsForSameSource impact how similar translation would be saved. In case if source and other field(doc, author, lang) are the same, but target different, and newEntryIfOnlyContextAndTimestampDifferent is set to true, you would have 2 translation, otherwise you would have 1 translation saved - with higher updateTime. but if you have different attributes, like document, you would have 2 translation anyway. 
newEntryIfOnlyContextAndTimestampDifferent would also increase number of saved segments. If values was not provided, defaule values from flags would be used.
here are data struct used for search, so you can see max numbers of symbols
typedef struct _LOOKUPINMEMORYDATA
{
  char szMemory[260];
  wchar_t szSource[2050];
  wchar_t szTarget[2050];
  char szIsoSourceLang[40];
  char szIsoTargetLang[40];
  int lSegmentNum;
  char szDocName[260];
  char szMarkup[128];
  wchar_t szContext[2050];
  wchar_t szAddInfo[2050];
  wchar_t szError[512];
  char szType[256];
  char szAuthor[80];
  char szDateTime[40];
  char szSearchMode[40]; // only for concordance search
  char szSearchPos[80]; // only for concordance search
  int iNumOfProposals;
  int iSearchTime;
  wchar_t szSearchString[2050];
} LOOKUPINMEMORYDATA, *PLOOKUPINMEMORYDATA;

Response example:success:
example_new{
  "source": "The end",
  "sourceNPRepl": "The end",
  "sourceNorm": "The end",
  "target": "The target",
  "segmentNumber": 0,
  "id": "",
  "documentName": "Te2.xlf",
  "sourceLang": "DE-DE",
  "targetLang": "EN-GB",
  "type": "Manual",
  "author": "THOMAS LAURIA",
  "timestamp": "",
  "markupTable": "OTMXUXLF",
  "context": "2_3",
  "additionalInfo": "addInfo2",
  "internalKey": "8:1"
}

example_old
{
"sourceLang": "de-DE",
"targetLang": "en-GB",
"source": "The end",
"target": "The target",
"documentName": "Translate5 Demo Text-en-de.xlf",
"segmentNumber": 222,
"markupTable": "OTMXUXLF",
"timeStamp": "20210621T071042Z",
"author": "Thomas Lauria"
}

in case if similar record exists, t5memory comparing source text, 
if it's the same, t5memory would compare docName, 
if it's the same,t5memory would compare timestamps and would leave only newer one

in case if TM is alreade reached it's limit, you would get 
{
"ReturnValue": 5034,
"ErrorMsg": ""
}or{
"ReturnValue": 5035,
"ErrorMsg": ""
}


Code Block
languagejs
titleUpdateEntry Pseudo code
collapsetrue
Update entry pseudo code:update segment/import
{
  if we have triples equal match (candidate for exact match)
  {
    UpdateTmRecord
    if(updateFailed)
      AddToTMAsNewKey
      if(added) UpdateTmIndex
  }else{
    AddToTMAsNewKey
    if(added) UpdateTmIndex
  }
}

UpdateTmRecord{
  getListOfDataKeysFromIndexRecord
  sortThemByTriplesMatchesWithProposal(first have biggest match)

  foreach key untill fStop==true{
    readTmRecord // tm record is 16kB block in file, first number in "7:1"

    //compare tm record data with data passed in the get in structure
    CompareAndModifyPutData
    if(NO_ERROR) set fStop = true;
  }
}

CompareAndModifyPutData{
  if source strings are equal
    Delete old entry - with TMLoopAndDelTargetClb
  if fNewerTargetExists -> fStop = TRUE
  Loop thru target records
    loop over all target CLBs or until fStop
      if segment+file id found (exact-exact-found!)
        update time field in control block
        set fUpdate= fStop=TRUE
        update context info
      if not fStop
        goto next CLB
    endloop
    if no matching CLB has been found (if not fStop)
      add new CLB (ids, context, timestamp etc. )
    endloop
  endloop

  if fupdated, update TM record
  if !fStop (all target record have been tried & none matches )
    add new target record to end of tm record
  else
    return source_string_error // errcode for UpdateTmRecord to go to the next TM record in prepared list
}

TMLoopAndDelTargetClb{
  loop through all target records in tm record checking
    loop over all target CLBs or until fStop
      if lang + segment+file id found (exact-exact-found!)
        if entry is older
          delete it, fDel = TRUE
        else set fNewerTargetExists=TRUE(would be used in CompareAndModifyPutData)
          goon with search in next tgt CLB (control block)
      else
        goon with search in next tgt CLB (control block)
        endloop
      endif
    if not fDel
      position at next target record
  endloop
}



Configuration of service

You can configure the service in ~/.t5service/t5memory.conf

Logging

LevelMnemonicDescription
0DEVELOPcould make code work really slow, should be used only when debugging some specific places in code, like binary search in files, etc.
1DEBUG

logging values of variables. Wouldn't delete temporary files(In MEM and TMP subdirectories), like base64 encoded\decoded tmx files and archives for import\export

2INFO logging top-level functions entrances, return codes, etc. Default value.
3WARNING logging if we reached some commented or hardcoded code.  Usually commented code  here is replaced with new code, and if not, it's marked as ERROR level
4ERRORerrors, why and where something fails during parsing, search, etc
5FATAL

you shouldn't reach this code, something is really wrongOther values would be ignored.  The set level would stay the same till you change it in a new request or close the app. Logs suppose to be written into a file with date\time name under ~/.OtmMemoryService/Logs and errors/fatal are supposed to be duplicated in another log file with FATAL suffices

6TRANSACTION

 - Logs only things like begin\end of request etc.  No purpose to setup this hight

 --v is glogs flag, for t5memory it make sense to set it to 0(by default) for production or to 2 for debuging.glog have it's own log levels and flags, but we are not touching them, default is okay, but it have just INFO, WARNING, ERROR. t5memory have it's own system, that was implemeted before proxygen, so it have 6 log levels (0=develop, 1=debug, 2=info, 3=warning, 4=error, 5=fatal, 6=transaction), which would be streamed to glog streams this way:
1. develop, debug, info, transaction would be streamed to glogs INFO stream
2. warning to WARNING
3. error and fatal error to ERROR stream, but also when first error log would happen, cached info about tm name and body of request that caused error would be flushed once per request. with next errors in the same request, you should not see "...with body... etc" in the log.--v and t5log level are just two separate filters for logs. when you set --v=0, glog allow only ERROR stream to work, so if you set t5loglevel to [0,1,2,3,4] wouldn't matter. but you can set to 5 to skip regular errors and have only fatal errors. in that mode also transaction log level is downgraded to be just info log levelwhen you set --v=2 you disable glogs filter, so you would have a lot of logs and now you can set logging intensivity with --t5loglevel. also t5transaction would be the highest log level now, you you can skip all info logs(with --t5loglevel=4) and still have transaction logs(which are not warnings or errors, usually something about that request handling begin or end, if you enable that). 
Shortly: for production just leave defaults(–v=0, --t5loglevel=2(info)), for debugging you need to set --v=2 and --t5loglevel to 0,1,2. Sometimes it make sense to use 1 or 2, because for 0 t5loglevel would print a ton of logs and t5memory would be slow.


Logging could impact application speed very much, especially during import or export. In t5memory there are 2 systems of logs - one from glog library and could be set in launch as commandline parameter and one is internal to filter out logs based on their level, can be set with every request that have json body with additional ["loggingThreshold": 0]  parameter or at startup with flag. 
[loggingThreshold:"2"]
Like here 

POST http://localhost:4040/t5memory/example_tm/

{
sourceLang: “en”, // the source language is required for a new TM
name: „TM Name“,
loggingThreshold:"2"

}
This would set the logging level to INFO just before the main work of creating mem endpoint starts. DEVELOP could be used in really low level debugging, but most of the time DEBUG log is more useful, since DEVELOP would log a lot of logs.  Transaction logs have the highest level of severity but it's severity is also changes with -v parameter, so with --v=2 it would be the highest log level(this log is not used often, it's only to track something like end or start of request) but with default --v=0 it's severity is belowe WARNING

gLog part - it have it's own configuration with command line flags. you can see all possible flags for t5memory with ./t5memory --help command.
main parameter here is --v and you can set it to 2 or 0(default). 
By default it set to 0,  in that case all not-errors would be avoided in logs, except startup. 
idea of --v=1 was to have logBuffer to keep log in some stream and in case of error show previous logs for that request, but it seems not so usefull, so it was not fixed and it's not working properly
--v=2 is basicaly disables that buffering, so 
In case of error or fatalError, log would be written with info about what request caused that log to happen(but that info would be truncated to 3000 symbols, this is important for importTMX), but if there are second error with the same request, new logs would not have that requests info

Some parameters combinations:
Default - --t5loglevel=2(T5INFO), --v=0,  in this case you could see only init messages and errors only, with info about requests that caused error to happen
Change only --v=2 - t5loglevel would be set by default to 2(T5INFO), so you could see T5INFO, T5WARNING, T5ERROR, T5FATAL, T5TRANSACTION messages
Debug production --t5loglevel=1(T5DEBUG), --v=2 - should be enough to have some info about issues, a lot of logs, but not as much as with Develop
Develop --t5loglevel=0(T5DEVELOP), --v=2 - all possible logs, includes entering to some functions, some step-by-step mechanisms logs(like how t5memory is parsing and hashing strings) etc. Useful only when you can reproduce issue so you don't get lost in logs from just normal behaviour or when it's crashing etc.

It's possible to change t5loglevel with some requests, so for example for some specific update request, you can set it to some lower log level and then set it back. It would affect other threads, but since in logs you have info about thread, it could be useful tool. 

Seems like --v parameter it's not quite useful, maybe should be refactored, since with --v=0 you wouldn't get any messages with severity lower than T5ERROR, except init process. 
But gLog library could be connected to some other libs in proxygen package

Here are all glog flags:
Flags from src/logging.cc:
    -alsologtoemail (log messages go to these email addresses in addition to
      logfiles) type: string default: ""
    -alsologtostderr (log messages go to stderr in addition to logfiles)
      type: bool default: false 
    -colorlogtostderr (color messages logged to stderr (if supported by
      terminal)) type: bool default: false
    -drop_log_memory (Drop in-memory buffers of log contents. Logs can grow
      very quickly and they are rarely read before they need to be evicted from
      memory. Instead, drop them from memory as soon as they are flushed to
      disk.) type: bool default: true
    -log_backtrace_at (Emit a backtrace when logging at file:linenum.)
      type: string default: ""
    -log_dir (If specified, logfiles are written into this directory instead of
      the default logging directory.) type: string default: ""
      currently: "/root/.t5memory/LOG/"
    -log_link (Put additional links to the log files in this directory)
      type: string default: ""
    -log_prefix (Prepend the log prefix to the start of each log line)
      type: bool default: true
    -logbuflevel (Buffer log messages logged at this level or lower (-1 means
      don't buffer; 0 means buffer INFO only; ...)) type: int32 default: 0
    -logbufsecs (Buffer log messages for at most this many seconds) type: int32
      default: 30
    -logemaillevel (Email log messages logged at this level or higher (0 means
      email all; 3 means email FATAL only; ...)) type: int32 default: 999
    -logfile_mode (Log file mode/permissions.) type: int32 default: 436
    -logmailer (Mailer used to send logging email) type: string
      default: "/bin/mail"
    -logtostderr (log messages go to stderr instead of logfiles) type: bool
      default: false
    -max_log_size (approx. maximum log file size (in MB). A value of 0 will be
      silently overridden to 1.) type: int32 default: 1800
    -minloglevel (Messages logged at a lower level than this don't actually get
      logged anywhere) type: int32 default: 0
    -stderrthreshold (log messages at or above this level are copied to stderr
      in addition to logfiles.  This flag obsoletes --alsologtostderr.)
      type: int32 default: 2
    -stop_logging_if_full_disk (Stop attempting to log to disk if the disk is
      full.) type: bool default: false

 



Working directory

PathDescription
~/.t5memoryThe main directory of service. Should always be under the home directory. Consists of nested folders and t5memory.conf file(see Config file). All directories\files below are nested
LOG

lIncludes log files. It should be cleanup manualy. One session(launch of service) creates two files Log_Thu May 12 10:15:48 2022 .log and Log_Thu May 12 10:15:48 2022 .log_IMPORTANT
Last have logs reduced to level Warning and higher. 

MEMMain data directory.  All tm files is stored here. One TM should include .TMD(data file), .TMI(index file), .MEM(properties file) with the same name as TM name
TABLEServices reserved readonly folder with tagtables, languages etc.
TEMPFor temporary files that were created for mainly import\export. On low debug leved(DEVELOP, DEBUG) should be cleaned manualy
t5memory.confMain config file(see config file)


Config directory should be located in a specific place



Config file - obsolete - use commandline flags instead

fielddefaultDescription
namet5memoryname of service that we use under %service% in address
port8080

service port

timeout3600service timeout
threads1
logLevel2logLevel - > see logging
AllowedRAM_MB1500

Ram limit to operate openning\closing TM(see Openning and closing TM)

Doesn't include services RAM
in Megabytes

TriplesThreshold33

Level of pre-fuzzy search filtering based on combinations  of triples of tokens(excluding tags). Could impact fuzzy search perfomance. For higher values service is faster, but could skip some segments in result. Not always corelated with resulted fuzzyRate 

Config file should be located under ~/.t5memory/t5memory.conf

Anyway, all field has default values so the service could start without the conf file

Reading\applying configs happen only once at service start

Once service started you should be able to see setup values in logs. 
Config file example:

Code Block
languagejs
titleResponse
collapsetrue
name=t5memory
port=4040
timeout=3600
threads=1
logLevel=0
AllowedRAM_MB=200
TriplesThreshold=5                 



Conceptional information


Openning and closing TM

In first concept it was planned to implement routines to open and close a TM. While concepting we found some problemes with this approach:

  • First one is the realization: opening and closing a TM by REST would mean to update the TM Resource and set a state to open or close. This is very awkward.
  • Since in translate5 multiple tasks can be used to the same time, multiple tasks try to access one TM. Closing TMs is getting complicated to prevent race conditions in TM usage.
  • Since OpenTM2 loads the whole TM in memory, OpenTM2 must control itself which TMs are loaded or not.

This leads to the following conclusion in implementation of opening and closing of TMs:

OpenTM2 has to automatically load the requested TMs if requested. Also OpenTM2 has to close the TMs after a TM was not used for some time. That means that OpenTM2 has to track the timestamps when a TM was last requested.


Concept endpoints, not implemented

http://opentm2/translationmemory/[TM_Name]/openHandle

GET – Opens a memory for queries by OpenTM2

Note: This method is not required as memories are automatically opened when they are accessed for the first time.

http://opentm2/translationmemory/[TM_Name]/openHandle

DELETE – Closes a memory for queries by OpenTM2

Note: This method is not required as memories are automatically opened when they are accessed for the first time.


For now we open  TM in case of call to work with it. TM stays opened till the shutdown we wouldn't try to open more TM's, exceeding the RAM limit setupped in config file. 
In that case we would close TM in order of longest not used, till we would fit in limit including TM that we try to open.
 TM size is calcucated basicaly as sum .TMD and .TMI files
Ram limit doesn't include service RAM and temporary files



Multithreading

In 0.6.44 multithreading are implemented this way

  1. you can set number of sevice threads with --servicethreads 8 commandline argument. This would be a number of threads which would handle requests, but also there would be 2-3 proxygen service threads running, and also for every import and reorganize - they would be new threads created. 
  2. there are mutexes for shared resources, like filesystem and some shared files in the ram, which are not configurable
  3. there are 3 configurable recursive timed mutexes, which can be also be used as non-timed mutexes(that means that it would not have timeout, but would wait till mutex would become free). To use them that way, they need to be set to 0

    This mutexes are -
  4. requestTMMutex - mutex for whole requestTM functions, which can just find tm in tm list, or reserve a place for tm in that list(tm would be opened later). Probably could be disabled to optimize code, was implemented as first high level mutex
  5. tmListMutex - every operation with tm list, like search, adding or deleting elements is managed with that mutex. 
  6. tmMutex - most requests which have tm_name in url, except status request, would be blocking - they would occupy tm for whole execution time(after request data would be parsed and checked). The reason for that is opentm2 code, which still have too many low level chunks, which makes multithreading impossible. 
  7. by default in import or reorganize threads(non request handlers - that would have regular mutexes, but threads, which are created by that handlers, which would run after you would receive response from your reorganize or import tmx requests) would be used non-timed mutexes. So this threads would wait till tm would be free.  You can change that with commandline argument 

    UseTimedMutexesForReorganizeAndImport 1

  8. You can set default values for tmRequestLockDefaultTimeout, tmLockDefaultTimeout, tmRequestLockDefaultTimeout using commandline arguments with this names. Value would be set in ms, default value is 0, which means that timeouts would be disabled. That change would apply for all requests without body and for requests with body if other value is not provided. For import and reorganize threads by default would be used non-timed mutexes, but if it's changed with commandline argument, would be used value from corresponding request(if provided, or default if not). 
  9. saveAllTms request could be used in 2 ways - with non timed mutexes for tms, or with timed mutexes, and in case of timeout, tm would not be saved and request would skip to the next tm. In response you would see message about saved and timeouted tms. 
  10. shutdown request would internally use saveAllTms with hardcoded non-timed mutexes. But it could fail on tmListMutexTimeout when checking how many tm are in import and reorganize status
  11.  resources request would use tmListMutex and timeout, when listing tms. It case of timeout, instead of list of tms, Failed to lock tm list: (timeout_error_msg) would be returned. But that wouldn't be threated as request error.   
  12. in case of timeout fail, you would get back (and into the logs) errorcode 506 and one of the next messages(where (msg) is generated service message about location of failed lock and backtrace):

    (msg); Failed to lock tm list:(msg) /(msg); Failed to lock requestTm:(msg) / (msg); Failed to lock tm:(msg)

    like this : {
    "ReturnValue":506,
    "ErrorMsg":"Failed to acquire the lock(tmMutex:18) within the timeout(1ms)(location: requestTM:339); Failed to lock tm:(location: requestTM:342); "
    }
  13. you can see default timeout values in initMsg. every timeout value is ms value.
  14. for requests with body, you can provide value for each type of mutexes as integer with this names:

   {

       "tmMutexTimeout": 5000, 

       "tmListMutexTimeout": 4000,

       "requestTMMutexTimeout": 15000,

...

}


Mutexes and request handling details:


Can you explain more, why the tm list mutex is needed?
Should it not be enough to block a new request from accessing a TM that is already in use?


it is mutex to block access to the tm list.
It's not quite a list, internally it's a map, hash table, or dictionary- so it has a key, which is tm name, and value, which is tm data, and its auto sorting
also, that means, as with any non-fixed size array, that I can't be sure about its memory location, because during, for example, search in one thread, it could be completely reallocated to another size in the memory, if some thread would try to add some new tm object to that list. so even read operations should be blocking
so yes, I would explain about simplier mutexes first
In 0.5 I made every request handler as a class, which have the same abstract ancestor, which implements the same strategy pattern to execute any request, but each request implements it's own methods as it need it to be implemented
here is a code to run every request, but each request type implements it's own parseJSON, checkData and execute methods.
TimedMutexGuard  is a class that I implemented to make timed recursive mutexes and be flexible and that could be used as RAII, and  tmLockTimeout is a timeout class, which I implemented to have timeout fields, flag that some timeout is out of time, so in case of that, execution should be rolled back with an error code. Also, in case of an error, that class writes down a string with all functions names and a line numbers, that was calling for that mutex even if it's nested, to trace location of failed mutex.  if timeout would be set to 0, it would be used as non-timed mutex
//pseudo-code but it's close to real 


int RequestData::run(){ 
  if(!res) res = parseJSON();
  if(!res) res = checkData();
  if(!res) res = requestTM();
  if(!res)
  {
    if(isLockingRequest())
    {
        TimedMutexGuard l(mem->tmMutex, tmLockTimeout, "tmMutex") ; // mutexes used as RAII, to open the lock automatically 
        if(tmLockTimeout.failed()){
          return buildErrorReturn(506, tmLockTimeout.getErrMsg(), _rc_);  // would return "Failed to lock tm:...   

        }      
        res = execute();
      }// here mutex would be destroyed 

      else

      { // request that doesn't require lock, so it's in general without tmName in URL + status request

          res = execute();    

      }

  }  
  buildRet(res);
  //reset pointer
  if(mem != nullptr)  mem.reset(); 

  return res;

so here you can see how tmLock is used
it's blocking selected tm for execution time only


then there are requestTM function, which would, depending on request type, request writePointer, readPointer or service pointer (some requests, like list of tm's , that don't have tm in url, or status request, as exception)
requesting writePointer or readPointer would check tm list and return tm if tm is found
else, it would init tm with found tm name and add that to tmList(it used to open tm also, but now it's separated)
service pointer wouldn't add nor open tm if it's not in list, but, can also return tm, if it's in list, without blocking it, for status request)
and that requestTM function have it's own mutex. it was implemented first, so only one tm could be requested at a time. Maybe with that tmList mutexes that I would explain later here, we don't need that requestTM function, but anyway... requestTM could also try to free some space, closing tms in the list, so at least that probably should be managed and synced.
so we would see if it's needed


and TmList mutex
so, every access to tm list block that mutex
few versions ago I had simple mutexes, but then it became recursive mutexes to simplify code and make it safer.
Then I had 2 versions of functions, safe, with mutex, and unsafe, that was used in functions that had that mutex locked on higher level
for example, I had this most primitive function


bool TMManager::IsMemoryInList(strMemName, tmListTimeout)

{  

  TimedMutexGuard l {mutex_access_tms, tmListTimeout, "tmListMutex"};// lock tms list
  if(tmListTimeout.failed()){     

    returnError(".Failed to lock tm list:");    

    return false;  

} // if TM mutex is failed in some nested function, tmListTimeout would be marked as spoiled so every other mutex that would use that timeout would be failed after first fail. in execution boolean function would return false, but also check if mutex was spoiled is needed to find out if function returned false because it didn't find (in this case) tm in list, or because its timeout is spoiled. But all checks is placed in code for now. 


  return TMManager::find(strMemName); // if lock was successful - try to find mem
}


 so it's boolean, but making it return some custom data type would make it harder to use
it's used everywhere, for example, to check if memory is loaded, but without having pointer, we have this function

bool TMManager::IsMemoryFailedToLoad(strMemName, tmListTimeout){  
 TimedMutexGuard l{mutex_access_tms, tmListTimeout, "tmListMutex"};
    bool res = false;
    if(tmListTimeout.failed())

   {      

      tmListTimeout.addToErrMsg(".Failed to lock tm list:");    

      return false;  

   }

   if(IsMemoryInList(strMemName, tmListTimeout)  
    && tms[strMemName]->isFailedToLoad())

        {    

                 res = true;    

        }

  if(tmListTimeout.failed())

  {     // if timeout was spoiled,  errMessage would be extended with new, so you would have backtrace with functions and lines in the file in the outputting message  

     tmListTimeout.addToErrMsg(".Failed to lock tm list:");

     return false;  

}

  return res;
}
 which also have blocking IsMemoryInList  but also blocks the same mutex because it's also working with tm list directly, and theoretically some other thread could change tm list between these lines

IsMemoryInList(strMemName, tmListTimeout)  // memory was checked to be present in the list
    && tms[strMemName]→isFailedToLoad()) // slow change that list was changed from the line above in another thread

so that type of boolean functions in case of timeout would return false, and then places where they were called should check if they returned false and timeout is not exiped. In case if it expired once, it would fail every next mutex lock, so even if that check is missing, that should now be handled in a right way. every addToErrMsg  sets that tmMutex is expired and adds some comment, function name and line number, so they could be tracked.

and tm list could be used not only when just requesting tm, but, for example, for resource request, or to free some space, or flush tm during shutdown.
so access to tm list should be managed and synced
that 2 classes TimedMutexGuard and MutexTimeout(tmListTimeout- is an object of that class) makes it more time consuming to implement that mutexes, because of that requiremnt
They should provide RAII possibility, be recursive and timed, but also supports non-timed mode, collect info in case if timeout failed, and also secure that in case if one timeout would fail, next mutexes would fail automatically, collect, provide and log data, where it failed

Regarding
and that requestTM function have it's own mutex. it was implemented first, so only one tm could be requested at a time
so I understand it right, as long as one TM is requested for opening (not for reading) another one can not be opened at the same time? That does not make sense to me. Why that? That means, a request for a small TM has to wait, until a large TM has been loaded into the RAM.

Load call is outside of mutex_requestTM mutex, so it wouldn't be blocked in current version.  Openning(loading) of the tm files is happening outside of the
mutex_requestTM  area.


So an active mutex to the tm list would still block every request then, also to other TMs, rigth? How long will it be blocked for example, if I import a new TMX into one TM? The whole time the TMX is imported? And same question for update?
And what about read requests? I understood, they will also block the TM list, right? But why I do not understand.

no, it wouldn't be blocked whole time for another tm's
it would be blocked for the time to check if tm is in list, and then if not, it would add it to the list. TMListLock is used only to prevent rearranging list when accessing its data. 
during that it would calculate how much space is needed and if we have enough space free
in not enough, it would delete tms from the list starting from the one that was not in use for the longest time
but if that tm is blocked, that wouldn't prevent for deletion from the list, because there are smart pointers used there, so tm would be closed when last pointer would be freed.
but it could take longer if there are some processes which uses tm list

blocking tm is necessary because of big chunk of low-level code which exists in opentm2 which operated with pointers to the memory (RAM) for a long time
I implemented read pointer and write pointer to be able in future have multiple read pointers and only single write pointer at the time, but with old code of opentm2 we need to treat every request as write request, because it can lead to memory reallocations that leads to crashes
For example if you have 2 projects translating with the same tm assigned, they would send both requests to the same tm, and that leads to crashes. that was actually one of the things that I fixed in recent versions
so tm should be blocked at least till some legacy code would be removed. like lookup tables.


t5memory Tag Handling Reference

Note: This document was written from the verified C++ source code (OTMTMXIE.cpp, tm.h). Where the earlier old_doc.md was incorrect those discrepancies are listed in §10.

...

Contents

  1. Introduction
  2. Supported Tag Types
  3. Normalisation Fundamentals
  4. Import — Source Segment
  5. Import — Target Segment
  6. Fuzzy Search — Request Normalisation
  7. Fuzzy Search — Response Construction
  8. Number Protection Tags (t5:n)
  9. Framing / Inclosing Tags
  10. Discrepancies from old_doc.md

...

1. Introduction

t5memory stores translation units in its own TMX-based binary format. Its REST API, however, accepts segments containing both TMX inline tags (<bpt>, <ept>, <ph>, …) and XLIFF 1.2 inline tags (<g>, <x/>, <bx/>, <hi>, …) interchangeably.

To enable consistent fuzzy matching regardless of which tag format a client uses, t5memory applies a two-step tag-handling pipeline:

  1. Normalisation — on every import or update call, all incoming tags are collapsed into three canonical forms (<ph>, <bpt>, <ept>) with freshly assigned numeric IDs. The original tag type and its attributes are preserved in an in-memory TagInfo record alongside the generated values.

  2. Restoration — on every fuzzy search response, the canonical tags stored in the TM are matched back to the tags from the lookup request and replaced with the client's original tag types and attribute values before the response is sent.

This means a segment stored from a TMX import and later retrieved by an XLIFF fuzzy request will have its tags returned in XLIFF format, exactly as provided in the request.

...

2. Supported Tag Types

Original tag(s)XML form expectedClassification
ph, x, it, utSelf-closingStandalone
bpt, bxSelf-closing or with contentOpening pair
g, hi, subOpening, with contentOpening pair
ept, exSelf-closingClosing pair
</g>, </hi>, </sub>Closing elementClosing pair
t5:nSelf-closingNumber protection (special)
Any tag with type="lb"AnyLine break (special)

type="lb" tags are replaced with a literal newline character (\n) and are not stored as a tag at all.

t5:n tags follow entirely separate rules; see §8.

...

3. Normalisation Fundamentals

Every tag except t5:n and lb-type tags is normalised to one of three canonical forms:

Normalised formRepresents
<ph x="N"/>Any standalone tag
<bpt x="N" i="M"/>Any opening pair tag
<ept i="M"/>Any closing pair tag

3.1 Attribute synonyms

When reading incoming tags t5memory treats the following attributes as equivalent:

Attribute roleAccepted attribute names
Tag IDx or id
Pair indexi or rid

Whichever name is present in the incoming tag is read and saved internally as original_x (for ID) and original_i (for pair index).

3.2 ID counter starting values

The counters are reset at the start of each import segment pair (source + target share one counter set):

CounterInitial valueRole
iHighestPHId100Assigned to standalone (ph) tags; first tag gets 101
iHighestPTId500Assigned to pair tags (bpt/ept); first tag gets 501
iHighestPTI0Pair index assigned to opening pair tags; first gets 1

The two separate starting values (100, 500) keep standalone and pair tag IDs in non-overlapping ranges, making it easy to tell them apart in stored data.

3.3 BPT inner content is dropped

In TMX, a <bpt> element may contain the raw code it marks (e.g. <bpt i="1">**</bpt>). t5memory discards this content and stores the tag self-closing. The content is considered irrelevant for matching.

3.4 EPT x is stored as a negative number

Internally an EPT's generated_x is set to -matchingBPT.generated_x. Since t5memory only prints attributes with positive values, the x attribute never appears on a stored <ept> tag — only i is emitted:

<bpt x="501" i="1"/>   ← positive x, printed
<ept i="1"/>           ← x = -501 internally, not printed

An EPT that has no matching BPT also gets a unique negative value (-(++iHighestPTId)), keeping the pair-index consistent.

3.5 Attribute names differ between storage and response

ContextID attributePair-index attribute
TM storage (import / update)xi
Fuzzy search responseidrid

...

4. Import — Source Segment

The source is always parsed first. Every incoming tag gets a freshly generated ID from the counters described in §3.2.

4.1 Standalone tags → <ph>

  1. Save original_tagType and original_x from the incoming id/x attribute (if present).
  2. Set generated_tagType = PH.
  3. Assign generated_x = ++iHighestPHId.
  4. Store as <ph x="N"/>.

4.2 Opening pair tags → <bpt>

  1. Save original_tagType, original_x, original_i.
  2. Set generated_tagType = BPT.
  3. Mark fPairedTagClosed = false.
  4. Assign generated_i = ++iHighestPTI, generated_x = ++iHighestPTId.
  5. Save to SOURCE_TAGS.
  6. Store as <bpt x="N" i="M"/>.

4.3 Closing pair tags → <ept>

  1. Save original_tagType, original_x, original_i.
  2. Set generated_tagType = EPT.
  3. Search SOURCE_TAGS in reverse order for an unclosed BPT tag where all of the following are true:
    • fPairedTagClosed == false
    • original_tagType matches the expected partner type:
      • eptbpt, exbx, </g><g>, </hi><hi>, </sub><sub>
    • original_i == this tag's original_i
      (for tags without an i attribute, like </g>, both are 0; reverse search ensures the innermost open tag is matched first)
  4. Match found: set generated_i = matchingBPT.generated_i, generated_x = -matchingBPT.generated_x. Mark both as fPairedTagClosed = true.
  5. No match found (tag appears before its opening partner, or partner is missing): assign generated_i = ++iHighestPTI, generated_x = -(++iHighestPTId).
  6. Store as <ept i="M"/> (x is negative, never printed).

4.4 Worked example — source import

Input segment (XLIFF 1.2 tags):

Select the <hi>net<x id="5"/>work <bx id="10" rid="1"/>BLK360<ex rid="1"/> tag </hi>

Processing (counters start: PHId=100, PTId=500, PTI=0):

#Incoming taggenerated_tagTypegenerated_xgenerated_iStored as
1<hi>BPT5011<bpt x="501" i="1"/>
2<x id="5"/>PH101<ph x="101"/>
3<bx id="10" rid="1"/>BPT5022<bpt x="502" i="2"/>
4<ex rid="1"/>EPT−5022<ept i="2"/>
5</hi>EPT−5011<ept i="1"/>

Stored source segment:

Select the <bpt x="501" i="1"/>net<ph x="101"/>work <bpt x="502" i="2"/>BLK360<ept i="2"/> tag <ept i="1"/>

...

5. Import — Target Segment

After the source segment is fully parsed and stored in SOURCE_TAGS, the target is parsed. The goal is for corresponding source and target tags to share the same generated IDs — this is what allows t5memory to score fuzzy matches consistently.

For each tag in the target:

  1. Save original_tagType, original_x, original_i.
  2. Determine generated_tagType (same classification rules as §4).
  3. Search SOURCE_TAGS (forward) for an unused source tag where:
    • fTagAlreadyUsedInTarget == false
    • original_tagType == this tag's original_tagType
    • For EPT-typed tags: original_i == this tag's original_i
    • For all other tags: original_x == this tag's original_x
  4. Match found: copy generated_x and generated_i from the source tag. Mark the source tag as fTagAlreadyUsedInTarget = true.
  5. No match found (tag present only in target): generate new IDs using the same rules as §4 (standalone → ++iHighestPHId, pair → ++iHighestPTI / ±++iHighestPTId).

5.1 Worked example — target import

Input segment (same structure as the source example):

Selezionare la <hi>rete<x id="5"/> <bx id="10" rid="1"/>BLK360<ex rid="1"/> tag </hi>

Each target tag matches its source counterpart by original_tagType + original_x / original_i, so all inherit the same generated IDs:

Stored target segment:

Selezionare la <bpt x="501" i="1"/>rete<ph x="101"/> <bpt x="502" i="2"/>BLK360<ept i="2"/> tag <ept i="1"/>

...

6. Fuzzy Search — Request Normalisation

When a fuzzy search request arrives, the query segment is normalised with exactly the same rules as a source segment (§4). The counters start at the same initial values (PHId=100, PTId=500, PTI=0).

Two additional high-water marks are tracked during this normalisation:

VariableTracks
iHighestRequestsOriginalIdHighest original_x seen across all request tags
iHighestRequestsOriginalIHighest original_i seen across all request tags

These are used as a seed for auto-generating new IDs when a stored tag has no counterpart in the request (§7).

The normalised request string is not returned to the client. It exists only to build REQUEST_TAGS — the lookup table used during response construction.

6.1 Worked example — request normalisation

Incoming fuzzy query (XLIFF 1.2 tags):

Select the <g id="1">net<x id="2"/>work <g id="3">BLK360</g> tag </g>

Normalisation result:

#Incoming taggenerated_tagTypegenerated_xgenerated_ioriginal_tagTypeoriginal_xoriginal_i
1<g id="1">BPT5011G1
2<x id="2"/>PH101X2
3<g id="3">BPT5022G3
4</g> (inner)EPT−5022G
5</g> (outer)EPT−5011G

iHighestRequestsOriginalId = 3, iHighestRequestsOriginalI = 2

...

7. Fuzzy Search — Response Construction

After a fuzzy match is found in the TM, each stored tag in the matched source and target segments must be converted back to the client's original tag format before the response is sent.

For each stored tag (from source or target):

  1. Search REQUEST_TAGS (forward) for a tag where:
    • generated_tagType == stored tag's generated_tagType
    • generated_x == stored tag's generated_x
  2. Match found:
    • Use original_tagType from the request tag.
    • If the stored tag is EPT but the original request tag was neither ept nor ex (i.e. it was a </g>, </hi>, or </sub>), emit a closing element: </tagType>.
    • Otherwise emit <tagType id="N" rid="M"/> using original_x as id and original_i as rid. Attributes are only emitted when their value is greater than 0.
  3. No match found (stored tag has no counterpart in the request):
    • Auto-generate: id = ++iHighestRequestsOriginalId.
    • For pair tags: rid = ++iHighestRequestsOriginalI.
    • Emit an XLIFF-style tag with these new IDs.

Key point: the response always uses id/rid attribute names, never x/i. The tag type names are taken from the original request tags, not from their normalised forms.

7.1 Worked example — full round-trip

Stored TM segments (from §4–5 examples):

source: Select the <bpt x="501" i="1"/>net<ph x="101"/>work <bpt x="502" i="2"/>BLK360<ept i="2"/> tag <ept i="1"/>
target: Selezionare la <bpt x="501" i="1"/>rete<ph x="101"/> <bpt x="502" i="2"/>BLK360<ept i="2"/> tag <ept i="1"/>

Request (from §6 example):

Select the <g id="1">net<x id="2"/>work <g id="3">BLK360</g> tag </g>

REQUEST_TAGS after normalisation:

generated_tagTypegenerated_xoriginal_tagTypeoriginal_xoriginal_i
BPT501G1
PH101X2
BPT502G3
EPT−502G (closing)
EPT−501G (closing)

Tag-by-tag replacement of stored source:

Stored tagMatches REQUEST entryOutput
<bpt x="501" i="1"/>BPT, generated_x=501 → G, original_x=1<g id="1">
<ph x="101"/>PH, generated_x=101 → X, original_x=2<x id="2"/>
<bpt x="502" i="2"/>BPT, generated_x=502 → G, original_x=3<g id="3">
<ept i="2"/> (stored x=−502)EPT, generated_x=−502 → G (closing)</g>
<ept i="1"/> (stored x=−501)EPT, generated_x=−501 → G (closing)</g>

The same replacement is applied identically to the target segment.

Response JSON:

{
  "source": "Select the <g id=\"1\">net<x id=\"2\"/>work <g id=\"3\">BLK360</g> tag </g>",
  "target": "Selezionare la <g id=\"1\">rete<x id=\"2\"/> <g id=\"3\">BLK360</g> tag </g>"
}

...

8. Number Protection Tags (t5:n)

Number protection tags mark numeric tokens (dates, quantities, codes) so that two segments containing the same pattern — but different actual numbers — still score a high fuzzy match.

8.1 Attributes

AttributeRole
idNumeric tag identifier (stored in original_x)
rRule hash key — identifies which number pattern matched (e.g. "date-dmy-long")
nThe actual number text in the document (e.g. "25th of 2043")

8.2 Storage

NP tags are stored verbatim — the full original id, r, and n attributes are preserved exactly as received. No ID regeneration takes place.

Stored form:

<t5:n id="5" r="date-dmy-long" n="25th of 2043"/>

8.3 Effect on fuzzy scoring

During fuzzy distance calculation, NP tags are replaced by their r value (the rule hash). This has two effects:

  • Two segments with the same rule hash but different n values count as identical words in the fuzzy word comparison.
  • The NP tag contributes exactly one word to the total word count.
  • All other (non-NP) inline tags are removed entirely from the scoring string before counting.

Example:

Segment stored in TM:

Press the <t5:n id="5" r="date-rule" n="2nd of 1999"/>, power button to turn on the <bpt x="501" i="1"/>light<ept i="1"/>

Scoring string (NP replaced by r-key, other tags stripped):

Press the date-rule, power button to turn on the light

Fuzzy request:

Press the <t5:n id="5" r="date-rule" n="25th of 2043"/>, power button to turn on <bpt x="501" i="1"/>light<ept i="1"/>

Scoring string of request:

Press the date-rule, power button to turn on light

Matching words: 12 out of 13 total → fuzzy score ≈ 92% [(13−1) / 13]

8.4 Response

NP tags are returned as-is with their original id, r, and n attribute values. They do not affect ID generation for other tags in the same segment.

...

9. Framing / Inclosing Tags

A "framing" tag is a pair of opening/closing tags that wrap the entire segment text. Stripping these can improve fuzzy matching because a segment whose only structural difference from a stored one is a surrounding wrapper will otherwise score lower than it should.

The behaviour is controlled by the framingTags field in the import or update request body:

ValueBehaviour
saveAll (default)No stripping. All tags are kept as-is.
skipAllThe outermost framing BPT/EPT pairs at the start and end of the segment are removed.
skipPairedSame as skipAll, but only applies to paired tags (BPT+EPT), not standalone <ph> tags.

9.1 How stripping works

After the source segment is fully normalised the following is applied when skipAll or skipPaired is set:

  1. Ignore leading whitespace in the stored segment string.
  2. If the string begins with the printed form of the first tag in SOURCE_TAGS, remove that tag and its preceding whitespace, mark it as deleted (fDeleted = true), and advance to the next tag. Repeat.
  3. Similarly, ignore trailing whitespace and check whether the string ends with the last tag in SOURCE_TAGS. If so, remove it, mark deleted, and move inward. Repeat.
  4. Deleted tags return an empty string from PrintTag and are invisible in TM storage and fuzzy scoring.

Example:

Input source:

<bpt x="501" i="1"/>Press the button<ept i="1"/>

After skipAll stripping:

Press the button

The <bpt>/<ept> pair is stripped from storage entirely, and fuzzy matching treats the segment as plain text.

...

10. Discrepancies from old_doc.md

Claim in old_doc.mdActual behaviour (source code)
ID counters start at 0iHighestPHId starts at 100 (ph tags begin at 101); iHighestPTId starts at 500 (pair tags begin at 501).
EPT generated_id is a positive valueEPT's generated_x is always negative (-matchingBPT.generated_x). Unmatched EPTs also receive a unique negative value. Since only values > 0 are printed, x never appears on a stored <ept>.
Output always uses x/i attributesImport and TM storage use x/i. Fuzzy search responses use id/rid (XLIFF-style).
Struct fields named generated_id / original_idActual field names in TagInfo are generated_x / original_x.
BPT matching looks forward for EPTThe reverse search also handles EPT appearing before its BPT (out-of-order XML). Normal order (BPT first) is the common case; the code handles both.
iHighestRequestsOriginalId seeds from 0It is seeded by the actual highest original_x observed while parsing the request segment, ensuring auto-generated fallback IDs never collide with IDs already present in the request.


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Configuring and debugging t5memory 


Updated for v0.6.75
To configure t5memory use it's commandline flags. To list all the flags start t5memory with help flag: ./t5memory --help. All flags related to t5memory and not libraries is under otmd.cpp section. Also you can send get request to t5memory_service/flags - it would print all the flags with description and current and default values. Here are those flags

  Flags from /home/or/workspace/translate5/translate5-tm-service-source/source/otmd.cpp:
    -add_premade_socket (if set to true, socket instance would be created
      outside of proxygen and then binded, that made possible to add tcp backog
      event handler and use socket_backog option) type: bool default: false
      currently: true
    -allowLoadingMultipleTmsSimultaneously (If set to true, multiple tms could
      be loaded from the disk at the same time. ) type: bool default: false
    -allowedram (Sets amought RAM(in MB) allowed for service to use)
      type: int64 default: 5000
    -allowedtmdsize (Sets max size of tmd file(in MB) after which t5m would not
      allow to add new data to the tm) type: int64 default: 500
    -debug_sleep_in_request_run (If set, provide artificial delay in every
      request handling execution equal to provided num of microseconds)
      type: int64 default: 0 currently: 10000000
    -disable_aslr (If set to true, process personality would be set to
      ADDR_NO_RANDOMIZE) type: bool default: false currently: true
    -enable_newlines_in_logs ((not working)if set to true, would keep newline
      symbols in the logs, otherwise(by default) newlines would be removed and
      logs would be oneliners) type: bool default: false
    -flush_tm_at_shutdown (If set to true, flushes tm when shutting down the
      app not using shutdown request) type: bool default: false
    -flush_tm_to_disk_with_every_update (If set to true, flushes tm to disk
      with every successfull update request) type: bool default: false
    -forbiddeletefiles (Set to true to keep all files(including temporary and
      tm)) type: bool default: false
    -http_listen_backlog (Sets http options listen backog) type: int64
      default: 128 currently: 32
    -ignore_newer_target_exists_check (if set to true, check for newer already
      saved target would be skipped for saving segments) type: bool
      default: true
    -keep_tm_backups (if set to true, when saving tmd and tmi files, old copies
      would be saved with .old suffix) type: bool default: true
    -limit_num_of_active_requests (If set to true, it would be possible to
      handle only up to servicethreads-1 requests at the same time, the last
      thread would respond with 503 to eliminate creating queue of requests
      waiting to be handled.) type: bool default: false
    -logMutexes (if set to true you would see mutex logs) type: bool
      default: false
    -log_every_request_end (Sets log for every request end  with it's url,
      method etc...) type: bool default: false
    -log_every_request_start (Sets log for every request call with it's url,
      method etc...) type: bool default: false
    -log_memmove_in_compareputdata (if set to true, when saving segment and
      causing memmove in compareputdata functions, just before memmove, data
      would be logged - use this to debug btree crashes.) type: bool
      default: false
    -log_tcp_backog_events (if set to true, tcp backlog events would be
      logged(to enable, add_premade_socket flag should be set to true))
      type: bool default: false currently: true
    -port (What port to listen on) type: int32 default: 4080
    -servicename (Sets service name to use in url) type: string
      default: "t5memory"
    -servicethreads (Sets amought of worker threads for service) type: int32
      default: 5
    -socket_backlog (Sets proxygen socket listen backog(disabled, to enable set
      add_premade_socket=true)) type: int64 default: 1024 currently: 32
    -t5_ip (Which ip to use in t5memory(default is any). Should be in format
      '1.1.1.1', default is to listen to all available ip) type: string
      default: ""
    -t5loglevel (Sets t5memory log level threshold from DEVELOP(0) to
      TRANSACTION(6)) type: int32 default: 2 currently: 3
    -timeout (Sets timeout for service request handling) type: int32
      default: 180000
    -tmListLockDefaultTimeout (Sets tm mutex lock timeout(in ms) for TM
      list(which is used to open and close tms, and hold list of opened tms),
      after which operation would be canceled and mutex would return an error,
      if set to 0, mutex lock would be waited without timeout) type: int64
      default: 3000
    -tmLockDefaultTimeout (Sets tm mutex lock timeout(in ms) for TM after which
      operation would be canceled and mutex would return an error, if set to 0,
      mutex lock would be waited without timeout) type: int64 default: 3000
    -tmRequestLockDefaultTimeout (Sets tm mutex lock timeout(in ms) for part
      where request is requesting tm(which is used to open and close tms, and
      hold list of opened tms), after which operation would be canceled and
      mutex would return an error, if set to 0, mutex lock would be waited
      without timeout) type: int64 default: 3000
    -triplesthreshold (Sets threshold to pre fuzzy filtering based on hashes of
      neibour tokens) type: int32 default: 5
    -useTimedMutexesForReorganizeAndImport (If set to true, in reorganize or
      import thread would be used mutexes with timeouts, and reorganizee or
      import could be canceled, false(by default) - would be used non timed
      mutexes) type: bool default: false
    -wait_for_import_and_reorganize_requests (If set to true, waiting for all
      import and reorganize processes to be done at shutdown when not using
      shutdown request) type: bool default: true

Hints:

  • -debug_sleep_in_request_run would add delay to every requests
  • In theory you can restore tm with only tmd file using reorganize, but in case if there would be some issue during reorganize, tm in ram would be in instable state, so keep original tmd backed up anyway
  • you can set filter for requests using --t5loglevel from 0 to 6. --v could be set to 0 or 2. if set to 0 only errors would be logged and transaction log level would be mapped to info. 
  • keep_tm_backups in case of flushing to the disk, older version would be kept with .old suffix, enabled by default
  • triplesthreshold have big impact on fuzzy search speed, but if you set it to too big value, some good matches could be filtered out. in old opentm2 value was, I think, 33. 
  • to test tcp backlog you can set --add_premade_socket=1 --t5loglevel=4 --v=2 --debug_sleep_in_request_run=10000000 --log_tcp_backog_events=true --log_every_request_end=1 --log_every_request_start=1 --http_listen_backlog=4 --socket_backlog=2


Testing TCP backlog options

related toissue T5TMS-281

most up-to-date version for this ticket is 0.6.75, where there are new flags and functionality to manipulate tcp stack.
--http_listen_backlog, default was 1024, in 0.6.75 it's 128, suppose to set tcp backlog for proxygen server, but seems like in reality it's just a hint, because requests over that limit is not dropping, except of timeout

--add_premade_socket - this is used to create socket and bind it to proxygen server instead of just providing ip address tot the server to open socket inside, should be set to true to enable, log_tcp_backog_events and socket_backlog flags

--log_tcp_backog_events  if set to true allow to test tcp backog, for that also recomended to set  --v=2 --t5loglevel=4. Require  add_premade_socket  to be set to true. You would see then in logs behaviour of tcp backlog

--socket_backlog is simillar to http_listen_backlog, but for socket.  But this require add_premade_socket to be set to true

--limit_num_of_active_requests, this would limit num of requests that could be handled at the same time in a way, when only n-1 of n created worker threads could be executed at the same time. last one would send 503 error and message that service is busy. I think that it make sense to play with num of worker threads and measure performance, for example try service with 32 threads on 8 cores. in that case service would handle properly 31 thread but 32nd would be responded with error.

--debug_sleep_in_request_run just make sleep  n microseconds(1/1000000 s) in every requests  to artificially slow them down. 

to test behaviour of tcp backlog you can use attached python script via command: 
python(3) sendNrequests4.py -n 40 
this would send 40 request on default local t5memory address
feel free to edit script if needed
flags for

to test tcp backlog you can set --add_premade_socket=1 --t5loglevel=4 --v=2 --debug_sleep_in_request_run=10000000 --log_tcp_backog_events=true --log_every_request_end=1 --log_every_request_start=1 --http_listen_backlog=4 --socket_backlog=2

and other flags as you wish

This would make every request at least 10 sec longer, every tcp backlog action would be logged, and also start and end of request handler execution, proxygens http tcp backog would be set to 4(or set it to some other value), and sockets backlog to 2
add_premade_socket is required to set sockets backlog and also tcp backlogs event logs.


other approach is to set docker containers environment, but seems like it's also just a hint and could be ignored by os
in docker-compose.yaml:
  myt5m:
    image: translate5/t5memory:0.6.75
    sysctls:
      net.core.somaxconn: 1
      net.ipv4.tcp_max_syn_backlog: 1
      net.ipv4.tcp_abort_on_overflow: 1
    ports:
      - '4086:4086'


Code Block
languagepy
titlesendNRequests.py
collapsetrue
import asyncio
import aiohttp
import argparse
import time
import traceback

async def fetch(session, url, request_id):
    try:
        async with session.get(url, timeout=60) as response:
            text = await response.text()
            if response.status != 200:
                print(f"Request {request_id}: Error with status {response.status}. Response:")
                print(text)
            else:
                print(f"Request {request_id}: Success with status {response.status}")
            return response.status, text
    except Exception as e:
        print(f"Request {request_id}: Exception occurred: {e}")
        traceback.print_exc()  # Print the full traceback for the exception
        return e  # Return the exception for further handling

async def main(num_requests, url, delay):
    async with aiohttp.ClientSession() as session:
        tasks = []
        for i in range(num_requests):
            tasks.append(asyncio.create_task(fetch(session, url, i)))
            if delay > 0:
                await asyncio.sleep(delay)
        results = await asyncio.gather(*tasks, return_exceptions=True)

    success_count = 0
    failure_count = 0
    for idx, result in enumerate(results):
        if isinstance(result, Exception):
            failure_count += 1
            print(f"Request {idx} raised an exception: {result}")
        else:
            status, text = result
            if status is None or status != 200:
                failure_count += 1
                print(f"Request {idx}: Failed. Status: {status}. Response: {text}")
            else:
                success_count += 1

    print(f"\nTotal successes: {success_count}")
    print(f"Total failures: {failure_count}")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Send multiple HTTP GET requests concurrently with an optional delay between requests"
    )
    parser.add_argument("-n", "--num_requests", type=int, default=200,
                        help="Number of parallel requests to send (default: 200)")
    parser.add_argument("-u", "--url", type=str, default="http://127.0.0.1:4080/t5memory",
                        help="URL to send requests to (default: http://127.0.0.1:4080/t5memory)")
    parser.add_argument("-d", "--delay", type=float, default=0.1,
                        help="Delay in seconds between starting each request (default: 0.1)")
    args = parser.parse_args()
    
    asyncio.run(main(args.num_requests, args.url, args.delay))


...

Logging

LevelMnemonicDescription0DEVELOPcould make code work really slow, should be used only when debugging some specific places in code, like binary search in files, etc.1DEBUG

logging values of variables. Wouldn't delete temporary files(In MEM and TMP subdirectories), like base64 encoded\decoded tmx files and archives for import\export

2INFO logging top-level functions entrances, return codes, etc. Default value.3WARNING logging if we reached some commented or hardcoded code.  Usually commented code  here is replaced with new code, and if not, it's marked as ERROR level4ERRORerrors, why and where something fails during parsing, search, etc5FATAL

you shouldn't reach this code, something is really wrongOther values would be ignored.  The set level would stay the same till you change it in a new request or close the app. Logs suppose to be written into a file with date\time name under ~/.OtmMemoryService/Logs and errors/fatal are supposed to be duplicated in another log file with FATAL suffices

6TRANSACTION

 - Logs only things like begin\end of request etc.  No purpose to setup this hight

Logging could impact application speed very much, especially during import or export. 
You can setup the logging level from the config file or in any POST JSON request by attaching a parameter to a JSON object 
[loggingThreshold:"2"]

Like here 

POST http://localhost:4040/t5memory/example_tm/

{
sourceLang: “en”, // the source language is required for a new TM
name: „TM Name“,
loggingThreshold:"2"

}
This would set the logging level to INFO just before the main work of creating mem endpoint starts

Or in t5memory.conf file in line
logLevel=0 
Would set the log level to DEVELOP, this would be applied only after restarting of service
Working directoryPathDescription~/.t5memoryThe main directory of service. Should always be under the home directory. Consists of nested folders and t5memory.conf file(see Config file). All directories\files below are nestedLOG

lIncludes log files. It should be cleanup manualy. One session(launch of service) creates two files Log_Thu May 12 10:15:48 2022 .log and Log_Thu May 12 10:15:48 2022 .log_IMPORTANT
Last have logs reduced to level Warning and higher. 

MEMMain data directory.  All tm files is stored here. One TM should include .TMD(data file), .TMI(index file), .MEM(properties file) with the same name as TM nameTABLEServices reserved readonly folder with tagtables, languages etc.TEMPFor temporary files that were created for mainly import\export. On low debug leved(DEVELOP, DEBUG) should be cleaned manualyt5memory.confMain config file(see config file)Config directory should be located in a specific place

Config file

fielddefaultDescriptionnamet5memoryname of service that we use under %service% in addressport8080

service port

timeout3600service timeoutthreads1logLevel2logLevel - > see loggingAllowedRAM_MB1500

Ram limit to operate openning\closing TM(see Openning and closing TM)

Doesn't include services RAM
in Megabytes

TriplesThreshold33

Level of pre-fuzzy search filtering based on combinations  of triples of tokens(excluding tags). Could impact fuzzy search perfomance. For higher values service is faster, but could skip some segments in result. Not always corelated with resulted fuzzyRate 

Config file should be located under ~/.t5memory/t5memory.conf

Anyway, all field has default values so the service could start without the conf file

Reading\applying configs happen only once at service start

Once service started you should be able to see setup values in logs. 
Config file example:

Code Block
languagejs
titleResponse
collapsetrue
name=t5memory
port=4040
timeout=3600
threads=1
logLevel=0
AllowedRAM_MB=200
TriplesThreshold=5                 

Openning and closing TM

Opening and closing a TM

In first concept it was planned to implement routines to open and close a TM. While concepting we found some problemes with this approach:

  • First one is the realization: opening and closing a TM by REST would mean to update the TM Resource and set a state to open or close. This is very awkward.
  • Since in translate5 multiple tasks can be used to the same time, multiple tasks try to access one TM. Closing TMs is getting complicated to prevent race conditions in TM usage.
  • Since OpenTM2 loads the whole TM in memory, OpenTM2 must control itself which TMs are loaded or not.

This leads to the following conclusion in implementation of opening and closing of TMs:

OpenTM2 has to automatically load the requested TMs if requested. Also OpenTM2 has to close the TMs after a TM was not used for some time. That means that OpenTM2 has to track the timestamps when a TM was last requested.

Concept endpoints, not implemented

http://opentm2/translationmemory/[TM_Name]/openHandle

GET – Opens a memory for queries by OpenTM2

Note: This method is not required as memories are automatically opened when they are accessed for the first time.

http://opentm2/translationmemory/[TM_Name]/openHandle

DELETE – Closes a memory for queries by OpenTM2

Note: This method is not required as memories are automatically opened when they are accessed for the first time.

For now we open  TM in case of call to work with it. TM stays opened till the shutdown we wouldn't try to open more TM's, exceeding the RAM limit setupped in config file. 
In that case we would close TM in order of longest not used, till we would fit in limit including TM that we try to open.
 TM size is calcucated basicaly as sum .TMD and .TMI files
Ram limit doesn't include service RAM and temporary files

Previous documentation:

Code Block
languagejs
titleResponse
collapsetrue
http://opentm2/translationmemory/ POST - creating a new or importing an existing filebased binary OpenTM2 TM The Parameter „name“ contains the TM Name as a string. The string has a maxlength of 256 chars. It can contain any characters except the characters backslash (\), slash(/), colon (:), question mark (?), asterisk (*), vertical line (|), less than sign (<), and greater than sign (>). Uploading a file is optional, omitting a file means creating a empty TM only. If an empty TM is created, the POST request contains only the JSON structure with the TM Name. If an existing binary OpenTM2 file should be additionally imported to the new TM, the POST must be encoded as multipart/form-data. The JSON structure with the meta data will then be in the first chunk of the multiparted request, the chunk must be named “meta”. The second chunk contains the plain binary file content and must be named “data”. This binary data contains the TM content The resulting body contains the name of the TM, as given in the POST request. To OpenTM2 – without data / creating an empty TM: { sourceLang: “en”, // the source language is required for a new TM name: „TM Name“, [loggingThreshold:"2"] } Raw POST to OpenTM2 – with provided import file: POST http://opentm2/translationmemory HTTP/1.1 Content-Type: multipart/form-data; boundary="autogenerated" -- autogenerated Content-Type: application/json; charset=utf-8 Content-Disposition: form-data; name=meta {"name":"TM Name", sourceLang:"en"} --autogenerated Content-Type: image/jpeg Content-Disposition: form-data; name=data; filename=Original Filename.jpg ...TM content ... --autogenerated-- In both cases from OpenTM2 - HTTP 200 OK: { name: „TM Name“ } Errors: 400 Bad Request – if parameters are missing or are not well formed. 409 Conflict – if a memory with the given name already exists. 500 Server Error – for other technical problems. http://opentm2/translationmemory/[TM_Name]/import POST import a TMX file into an existing OpenTM2 TM To OpenTM2: multipart/form-data like on POST above, expect that no separate JSON section is needed here. Call answers directly after the upload is done, but before the import starts with HTTP 201 – this means: Import is created and will be started now. From OpenTM2 - HTTP 201 OK: { // empty JSON object, since no data expected as result here! } Errors: 400 Bad Request – if parameters are missing or are not well formed. 404 Not Found – if the memory of the given name does not exist 500 Server Error – for other technical problems. http://opentm2/translationmemory/[TM_Name]/status GET status of a TM To OpenTM2: multipart/form-data like on POST above, expect that no separate JSON section is needed here. From OpenTM2 - HTTP 200 OK: { ‘status’:’import’ //allowed status values: import, available, error } Errors: 400 Bad Request – if parameters are missing or are not well formed. 404 Not Found – if the memory of the given name does not exist 500 Server Error – for other technical problems. http://opentm2/translationmemory/ GET – retrieving a list of available TM Files To OpenTM2: - From OpenTM2 - HTTP 200 OK: [{ name: 'my nice TM' }] Errors: 500 Server Error – for other technical problems. http://opentm2/translationmemory/[TM_Name]/ TM_Name is URL-encoded GET – retrieving a single TM File To OpenTM2: - From OpenTM2 - HTTP 200 OK: Same as POST from OpenTM2 result. Errors: 404 Not Found – if TM file to given [TMID] in URL was not found 500 Server Error – for other technical problems. DELETE – deletes an existing TM File Adressed by the given URL, no body needed. Errors: 404 Not Found – if TM file to given [TMID] in URL was not found 500 Server Error – for other technical problems. PUT – updating an existing TM File in one request Currently not needed, would be only to change the TM name GET – list of all segments from TM Currently not needed. http://opentm2/translationmemory/[TM_Name]/entry/ POST – creates a new entry or updates target entry if match pair already exists This method updates an existing proposal when a proposal with the same key information (source text, language, segment number, and document name) exists. Parameters sourceLang and targetLang are containing the languages as RFC5646. Parameters source and target are containing the entry contents to be stored. Format? plain string? Attribute Parameters: documentName: contains the filename where the segment resides in Translate5. context: evaluates to Translate5 segment mid. markupTable: OpenTM2 gets a new markup table named „translate5“, so this is the value which is delivered by Translate5. timestamp: this parameter is not set by translate5, but calculated automatically and delivered from OpenTM2 to translate5. author: contains the named user which provides the update / new entry In addition there are the following OpenTM2 Attributes currently not used by translate5: segmentNumber additional info type To OpenTM2: { sourceLang: 'de', targetLang: 'en', source: „Das ist das Haus des Nikolaus“, target: „This is the house of St. Nicholas“, documentName: 'my file.sdlxliff', segmentNumber: , markupTable: 'translate5', author: „Thomas Lauria“, type: '', timeStamp: '', context: '123', addInfo: '', [loggingThreshold:"2"] } The result from the server contains the same data as posted to the server. No additonal ID is added, since the entries are identified by the whole source string instead by an ID, only the timestamp is added. From OpenTM2 – HTTP 200 OK: { sourceLang: 'de', targetLang: 'en', source: „Das ist das Haus des Nikolaus“, target: „This is the house of St. Nicholas“, documentName: 'my file.sdlxliff', segmentNumber: 123, markupTable: 'translate5', timestamp: '2015-05-12 13:46:12', author: „Thomas Lauria“ } Errors: 404 Not Found – if TM file to given [TM_Name] in URL was not found 500 Server Error – for other technical problems. 400 Bad Request – if JSON parameters are missing or are not well formed. http://opentm2/translationmemory/[TM_Name]/fuzzysearch/ POST– Serves a memory lookup based on the provided search criteria To OpenTM2: { sourceLang: 'de', targetLang: 'en-US', source: „Das ist das Haus des Nikolaus“, documentName: 'my file.sdlxliff', // can be empty segmentNumber: 123, // can be empty markupTable: 'translate5', // can be empty context: „xyz“, // can be empty [loggingThreshold:"2"] } From OpenTM2 HTTP 200 OK: { 'NumOfFoundProposals': 2, 'results': [{ source: „Das ist das Haus des Nikolaus“, target: „This is the house of St. Nicholas“, sourceLang: 'de', ← rfc5646 targetLang: 'en', ← rfc5646 matchRate: '100', documentName: 'my file.sdlxliff', DocumentShortName: 'shortnam.txt', id: 'identifier', type: 'Manual', matchType: 'Exact', segmentNumber: 123, markupTable: 'XYZ', timestamp: '2015-05-12 13:46:12', author: „Thomas Lauria“. context: '', addInfo: '' },{ source: „Das ist das Haus des Nikolaus“, target: „This is the house of St. Nicholas“, sourceLang: 'de', ← rfc5646 targetLang: 'en', ← rfc5646 matchRate: '100', documentName: 'my file.sdlxliff', DocumentShortName: 'shortnam.txt', id: 'identifier', type: 'Manual', matchType: 'Exact', segmentNumber: 123, markupTable: 'XYZ', timestamp: '2015-05-12 13:46:12', author: „Thomas Lauria“. context: '', addInfo: '' }]} Errors: 400 Bad Request – if search, query or language parameters are missing or are not well formed. 404 Not Found – if TM file to given [TM_Name] in URL was not found 500 Server Error – for other technical problems. http://opentm2/translationmemory/[TM_Name]/concordancesearch /? POST – Performs a context search of the given search string in the proposals contained in a memory. Returns one proposal per request. To OpenTM2: { searchString: 'Haus des Nikolaus', searchType: 'source', // values can be source or target searchPosition: 123// can be empty; Position where a search should start in the memory, see below numResults: 1, msSearchAfterNumResults: 100 //number of milliseconds the search will continue, after the first result is found. All additional results that are found in this additional time will also be returned until numResults is reached. If numResults is reached before msSearchAfterNumResults is reached, the search will abort. If msSearchAfterNumResults is reached before numResults is reached, search is also aborted. All found results are delivered in both cases. [loggingThreshold:"2"] } From OpenTM2 HTTP 200 OK: { NewSearchPosition: '123:54', /returns NULL, if end of TM is reached, see below results:[{ source: „Das ist das Haus des Nikolaus“, target: „This is the house of St. Nicholas“, sourceLang: 'de', ← rfc5646 targetLang: 'en', ← rfc5646 matchRate: '100', documentName: 'my file.sdlxliff', DocumentShortName: 'shortnam.txt', id: 'identifier', type: 'Manual', matchType: 'Exact', segmentNumber: 123, markupTable: 'XYZ', timestamp: '2015-05-12 13:46:12', author: „Thomas Lauria“. context: '', addInfo: '' },{ source: „Das ist das Haus des Nikolaus“, target: „This is the house of St. Nicholas“, sourceLang: 'de', ← rfc5646 targetLang: 'en', ← rfc5646 matchRate: '100', documentName: 'my file.sdlxliff', DocumentShortName: 'shortnam.txt', id: 'identifier', type: 'Manual', matchType: 'Exact', segmentNumber: 123, markupTable: 'XYZ', timestamp: '2015-05-12 13:46:12', author: „Thomas Lauria“. context: '', addInfo: '' }]} Errors: 400 Bad Request – if search, query or language parameters are missing or are not well formed. 404 Not Found – if TM file to given [TM_Name] in URL was not found 500 Server Error – for other technical problems.