|
| 1 | +package redis.clients.jedis; |
| 2 | + |
| 3 | +import redis.clients.jedis.args.Rawable; |
| 4 | +import redis.clients.jedis.commands.ProtocolCommand; |
| 5 | +import redis.clients.jedis.util.JedisByteMap; |
| 6 | +import redis.clients.jedis.util.SafeEncoder; |
| 7 | + |
| 8 | +import java.util.EnumSet; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +/** |
| 12 | + * Static implementation of CommandFlagsRegistry. |
| 13 | + */ |
| 14 | +public class StaticCommandFlagsRegistry implements CommandFlagsRegistry { |
| 15 | + |
| 16 | + // Empty flags constant for commands with no flags |
| 17 | + public static final EnumSet<CommandFlag> EMPTY_FLAGS = EnumSet.noneOf(CommandFlag.class); |
| 18 | + |
| 19 | + // Singleton instance |
| 20 | + private static final StaticCommandFlagsRegistry REGISTRY = createRegistry(); |
| 21 | + |
| 22 | + private final Commands commands; |
| 23 | + |
| 24 | + private StaticCommandFlagsRegistry(Commands commands) { |
| 25 | + this.commands = commands; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Get the singleton instance of the static command flags registry. |
| 30 | + * <p> |
| 31 | + * DO NOT USE THIS METHOD UNLESS YOU KNOW WHAT YOU ARE DOING. |
| 32 | + * </p> |
| 33 | + * @return StaticCommandFlagsRegistry |
| 34 | + */ |
| 35 | + public static StaticCommandFlagsRegistry registry() { |
| 36 | + return REGISTRY; |
| 37 | + } |
| 38 | + |
| 39 | + private static StaticCommandFlagsRegistry createRegistry() { |
| 40 | + |
| 41 | + Builder builder = new Builder(); |
| 42 | + |
| 43 | + // Delegate population to generated class |
| 44 | + StaticCommandFlagsRegistryInitializer.initialize(builder); |
| 45 | + |
| 46 | + return builder.build(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Get the flags for a given command. Flags are looked up from a static registry based on the |
| 51 | + * command arguments. This approach significantly reduces memory usage by sharing flag instances |
| 52 | + * across all CommandObject instances. |
| 53 | + * <p> |
| 54 | + * For commands with subcommands (e.g., FUNCTION LOAD, ACL SETUSER), this method implements a |
| 55 | + * hierarchical lookup strategy: |
| 56 | + * <ol> |
| 57 | + * <li>First, retrieve the parent command using CommandArguments.getCommand()</li> |
| 58 | + * <li>Check if this is a parent command (has subcommands in the registry)</li> |
| 59 | + * <li>If it is a parent command, attempt to get the child/subcommand by: |
| 60 | + * <ul> |
| 61 | + * <li>Extracting the second argument from the CommandArguments object</li> |
| 62 | + * <li>Matching this second argument against the child items of the parent command</li> |
| 63 | + * </ul> |
| 64 | + * </li> |
| 65 | + * <li>Return the appropriate flags based on whether a child command was found or just use the |
| 66 | + * parent command's flags</li> |
| 67 | + * </ol> |
| 68 | + * @param commandArguments the command arguments containing the command and its parameters |
| 69 | + * @return EnumSet of CommandFlag for this command, or empty set if command has no flags |
| 70 | + */ |
| 71 | + @Override |
| 72 | + public EnumSet<CommandFlag> getFlags(CommandArguments commandArguments) { |
| 73 | + // Get the parent command |
| 74 | + ProtocolCommand cmd = commandArguments.getCommand(); |
| 75 | + byte[] raw = cmd.getRaw(); |
| 76 | + |
| 77 | + // Convert to uppercase using SafeEncoder utility (faster than String.toUpperCase()) |
| 78 | + byte[] uppercaseBytes = SafeEncoder.toUpperCase(raw); |
| 79 | + |
| 80 | + // Look up the parent command in the registry using byte array key |
| 81 | + // Object registryEntry = COMMAND_FLAGS_REGISTRY.get(uppercaseBytes); |
| 82 | + CommandMeta commandMeta = commands.getCommand(uppercaseBytes); |
| 83 | + |
| 84 | + if (commandMeta == null) { |
| 85 | + // Command not found in registry |
| 86 | + return EMPTY_FLAGS; |
| 87 | + } |
| 88 | + |
| 89 | + if (!commandMeta.hasSubcommands()) { |
| 90 | + // Check if this is a simple command without subcommands |
| 91 | + return commandMeta.getFlags(); |
| 92 | + } else { |
| 93 | + // Parent command with subcommands |
| 94 | + // Try to extract the subcommand from the second argument |
| 95 | + byte[] subCommand = getSubCommand(commandArguments); |
| 96 | + if (subCommand != null) { |
| 97 | + CommandMeta subCommandMeta = commandMeta.getSubcommand(subCommand); |
| 98 | + if (subCommandMeta != null) { |
| 99 | + return subCommandMeta.getFlags(); |
| 100 | + } else { |
| 101 | + // (second argument exists but not a recognized subcommand , return parent flags |
| 102 | + return commandMeta.getFlags(); |
| 103 | + } |
| 104 | + } else { |
| 105 | + // no second argument (no subcommand), return parent flags |
| 106 | + return commandMeta.getFlags(); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private byte[] getSubCommand(CommandArguments commandArguments) { |
| 112 | + if (commandArguments.size() > 1) { |
| 113 | + Rawable secondArg = commandArguments.get(1); |
| 114 | + byte[] subRaw = secondArg.getRaw(); |
| 115 | + |
| 116 | + // Convert to uppercase using SafeEncoder utility |
| 117 | + return SafeEncoder.toUpperCase(subRaw); |
| 118 | + } else { |
| 119 | + return null; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // Internal class to hold subcommand mappings for parent commands. |
| 124 | + static class Commands { |
| 125 | + |
| 126 | + final JedisByteMap<CommandMeta> commands = new JedisByteMap<>(); |
| 127 | + |
| 128 | + boolean isEmpty() { |
| 129 | + return commands.isEmpty(); |
| 130 | + } |
| 131 | + |
| 132 | + public Commands register(byte[] cmd, CommandMeta command) { |
| 133 | + commands.put(cmd, command); |
| 134 | + return this; |
| 135 | + } |
| 136 | + |
| 137 | + public boolean containsKey(byte[] command) { |
| 138 | + return commands.containsKey(command); |
| 139 | + } |
| 140 | + |
| 141 | + public CommandMeta getCommand(byte[] command) { |
| 142 | + return commands.get(command); |
| 143 | + } |
| 144 | + |
| 145 | + public Map<byte[], CommandMeta> getCommands() { |
| 146 | + return commands; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + // |
| 151 | + static class CommandMeta { |
| 152 | + |
| 153 | + final EnumSet<CommandFlag> flags; |
| 154 | + |
| 155 | + final Commands subcommands = new Commands(); |
| 156 | + |
| 157 | + CommandMeta(EnumSet<CommandFlag> flags) { |
| 158 | + this.flags = flags; |
| 159 | + } |
| 160 | + |
| 161 | + void putSubCommand(byte[] subCommand, CommandMeta subCommandMeta) { |
| 162 | + this.subcommands.register(subCommand, subCommandMeta); |
| 163 | + } |
| 164 | + |
| 165 | + boolean hasSubcommands() { |
| 166 | + return !subcommands.isEmpty(); |
| 167 | + } |
| 168 | + |
| 169 | + EnumSet<CommandFlag> getFlags() { |
| 170 | + if (flags == null) { |
| 171 | + return EMPTY_FLAGS; |
| 172 | + } |
| 173 | + |
| 174 | + return flags; |
| 175 | + } |
| 176 | + |
| 177 | + CommandMeta getSubcommand(byte[] subcommand) { |
| 178 | + return subcommands.getCommand(subcommand); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + static public class Builder { |
| 183 | + |
| 184 | + private final Commands commands = new Commands(); |
| 185 | + |
| 186 | + public Builder register(String name, EnumSet<CommandFlag> flags) { |
| 187 | + commands.register(SafeEncoder.encode(name), new CommandMeta(flags)); |
| 188 | + return this; |
| 189 | + } |
| 190 | + |
| 191 | + public Builder register(String name, String subcommand, EnumSet<CommandFlag> flags) { |
| 192 | + byte[] cmdName = SafeEncoder.encode(name); |
| 193 | + |
| 194 | + if (!commands.containsKey(cmdName)) { |
| 195 | + commands.register(SafeEncoder.encode(name), new CommandMeta(EMPTY_FLAGS)); |
| 196 | + } |
| 197 | + |
| 198 | + byte[] subCmdName = SafeEncoder.encode(subcommand); |
| 199 | + commands.getCommand(cmdName).putSubCommand(subCmdName, new CommandMeta(flags)); |
| 200 | + return this; |
| 201 | + } |
| 202 | + |
| 203 | + public StaticCommandFlagsRegistry build() { |
| 204 | + return new StaticCommandFlagsRegistry(commands); |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments