Search in sources :

Example 6 with ResourceMetadata

use of org.opennms.newts.cassandra.search.ResourceMetadata in project opennms by OpenNMS.

the class RedisResourceMetadataCache method merge.

@Override
public void merge(Context context, Resource resource, ResourceMetadata metadata) {
    final Optional<ResourceMetadata> o = get(context, resource);
    try (Jedis jedis = m_pool.getResource()) {
        if (!o.isPresent()) {
            final ResourceMetadata newMetadata = new ResourceMetadata();
            newMetadata.merge(metadata);
            final Transaction t = jedis.multi();
            final byte[] key = key(METADATA_PREFIX, context.getId(), resource.getId());
            t.set(key, conf.asByteArray(newMetadata));
            // Index the key, element by element, in order to support calls to getResourceIdsWithPrefix()
            final List<String> elements = Lists.newArrayList(SEARCH_PREFIX, context.getId());
            for (String el : m_resourceIdSplitter.splitIdIntoElements(resource.getId())) {
                elements.add(el);
                t.lpush(m_resourceIdSplitter.joinElementsToId(elements).getBytes(), key);
            }
            // Update the keys in transaction
            t.exec();
        } else if (o.get().merge(metadata)) {
            // Update the value stored in the cache if it was changed as a result of the merge
            jedis.set(key(METADATA_PREFIX, context.getId(), resource.getId()), conf.asByteArray(o.get()));
        }
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) Transaction(redis.clients.jedis.Transaction) ResourceMetadata(org.opennms.newts.cassandra.search.ResourceMetadata)

Aggregations

ResourceMetadata (org.opennms.newts.cassandra.search.ResourceMetadata)6 Test (org.junit.Test)4 Context (org.opennms.newts.api.Context)4 Resource (org.opennms.newts.api.Resource)4 EscapableResourceIdSplitter (org.opennms.newts.cassandra.search.EscapableResourceIdSplitter)2 Ignore (org.junit.Ignore)1 Jedis (redis.clients.jedis.Jedis)1 Transaction (redis.clients.jedis.Transaction)1