Java Code Examples for net.sf.ehcache.search.Results#discard()

The following examples show how to use net.sf.ehcache.search.Results#discard() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeviceCacheImpl.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public List<Device> getByDeviceClassId(Long deviceClassId) {
  List<Device> deviceCacheObjects = new ArrayList<>();

  Results results = null;

  try {
    Query query = getCache().createQuery();
    Attribute<Long> id = getCache().getSearchAttribute("deviceClassId");
    results = query.includeKeys().includeValues().addCriteria(id.eq(deviceClassId)).execute();

    if (results.size() == 0) {
      throw new CacheElementNotFoundException("Failed to get device ids from cache");
    }

    results.all().forEach((result) -> deviceCacheObjects.add((DeviceCacheObject) result.getValue()));
  } finally {
    if (results != null) {
      results.discard();
    }
  }

  return deviceCacheObjects;
}
 
Example 2
Source File: DeviceClassCacheImpl.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Long getDeviceClassIdByName(String deviceClassName) {
  Long deviceClassId;

  if (deviceClassName == null || deviceClassName.equalsIgnoreCase("")) {
    throw new IllegalArgumentException("Attempting to retrieve a DeviceClass from the cache with a NULL or empty name parameter.");
  }

  Results results = null;
  try {
    Attribute<String> className = getCache().getSearchAttribute("deviceClassName");
    Query query = getCache().createQuery();
    results = query.includeKeys().addCriteria(className.eq(deviceClassName)).maxResults(1).execute();

    if (results.size() == 0) {
      throw new CacheElementNotFoundException("Failed to find a device class with name " + deviceClassName + " in the cache.");
    }

    deviceClassId = (long) results.all().get(0).getKey();
  } finally {
    if (results != null) {
      results.discard();
    }
  }

  return deviceClassId;
}
 
Example 3
Source File: AbstractTagCache.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public boolean hasTagWithName(String name) {
  if (name == null || name.equalsIgnoreCase("")) {
    throw new IllegalArgumentException("Attempting to retrieve a Tag from the cache with a NULL or empty name parameter.");
  }

  // This will prevent wildcard searches
  if (name.contains("*")) {
    name = name.replace("*", "\\*");
  }
  if (name.contains("?")) {
    name = name.replace("?", "\\?");
  }

  Results results = null;

  try {
    Ehcache ehcache = getCache();
    Attribute<String> tagName = ehcache.getSearchAttribute("tagName");

    Query query = ehcache.createQuery();
    results = query.includeKeys().addCriteria(tagName.ilike(name)).maxResults(1).execute();

    return results.hasKeys();
  }
  finally {
    if (results != null) {
      // Discard the results when done to free up cache resources.
      results.discard();
    }
  }

}
 
Example 4
Source File: DataTagCacheImpl.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Receives a list of all DataTag ids which are attached to the given equipment or sub-equipment.
 * @param id The id of the (sub-)equipment
 * @param searchAttribute The ehcache search attribute, which is specified in the wrapper method
 * @return A list of all DataTag ids belonging to the given (sub-)equipment
 */
private List<Long> getDataTagIds(Long id, String searchAttribute) {
  List<Long> tagIds = new LinkedList<>();
  Results results = null;

  if (id == null) {
    throw new IllegalArgumentException("Attempting to retrieve a List of DataTag ids from the cache with a NULL " +
        "parameter.");
  }

  try {
    Attribute<Long> cacheEquipmentId = getCache().getSearchAttribute(searchAttribute);
    results = getCache().createQuery().includeKeys().addCriteria(cacheEquipmentId.eq(id)).execute();

    if (results == null) {
      throw new CacheElementNotFoundException("Failed to execute query with (sub)EquipmentId " + id + " : " +
          "Result is null.");
    }

    results.all().forEach(r -> tagIds.add((Long) r.getKey()));

  } finally {
    if (results != null) {
      // Discard the results when done to free up cache resources.
      results.discard();
    }
  }
  return tagIds;
}
 
Example 5
Source File: ProcessCacheImpl.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Long getProcessId(final String name) {
  Long processKey = null;
  Results results = null;

  if (name == null || name.equalsIgnoreCase("")) {
    throw new IllegalArgumentException("Attempting to retrieve a Process from the cache with a NULL or empty name parameter.");
  }

  try {
    Attribute<String> processName = getCache().getSearchAttribute("processName");
    // By limiting the query result list to 1 it is up to the administrator to
    // make
    // sure that the process name is unique. Otherwise this will result in an
    // unpredictable behaviour.
    Query query = getCache().createQuery();
    results = query.includeKeys().addCriteria(processName.eq(name)).maxResults(1).execute();

    // Find the number of results -- the number of hits.
    int size = results.size();
    if (size == 0) {
      throw new CacheElementNotFoundException("Failed to find a process with name " + name + " in the cache.");
    }

    processKey = (Long) results.all().get(0).getKey();
  }
  finally {
    if (results != null) {
      // Discard the results when done to free up cache resources.
      results.discard();
    }
  }

  return processKey;
}
 
Example 6
Source File: CommandTagCacheImpl.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Long getCommandTagId(final String name) {
  Long commandTagKey = null;
  Results results = null;

  if (name == null || name.equalsIgnoreCase("")) {
    throw new IllegalArgumentException("Attempting to retrieve a CommandTag from the cache with a NULL or empty name parameter.");
  }

  try {
    Attribute<String> commandTagName = getCache().getSearchAttribute("commandTagName");
    Query query = getCache().createQuery();
    results = query.includeKeys().addCriteria(commandTagName.eq(name)).maxResults(1).execute();

    // Find the number of results -- the number of hits.
    int size = results.size();
    if (size == 0) {
      log.info("Failed to find a command tag with name " + name + " in the cache.");
    }

    commandTagKey = results.all().size() > 0 ? (Long) results.all().get(0).getKey() : null;
  }
  finally {
    if (results != null) {
      // Discard the results when done to free up cache resources.
      results.discard();
    }
  }

  return commandTagKey;
}