Skip to content

Commit

Permalink
Decouple geode in sadd,srem,smembers (apache#4912)
Browse files Browse the repository at this point in the history
This commit builds a GeodeRedisSetSynchronized class that brings
together "set" methods.  These three are necessary for Spring-Session so
they are the only ones included.
  • Loading branch information
Murtuza Boxwala authored Apr 10, 2020
1 parent 2ffc874 commit 50ce279
Show file tree
Hide file tree
Showing 12 changed files with 278 additions and 212 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.nio.channels.SocketChannel;
import java.util.List;
import java.util.stream.Collectors;

import io.netty.buffer.ByteBuf;

Expand Down Expand Up @@ -67,6 +68,15 @@ public List<byte[]> getProcessedCommand() {
return this.commandElems;
}

/**
* Used to get the command element list
*
* @return List of command elements in form of {@link List}
*/
public List<ByteArrayWrapper> getProcessedCommandWrappers() {
return this.commandElems.stream().map(ByteArrayWrapper::new).collect(Collectors.toList());
}

/**
* Getter method for the command type
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package org.apache.geode.redis.internal.executor.hash;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.geode.cache.Region;
import org.apache.geode.redis.internal.ByteArrayWrapper;
import org.apache.geode.redis.internal.ExecutionHandlerContext;
import org.apache.geode.redis.internal.RedisDataType;

class GeodeRedisHashSynchronized implements RedisHash {
private final ByteArrayWrapper key;
private final ExecutionHandlerContext context;

public GeodeRedisHashSynchronized(ByteArrayWrapper key, ExecutionHandlerContext context) {
this.key = key;
this.context = context;
}

@Override
public int hset(List<ByteArrayWrapper> fieldsToSet,
boolean NX) {
AtomicInteger fieldsAdded = new AtomicInteger();

Map<ByteArrayWrapper, ByteArrayWrapper> computedHash =
region().compute(key, (_unused_, oldHash) -> {

fieldsAdded.set(0);
HashMap<ByteArrayWrapper, ByteArrayWrapper> newHash;
if (oldHash == null) {
newHash = new HashMap<>();
} else {
newHash = new HashMap<>(oldHash);
}

for (int i = 0; i < fieldsToSet.size(); i += 2) {
ByteArrayWrapper field = fieldsToSet.get(i);
ByteArrayWrapper value = fieldsToSet.get(i + 1);

if (NX) {
newHash.putIfAbsent(field, value);
} else {
newHash.put(field, value);
}
}
if (oldHash == null) {
fieldsAdded.set(newHash.size());
} else {
fieldsAdded.set(newHash.size() - oldHash.size());
}
return newHash;
});

if (computedHash != null) {
context.getKeyRegistrar().register(this.key, RedisDataType.REDIS_HASH);
}

return fieldsAdded.get();
}

@Override
public int hdel(List<ByteArrayWrapper> subList) {
AtomicLong numDeleted = new AtomicLong();
region().computeIfPresent(key, (_unused_, oldHash) -> {
HashMap<ByteArrayWrapper, ByteArrayWrapper> newHash = new HashMap<>(oldHash);
for (ByteArrayWrapper fieldToRemove : subList) {
newHash.remove(fieldToRemove);
}
numDeleted.set(oldHash.size() - newHash.size());
return newHash;
});
return numDeleted.intValue();
}

@Override
public Collection<Map.Entry<ByteArrayWrapper, ByteArrayWrapper>> hgetall() {
return region().getOrDefault(key, Collections.emptyMap()).entrySet();
}

private Region<ByteArrayWrapper, Map<ByteArrayWrapper, ByteArrayWrapper>> region() {
return context.getRegionProvider().getHashRegion();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
package org.apache.geode.redis.internal.executor.hash;

import java.util.List;
import java.util.Map;

import org.apache.geode.cache.TimeoutException;
import org.apache.geode.redis.internal.AutoCloseableLock;
import org.apache.geode.redis.internal.ByteArrayWrapper;
import org.apache.geode.redis.internal.Coder;
import org.apache.geode.redis.internal.Command;
Expand Down Expand Up @@ -48,45 +45,18 @@ public class HDelExecutor extends HashExecutor {

@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
List<byte[]> commandElems = command.getProcessedCommand();
List<ByteArrayWrapper> commandElems = command.getProcessedCommandWrappers();

if (commandElems.size() < 3) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.HDEL));
return;
}

int numDeleted = 0;

ByteArrayWrapper key = command.getKey();

try (AutoCloseableLock regionLock = withRegionLock(context, key)) {
Map<ByteArrayWrapper, ByteArrayWrapper> map = getMap(context, key);

if (map == null || map.isEmpty()) {
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), numDeleted));
return;
}

for (int i = START_FIELDS_INDEX; i < commandElems.size(); i++) {
ByteArrayWrapper field = new ByteArrayWrapper(commandElems.get(i));
Object oldValue = map.remove(field);
if (oldValue != null) {
numDeleted++;
}
}
// save map
saveMap(map, context, key);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
command.setResponse(
Coder.getErrorResponse(context.getByteBufAllocator(), "Thread interrupted."));
return;
} catch (TimeoutException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(),
"Timeout acquiring lock. Please try again."));
return;
}

RedisHash hash = new GeodeRedisHashSynchronized(key, context);
int numDeleted = hash.hdel(commandElems.subList(2, commandElems.size()));
command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), numDeleted));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
*/
package org.apache.geode.redis.internal.executor.hash;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.geode.redis.internal.ByteArrayWrapper;
Expand Down Expand Up @@ -58,23 +56,8 @@ public void executeCommand(Command command, ExecutionHandlerContext context) {
Collection<Entry<ByteArrayWrapper, ByteArrayWrapper>> entries;
ByteArrayWrapper key = command.getKey();

Map<ByteArrayWrapper, ByteArrayWrapper> results = getMap(context, key);

if (results == null || results.isEmpty()) {
command.setResponse(Coder.getEmptyArrayResponse(context.getByteBufAllocator()));
return;
}

entries = results.entrySet();

if (entries == null || entries.isEmpty()) {
command.setResponse(Coder.getEmptyArrayResponse(context.getByteBufAllocator()));
return;
}

// create a copy
entries = new ArrayList<>(entries);

RedisHash hash = new GeodeRedisHashSynchronized(key, context);
entries = hash.hgetall();
command.setResponse(Coder.getKeyValArrayResponse(context.getByteBufAllocator(), entries));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@
package org.apache.geode.redis.internal.executor.hash;

import java.util.List;
import java.util.Map;

import org.apache.geode.cache.TimeoutException;
import org.apache.geode.redis.internal.AutoCloseableLock;
import org.apache.geode.redis.internal.ByteArrayWrapper;
import org.apache.geode.redis.internal.Coder;
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;

/**
* <pre>
Expand Down Expand Up @@ -52,36 +48,16 @@ public class HMSetExecutor extends HashExecutor {

@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
List<byte[]> commandElems = command.getProcessedCommand();
List<ByteArrayWrapper> commandElems = command.getProcessedCommandWrappers();

if (commandElems.size() < 4 || commandElems.size() % 2 == 1) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), ArityDef.HMSET));
return;
}

ByteArrayWrapper key = command.getKey();
try (AutoCloseableLock regionLock = withRegionLock(context, key)) {
Map<ByteArrayWrapper, ByteArrayWrapper> map = getMap(context, key);

for (int i = 2; i < commandElems.size(); i += 2) {
byte[] fieldArray = commandElems.get(i);
ByteArrayWrapper field = new ByteArrayWrapper(fieldArray);
byte[] value = commandElems.get(i + 1);
map.put(field, new ByteArrayWrapper(value));
}

saveMap(map, context, key);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
command.setResponse(
Coder.getErrorResponse(context.getByteBufAllocator(), "Thread interrupted."));
return;
} catch (TimeoutException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(),
"Timeout acquiring lock. Please try again."));
return;
}
context.getKeyRegistrar().register(key, RedisDataType.REDIS_HASH);
RedisHash hash = new GeodeRedisHashSynchronized(key, context);
hash.hset(commandElems.subList(2, commandElems.size()), false);
command.setResponse(Coder.getSimpleStringResponse(context.getByteBufAllocator(), SUCCESS));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
package org.apache.geode.redis.internal.executor.hash;

import java.util.List;
import java.util.Map;

import org.apache.geode.cache.TimeoutException;
import org.apache.geode.redis.internal.AutoCloseableLock;
import org.apache.geode.redis.internal.ByteArrayWrapper;
import org.apache.geode.redis.internal.Coder;
import org.apache.geode.redis.internal.Command;
Expand All @@ -45,7 +42,7 @@ public class HSetExecutor extends HashExecutor implements Extendable {

@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
List<byte[]> commandElems = command.getProcessedCommand();
List<ByteArrayWrapper> commandElems = command.getProcessedCommandWrappers();

if (commandElems.size() < 4 || commandElems.size() % 2 == 1) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(), getArgsError()));
Expand All @@ -54,40 +51,9 @@ public void executeCommand(Command command, ExecutionHandlerContext context) {

ByteArrayWrapper key = command.getKey();

Object oldValue;
int fieldsAdded = 0;
RedisHash hash = new GeodeRedisHashSynchronized(key, context);

try (AutoCloseableLock regionLock = withRegionLock(context, key)) {
Map<ByteArrayWrapper, ByteArrayWrapper> map = getMap(context, key);

for (int i = 2; i < commandElems.size(); i += 2) {
byte[] fieldArray = commandElems.get(i);
ByteArrayWrapper field = new ByteArrayWrapper(fieldArray);
byte[] value = commandElems.get(i + 1);
ByteArrayWrapper putValue = new ByteArrayWrapper(value);

if (onlySetOnAbsent()) {
oldValue = map.putIfAbsent(field, putValue);
} else {
oldValue = map.put(field, putValue);
}

if (oldValue == null) {
fieldsAdded++;
}
}

this.saveMap(map, context, key);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
command.setResponse(
Coder.getErrorResponse(context.getByteBufAllocator(), "Thread interrupted."));
return;
} catch (TimeoutException e) {
command.setResponse(Coder.getErrorResponse(context.getByteBufAllocator(),
"Timeout acquiring lock. Please try again."));
return;
}
int fieldsAdded = hash.hset(commandElems.subList(2, commandElems.size()), onlySetOnAbsent());

command.setResponse(Coder.getIntegerResponse(context.getByteBufAllocator(), fieldsAdded));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package org.apache.geode.redis.internal.executor.hash;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.geode.redis.internal.ByteArrayWrapper;

public interface RedisHash {
int hset(List<ByteArrayWrapper> fieldsToSet, boolean NX);

int hdel(List<ByteArrayWrapper> subList);

Collection<Map.Entry<ByteArrayWrapper, ByteArrayWrapper>> hgetall();
}
Loading

0 comments on commit 50ce279

Please sign in to comment.