org.graylog2.database.jackson.legacy.LegacyInsertOneResult Maven / Gradle / Ivy
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* .
*/
package org.graylog2.database.jackson.legacy;
import com.mongodb.ReadPreference;
import com.mongodb.client.result.InsertOneResult;
import org.bson.BsonValue;
import org.mongojack.JacksonMongoCollection;
import org.mongojack.WriteResult;
/**
* Compatibility layer to support existing code interacting with the Mongojack 2.x API.
*
* @deprecated use {@link org.graylog2.database.MongoCollections} as an entrypoint for interacting with MongoDB.
*/
@Deprecated
public class LegacyInsertOneResult implements WriteResult {
private final JacksonMongoCollection collection;
private final InsertOneResult insertOneResult;
private final Class idType;
public LegacyInsertOneResult(JacksonMongoCollection collection, InsertOneResult insertOneResult,
Class idType) {
this.collection = collection.withReadPreference(ReadPreference.primary());
this.insertOneResult = insertOneResult;
this.idType = idType;
}
@Override
public T getSavedObject() {
final BsonValue insertedId = insertOneResult.getInsertedId();
if (insertedId == null) {
return null;
}
return collection.findOneById(insertedId);
}
@Override
public int getN() {
return 0;
}
@Override
public boolean wasAcknowledged() {
return insertOneResult.wasAcknowledged();
}
@Override
public K getSavedId() {
return WriteResult.toIdType(insertOneResult.getInsertedId(), idType);
}
@Override
public boolean isUpdateOfExisting() {
return false;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy