@@ -43,9 +43,11 @@ public class PythonCpdAnalyzer {
4343 private static final Logger LOG = LoggerFactory .getLogger (PythonCpdAnalyzer .class );
4444
4545 private final SensorContext context ;
46+ private final Object monitor ;
4647
47- public PythonCpdAnalyzer (SensorContext context ) {
48+ public PythonCpdAnalyzer (SensorContext context , Object monitor ) {
4849 this .context = context ;
50+ this .monitor = monitor ;
4951 }
5052
5153 public void pushCpdTokens (InputFile inputFile , PythonVisitorContext visitorContext ) {
@@ -68,7 +70,7 @@ public void pushCpdTokens(InputFile inputFile, PythonVisitorContext visitorConte
6870 }
6971 }
7072 saveTokensToCache (visitorContext , tokensToCache );
71- cpdTokens . save ();
73+ save (cpdTokens );
7274 }
7375 }
7476
@@ -87,9 +89,8 @@ public boolean pushCachedCpdTokens(InputFile inputFile, CacheContext cacheContex
8789 NewCpdTokens cpdTokens = context .newCpdTokens ().onFile (inputFile );
8890 tokens .forEach (tokenInfo ->
8991 cpdTokens .addToken (tokenInfo .startLine , tokenInfo .startLineOffset , tokenInfo .endLine , tokenInfo .endLineOffset , tokenInfo .value ));
90- cpdTokens .save ();
91- cacheContext .getWriteCache ().copyFromPrevious (dataKey );
92- cacheContext .getWriteCache ().copyFromPrevious (tableKey );
92+ save (cpdTokens );
93+ copyFromPrevious (cacheContext , dataKey , tableKey );
9394 return true ;
9495 } catch (IOException e ) {
9596 LOG .warn ("Failed to deserialize CPD tokens ({}: {})" , e .getClass ().getSimpleName (), e .getMessage ());
@@ -98,7 +99,7 @@ public boolean pushCachedCpdTokens(InputFile inputFile, CacheContext cacheContex
9899 return false ;
99100 }
100101
101- private static void saveTokensToCache (PythonVisitorContext visitorContext , List <Token > tokensToCache ) {
102+ private void saveTokensToCache (PythonVisitorContext visitorContext , List <Token > tokensToCache ) {
102103 CacheContext cacheContext = visitorContext .cacheContext ();
103104 if (!cacheContext .isCacheEnabled ()) {
104105 return ;
@@ -108,13 +109,32 @@ private static void saveTokensToCache(PythonVisitorContext visitorContext, List<
108109 String fileKey = visitorContext .pythonFile ().key ();
109110
110111 CpdSerializer .SerializationResult result = CpdSerializer .serialize (tokensToCache );
111- cacheContext .getWriteCache ().write (stringTableCacheKey (fileKey ), result .stringTable );
112- cacheContext .getWriteCache ().write (dataCacheKey (fileKey ), result .data );
112+ writeToCache (cacheContext , fileKey , result );
113113 } catch (Exception e ) {
114114 LOG .warn ("Could not write CPD tokens to cache ({}: {})" , e .getClass ().getSimpleName (), e .getMessage ());
115115 }
116116 }
117117
118+ private void save (NewCpdTokens cpdTokens ) {
119+ synchronized (monitor ) {
120+ cpdTokens .save ();
121+ }
122+ }
123+
124+ private void copyFromPrevious (CacheContext cacheContext , String dataKey , String tableKey ) {
125+ synchronized (monitor ) {
126+ cacheContext .getWriteCache ().copyFromPrevious (dataKey );
127+ cacheContext .getWriteCache ().copyFromPrevious (tableKey );
128+ }
129+ }
130+
131+ private void writeToCache (CacheContext cacheContext , String fileKey , CpdSerializer .SerializationResult result ) {
132+ synchronized (monitor ) {
133+ cacheContext .getWriteCache ().write (stringTableCacheKey (fileKey ), result .stringTable );
134+ cacheContext .getWriteCache ().write (dataCacheKey (fileKey ), result .data );
135+ }
136+ }
137+
118138 private static boolean isNewLineWithIndentationChange (TokenType currentTokenType , TokenType nextTokenType ) {
119139 return currentTokenType .equals (PythonTokenType .NEWLINE ) && nextTokenType .equals (PythonTokenType .DEDENT );
120140 }
0 commit comments