Skip to content

Commit

Permalink
introducing cache for the database metadata requests from WebIDE
Browse files Browse the repository at this point in the history
  • Loading branch information
delchev committed Feb 24, 2024
1 parent 3375844 commit aa3dd73
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 9 deletions.
4 changes: 4 additions & 0 deletions components/data/data-management/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible contributors
*
* All rights reserved. This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v2.0 which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible
* contributors SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.dirigible.components.data.management.config;

import java.util.concurrent.TimeUnit;

import org.eclipse.dirigible.commons.config.Configuration;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

/**
* The Class CaffeineDatabaseMetadataCache.
*/
public class DatabaseMetadataCache {

private static final String DIRIGIBLE_DATABASE_METADATA_CACHE_TIME_LIMIT_IN_MINUTES =
"DIRIGIBLE_DATABASE_METADATA_CACHE_TIME_LIMIT_IN_MINUTES";

/** The cache. */
private Cache<String, String> cache;

/**
* Instantiates a new caffeine database metadata cache.
*/
public DatabaseMetadataCache() {
initCache();
}

/**
* Inits the cache.
*/
private void initCache() {
long timePolicy = Long.parseLong(Configuration.get(DIRIGIBLE_DATABASE_METADATA_CACHE_TIME_LIMIT_IN_MINUTES, "60"));
cache = Caffeine.newBuilder()
.expireAfterAccess(timePolicy, TimeUnit.MINUTES)
.build();
}

/**
* Gets the internal cache.
*
* @return the internal cache
*/
public Cache<String, String> getInternalCache() {
if (cache == null) {
initCache();
}

return cache;
}

/**
* Gets the.
*
* @param path the path
* @return the String
*/
public String get(String path) {
return cache.getIfPresent(path);
}

/**
* Put.
*
* @param path the path
* @param content the content
*/
public void put(String path, String content) {
if (content != null) {
cache.put(path, content);
}
}

/**
* Removes the.
*
* @param path the path
*/
public void remove(String path) {
cache.invalidate(path);
}

/**
* Clear.
*/
public void clear() {
cache.invalidateAll();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible contributors
*
* All rights reserved. This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v2.0 which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Eclipse Dirigible
* contributors SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.dirigible.components.data.management.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DatabaseMetadataCacheConfig {

@Bean
public DatabaseMetadataCache getDatabaseMetadataCache() {
return new DatabaseMetadataCache();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,23 @@ public List<TableMetadata> getTables() {
return tables;
}

/**
* Gets the views.
*
* @return the views
*/
public List<TableMetadata> getViews() {
return views;
}

/**
* Sets the views.
*
* @param views the new views
*/
public void setViews(List<TableMetadata> views) {
this.views = views;
}

/**
* Get the procedures metadata.
Expand All @@ -169,6 +186,24 @@ public List<FunctionMetadata> getFunctions() {
return functions;
}

/**
* Gets the sequences.
*
* @return the sequences
*/
public List<SequenceMetadata> getSequences() {
return sequences;
}

/**
* Sets the sequences.
*
* @param sequences the new sequences
*/
public void setSequences(List<SequenceMetadata> sequences) {
this.sequences = sequences;
}

/**
* Gets the kind.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public ResponseEntity<Set<String>> getDataSourcesNames() {
* @throws SQLException the SQL exception
*/
@GetMapping(value = "/{datasource}", produces = "application/json")
public ResponseEntity<String> getSchemaMetadata(@PathVariable("datasource") String datasource) throws SQLException {
public ResponseEntity<String> getDataSourceMetadata(@PathVariable("datasource") String datasource) throws SQLException {
return ResponseEntity.ok(databasesService.getDataSourceMetadata(datasource));
}

Expand Down Expand Up @@ -94,8 +94,19 @@ public ResponseEntity<String> getSchemaMetadata(@PathVariable("datasource") Stri
@GetMapping(value = "/{datasource}/{schema}/{structure}", produces = "application/json")
public ResponseEntity<String> getStructureMetadata(@PathVariable("datasource") String datasource, @PathVariable("schema") String schema,
@PathVariable("structure") String structure, @Nullable @RequestParam("kind") String kind) throws SQLException {

return ResponseEntity.ok(databasesService.getStructureMetadata(datasource, schema, structure, kind));
}

/**
* Invalidates the metadata cache.
*
* @return the response entity
*/
@GetMapping(value = "/invalidate-cache")
public ResponseEntity<Void> invalidateCache() {
databasesService.invalidateCache();
return ResponseEntity.noContent()
.<Void>build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.dirigible.components.data.management.config.DatabaseMetadataCache;
import org.eclipse.dirigible.components.data.management.domain.DatabaseStructureTypes;
import org.eclipse.dirigible.components.data.management.helpers.DatabaseMetadataHelper;
import org.eclipse.dirigible.components.data.sources.domain.DataSource;
Expand All @@ -40,16 +41,21 @@ public class DatabaseMetadataService {
/** The data sources service. */
private final DataSourceService datasourceService;

/** The cache. */
private final DatabaseMetadataCache cache;

/**
* Instantiates a new data source endpoint.
*
* @param datasourceManager the datasource manager
* @param datasourceService the datasource service
* @param cache the cache
*/
@Autowired
public DatabaseMetadataService(DataSourcesManager datasourceManager, DataSourceService datasourceService) {
public DatabaseMetadataService(DataSourcesManager datasourceManager, DataSourceService datasourceService, DatabaseMetadataCache cache) {
this.datasourceManager = datasourceManager;
this.datasourceService = datasourceService;
this.cache = cache;
}

/**
Expand All @@ -72,8 +78,13 @@ public Set<String> getDataSourcesNames() {
* @throws SQLException the SQL exception
*/
public String getDataSourceMetadata(String datasource) throws SQLException {
javax.sql.DataSource dataSource = datasourceManager.getDataSource(datasource);
String metadata = DatabaseMetadataHelper.getMetadataAsJson(dataSource);
String key = "DATASOURCE_" + datasource;
String metadata = cache.get(key);
if (metadata == null) {
javax.sql.DataSource dataSource = datasourceManager.getDataSource(datasource);
metadata = DatabaseMetadataHelper.getMetadataAsJson(dataSource);
cache.put(key, metadata);
}
return metadata;
}

Expand All @@ -98,11 +109,24 @@ public boolean existsDataSourceMetadata(String datasource) throws SQLException {
* @throws SQLException the SQL exception
*/
public String getSchemaMetadata(String datasource, String schema) throws SQLException {
javax.sql.DataSource dataSource = datasourceManager.getDataSource(datasource);
String metadata = DatabaseMetadataHelper.getSchemaMetadataAsJson(dataSource, schema);
String key = "DATASOURCE_" + datasource + "_SCHEMA_" + schema;
String metadata = cache.get(key);
if (metadata == null) {
javax.sql.DataSource dataSource = datasourceManager.getDataSource(datasource);
metadata = DatabaseMetadataHelper.getSchemaMetadataAsJson(dataSource, schema);
cache.put(key, metadata);
}
return metadata;
}

/**
* Invalidate cache.
*/
public void invalidateCache() {
cache.getInternalCache()
.invalidateAll();
}

/**
* Describe the requested artifact in JSON.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<fd-button compact="true" dg-type="transparent" glyph="sap-icon--play" title="Run" aria-label="Run"
ng-click="runSQL()"></fd-button>
<fd-button compact="true" dg-type="transparent" glyph="sap-icon--refresh" title="Refresh"
aria-label="Refresh" ng-click="getDatabases()"></fd-button>
aria-label="Refresh" ng-click="refresh()"></fd-button>
<fd-toolbar-separator></fd-toolbar-separator>
<fd-popover>
<fd-popover-control>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
const database = angular.module('database', ['ideUI', 'ideView']);
database.controller('DatabaseController', function ($scope, $http, messageHub) {
let databasesSvcUrl = "/services/data/";
let databasesInvalidateSvcUrl = "/services/data/metadata/invalidate-cache";
$scope.selectedDatabase;
$scope.jstreeWidget = angular.element('#dgDatabases');
$scope.spinnerColumns = {
Expand Down Expand Up @@ -1003,10 +1004,19 @@ database.controller('DatabaseController', function ($scope, $http, messageHub) {
'ide-databases.explorer.refresh',
function () {
$scope.$apply(function () {
$scope.getDatabases();
$scope.refresh();
});
},
true
);

$scope.refresh = function () {
$scope.invalidateCache();
$scope.getDatabases();
}

$scope.invalidateCache = function () {
$http.get(databasesInvalidateSvcUrl);
}

});

0 comments on commit aa3dd73

Please sign in to comment.