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

com.azure.cosmos.implementation.CosmosClientMetadataCachesSnapshot 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.60.0
Show newest version
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.cosmos.implementation;

import com.azure.cosmos.CosmosAsyncClient;
import com.azure.cosmos.CosmosBridgeInternal;
import com.azure.cosmos.implementation.caches.AsyncCache;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class CosmosClientMetadataCachesSnapshot implements Serializable {
    private static final long serialVersionUID = 1l;
    private static final int ERROR_CODE = 0;
    public byte[] collectionInfoByNameCache;
    public byte[] collectionInfoByIdCache;

    public CosmosClientMetadataCachesSnapshot() {
    }

    public void serialize(CosmosAsyncClient client) {
        RxDocumentClientImpl documentClient =
            (RxDocumentClientImpl) CosmosBridgeInternal.getAsyncDocumentClient(client);
        documentClient.serialize(this);
    }

    public void serializeCollectionInfoByNameCache(AsyncCache cache) {
        this.collectionInfoByNameCache = serializeAsyncCollectionCache(cache);
    }

    public void serializeCollectionInfoByIdCache(AsyncCache cache) {
        this.collectionInfoByIdCache = serializeAsyncCollectionCache(cache);
    }

    private byte[] serializeAsyncCollectionCache(AsyncCache cache) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(baos);
            objectOutputStream.writeObject(AsyncCache.SerializableAsyncCache.from(cache, String.class,
                DocumentCollection.class));

            objectOutputStream.close();
            return baos.toByteArray();
        } catch (IOException e) {
            throw CosmosBridgeInternal.cosmosException(ERROR_CODE, e);
        }
    }

    public AsyncCache getCollectionInfoByNameCache() {
        try {
            return ((AsyncCache.SerializableAsyncCache.SerializableAsyncCollectionCache)
                new ObjectInputStream(new ByteArrayInputStream(collectionInfoByNameCache)).readObject())
                .toAsyncCache();
        } catch (IOException | ClassNotFoundException e) {
            throw CosmosBridgeInternal.cosmosException(ERROR_CODE, e);
        }
    }

    public AsyncCache getCollectionInfoByIdCache() {
        try {
            return ((AsyncCache.SerializableAsyncCache.SerializableAsyncCollectionCache)
                new ObjectInputStream(new ByteArrayInputStream(collectionInfoByIdCache)).readObject())
                .toAsyncCache();
        } catch (IOException | ClassNotFoundException e) {
            throw CosmosBridgeInternal.cosmosException(ERROR_CODE, e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy