33
33
import java .nio .file .Paths ;
34
34
import java .util .ArrayList ;
35
35
import java .util .List ;
36
+ import java .util .Properties ;
37
+ import java .util .UUID ;
36
38
import java .util .concurrent .*;
37
39
38
40
import org .slf4j .Logger ;
@@ -42,6 +44,11 @@ public class JSONReader {
42
44
43
45
private static Logger logger = LoggerFactory .getLogger ("TCK-Test" );
44
46
47
+ private static final String CACHE_DIR = SystemProperties .getDefault ().githubTestsPath ();
48
+ private static final boolean USE_CACHE = SystemProperties .getDefault ().githubTestsLoadLocal ();
49
+ private static final String CACHE_INDEX = "index.prop" ;
50
+ private static final String CACHE_FILES_SUB_DIR = "files" ;
51
+
45
52
static ExecutorService threadPool ;
46
53
47
54
private static int MAX_RETRIES = 3 ;
@@ -74,10 +81,9 @@ public static List<String> loadJSONsFromCommit(List<String> filenames, final Str
74
81
75
82
public static String loadJSONFromCommit (String filename , String shacommit ) throws IOException {
76
83
String json = "" ;
77
- if (!SystemProperties .getDefault ().githubTestsLoadLocal ())
78
- json = getFromUrl ("https://raw.githubusercontent.com/ethereum/tests/" + shacommit + "/" + filename );
84
+ json = getFromUrl ("https://raw.githubusercontent.com/ethereum/tests/" + shacommit + "/" + filename );
79
85
if (!json .isEmpty ()) json = json .replaceAll ("//" , "data" );
80
- return json . isEmpty () ? getFromLocal ( filename ) : json ;
86
+ return json ;
81
87
}
82
88
83
89
public static String getFromLocal (String filename ) throws IOException {
@@ -94,7 +100,15 @@ public static String getFromUrl(String urlToRead) {
94
100
String result = null ;
95
101
for (int i = 0 ; i < MAX_RETRIES ; ++i ) {
96
102
try {
97
- result = getFromUrlImpl (urlToRead );
103
+ if (USE_CACHE ) {
104
+ result = getFromCacheImpl (urlToRead );
105
+ if (result == null ) {
106
+ result = getFromUrlImpl (urlToRead );
107
+ recordCache (urlToRead , result );
108
+ }
109
+ } else {
110
+ result = getFromUrlImpl (urlToRead );
111
+ }
98
112
break ;
99
113
} catch (Exception ex ) {
100
114
logger .debug (String .format ("Failed to retrieve %s, retry %d/%d" , urlToRead , (i + 1 ), MAX_RETRIES ), ex );
@@ -138,26 +152,68 @@ private static String getFromUrlImpl(String urlToRead) throws Exception {
138
152
return result .toString ();
139
153
}
140
154
141
- public static List <String > listJsonBlobsForTreeSha (String sha , String testRoot ) throws IOException {
155
+ private static String getFromCacheImpl (String urlToRead ) {
156
+ String result = null ;
157
+ String filename = null ;
158
+ try (InputStream input = new FileInputStream (CACHE_DIR + System .getProperty ("file.separator" ) + CACHE_INDEX )) {
159
+ Properties prop = new Properties ();
160
+ prop .load (input );
161
+ filename = prop .getProperty (urlToRead );
162
+ } catch (Exception ex ) {
163
+ ex .printStackTrace ();
164
+ }
165
+
166
+ if (filename != null ) {
167
+ try {
168
+ result = new String (Files .readAllBytes (new File (CACHE_DIR + System .getProperty ("file.separator" ) +
169
+ CACHE_FILES_SUB_DIR + System .getProperty ("file.separator" ) + filename ).toPath ()));
170
+ } catch (IOException ex ) {
171
+ ex .printStackTrace ();
172
+ }
173
+ }
142
174
143
- if (SystemProperties .getDefault ().githubTestsLoadLocal ()) {
175
+ return result ;
176
+ }
144
177
145
- String path = SystemProperties .getDefault ().githubTestsPath () +
146
- System .getProperty ("file.separator" ) + testRoot .replaceAll ("/" , "" );
178
+ private synchronized static void recordCache (String urlToRead , String data ) {
179
+ String filename = UUID .randomUUID ().toString ();
180
+ File targetFile = new File (CACHE_DIR + System .getProperty ("file.separator" ) +
181
+ CACHE_FILES_SUB_DIR + System .getProperty ("file.separator" ) + filename );
147
182
148
- List <String > files = FileUtil .recursiveList (path );
183
+ // Ensure we have directories created
184
+ File parent = targetFile .getParentFile ();
185
+ if (!parent .exists () && !parent .mkdirs ()) {
186
+ throw new IllegalStateException ("Couldn't create dir: " + parent );
187
+ }
149
188
150
- List <String > jsons = new ArrayList <>();
151
- for (String f : files ) {
152
- if (f .endsWith (".json" ))
153
- jsons .add (
154
- f .replace (path + System .getProperty ("file.separator" ), "" )
155
- .replaceAll (System .getProperty ("file.separator" ), "/" ));
156
- }
189
+ // Load index
190
+ Properties prop = new Properties ();
191
+ String propFile = CACHE_DIR + System .getProperty ("file.separator" ) + CACHE_INDEX ;
192
+ try (InputStream input = new FileInputStream (propFile )) {
193
+ prop .load (input );
194
+ } catch (Exception ex ) {
195
+ ex .printStackTrace ();
196
+ }
157
197
158
- return jsons ;
198
+ // Save with new entry
199
+ prop .setProperty (urlToRead , filename );
200
+ try (OutputStream output = new FileOutputStream (propFile )) {
201
+ prop .store (output , null );
202
+ } catch (Exception ex ) {
203
+ ex .printStackTrace ();
204
+ return ;
159
205
}
160
206
207
+ // Save data
208
+ try (OutputStream output = new FileOutputStream (targetFile )) {
209
+ output .write (data .getBytes ());
210
+ } catch (Exception ex ) {
211
+ ex .printStackTrace ();
212
+ }
213
+ }
214
+
215
+ public static List <String > listJsonBlobsForTreeSha (String sha , String testRoot ) throws IOException {
216
+
161
217
String result = getFromUrl ("https://api.github.com/repos/ethereum/tests/git/trees/" + sha + "?recursive=1" );
162
218
163
219
JSONParser parser = new JSONParser ();
0 commit comments