All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ai.grakn.redismock.commands.RO_keys Maven / Gradle / Ivy

package ai.grakn.redismock.commands;

import ai.grakn.redismock.RedisBase;
import ai.grakn.redismock.Response;
import ai.grakn.redismock.Slice;

import java.util.ArrayList;
import java.util.List;

class RO_keys extends AbstractRedisOperation {
    RO_keys(RedisBase base, List params) {
        super(base, params, 1, null, null);
    }

    Slice response() {
        List matchingKeys = new ArrayList<>();
        String regex = createRegexFromGlob(new String(params().get(0).data()));

        base().keys().forEach(keyData -> {
            String key = new String(keyData.data());
            if(key.matches(regex)){
                matchingKeys.add(Response.bulkString(keyData));
            }
        });

        return Response.array(matchingKeys);
    }

    private static String createRegexFromGlob(String glob)
    {
        StringBuilder out = new StringBuilder("^");
        for(int i = 0; i < glob.length(); ++i)
        {
            final char c = glob.charAt(i);
            switch(c)
            {
                case '*':
                    out.append(".*");
                    break;
                case '?':
                    out.append('.');
                    break;
                case '.':
                    out.append("\\.");
                    break;
                case '\\':
                    out.append("\\\\");
                    break;
                default:
                    out.append(c);
            }
        }
        out.append('$');
        return out.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy