/** * Default Constructor. */ public FeatureStoreRedis(String xmlFeaturesfFile) { this(); importFeaturesFromXmlFile(xmlFeaturesfFile); }
/** {@inheritDoc} */ @Override public void removeFromGroup(String featureId, String groupName) { Util.assertParamHasLength(groupName, "groupName (#2)"); if (!existGroup(groupName)) { throw new GroupNotFoundException(groupName); } // retrieve Feature f = read(featureId); f.setGroup(null); // persist modification update(f); }
/** {@inheritDoc} */ public void delete(String fpId) { if (!exist(fpId)) { throw new FeatureNotFoundException(fpId); } Jedis jedis = null; try { jedis = getJedis(); // Store the feature in the mapping bucket. jedis.srem(KEY_FEATURE_MAP, fpId); jedis.del(KEY_FEATURE + fpId); } finally { if (jedis != null) { jedis.close(); } } }
/** {@inheritDoc} */ @Override public Map<String, Feature> readAll() { Jedis jedis = null; try { jedis = getJedis(); Set<String> features = jedis.smembers(KEY_FEATURE_MAP); Map<String, Feature> featuresMap = new HashMap<>(); if (features != null) { for (String key : features) { featuresMap.put(key, read(key)); } } return featuresMap; } finally { if (jedis != null) { jedis.close(); } } }
/** {@inheritDoc} */ @Override public Set<String> readAllGroups() { Map < String, Feature > features = readAll(); Set < String > groups = new HashSet<String>(); for (Map.Entry<String,Feature> uid : features.entrySet()) { groups.add(uid.getValue().getGroup()); } groups.remove(null); return groups; }
/** {@inheritDoc} */ public boolean exist(String uid) { Util.assertParamHasLength(uid, "Feature identifier"); Jedis jedis = null; try { jedis = getJedis(); return jedis.exists(KEY_FEATURE + uid); } finally { if (jedis != null) { jedis.close(); } } }
/** {@inheritDoc} */ @Override public boolean existGroup(String groupName) { Util.assertParamHasLength(groupName, "groupName"); Map < String, Feature > features = readAll(); Map < String, Feature > group = new HashMap<String, Feature>(); for (Map.Entry<String,Feature> uid : features.entrySet()) { if (groupName.equals(uid.getValue().getGroup())) { group.put(uid.getKey(), uid.getValue()); } } return !group.isEmpty(); }
/** {@inheritDoc} */ @Override public void update(Feature fp) { Util.assertNotNull("Feature" , fp); if (!exist(fp.getUid())) { throw new FeatureNotFoundException(fp.getUid()); } Jedis jedis = null; try { jedis = getJedis(); jedis.set(KEY_FEATURE + fp.getUid(), fp.toJson()); jedis.persist(KEY_FEATURE + fp.getUid()); } finally { if (jedis != null) { jedis.close(); } } }
/** * Contact remote redis server. * * @param host * target redis host * @param port * target redis port */ public FeatureStoreRedis(String host, int port, String xmlFeaturesfFile) { this(host, port); importFeaturesFromXmlFile(xmlFeaturesfFile); }
/** {@inheritDoc} */ @Override public Map<String, Feature> readGroup(String groupName) { Util.assertParamHasLength(groupName, "groupName"); Map < String, Feature > features = readAll(); Map < String, Feature > group = new HashMap<String, Feature>(); for (Map.Entry<String,Feature> uid : features.entrySet()) { if (groupName.equals(uid.getValue().getGroup())) { group.put(uid.getKey(), uid.getValue()); } } if (group.isEmpty()) { throw new GroupNotFoundException(groupName); } return group; }
/** {@inheritDoc} */ @Override public void create(Feature fp) { Util.assertNotNull("Feature", fp); if (exist(fp.getUid())) { throw new FeatureAlreadyExistException(fp.getUid()); } Jedis jedis = null; try { String id = fp.getUid(); jedis = getJedis(); // Store the feature in the mapping bucket. jedis.sadd(KEY_FEATURE_MAP, id); jedis.set(KEY_FEATURE + id, fp.toJson()); jedis.persist(KEY_FEATURE + id); } finally { if (jedis != null) { jedis.close(); } } }
/** * Contact remote redis server. * * @param host * target redis host * @param port * target redis port */ public FeatureStoreRedis(String host, int port, String password, String xmlFeaturesfFile) { this(new RedisConnection(host, port, password)); importFeaturesFromXmlFile(xmlFeaturesfFile); }