Skip to content

Commit

Permalink
Remove JacksonDBCollection usage from DBTelemetryUserSettingsService
Browse files Browse the repository at this point in the history
  • Loading branch information
thll committed Jan 20, 2025
1 parent c4b3e65 commit 48b70c1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
*/
package org.graylog2.telemetry.user.db;

import org.bson.types.ObjectId;
import org.graylog2.bindings.providers.MongoJackObjectMapperProvider;
import org.graylog2.database.MongoConnection;
import org.mongojack.DBQuery;
import org.mongojack.JacksonDBCollection;

import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import jakarta.inject.Inject;
import org.graylog2.database.MongoCollections;
import org.graylog2.database.utils.MongoUtils;

import java.util.Optional;

Expand All @@ -32,26 +30,24 @@ public class DBTelemetryUserSettingsService {

public static final String COLLECTION_NAME = "telemetry_user_settings";

private final JacksonDBCollection<TelemetryUserSettingsDto, ObjectId> db;
private final MongoCollection<TelemetryUserSettingsDto> collection;
private final MongoUtils<TelemetryUserSettingsDto> mongoUtils;

@Inject
public DBTelemetryUserSettingsService(MongoConnection mongoConnection,
MongoJackObjectMapperProvider mapper) {
this.db = JacksonDBCollection.wrap(mongoConnection.getDatabase().getCollection(COLLECTION_NAME),
TelemetryUserSettingsDto.class,
ObjectId.class,
mapper.get());
public DBTelemetryUserSettingsService(MongoCollections mongoCollections) {
collection = mongoCollections.collection(COLLECTION_NAME, TelemetryUserSettingsDto.class);
mongoUtils = mongoCollections.utils(collection);
}

public Optional<TelemetryUserSettingsDto> findByUserId(String userId) {
return Optional.ofNullable(db.findOne(DBQuery.is(FIELD_USER_ID, userId)));
return Optional.ofNullable(collection.find(Filters.eq(FIELD_USER_ID, userId)).first());
}

public void save(TelemetryUserSettingsDto dto) {
db.save(dto);
mongoUtils.save(dto);
}

public void delete(String userId) {
db.remove(DBQuery.is(FIELD_USER_ID, userId));
collection.deleteMany(Filters.eq(FIELD_USER_ID, userId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.auto.value.AutoValue;
import org.graylog2.database.BuildableMongoEntity;
import org.mongojack.Id;
import org.mongojack.ObjectId;

import javax.annotation.Nullable;

@AutoValue
public abstract class TelemetryUserSettingsDto {
public abstract class TelemetryUserSettingsDto
implements BuildableMongoEntity<TelemetryUserSettingsDto, TelemetryUserSettingsDto.Builder> {

public static final String FIELD_USER_ID = "user_id";

Expand Down Expand Up @@ -62,7 +64,7 @@ public static TelemetryUserSettingsDto create(@JsonProperty("id") @Id @ObjectId
public abstract Boolean telemetryPermissionAsked();

@AutoValue.Builder
public abstract static class Builder {
public abstract static class Builder implements BuildableMongoEntity.Builder<TelemetryUserSettingsDto, Builder> {

public abstract Builder id(String id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.graylog2.bindings.providers.MongoJackObjectMapperProvider;
import org.graylog2.cluster.leader.LeaderElectionService;
import org.graylog2.cluster.nodes.NodeService;
import org.graylog2.database.MongoCollections;
import org.graylog2.indexer.cluster.ClusterAdapter;
import org.graylog2.plugin.PluginMetaData;
import org.graylog2.plugin.ServerStatus;
Expand Down Expand Up @@ -111,7 +112,7 @@ public void setUp() {
elasticClusterAdapter,
elasticsearchVersion,
new TelemetryResponseFactory(new ObjectMapperProvider().get()),
new DBTelemetryUserSettingsService(mongodb.mongoConnection(), mongoJackObjectMapperProvider),
new DBTelemetryUserSettingsService(new MongoCollections(mongoJackObjectMapperProvider, mongodb.mongoConnection())),
eventBus,
telemetryClusterService,
"unknown",
Expand Down

0 comments on commit 48b70c1

Please sign in to comment.