Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geode 2469 #404

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
import org.apache.geode.redis.internal.executor.ExpirationExecutor;
import org.apache.geode.redis.internal.executor.ListQuery;
import org.apache.geode.redis.internal.executor.SortedSetQuery;
import org.apache.geode.redis.internal.executor.hash.HashInterpreter;
import org.apache.geode.redis.internal.executor.set.SetInterpreter;
import org.apache.geode.redis.internal.executor.hash.HashExecutor;
import org.apache.geode.redis.internal.executor.set.SetExecutor;
import org.apache.geode.internal.hll.HyperLogLogPlus;
import org.apache.geode.management.cli.Result;
import org.apache.geode.management.cli.Result.Status;
Expand Down Expand Up @@ -223,14 +223,14 @@ public boolean removeKey(ByteArrayWrapper key, RedisDataType type, boolean cance


// Check hash
ByteArrayWrapper regionName = HashInterpreter.toRegionNameByteArray(key);
ByteArrayWrapper regionName = HashExecutor.toRegionNameByteArray(key);
Region<?, ?> region = this.getRegion(regionName);
if (region != null) {
region.remove(HashInterpreter.toEntryKey(key));
region.remove(HashExecutor.toEntryKey(key));
}

// remove the set
region = this.getRegion(SetInterpreter.SET_REGION_KEY);
region = this.getRegion(SetExecutor.SET_REGION_KEY);
if (region != null) {
region.remove(key);
}
Expand Down Expand Up @@ -310,12 +310,6 @@ public void createRemoteRegionReferenceLocally(ByteArrayWrapper key, RedisDataTy

String regionName = key.toString();

// check if type is hash TODO: remove
/*
* int indexOfColonForHashObject = regionName.indexOf(":"); if (indexOfColonForHashObject > 0) {
* // Set HSET region:<key> regionName = regionName.substring(0, indexOfColonForHashObject); key
* = new ByteArrayWrapper(regionName.getBytes(StandardCharsets.UTF_8)); }
*/

checkDataType(key, type);
Region<?, ?> r = this.regions.get(key);
Expand Down Expand Up @@ -502,10 +496,9 @@ protected void checkDataType(ByteArrayWrapper key, RedisDataType type) {
return;
if (currentType == RedisDataType.REDIS_PROTECTED)
throw new RedisDataTypeMismatchException("The key name \"" + key + "\" is protected");
/*
* if (currentType != type) throw new RedisDataTypeMismatchException( "The key name \"" + key +
* "\" is already used by a " + currentType.toString());
*/
if (currentType != type)
throw new RedisDataTypeMismatchException(
"The key name \"" + key + "\" is already used by a " + currentType.toString());
}

public boolean regionExists(ByteArrayWrapper key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ public void executeCommand(Command command, ExecutionHandlerContext context) {
// save map
saveMap(map, context, key);

// if (map.isEmpty()) {
// context.getRegionProvider().removeKey(key, RedisDataType.REDIS_HASH);
// }
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), numDeleted));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void executeCommand(Command command, ExecutionHandlerContext context) {
return;
}

ByteArrayWrapper regionName = HashInterpreter.toRegionNameByteArray(command.getKey());
ByteArrayWrapper regionName = toRegionNameByteArray(command.getKey());

checkDataType(regionName, RedisDataType.REDIS_HASH, context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.apache.geode.redis.internal.Command;
import org.apache.geode.redis.internal.ExecutionHandlerContext;
import org.apache.geode.redis.internal.RedisConstants.ArityDef;
import org.apache.geode.redis.internal.RedisDataType;

public class HGetAllExecutor extends HashExecutor {

Expand All @@ -39,8 +38,8 @@ public void executeCommand(Command command, ExecutionHandlerContext context) {
return;
}

ByteArrayWrapper regionName = HashInterpreter.toRegionNameByteArray(command.getKey());
ByteArrayWrapper entryKey = HashInterpreter.toEntryKey(command.getKey());
ByteArrayWrapper regionName = toRegionNameByteArray(command.getKey());
ByteArrayWrapper entryKey = toEntryKey(command.getKey());

// checkDataType(regionName, RedisDataType.REDIS_HASH, context);
Region<ByteArrayWrapper, Map<ByteArrayWrapper, ByteArrayWrapper>> keyRegion =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void executeCommand(Command command, ExecutionHandlerContext context) {
return;
}

ByteArrayWrapper regioName = HashInterpreter.toEntryKey(command.getKey());
ByteArrayWrapper regioName = toEntryKey(command.getKey());

checkDataType(regioName, RedisDataType.REDIS_HASH, context);
Region<ByteArrayWrapper, Map<ByteArrayWrapper, ByteArrayWrapper>> keyRegion =
Expand All @@ -50,7 +50,7 @@ public void executeCommand(Command command, ExecutionHandlerContext context) {
byte[] byteField = commandElems.get(FIELD_INDEX);
ByteArrayWrapper field = new ByteArrayWrapper(byteField);

ByteArrayWrapper key = HashInterpreter.toEntryKey(command.getKey());
ByteArrayWrapper key = toEntryKey(command.getKey());

Map<ByteArrayWrapper, ByteArrayWrapper> entry = keyRegion.get(key);
if (entry == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public void executeCommand(Command command, ExecutionHandlerContext context) {

value += increment;
map.put(field, new ByteArrayWrapper(Coder.doubleToBytes(value)));
this.saveMap(map, context, key);

command.setResponse(Coder.getBulkStringResponse(context.getByteBufAllocator(), value));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void executeCommand(Command command, ExecutionHandlerContext context) {
Region<ByteArrayWrapper, Map<ByteArrayWrapper, ByteArrayWrapper>> keyRegion =
this.getRegion(context, command.getKey());

ByteArrayWrapper entryKey = HashInterpreter.toEntryKey(command.getKey());
ByteArrayWrapper entryKey = toEntryKey(command.getKey());

Map<ByteArrayWrapper, ByteArrayWrapper> keyMap = keyRegion.get(entryKey);
if (keyMap == null || keyMap.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,42 @@
import java.util.Map;

import org.apache.geode.cache.Region;
import org.apache.geode.redis.GeodeRedisServer;
import org.apache.geode.redis.internal.ByteArrayWrapper;
import org.apache.geode.redis.internal.Coder;
import org.apache.geode.redis.internal.ExecutionHandlerContext;
import org.apache.geode.redis.internal.RedisDataType;
import org.apache.geode.redis.internal.executor.AbstractExecutor;

public abstract class HashExecutor extends AbstractExecutor {
/**
* <pre>
* The region:key separator.
*
* REGION_KEY_SEPERATOR = ":"
*
* See Hash section of <a href=
"https://redis.io/topics/data-types">https://redis.io/topics/data-types#Hashes</a>
* </pre>
*/
public static final String REGION_KEY_SEPERATOR = ":";

/**
* The default hash region name REGION_HASH_REGION = Coder.stringToByteArrayWrapper("ReDiS_HASH")
*/
public static final ByteArrayWrapper REGION_HASH_REGION =
Coder.stringToByteArrayWrapper(GeodeRedisServer.HASH_REGION);

protected final int FIELD_INDEX = 2;

protected Region<ByteArrayWrapper, Map<ByteArrayWrapper, ByteArrayWrapper>> getOrCreateRegion(
ExecutionHandlerContext context, ByteArrayWrapper key, RedisDataType type) {

return HashInterpreter.getRegion(key, context);
return getRegion(context, key);
}



/**
*
* @param context the context
Expand All @@ -55,7 +76,7 @@ protected Map<ByteArrayWrapper, ByteArrayWrapper> getMap(ExecutionHandlerContext
protected Map<ByteArrayWrapper, ByteArrayWrapper> getMap(ExecutionHandlerContext context,
ByteArrayWrapper key, RedisDataType type) {

ByteArrayWrapper regionName = HashInterpreter.toRegionNameByteArray(key);
ByteArrayWrapper regionName = toRegionNameByteArray(key);

Region<ByteArrayWrapper, Map<ByteArrayWrapper, ByteArrayWrapper>> region =
getOrCreateRegion(context, regionName, type);
Expand All @@ -64,7 +85,7 @@ protected Map<ByteArrayWrapper, ByteArrayWrapper> getMap(ExecutionHandlerContext
return null;


ByteArrayWrapper entryKey = HashInterpreter.toEntryKey(key);
ByteArrayWrapper entryKey = toEntryKey(key);
Map<ByteArrayWrapper, ByteArrayWrapper> map = region.get(entryKey);
if (map == null) {
map = new HashMap<ByteArrayWrapper, ByteArrayWrapper>();
Expand All @@ -74,6 +95,27 @@ protected Map<ByteArrayWrapper, ByteArrayWrapper> getMap(ExecutionHandlerContext

}

/**
* Return key from format region:key
*
* @param key the raw key
* @return the ByteArray for the key
*/
public static ByteArrayWrapper toEntryKey(ByteArrayWrapper key) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be private?

if (key == null)
return null;

String keyString = key.toString();
int nameSeparatorIndex = keyString.indexOf(REGION_KEY_SEPERATOR);
if (nameSeparatorIndex < 0) {
return key;
}

keyString = keyString.substring(nameSeparatorIndex + 1);
key = new ByteArrayWrapper(Coder.stringToBytes(keyString));
return key;
}

/**
*
* @param context the execution handler context
Expand All @@ -82,7 +124,48 @@ protected Map<ByteArrayWrapper, ByteArrayWrapper> getMap(ExecutionHandlerContext
*/
protected Region<ByteArrayWrapper, Map<ByteArrayWrapper, ByteArrayWrapper>> getRegion(
ExecutionHandlerContext context, ByteArrayWrapper key) {
return HashInterpreter.getRegion(key, context);
if (key == null)
return null;

if (key.toString().contains(REGION_KEY_SEPERATOR)) {
return (Region<ByteArrayWrapper, Map<ByteArrayWrapper, ByteArrayWrapper>>) context
.getRegionProvider()
.getOrCreateRegion(toRegionNameByteArray(key), RedisDataType.REDIS_HASH, context);
}

// default region
return context.getRegionProvider().getHashRegion();
}

/**
* Supports conversation of keys with format region:key
*
* @param key the byte array wrapper
* @return the byte array wrapper
*/
public static ByteArrayWrapper toRegionNameByteArray(ByteArrayWrapper key) {
if (key == null)
return null;

String keyString = key.toString();

int nameSeparatorIndex = keyString.indexOf(REGION_KEY_SEPERATOR);
if (nameSeparatorIndex > -1) {
keyString = keyString.substring(0, nameSeparatorIndex);
if (keyString == null)
return REGION_HASH_REGION;

keyString = keyString.trim();

if (keyString.length() == 0)
return REGION_HASH_REGION;

key = new ByteArrayWrapper(Coder.stringToBytes(keyString.trim()));

return key;
}

return REGION_HASH_REGION;
}

/**
Expand Down Expand Up @@ -113,13 +196,13 @@ protected void saveMap(Map<ByteArrayWrapper, ByteArrayWrapper> map,
return;
}

ByteArrayWrapper regionName = HashInterpreter.toRegionNameByteArray(key);
ByteArrayWrapper regionName = toRegionNameByteArray(key);


Region<ByteArrayWrapper, Map<ByteArrayWrapper, ByteArrayWrapper>> region =
getOrCreateRegion(context, regionName, type);

ByteArrayWrapper entryKey = HashInterpreter.toEntryKey(key);
ByteArrayWrapper entryKey = toEntryKey(key);

region.put(entryKey, map);
context.getRegionProvider().metaPut(key, RedisDataType.REDIS_HASH);
Expand Down