Skip to content

Commit

Permalink
fetch mmdb from GCS
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgeniiMunin committed Dec 13, 2024
1 parent fb38c22 commit 5a11d49
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.prebid.server.hooks.modules.greenbids.real.time.data.config;

import com.google.cloud.storage.Blob;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import com.maxmind.geoip2.DatabaseReader;
Expand All @@ -12,35 +15,50 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

public class DatabaseReaderFactory implements Initializable {

private final String geoLiteCountryUrl;
private final String gcsBucketName;

private final String geoLiteCountryPath;

private final Vertx vertx;

private final Storage storage;

private final AtomicReference<DatabaseReader> databaseReaderRef = new AtomicReference<>();

public DatabaseReaderFactory(String geoLitCountryUrl, Vertx vertx) {
this.geoLiteCountryUrl = geoLitCountryUrl;
public DatabaseReaderFactory(String gcsBucketName, String geoLiteCountryPath, Vertx vertx, Storage storage) {
this.gcsBucketName = gcsBucketName;
this.geoLiteCountryPath = geoLiteCountryPath;
this.vertx = vertx;
this.storage = storage;
}

@Override
public void initialize(Promise<Void> initializePromise) {

vertx.executeBlocking(() -> {
try {
final URL url = new URL(geoLiteCountryUrl);
final Blob blob = getBlob(geoLiteCountryPath);
final Path databasePath = Files.createTempFile("GeoLite2-Country", ".mmdb");

try (InputStream inputStream = url.openStream();
FileOutputStream outputStream = new FileOutputStream(databasePath.toFile())) {
inputStream.transferTo(outputStream);
try (FileOutputStream outputStream = new FileOutputStream(databasePath.toFile())) {
outputStream.write(blob.getContent());
}

databaseReaderRef.set(new DatabaseReader.Builder(databasePath.toFile()).build());

System.out.println(
"DatabaseReaderFactory/initialize: \n" +
" gcsBucketName: " + gcsBucketName + "\n" +
" geoLiteCountryPath: " + geoLiteCountryPath + "\n" +
" blob: " + blob + "\n" +
" databasePath: " + databasePath + "\n" +
" gcsBucketName: " + gcsBucketName
);

} catch (IOException e) {
throw new PreBidException("Failed to initialize DatabaseReader from URL", e);
}
Expand All @@ -49,6 +67,12 @@ public void initialize(Promise<Void> initializePromise) {
.onComplete(initializePromise);
}

private Blob getBlob(String blobName) {
return Optional.ofNullable(storage.get(gcsBucketName))
.map(bucket -> bucket.get(blobName))
.orElseThrow(() -> new PreBidException("Bucket not found: " + gcsBucketName));
}

public DatabaseReader getDatabaseReader() {
return databaseReaderRef.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
public class GreenbidsRealTimeDataConfiguration {

@Bean
DatabaseReaderFactory databaseReaderFactory(GreenbidsRealTimeDataProperties properties, Vertx vertx) {
return new DatabaseReaderFactory(properties.getGeoLiteCountryPath(), vertx);
DatabaseReaderFactory databaseReaderFactory(
GreenbidsRealTimeDataProperties properties, Vertx vertx, Storage storage) {
return new DatabaseReaderFactory(properties.gcsBucketName, properties.getGeoLiteCountryPath(), vertx, storage);
}

@Bean
Expand Down

0 comments on commit 5a11d49

Please sign in to comment.