redis.clients.jedis.util.SafeEncoder Java Examples

The following examples show how to use redis.clients.jedis.util.SafeEncoder. 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: ClusterClient.java    From JRedisBloom with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * TOPK.RESERVE key topk width depth decay
 *
 * Reserve a topk filter.
 * @param key The key of the filter
 * @param topk
 * @param width
 * @param depth
 * @param decay
 *
 * Note that if a filter is not reserved, a new one is created when {@link #add(String, byte[])}
 * is called.
 */
public void topkCreateFilter(String key, long topk, long width, long depth, double decay) {
    (new JedisClusterCommand<Void>(this.connectionHandler, this.maxAttempts){
        @Override
        public Void execute(Jedis jedis) {
            Connection conn = jedis.getClient();
            conn.sendCommand(TopKCommand.RESERVE, SafeEncoder.encode(key), Protocol.toByteArray(topk),
                    Protocol.toByteArray(width), Protocol.toByteArray(depth),Protocol.toByteArray(decay));
            String resp = conn.getStatusCodeReply();
            if (!resp.equals("OK")){
                throw new JedisException(resp);
            }
            return null;
        }
    }).run(key);
}
 
Example #2
Source File: ClientTest.java    From JRediSearch with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testMGet() throws Exception {
    Client cl = getClient();
    cl.createIndex(new Schema().addTextField("txt1", 1.0), Client.IndexOptions.defaultOptions());
    cl.addDocument(new Document("doc1").set("txt1", "Hello World!1"), new AddOptions());
    cl.addDocument(new Document("doc2").set("txt1", "Hello World!2"), new AddOptions());
    cl.addDocument(new Document("doc3").set("txt1", "Hello World!3"), new AddOptions());
   
    List<Document> docs = cl.getDocuments();
    assertEquals(0, docs.size());
    
    docs = cl.getDocuments("doc1", "doc3", "doc4");
    assertEquals(3, docs.size());
    assertEquals("Hello World!1", docs.get(0).get("txt1"));
    assertEquals("Hello World!3", docs.get(1).get("txt1"));
    assertNull(docs.get(2));

    // Test decode=false mode
    docs = cl.getDocuments(false, "doc2");
    assertEquals(1, docs.size());
    assertTrue(Arrays.equals( SafeEncoder.encode("Hello World!2"), (byte[])docs.get(0).get("txt1")));
}
 
Example #3
Source File: ContextedRedisGraph.java    From JRedisGraph with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Deletes the entire graph
 * @param graphId graph to delete
 * @return delete running time statistics
 */
@Override
public String deleteGraph(String graphId) {
    Jedis conn = getConnection();
    Object response;
    try {
        response = conn.sendCommand(RedisGraphCommand.DELETE, graphId);
    }
    catch (Exception e) {
        conn.close();
        throw e;
    }
    //clear local state
    caches.removeGraphCache(graphId);
    return SafeEncoder.encode((byte[]) response);
}
 
Example #4
Source File: ClientTest.java    From JRediSearch with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testGet() throws Exception {
    Client cl = getClient();
    cl.createIndex(new Schema().addTextField("txt1", 1.0), Client.IndexOptions.defaultOptions());
    cl.addDocument(new Document("doc1").set("txt1", "Hello World!"), new AddOptions());
    Document d = cl.getDocument("doc1");
    assertNotNull(d);
    assertEquals("Hello World!", d.get("txt1"));

    // Get something that does not exist. Shouldn't explode
    assertNull(cl.getDocument("nonexist"));
    
    // Test decode=false mode
    d = cl.getDocument("doc1", false);
    assertNotNull(d);
    assertTrue(Arrays.equals( SafeEncoder.encode("Hello World!"), (byte[])d.get("txt1")));
}
 
Example #5
Source File: JReJSON.java    From JRedisJSON with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Gets an object
 * @param key the key name
 * @param clazz 
 * @param paths optional one ore more paths in the object
 * @return the requested object
 */
public <T> T get(String key, Class<T> clazz, Path... paths) {
    byte[][] args = new byte[1 + paths.length][];
    int i=0;
    args[i] = SafeEncoder.encode(key);
    for (Path p :paths) {
    	args[++i] = SafeEncoder.encode(p.toString());
    }

    String rep;
	try (Jedis conn = getConnection()) {
		conn.getClient().sendCommand(Command.GET, args);
    	rep = conn.getClient().getBulkReply();
	}
	assertReplyNotError(rep);
	return gson.fromJson(rep, clazz);
}
 
Example #6
Source File: JReJSON.java    From JRedisJSON with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Sets an object
 * @param key the key name
 * @param object the Java object to store
 * @param flag an existential modifier
 * @param path in the object
 */
public void set(String key, Object object, ExistenceModifier flag, Path path) {

    List<byte[]> args = new ArrayList<>(4);

    args.add(SafeEncoder.encode(key));
    args.add(SafeEncoder.encode(path.toString()));
    args.add(SafeEncoder.encode(gson.toJson(object)));
    if (ExistenceModifier.DEFAULT != flag) {
        args.add(flag.getRaw());
    }

    String status;
	try (Jedis conn = getConnection()) {
     conn.getClient()
             .sendCommand(Command.SET, args.toArray(new byte[args.size()][]));
     status = conn.getClient().getStatusCodeReply();
	}
    assertReplyOK(status);
}
 
Example #7
Source File: Client.java    From JRediSearch with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Get runtime configuration option value
 *
 * @param option the name of the configuration option
 * @return
 */
@Override
public String getConfig(ConfigOption option) {

    try (Jedis conn = _conn()) {
        List<Object> objects = sendCommand(conn, commands.getConfigCommand(), 
            Keywords.GET.getRaw(), option.getRaw())
            .getObjectMultiBulkReply();
        if (objects != null && !objects.isEmpty()) {
            List<byte[]> kvs = (List<byte[]>) objects.get(0);
            byte[] val = kvs.get(1);
            return val == null ? null : SafeEncoder.encode(val);
        }
    }
    return null;
}
 
Example #8
Source File: JReJSON.java    From JRedisJSON with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Deletes a path
 * @param conn the Jedis connection
 * @param key the key name
 * @param path optional single path in the object, defaults to root
 * @return the number of paths deleted (0 or 1)
 * @deprecated use {@link #del(String, Path)} instead 
 */
@Deprecated
public static Long del(Jedis conn, String key, Path... path) {

    List<byte[]> args = new ArrayList<>(2);

    args.add(SafeEncoder.encode(key));
    args.add(SafeEncoder.encode(getSingleOptionalPath(path).toString()));

    conn.getClient()
                .sendCommand(Command.DEL, args.toArray(new byte[args.size()][]));
    Long rep = conn.getClient().getIntegerReply();
    conn.close();

    return rep;
}
 
Example #9
Source File: JReJSON.java    From JRedisJSON with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Gets an object
 * @param conn the Jedis connection
 * @param key the key name
 * @param paths optional one ore more paths in the object, defaults to root
 * @return the requested object
 * @deprecated use {@link #get(String, Path...)} instead
 */
@Deprecated
public static Object get(Jedis conn, String key, Path... paths) {

    List<byte[]> args = new ArrayList<>(2);

    args.add(SafeEncoder.encode(key));
    for (Path p :paths) {
        args.add(SafeEncoder.encode(p.toString()));
    }

    conn.getClient()
            .sendCommand(Command.GET, args.toArray(new byte[args.size()][]));
    String rep = conn.getClient().getBulkReply();
    conn.close();

    assertReplyNotError(rep);
    return gson.fromJson(rep, Object.class);
}
 
Example #10
Source File: JReJSON.java    From JRedisJSON with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Sets an object
 * @param conn the Jedis connection
 * @param key the key name
 * @param object the Java object to store
 * @param flag an existential modifier
 * @param path optional single path in the object, defaults to root
 * @deprecated use {@link #set(String, Object, ExistenceModifier, Path)} instead
 */
@Deprecated
public static void set(Jedis conn, String key, Object object, ExistenceModifier flag, Path... path) {

    List<byte[]> args = new ArrayList<>(4);

    args.add(SafeEncoder.encode(key));
    args.add(SafeEncoder.encode(getSingleOptionalPath(path).toString()));
    args.add(SafeEncoder.encode(gson.toJson(object)));
    if (ExistenceModifier.DEFAULT != flag) {
        args.add(flag.getRaw());
    }

    conn.getClient()
            .sendCommand(Command.SET, args.toArray(new byte[args.size()][]));
    String status = conn.getClient().getStatusCodeReply();
    conn.close();

    assertReplyOK(status);
}
 
Example #11
Source File: Client.java    From JRediSearch with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Add a batch of documents to the index
 * @param options Options for the operation
 * @param docs The documents to add
 * @return true on success for each document 
 */
@Override
public boolean[] addDocuments(AddOptions options, Document... docs){
	try (Jedis conn = _conn()) {
 	for(Document doc : docs) {
 		addDocument(doc, options, conn);
 	}
 	List<Object> objects = conn.getClient().getMany(docs.length);
 	boolean[] results = new boolean[docs.length];
 	int i=0;
 	for(Object obj : objects) {
 		results[i++] = !(obj instanceof JedisDataException) && 
 		SafeEncoder.encode((byte[]) obj).equals("OK");
 	}
 	return results;
	}
}
 
Example #12
Source File: Client.java    From JRediSearch with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Get all configuration options, consisting of the option's name and current value
 *
 * @return
 */
@Override
public Map<String, String> getAllConfig() {
    try (Jedis conn = _conn()) {
        List<Object> objects = sendCommand(conn, commands.getConfigCommand(), 
            Keywords.GET.getRaw(), ConfigOption.ALL.getRaw())
            .getObjectMultiBulkReply();
        
        Map<String, String> configs = new HashMap<>(objects.size());
        for (Object object : objects) {
            List<byte[]> kvs = (List<byte[]>) object;
            byte[] val = kvs.get(1);
            configs.put(SafeEncoder.encode(kvs.get(0)), val == null ? null : SafeEncoder.encode(val));
        }
        return configs;
    }
}
 
Example #13
Source File: Client.java    From JRediSearch with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Get a documents from the index
 *
 * @param docIds The document IDs to retrieve
 * @param decode <code>false</code> - keeps the fields value as byte[] 
 * @return The document as stored in the index. If the document does not exist, null is returned.
 */
@Override
public List<Document> getDocuments(boolean decode, String ...docIds) {
    int len = docIds.length;
    if(len == 0) {
      return new ArrayList<>(0);
    }
  
    byte[][] args = new byte[docIds.length+1][];
    args[0] = endocdedIndexName;
    for(int i=0 ; i<len ; ++i) {
      args[i+1] = SafeEncoder.encode(docIds[i]); 
    }
    
    List<Document> documents = new ArrayList<>(len); 
    try (Jedis conn = _conn()) {
        List<Object> res = sendCommand(conn, commands.getMGetCommand(), args).getObjectMultiBulkReply();
        for(int i=0; i<len; ++i) {
          List<Object> line = (List<Object>)res.get(i);
          if (line == null) {
            documents.add(null);
          } else {
            Document doc = new Document(docIds[i]);
            handleListMapping(line, doc::set, decode);
            documents.add(doc);
          }
        }            
        return documents;
    }
}
 
Example #14
Source File: Client.java    From JRediSearch with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Create a new client to a RediSearch index
 *
 * @param indexName the name of the index we are connecting to or creating
 * @param pool jedis connection pool to be used
 */
public Client(String indexName, Pool<Jedis> pool) {
  this.indexName = indexName;
  this.endocdedIndexName = SafeEncoder.encode(indexName);
  this.pool = pool;
  this.commands = new Commands.SingleNodeCommands();
}
 
Example #15
Source File: Client.java    From JRediSearch with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static void handleListMapping(List<Object> items, KVHandler handler, boolean decode) {
    for (int i = 0; i < items.size(); i += 2) {
        String key = SafeEncoder.encode((byte[]) items.get(i));
        Object val = items.get(i + 1);
        if (decode && val instanceof byte[]) {
            val = SafeEncoder.encode((byte[]) val);
        }
        handler.apply(key, val);
    }
}
 
Example #16
Source File: Client.java    From JRediSearch with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Set runtime configuration option
 *
 * @param option the name of the configuration option
 * @param value a value for the configuration option
 * @return
 */
@Override
public boolean setConfig(ConfigOption option, String value) {
    try (Jedis conn = _conn()) {
        String rep = sendCommand(conn, commands.getConfigCommand(), Keywords.SET.getRaw(),
            option.getRaw(), SafeEncoder.encode(value))
            .getStatusCodeReply();
        return rep.equals("OK");
    }
}
 
Example #17
Source File: ResultSetImpl.java    From JRedisGraph with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param rawScalarData - a list of object. list[0] is the scalar type, list[1] is the scalar value
 * @return value of the specific scalar type
 */
private Object deserializeScalar(List<Object> rawScalarData) {
    ResultSetScalarTypes type = getValueTypeFromObject(rawScalarData.get(0));

    Object obj = rawScalarData.get(1);
    switch (type) {
        case VALUE_NULL:
            return null;
        case VALUE_BOOLEAN:
            return Boolean.parseBoolean(SafeEncoder.encode((byte[]) obj));
        case VALUE_DOUBLE:
            return Double.parseDouble(SafeEncoder.encode((byte[]) obj));
        case VALUE_INTEGER:
            return (Long) obj;
        case VALUE_STRING:
            return SafeEncoder.encode((byte[]) obj);
        case VALUE_ARRAY:
            return deserializeArray(obj);
        case VALUE_NODE:
            return deserializeNode((List<Object>) obj);
        case VALUE_EDGE:
            return deserializeEdge((List<Object>) obj);
        case VALUE_PATH:
            return deserializePath(obj);
        case VALUE_UNKNOWN:
        default:
            return obj;
    }
}
 
Example #18
Source File: Client.java    From JRediSearch with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public Map<String, List<Long>> dumpSynonym() {
  try (Jedis conn = _conn()) {
    List<Object> res = sendCommand(conn, commands.getSynDumpCommand(), this.indexName).getObjectMultiBulkReply();
    
    Map<String, List<Long>> dump = new HashMap<>(res.size()/2);
    for(int i=0; i<res.size(); i+=2) {
        dump.put(SafeEncoder.encode((byte[])res.get(i)), (List<Long>)res.get(i+1));
    }
    return dump;
  }
}
 
Example #19
Source File: ClientTest.java    From JRediSearch with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void testAlterAdd() throws Exception {
    Client cl = getClient();
    cl._conn().flushDB();

    Schema sc = new Schema().addTextField("title", 1.0);

    assertTrue(cl.createIndex(sc, Client.IndexOptions.defaultOptions()));
    Map<String, Object> fields = new HashMap<>();
    fields.put("title", "hello world");
    for (int i = 0; i < 100; i++) {
        assertTrue(cl.addDocument(String.format("doc%d", i), fields));
    }
    
    SearchResult res = cl.search(new Query("hello world"));
    assertEquals(100, res.totalResults);
    
    assertTrue(cl.alterIndex(new TagField("tags", ","), new TextField("name", 0.5)));
    for (int i = 0; i < 100; i++) {
      Map<String, Object> fields2 = new HashMap<>();
      fields2.put("name", "name" + i);
      fields2.put("tags", String.format("tagA,tagB,tag%d", i));
      assertTrue(cl.updateDocument(String.format("doc%d", i), 1.0, fields2));
    }
    SearchResult res2 = cl.search(new Query("@tags:{tagA}"));
    assertEquals(100, res2.totalResults);        
    
    Map<String, Object> info = cl.getInfo();
    assertEquals(TEST_INDEX, info.get("index_name"));
    assertEquals("tags", SafeEncoder.encode(((List<List<byte[]>>)info.get("fields")).get(1).get(0)));
    assertEquals("TAG", SafeEncoder.encode(((List<List<byte[]>>)info.get("fields")).get(1).get(2)));
}
 
Example #20
Source File: ClusterClient.java    From JRedisBloom with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * TOPK.INCRBY key item increment [item increment ...]
 *
 * Adds an item to the filter
 * @param key The key of the filter
 * @param item The item to increment
 * @return item dropped from the list.
 */
public String topkIncrBy(String key, String item, long increment) {
    return (new JedisClusterCommand<String>(this.connectionHandler, this.maxAttempts){
        @Override
        public String execute(Jedis jedis) {
            Connection conn = jedis.getClient();
            conn.sendCommand(TopKCommand.INCRBY, SafeEncoder.encode(key), SafeEncoder.encode(item), Protocol.toByteArray(increment));
            return conn.getMultiBulkReply().get(0);
        }
    }).run(key);
}
 
Example #21
Source File: Document.java    From JRediSearch with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static Document load(String id, Double score, byte[] payload, List<byte[]> fields, boolean decode) {
    Document ret = new Document(id, score);
    ret.payload = payload;
    if (fields != null) {
        for (int i = 0; i < fields.size(); i += 2) {
            ret.set(SafeEncoder.encode(fields.get(i)), decode ? SafeEncoder.encode(fields.get(i + 1)) : fields.get(i + 1));
        }
    }
    return ret;
}
 
Example #22
Source File: RedisGraph.java    From JRedisGraph with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Deletes the entire graph
 * @param graphId graph to delete
 * @return delete running time statistics
 */
@Override
public String deleteGraph(String graphId) {
    try (Jedis conn = getConnection()) {
        Object response = conn.sendCommand(RedisGraphCommand.DELETE, graphId);
        //clear local state
        caches.removeGraphCache(graphId);
        return SafeEncoder.encode((byte[]) response);
    }
}
 
Example #23
Source File: HeaderImpl.java    From JRedisGraph with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Extracts schema names and types from the raw representation
 */
private void buildSchema() {
    for (List<Object> tuple : this.raw) {

        //get type
        ResultSetColumnTypes type = ResultSetColumnTypes.values()[((Long) tuple.get(0)).intValue()];
        //get text
        String text = SafeEncoder.encode((byte[]) tuple.get(1));
        if (type != null) {
            schemaTypes.add(type);
            schemaNames.add(text);
        }
    }
}
 
Example #24
Source File: StatisticsImpl.java    From JRedisGraph with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Map<Statistics.Label, String> getStatistics(){
	if(statistics.size() == 0) {		
		for(byte[]  tuple :  this.raw) {
		    String text = SafeEncoder.encode(tuple);
			String[] rowTuple = text.split(":");
			if(rowTuple.length == 2) {
			  Statistics.Label label = Statistics.Label.getEnum(rowTuple[0]);
			  if(label != null) {
			    this.statistics.put( label, rowTuple[1].trim());
			  }
			} 
		}
	}
	return statistics;
}
 
Example #25
Source File: JReJSON.java    From JRedisJSON with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Deletes a path
 * @param key the key name
 * @param path optional single path in the object, defaults to root
 * @return path deleted
 */
public Long del(String key, Path path) {
	byte[][] args = new byte[2][];
	args[0] = SafeEncoder.encode(key);
	args[1] = SafeEncoder.encode(path.toString());

	try (Jedis conn = getConnection()) {
		conn.getClient().sendCommand(Command.DEL, args);
		return conn.getClient().getIntegerReply();
	}
}
 
Example #26
Source File: JReJSON.java    From JRedisJSON with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Gets the class of an object
 * @param key the key name
 * @param path a path in the object
 * @return the Java class of the requested object
 */
public Class<?> type(String key, Path path) {

    List<byte[]> args = new ArrayList<>(2);

    args.add(SafeEncoder.encode(key));
    args.add(SafeEncoder.encode(path.toString()));

    String rep;
	try (Jedis conn = getConnection()) {
		conn.getClient()
            .sendCommand(Command.TYPE, args.toArray(new byte[args.size()][]));
    	rep = conn.getClient().getBulkReply();
	}

    assertReplyNotError(rep);

    switch (rep) {
        case "null":
            return null;
        case "boolean":
            return boolean.class;
        case "integer":
            return int.class;
        case "number":
            return float.class;
        case "string":
            return String.class;
        case "object":
            return Object.class;
        case "array":
            return List.class;
        default:
            throw new java.lang.RuntimeException(rep);
    }
}
 
Example #27
Source File: JReJSON.java    From JRedisJSON with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Gets the class of an object
 * @param conn the Jedis connection
 * @param key the key name
 * @param path optional single path in the object, defaults to root
 * @return the Java class of the requested object
 * @deprecated use {@link #type(String, Path)} instead
 */
@Deprecated
public static Class<?> type(Jedis conn, String key, Path... path) {

    List<byte[]> args = new ArrayList<>(2);

    args.add(SafeEncoder.encode(key));
    args.add(SafeEncoder.encode(getSingleOptionalPath(path).toString()));

    conn.getClient()
            .sendCommand(Command.TYPE, args.toArray(new byte[args.size()][]));
    String rep = conn.getClient().getBulkReply();
    conn.close();

    assertReplyNotError(rep);

    switch (rep) {
        case "null":
            return null;
        case "boolean":
            return boolean.class;
        case "integer":
            return int.class;
        case "number":
            return float.class;
        case "string":
            return String.class;
        case "object":
            return Object.class;
        case "array":
            return List.class;
        default:
            throw new java.lang.RuntimeException(rep);
    }
}
 
Example #28
Source File: Document.java    From JRediSearch with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * return the property value inside a key
 *
 * @param key key of the property
 * 
 * @return the property value 
 */
public String getString(String key) {
    Object value = properties.get(key);
    if(value instanceof String) {
      return (String)value;
    }
    return value instanceof byte[] ? SafeEncoder.encode((byte[])value) : value.toString();
}
 
Example #29
Source File: Query.java    From JRediSearch with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private byte[] formatNum(double num, boolean exclude) {
    if (num == Double.POSITIVE_INFINITY) { 
        return Keywords.POSITIVE_INFINITY.getRaw();
    }
    if (num == Double.NEGATIVE_INFINITY) {
      return Keywords.NEGATIVE_INFINITY.getRaw();
    }
    
    return exclude ?  SafeEncoder.encode("(" + num)  : Protocol.toByteArray(num);
}
 
Example #30
Source File: Query.java    From JRediSearch with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void serializeRedisArgs(List<byte[]> args) {
    args.add(Keywords.FILTER.getRaw());
    args.add(SafeEncoder.encode(property));
    args.add(formatNum(min, exclusiveMin));
    args.add(formatNum(max, exclusiveMax));
}