Search in sources :

Example 1 with UpdateDescription

use of io.realm.mongodb.mongo.events.UpdateDescription in project realm-java by realm.

the class ChangeEvent method toBsonDocument.

/**
 * Serializes this change event into a {@link BsonDocument}.
 *
 * @return the serialized document.
 */
@Override
public BsonDocument toBsonDocument() {
    final BsonDocument asDoc = new BsonDocument();
    asDoc.put(Fields.ID_FIELD, id);
    asDoc.put(Fields.OPERATION_TYPE_FIELD, new BsonString(toRemote(getOperationType())));
    final BsonDocument nsDoc = new BsonDocument();
    nsDoc.put(Fields.NS_DB_FIELD, new BsonString(ns.getDatabaseName()));
    nsDoc.put(Fields.NS_COLL_FIELD, new BsonString(getNamespace().getCollectionName()));
    asDoc.put(Fields.NS_FIELD, nsDoc);
    asDoc.put(Fields.DOCUMENT_KEY_FIELD, getDocumentKey());
    DocumentT fullDocument = getFullDocument();
    if ((fullDocument instanceof BsonValue) && ((BsonValue) fullDocument).isDocument()) {
        asDoc.put(Fields.FULL_DOCUMENT_FIELD, (BsonValue) fullDocument);
    }
    UpdateDescription updateDescription = getUpdateDescription();
    if (updateDescription != null) {
        asDoc.put(Fields.UPDATE_DESCRIPTION_FIELD, updateDescription.toBsonDocument());
    }
    asDoc.put(Fields.WRITE_PENDING_FIELD, new BsonBoolean(hasUncommittedWrites()));
    return asDoc;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) UpdateDescription(io.realm.mongodb.mongo.events.UpdateDescription) BsonBoolean(org.bson.BsonBoolean) BsonValue(org.bson.BsonValue)

Example 2 with UpdateDescription

use of io.realm.mongodb.mongo.events.UpdateDescription in project realm-java by realm.

the class ChangeEvent method fromBsonDocument.

/**
 * Deserializes a {@link BsonDocument} into an instance of change event.
 *
 * @param document the serialized document
 * @return the deserialized change event
 */
static <T> ChangeEvent<T> fromBsonDocument(final BsonDocument document, final Class<T> documentClass, CodecRegistry codecRegistry) {
    try {
        checkContainsKey(Fields.ID_FIELD, document, "document");
        checkContainsKey(Fields.OPERATION_TYPE_FIELD, document, "document");
        checkContainsKey(Fields.NS_FIELD, document, "document");
        checkContainsKey(Fields.DOCUMENT_KEY_FIELD, document, "document");
    } catch (IllegalArgumentException exception) {
        throw new AppException(ErrorCode.EVENT_DESERIALIZING, exception);
    }
    final BsonDocument nsDoc = document.getDocument(Fields.NS_FIELD);
    final UpdateDescription updateDescription;
    if (document.containsKey(Fields.UPDATE_DESCRIPTION_FIELD)) {
        updateDescription = UpdateDescription.fromBsonDocument(document.getDocument(Fields.UPDATE_DESCRIPTION_FIELD));
    } else {
        updateDescription = null;
    }
    final T fullDocument;
    if (document.containsKey(Fields.FULL_DOCUMENT_FIELD)) {
        final BsonValue fdVal = document.get(Fields.FULL_DOCUMENT_FIELD);
        if (fdVal.isDocument()) {
            fullDocument = codecRegistry.get(documentClass).decode(fdVal.asDocument().asBsonReader(), DecoderContext.builder().build());
        } else {
            fullDocument = null;
        }
    } else {
        fullDocument = null;
    }
    return new ChangeEvent<>(document.getDocument(Fields.ID_FIELD), fromRemote(document.getString(Fields.OPERATION_TYPE_FIELD).getValue()), fullDocument, new MongoNamespace(nsDoc.getString(Fields.NS_DB_FIELD).getValue(), nsDoc.getString(Fields.NS_COLL_FIELD).getValue()), document.getDocument(Fields.DOCUMENT_KEY_FIELD), updateDescription, document.getBoolean(Fields.WRITE_PENDING_FIELD, BsonBoolean.FALSE).getValue());
}
Also used : AppException(io.realm.mongodb.AppException) BsonDocument(org.bson.BsonDocument) INSERT(io.realm.mongodb.mongo.events.BaseChangeEvent.OperationType.INSERT) BaseChangeEvent(io.realm.mongodb.mongo.events.BaseChangeEvent) UpdateDescription(io.realm.mongodb.mongo.events.UpdateDescription) MongoNamespace(io.realm.mongodb.mongo.MongoNamespace) BsonValue(org.bson.BsonValue)

Aggregations

UpdateDescription (io.realm.mongodb.mongo.events.UpdateDescription)2 BsonDocument (org.bson.BsonDocument)2 BsonValue (org.bson.BsonValue)2 AppException (io.realm.mongodb.AppException)1 MongoNamespace (io.realm.mongodb.mongo.MongoNamespace)1 BaseChangeEvent (io.realm.mongodb.mongo.events.BaseChangeEvent)1 INSERT (io.realm.mongodb.mongo.events.BaseChangeEvent.OperationType.INSERT)1 BsonBoolean (org.bson.BsonBoolean)1 BsonString (org.bson.BsonString)1