Search in sources :

Example 1 with RootMetaPacket

use of me.retrodaredevil.solarthing.type.closed.meta.RootMetaPacket in project solarthing by wildmountainfarms.

the class SimpleQueryHandler method queryMeta.

public MetaDatabase queryMeta() {
    final VersionedPacket<RootMetaPacket> metadata;
    synchronized (this) {
        final VersionedPacket<RootMetaPacket> currentCache = metadataCache;
        final Long lastMetadataCacheNanos = this.lastMetadataCacheNanos;
        if (lastMetadataCacheNanos != null && System.nanoTime() - lastMetadataCacheNanos < METADATA_CACHE_VALID.toNanos()) {
            requireNonNull(currentCache);
            metadata = currentCache;
        } else {
            UpdateToken updateToken = currentCache == null ? null : currentCache.getUpdateToken();
            final VersionedPacket<RootMetaPacket> newMetadata;
            try {
                newMetadata = database.queryMetadata(updateToken);
            } catch (NotFoundSolarThingDatabaseException e) {
                // If we have not defined metadata, then we return an "empty" instance
                return EmptyMetaDatabase.getInstance();
            } catch (SolarThingDatabaseException e) {
                throw new DatabaseException("Could not query meta", e);
            }
            this.lastMetadataCacheNanos = System.nanoTime();
            if (newMetadata == null) {
                requireNonNull(currentCache);
                metadata = currentCache;
            } else {
                metadataCache = newMetadata;
                metadata = newMetadata;
            }
        }
    }
    return new DefaultMetaDatabase(metadata.getPacket());
}
Also used : NotFoundSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.NotFoundSolarThingDatabaseException) RootMetaPacket(me.retrodaredevil.solarthing.type.closed.meta.RootMetaPacket) DefaultMetaDatabase(me.retrodaredevil.solarthing.type.closed.meta.DefaultMetaDatabase) DatabaseException(me.retrodaredevil.solarthing.rest.exceptions.DatabaseException) NotFoundSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.NotFoundSolarThingDatabaseException) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException) UpdateToken(me.retrodaredevil.solarthing.database.UpdateToken) NotFoundSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.NotFoundSolarThingDatabaseException) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException)

Example 2 with RootMetaPacket

use of me.retrodaredevil.solarthing.type.closed.meta.RootMetaPacket in project solarthing by wildmountainfarms.

the class CouchDbSolarThingDatabase method queryMetadata.

@Override
@Nullable
public VersionedPacket<RootMetaPacket> queryMetadata(UpdateToken updateToken) throws SolarThingDatabaseException {
    DocumentData data = queryDocument(closedDatabase, RootMetaPacket.DOCUMENT_ID, updateToken);
    if (data == null) {
        return null;
    }
    JsonData jsonData = data.getJsonData();
    try {
        RootMetaPacket packet = CouchDbJacksonUtil.readValue(metaObjectMapper, jsonData, RootMetaPacket.class);
        return new VersionedPacket<>(packet, new RevisionUpdateToken(data.getRevision()));
    } catch (JsonProcessingException e) {
        throw new SolarThingDatabaseException("Invalid meta! Failed to parse!", e);
    }
}
Also used : DocumentData(me.retrodaredevil.couchdbjava.response.DocumentData) VersionedPacket(me.retrodaredevil.solarthing.database.VersionedPacket) RootMetaPacket(me.retrodaredevil.solarthing.type.closed.meta.RootMetaPacket) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonData(me.retrodaredevil.couchdbjava.json.JsonData) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException) Nullable(me.retrodaredevil.solarthing.annotations.Nullable)

Aggregations

SolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException)2 RootMetaPacket (me.retrodaredevil.solarthing.type.closed.meta.RootMetaPacket)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonData (me.retrodaredevil.couchdbjava.json.JsonData)1 DocumentData (me.retrodaredevil.couchdbjava.response.DocumentData)1 Nullable (me.retrodaredevil.solarthing.annotations.Nullable)1 UpdateToken (me.retrodaredevil.solarthing.database.UpdateToken)1 VersionedPacket (me.retrodaredevil.solarthing.database.VersionedPacket)1 NotFoundSolarThingDatabaseException (me.retrodaredevil.solarthing.database.exception.NotFoundSolarThingDatabaseException)1 DatabaseException (me.retrodaredevil.solarthing.rest.exceptions.DatabaseException)1 DefaultMetaDatabase (me.retrodaredevil.solarthing.type.closed.meta.DefaultMetaDatabase)1