22
22
import java .net .URI ;
23
23
import java .nio .charset .StandardCharsets ;
24
24
import java .nio .file .Files ;
25
+ import java .util .HashMap ;
25
26
import java .util .HashSet ;
27
+ import java .util .Map ;
26
28
import java .util .Optional ;
27
29
import java .util .Set ;
28
30
import java .util .concurrent .Callable ;
31
+ import java .util .concurrent .atomic .AtomicReference ;
29
32
30
33
import com .fasterxml .jackson .annotation .JsonAutoDetect ;
31
34
import com .fasterxml .jackson .annotation .JsonInclude ;
@@ -70,10 +73,27 @@ public class RocksdbMavenPomCache implements MavenPomCache {
70
73
.withGetterVisibility (JsonAutoDetect .Visibility .NONE )
71
74
.withSetterVisibility (JsonAutoDetect .Visibility .NONE )
72
75
.withCreatorVisibility (JsonAutoDetect .Visibility .PUBLIC_ONLY ));
76
+
77
+ //Init the rockdb native jni library
78
+ RocksDB .loadLibrary ();
79
+ }
80
+
81
+ //While the RocksDB instance is thread-safe, attempts to create two instances of the database to the same
82
+ //folder will fail.
83
+ private static final Map <String , RocksDB > cacheMap = new HashMap <>();
84
+ static synchronized RocksDB getCache (String workspace ) {
85
+ return cacheMap .computeIfAbsent (workspace , k -> {
86
+ final Options options = new Options ();
87
+ options .setCreateIfMissing (true );
88
+ try {
89
+ return RocksDB .open (options , k );
90
+ } catch (RocksDBException exception ) {
91
+ throw new IllegalStateException (("Unable to create cache database." + exception .getMessage ()));
92
+ }
93
+ });
73
94
}
74
95
75
- //All three caches are put into the same database, they could be separated out at some point if we find we have
76
- //a need.
96
+
77
97
private final RocksDB cache ;
78
98
private final Set <String > unresolvablePoms = new HashSet <>();
79
99
@@ -90,15 +110,7 @@ public RocksdbMavenPomCache(@Nullable File workspace) {
90
110
} else if (!workspace .isDirectory ()) {
91
111
throw new IllegalStateException ("The maven cache workspace must be a directory" );
92
112
}
93
-
94
- RocksDB .loadLibrary ();
95
- final Options options = new Options ();
96
- options .setCreateIfMissing (true );
97
- try {
98
- cache = RocksDB .open (options , workspace .getAbsolutePath ());
99
- } catch (RocksDBException exception ) {
100
- throw new IllegalStateException (("Unable to create cache database." ));
101
- }
113
+ cache = getCache (workspace .getAbsolutePath ());
102
114
fillUnresolvablePoms ();
103
115
}
104
116
0 commit comments