1515 */
1616package org .opends .server .backends .cassandra ;
1717
18-
1918import static org .opends .server .backends .pluggable .spi .StorageUtils .addErrorMessage ;
2019import static org .opends .server .util .StaticUtils .stackTraceToSingleLineString ;
2120
6261import com .datastax .oss .driver .api .core .cql .ResultSet ;
6362import com .datastax .oss .driver .api .core .cql .Row ;
6463import com .datastax .oss .driver .api .core .cql .Statement ;
65- import com . google . common . cache . CacheBuilder ;
66- import com .google . common . cache .CacheLoader ;
67- import com .google . common .cache .LoadingCache ;
64+
65+ import com .github . benmanes . caffeine . cache .Caffeine ;
66+ import com .github . benmanes . caffeine .cache .LoadingCache ;
6867
6968public class Storage implements org .opends .server .backends .pluggable .spi .Storage , ConfigurationChangeListener <CASBackendCfg >{
7069
7170 private static final LocalizedLogger logger = LocalizedLogger .getLoggerForThisClass ();
7271
73- //private final ServerContext serverContext;
7472 private CASBackendCfg config ;
75-
73+
7674 public Storage (CASBackendCfg cfg , ServerContext serverContext ) {
77- //this.serverContext = serverContext;
7875 this .config = cfg ;
79- cfg .addCASChangeListener (this );
76+ cfg .addCASChangeListener (this );
8077 }
8178
8279 //config
@@ -88,37 +85,32 @@ public boolean isConfigurationChangeAcceptable(CASBackendCfg configuration,List<
8885 @ Override
8986 public ConfigChangeResult applyConfigurationChange (CASBackendCfg cfg ) {
9087 final ConfigChangeResult ccr = new ConfigChangeResult ();
91- try
92- {
93- this .config = cfg ;
94- }
95- catch (Exception e )
96- {
97- addErrorMessage (ccr , LocalizableMessage .raw (stackTraceToSingleLineString (e )));
98- }
99- return ccr ;
88+ try
89+ {
90+ this .config = cfg ;
91+ }
92+ catch (Exception e )
93+ {
94+ addErrorMessage (ccr , LocalizableMessage .raw (stackTraceToSingleLineString (e )));
95+ }
96+ return ccr ;
10097 }
10198
10299 CqlSession session =null ;
103-
104- final LoadingCache <String ,PreparedStatement > prepared =CacheBuilder .newBuilder ()
105- .expireAfterAccess (Duration .ofMinutes (10 ))
106- .maximumSize (4096 )
107- .build (new CacheLoader <String ,PreparedStatement >(){
108- @ Override
109- public PreparedStatement load (String query ) throws Exception {
110- return session .prepare (query );
111- }
112- });
113-
100+
101+ final LoadingCache <String ,PreparedStatement > prepared = Caffeine .newBuilder ()
102+ .expireAfterAccess (Duration .ofMinutes (10 ))
103+ .maximumSize (4096 )
104+ .build (query -> session .prepare (query ));
105+
114106 ResultSet execute (Statement <?> statement ) {
115107 if (logger .isTraceEnabled ()) {
116108 final ResultSet res =session .execute (statement .setTracing (true ));
117109 logger .trace (LocalizableMessage .raw (
118110 "cassandra: %s"
119111 ,res .getExecutionInfo ().getQueryTrace ().getParameters ()
120- )
121- );
112+ )
113+ );
122114 return res ;
123115 }
124116 return session .execute (statement );
@@ -129,11 +121,11 @@ ResultSet execute(Statement<?> statement) {
129121 public void open (AccessMode accessMode ) throws Exception {
130122 this .accessMode =accessMode ;
131123 session =CqlSession .builder ()
132- .withApplicationName ("OpenDJ " +getKeyspaceName ()+"." +config .getBackendId ())
133- .withConfigLoader (DriverConfigLoader .fromDefaults (Storage .class .getClassLoader ()))
134- .build ();
124+ .withApplicationName ("OpenDJ " +getKeyspaceName ()+"." +config .getBackendId ())
125+ .withConfigLoader (DriverConfigLoader .fromDefaults (Storage .class .getClassLoader ()))
126+ .build ();
135127 if (AccessMode .READ_WRITE .equals (accessMode )) {
136- execute (prepared .getUnchecked ("CREATE KEYSPACE IF NOT EXISTS " +getKeyspaceName ()+" WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'};" ).bind ().setExecutionProfileName (profile ));
128+ execute (prepared .get ("CREATE KEYSPACE IF NOT EXISTS " +getKeyspaceName ()+" WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'};" ).bind ().setExecutionProfileName (profile ));
137129 }
138130 storageStatus = StorageStatus .working ();
139131 }
@@ -156,14 +148,14 @@ public void close() {
156148 String getKeyspaceName () {
157149 return "\" " +System .getProperty ("keyspace" ,config .getDBDirectory ()).replaceAll ("[^a-zA-z0-9_]" , "_" )+"\" " ;
158150 }
159-
151+
160152 String getTableName () {
161153 return getKeyspaceName ()+".\" " +config .getBackendId ().replaceAll ("[^a-zA-z0-9_]" , "_" )+"\" " ;
162154 }
163155
164156 @ Override
165157 public void removeStorageFiles () throws StorageRuntimeException {
166- final Boolean isOpen =getStorageStatus ().isWorking ();
158+ final boolean isOpen =getStorageStatus ().isWorking ();
167159 if (!isOpen ) {
168160 try {
169161 open (AccessMode .READ_WRITE );
@@ -172,7 +164,7 @@ public void removeStorageFiles() throws StorageRuntimeException {
172164 }
173165 }
174166 try {
175- execute (prepared .getUnchecked ("TRUNCATE TABLE " +getTableName ()+";" ).bind ().setExecutionProfileName (profile ));
167+ execute (prepared .get ("TRUNCATE TABLE " +getTableName ()+";" ).bind ().setExecutionProfileName (profile ));
176168 }catch (Throwable e ) {}
177169 if (!isOpen ) {
178170 close ();
@@ -210,7 +202,7 @@ public TransactionImpl(AccessMode accessMode) {
210202 @ Override
211203 public void openTree (TreeName name , boolean createOnDemand ) {
212204 if (createOnDemand ) {
213- execute (prepared .getUnchecked ("CREATE TABLE IF NOT EXISTS " +getTableName ()+" (baseDN text,indexId text,key blob,value blob,PRIMARY KEY ((baseDN,indexId),key));" ).bind ().setExecutionProfileName (profile ));
205+ execute (prepared .get ("CREATE TABLE IF NOT EXISTS " +getTableName ()+" (baseDN text,indexId text,key blob,value blob,PRIMARY KEY ((baseDN,indexId),key));" ).bind ().setExecutionProfileName (profile ));
214206 }
215207 }
216208
@@ -222,10 +214,10 @@ public void clearTree(TreeName treeName) {
222214 @ Override
223215 public ByteString read (TreeName treeName , ByteSequence key ) {
224216 final Row row =execute (
225- prepared .getUnchecked ("SELECT value FROM " +getTableName ()+" WHERE baseDN=:baseDN and indexId=:indexId and key=:key" ).bind ()
226- .setString ("baseDN" , treeName .getBaseDN ()).setString ("indexId" , treeName .getIndexId ())
227- .setByteBuffer ("key" , ByteBuffer .wrap (key .toByteArray ()))
228- ).one ();
217+ prepared .get ("SELECT value FROM " +getTableName ()+" WHERE baseDN=:baseDN and indexId=:indexId and key=:key" ).bind ()
218+ .setString ("baseDN" , treeName .getBaseDN ()).setString ("indexId" , treeName .getIndexId ())
219+ .setByteBuffer ("key" , ByteBuffer .wrap (key .toByteArray ()))
220+ ).one ();
229221 return row ==null ?null :ByteString .wrap (row .getByteBuffer ("value" ).array ());
230222 }
231223
@@ -237,30 +229,30 @@ public Cursor<ByteString, ByteString> openCursor(TreeName treeName) {
237229 @ Override
238230 public long getRecordCount (TreeName treeName ) {
239231 return execute (
240- prepared .getUnchecked ("SELECT count(*) FROM " +getTableName ()+" WHERE baseDN=:baseDN and indexId=:indexId" ).bind ()
241- .setString ("baseDN" , treeName .getBaseDN ()).setString ("indexId" , treeName .getIndexId ())
242- ).one ().getLong (0 );
232+ prepared .get ("SELECT count(*) FROM " +getTableName ()+" WHERE baseDN=:baseDN and indexId=:indexId" ).bind ()
233+ .setString ("baseDN" , treeName .getBaseDN ()).setString ("indexId" , treeName .getIndexId ())
234+ ).one ().getLong (0 );
243235 }
244236
245237 @ Override
246238 public void deleteTree (TreeName treeName ) {
247239 checkReadOnly ();
248240 openTree (treeName ,true );
249241 execute (
250- prepared .getUnchecked ("DELETE FROM " +getTableName ()+" WHERE baseDN=:baseDN and indexId=:indexId" ).bind ()
251- .setString ("baseDN" , treeName .getBaseDN ()).setString ("indexId" , treeName .getIndexId ())
252- );
242+ prepared .get ("DELETE FROM " +getTableName ()+" WHERE baseDN=:baseDN and indexId=:indexId" ).bind ()
243+ .setString ("baseDN" , treeName .getBaseDN ()).setString ("indexId" , treeName .getIndexId ())
244+ );
253245 }
254246
255247 @ Override
256248 public void put (TreeName treeName , ByteSequence key , ByteSequence value ) {
257249 checkReadOnly ();
258250 execute (
259- prepared .getUnchecked ("INSERT INTO " +getTableName ()+" (baseDN,indexId,key,value) VALUES (:baseDN,:indexId,:key,:value)" ).bind ()
260- .setString ("baseDN" , treeName .getBaseDN ()).setString ("indexId" , treeName .getIndexId ())
251+ prepared .get ("INSERT INTO " +getTableName ()+" (baseDN,indexId,key,value) VALUES (:baseDN,:indexId,:key,:value)" ).bind ()
252+ .setString ("baseDN" , treeName .getBaseDN ()).setString ("indexId" , treeName .getIndexId ())
261253 .setByteBuffer ("key" , ByteBuffer .wrap (key .toByteArray ()))
262254 .setByteBuffer ("value" ,ByteBuffer .wrap (value .toByteArray ()))
263- );
255+ );
264256 }
265257
266258 @ Override
@@ -269,26 +261,26 @@ public boolean update(TreeName treeName, ByteSequence key, UpdateFunction f) {
269261 final ByteString oldValue =read (treeName ,key );
270262 final ByteSequence newValue =f .computeNewValue (oldValue );
271263 if (Objects .equals (newValue , oldValue ))
272- {
264+ {
273265 return false ;
274- }
275- if (newValue == null )
276- {
277- delete (treeName , key );
278- return true ;
279- }
280- put (treeName ,key ,newValue );
266+ }
267+ if (newValue == null )
268+ {
269+ delete (treeName , key );
270+ return true ;
271+ }
272+ put (treeName ,key ,newValue );
281273 return true ;
282274 }
283275
284276 @ Override
285277 public boolean delete (TreeName treeName , ByteSequence key ) {
286278 checkReadOnly ();
287279 execute (
288- prepared .getUnchecked ("DELETE FROM " +getTableName ()+" WHERE baseDN=:baseDN and indexId=:indexId and key=:key" ).bind ()
289- .setString ("baseDN" , treeName .getBaseDN ()).setString ("indexId" , treeName .getIndexId ())
290- .setByteBuffer ("key" , ByteBuffer .wrap (key .toByteArray ()))
291- );
280+ prepared .get ("DELETE FROM " +getTableName ()+" WHERE baseDN=:baseDN and indexId=:indexId and key=:key" ).bind ()
281+ .setString ("baseDN" , treeName .getBaseDN ()).setString ("indexId" , treeName .getIndexId ())
282+ .setByteBuffer ("key" , ByteBuffer .wrap (key .toByteArray ()))
283+ );
292284 return true ;
293285 }
294286
@@ -316,9 +308,9 @@ public CursorImpl(TransactionImpl tx,TreeName treeName) {
316308
317309 ResultSet full (){
318310 return execute (
319- prepared .getUnchecked ("SELECT key,value FROM " +getTableName ()+" WHERE baseDN=:baseDN and indexId=:indexId ORDER BY key" ).bind ()
320- .setString ("baseDN" , treeName .getBaseDN ()).setString ("indexId" , treeName .getIndexId ())
321- );
311+ prepared .get ("SELECT key,value FROM " +getTableName ()+" WHERE baseDN=:baseDN and indexId=:indexId ORDER BY key" ).bind ()
312+ .setString ("baseDN" , treeName .getBaseDN ()).setString ("indexId" , treeName .getIndexId ())
313+ );
322314 }
323315
324316 @ Override
@@ -413,7 +405,7 @@ public boolean positionToLastKey() {
413405 }
414406 return false ;
415407 }
416-
408+
417409 @ Override
418410 public boolean positionToIndex (int index ) {
419411 iterator =rc .iterator (); //restart iterator
@@ -424,12 +416,12 @@ public boolean positionToIndex(int index) {
424416 return true ;
425417 }
426418 ct ++;
427- }
419+ }
428420 current =null ;
429421 return false ;
430422 }
431423 }
432-
424+
433425 @ Override
434426 public Set <TreeName > listTrees () {
435427 // TODO Auto-generated method stub
@@ -453,29 +445,29 @@ public ImporterImpl() {
453445 }
454446 tx =new TransactionImpl (accessMode );
455447 }
456-
448+
457449 @ Override
458450 public void close () {
459451 if (!isOpen ) {
460452 Storage .this .close ();
461453 }
462454 }
463-
455+
464456 @ Override
465457 public void clearTree (TreeName name ) {
466458 tx .clearTree (name );
467459 }
468-
460+
469461 @ Override
470462 public void put (TreeName treeName , ByteSequence key , ByteSequence value ) {
471463 tx .put (treeName , key , value );
472464 }
473-
465+
474466 @ Override
475467 public ByteString read (TreeName treeName , ByteSequence key ) {
476468 return tx .read (treeName , key );
477469 }
478-
470+
479471 @ Override
480472 public SequentialCursor <ByteString , ByteString > openCursor (TreeName treeName ) {
481473 return tx .openCursor (treeName );
0 commit comments