All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.azure.cosmos.implementation.Document Maven / Gradle / Ivy

Go to download

This Package contains Microsoft Azure Cosmos SDK (with Reactive Extension Reactor support) for Azure Cosmos DB SQL API

There is a newer version: 4.63.3
Show newest version
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.cosmos.implementation;

import com.azure.cosmos.CosmosItemSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.Map;

/**
 * Represents a document in the Azure Cosmos DB database service.
 * 

* A document is a structured JSON document. There is no set schema for the JSON documents, and a document may contain * any number of custom properties as well as an optional list of attachments. Document is an application resource and * can be authorized using the master key or resource keys. */ public class Document extends Resource { private final static ObjectMapper OBJECT_MAPPER = Utils.getSimpleObjectMapper(); private final static ImplementationBridgeHelpers.CosmosItemSerializerHelper.CosmosItemSerializerAccessor itemSerializerAccessor = ImplementationBridgeHelpers.CosmosItemSerializerHelper.getCosmosItemSerializerAccessor(); /** * Constructor. * * @param objectNode the {@link ObjectNode} that represent the * {@link JsonSerializable} */ public Document(ObjectNode objectNode) { super(objectNode); } /** * Constructor. * * @param content byte[] encoding of the json string that represents the resource. */ public Document(byte[] content) { super(content); } /** * Initialize a document object. */ public Document() { super(); } /** * Sets the id * @param id the name of the resource. * @return the current instance of the Document */ public Document setId(String id){ super.setId(id); return this; } /** * Initialize a document object from json string. * * @param jsonString the json string that represents the document object. */ public Document(String jsonString) { super(jsonString); } public static Document fromObject(Object document, CosmosItemSerializer itemSerializer) { Document typedDocument; if (document instanceof Document) { typedDocument = (Document) document; } else { Map jsonTreeMap = itemSerializerAccessor.serializeSafe(itemSerializer, document); ObjectNode objectNode = null; if (jsonTreeMap instanceof ObjectNodeMap) { objectNode = ((ObjectNodeMap)jsonTreeMap).getObjectNode(); } else { objectNode = OBJECT_MAPPER.convertValue(jsonTreeMap, ObjectNode.class); } return new Document(objectNode); } return typedDocument; } /** * Gets the document's time-to-live value. * * @return the document's time-to-live value in seconds. */ public Integer getTimeToLive() { if (super.has(Constants.Properties.TTL)) { return super.getInt(Constants.Properties.TTL); } return null; } /** * Sets the document's time-to-live value. *

* A document's time-to-live value is an optional property. If set, the document expires after the specified number * of seconds since its last write time. The value of this property should be one of the following: *

* null - indicates the time-to-live value for this document inherits from the parent collection's default time-to-live value. *

* nonzero positive integer - indicates the number of seconds before the document expires. It overrides the default time-to-live * value specified on the parent collection, unless the parent collection's default time-to-live is null. *

* -1 - indicates the document never expires. It overrides the default time-to-live * value specified on the parent collection, unless the parent collection's default time-to-live is null. * * @param timeToLive the document's time-to-live value in seconds. */ public void setTimeToLive(Integer timeToLive) { // a "null" value is represented as a missing element on the wire. // setting timeToLive to null should remove the property from the property bag. if (timeToLive != null) { this.set(Constants.Properties.TTL, timeToLive, CosmosItemSerializer.DEFAULT_SERIALIZER); } else if (super.has(Constants.Properties.TTL)) { this.remove(Constants.Properties.TTL); } } @Override public String toJson() { return super.toJson(); } @Override public String getString(String propertyName) { return super.getString(propertyName); } @Override public Integer getInt(String propertyName) { return super.getInt(propertyName); } @Override public Object get(String propertyName) { return super.get(propertyName); } @Override public boolean equals(Object o) { return super.equals(o); } @Override public int hashCode() { return super.hashCode(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy