use of org.apache.geode.management.internal.cli.domain.DataCommandResult in project geode by apache.
the class DataCommandFunction method put.
@SuppressWarnings({ "rawtypes" })
public DataCommandResult put(String key, String value, boolean putIfAbsent, String keyClass, String valueClass, String regionName) {
if (StringUtils.isEmpty(regionName)) {
return DataCommandResult.createPutResult(key, null, null, CliStrings.PUT__MSG__REGIONNAME_EMPTY, false);
}
if (StringUtils.isEmpty(key)) {
return DataCommandResult.createPutResult(key, null, null, CliStrings.PUT__MSG__KEY_EMPTY, false);
}
if (StringUtils.isEmpty(value)) {
return DataCommandResult.createPutResult(key, null, null, CliStrings.PUT__MSG__VALUE_EMPTY, false);
}
InternalCache cache = getCache();
Region region = cache.getRegion(regionName);
if (region == null) {
return DataCommandResult.createPutResult(key, null, null, CliStrings.format(CliStrings.PUT__MSG__REGION_NOT_FOUND, regionName), false);
} else {
Object keyObject;
Object valueObject;
try {
keyObject = getClassObject(key, keyClass);
} catch (ClassNotFoundException e) {
return DataCommandResult.createPutResult(key, null, null, "ClassNotFoundException " + keyClass, false);
} catch (IllegalArgumentException e) {
return DataCommandResult.createPutResult(key, null, null, "Error in converting JSON " + e.getMessage(), false);
}
try {
valueObject = getClassObject(value, valueClass);
} catch (ClassNotFoundException e) {
return DataCommandResult.createPutResult(key, null, null, "ClassNotFoundException " + valueClass, false);
}
Object returnValue;
if (putIfAbsent && region.containsKey(keyObject)) {
returnValue = region.get(keyObject);
} else {
returnValue = region.put(keyObject, valueObject);
}
Object[] array = getJSONForNonPrimitiveObject(returnValue);
DataCommandResult result = DataCommandResult.createPutResult(key, array[1], null, null, true);
if (array[0] != null) {
result.setValueClass((String) array[0]);
}
return result;
}
}
use of org.apache.geode.management.internal.cli.domain.DataCommandResult in project geode by apache.
the class DataCommandFunction method execute.
@Override
public void execute(FunctionContext functionContext) {
try {
InternalCache cache = getCache();
DataCommandRequest request = (DataCommandRequest) functionContext.getArguments();
if (logger.isDebugEnabled()) {
logger.debug("Executing function : \n{}\n on member {}", request, System.getProperty("memberName"));
}
DataCommandResult result = null;
if (request.isGet()) {
result = get(request);
} else if (request.isLocateEntry()) {
result = locateEntry(request);
} else if (request.isPut()) {
result = put(request);
} else if (request.isRemove()) {
result = remove(request);
} else if (request.isSelect()) {
result = select(request);
}
if (logger.isDebugEnabled()) {
logger.debug("Result is {}", result);
}
functionContext.getResultSender().lastResult(result);
} catch (Exception e) {
logger.info("Exception occurred:", e);
functionContext.getResultSender().sendException(e);
}
}
use of org.apache.geode.management.internal.cli.domain.DataCommandResult in project geode by apache.
the class DataCommands method callFunctionForRegion.
@SuppressWarnings("rawtypes")
public static DataCommandResult callFunctionForRegion(DataCommandRequest request, DataCommandFunction putfn, Set<DistributedMember> members) {
if (members.size() == 1) {
DistributedMember member = members.iterator().next();
ResultCollector collector = FunctionService.onMember(member).setArguments(request).execute(putfn);
List list = (List) collector.getResult();
Object object = list.get(0);
if (object instanceof Throwable) {
Throwable error = (Throwable) object;
DataCommandResult result = new DataCommandResult();
result.setErorr(error);
result.setErrorString(error.getMessage());
return result;
}
DataCommandResult result = (DataCommandResult) list.get(0);
result.aggregate(null);
return result;
} else {
ResultCollector collector = FunctionService.onMembers(members).setArguments(request).execute(putfn);
List list = (List) collector.getResult();
DataCommandResult result = null;
for (Object object : list) {
if (object instanceof Throwable) {
Throwable error = (Throwable) object;
result = new DataCommandResult();
result.setErorr(error);
result.setErrorString(error.getMessage());
return result;
}
if (result == null) {
result = (DataCommandResult) object;
result.aggregate(null);
} else {
result.aggregate((DataCommandResult) object);
}
}
return result;
}
}
use of org.apache.geode.management.internal.cli.domain.DataCommandResult in project geode by apache.
the class DataCommandFunction method locateEntry.
@SuppressWarnings({ "unchecked", "rawtypes" })
public DataCommandResult locateEntry(String key, String keyClass, String valueClass, String regionPath, boolean recursive) {
InternalCache cache = getCache();
if (StringUtils.isEmpty(regionPath)) {
return DataCommandResult.createLocateEntryResult(key, null, null, CliStrings.LOCATE_ENTRY__MSG__REGIONNAME_EMPTY, false);
}
if (StringUtils.isEmpty(key)) {
return DataCommandResult.createLocateEntryResult(key, null, null, CliStrings.LOCATE_ENTRY__MSG__KEY_EMPTY, false);
}
List<Region> listOfRegionsStartingWithRegionPath = new ArrayList<>();
if (recursive) {
// Recursively find the keys starting from the specified region path.
List<String> regionPaths = getAllRegionPaths(cache, true);
for (String path : regionPaths) {
if (path.startsWith(regionPath) || path.startsWith(Region.SEPARATOR + regionPath)) {
Region targetRegion = cache.getRegion(path);
listOfRegionsStartingWithRegionPath.add(targetRegion);
}
}
if (listOfRegionsStartingWithRegionPath.size() == 0) {
if (logger.isDebugEnabled()) {
logger.debug("Region Not Found - {}", regionPath);
}
return DataCommandResult.createLocateEntryResult(key, null, null, CliStrings.format(CliStrings.REMOVE__MSG__REGION_NOT_FOUND, regionPath), false);
}
} else {
Region region = cache.getRegion(regionPath);
if (region == null) {
if (logger.isDebugEnabled()) {
logger.debug("Region Not Found - {}", regionPath);
}
return DataCommandResult.createLocateEntryResult(key, null, null, CliStrings.format(CliStrings.REMOVE__MSG__REGION_NOT_FOUND, regionPath), false);
} else {
listOfRegionsStartingWithRegionPath.add(region);
}
}
Object keyObject;
try {
keyObject = getClassObject(key, keyClass);
} catch (ClassNotFoundException e) {
logger.error(e.getMessage(), e);
return DataCommandResult.createLocateEntryResult(key, null, null, "ClassNotFoundException " + keyClass, false);
} catch (IllegalArgumentException e) {
logger.error(e.getMessage(), e);
return DataCommandResult.createLocateEntryResult(key, null, null, "Error in converting JSON " + e.getMessage(), false);
}
Object value;
DataCommandResult.KeyInfo keyInfo;
keyInfo = new DataCommandResult.KeyInfo();
DistributedMember member = cache.getDistributedSystem().getDistributedMember();
keyInfo.setHost(member.getHost());
keyInfo.setMemberId(member.getId());
keyInfo.setMemberName(member.getName());
for (Region region : listOfRegionsStartingWithRegionPath) {
if (region instanceof PartitionedRegion) {
// Following code is adaptation of which.java of old Gfsh
PartitionedRegion pr = (PartitionedRegion) region;
Region localRegion = PartitionRegionHelper.getLocalData(region);
value = localRegion.get(keyObject);
if (value != null) {
DistributedMember primaryMember = PartitionRegionHelper.getPrimaryMemberForKey(region, keyObject);
int bucketId = pr.getKeyInfo(keyObject).getBucketId();
boolean isPrimary = member == primaryMember;
keyInfo.addLocation(new Object[] { region.getFullPath(), true, getJSONForNonPrimitiveObject(value)[1], isPrimary, "" + bucketId });
} else {
if (logger.isDebugEnabled()) {
logger.debug("Key is not present in the region {}", regionPath);
}
return DataCommandResult.createLocateEntryInfoResult(key, null, null, CliStrings.LOCATE_ENTRY__MSG__KEY_NOT_FOUND_REGION, false);
}
} else {
if (region.containsKey(keyObject)) {
value = region.get(keyObject);
if (logger.isDebugEnabled()) {
logger.debug("Get for key {} value {} in region {}", key, value, region.getFullPath());
}
if (value != null) {
keyInfo.addLocation(new Object[] { region.getFullPath(), true, getJSONForNonPrimitiveObject(value)[1], false, null });
} else {
keyInfo.addLocation(new Object[] { region.getFullPath(), false, null, false, null });
}
} else {
if (logger.isDebugEnabled()) {
logger.debug("Key is not present in the region {}", regionPath);
}
keyInfo.addLocation(new Object[] { region.getFullPath(), false, null, false, null });
}
}
}
if (keyInfo.hasLocation()) {
return DataCommandResult.createLocateEntryResult(key, keyInfo, null, null, true);
} else {
return DataCommandResult.createLocateEntryInfoResult(key, null, null, CliStrings.LOCATE_ENTRY__MSG__KEY_NOT_FOUND_REGION, false);
}
}
use of org.apache.geode.management.internal.cli.domain.DataCommandResult in project geode by apache.
the class DataCommandFunction method remove.
@SuppressWarnings({ "rawtypes" })
public DataCommandResult remove(String key, String keyClass, String regionName, String removeAllKeys) {
InternalCache cache = getCache();
if (StringUtils.isEmpty(regionName)) {
return DataCommandResult.createRemoveResult(key, null, null, CliStrings.REMOVE__MSG__REGIONNAME_EMPTY, false);
}
if (StringUtils.isEmpty(removeAllKeys) && (key == null)) {
return DataCommandResult.createRemoveResult(null, null, null, CliStrings.REMOVE__MSG__KEY_EMPTY, false);
}
Region region = cache.getRegion(regionName);
if (region == null) {
return DataCommandResult.createRemoveInfoResult(key, null, null, CliStrings.format(CliStrings.REMOVE__MSG__REGION_NOT_FOUND, regionName), false);
} else {
if (removeAllKeys == null) {
Object keyObject;
try {
keyObject = getClassObject(key, keyClass);
} catch (ClassNotFoundException e) {
return DataCommandResult.createRemoveResult(key, null, null, "ClassNotFoundException " + keyClass, false);
} catch (IllegalArgumentException e) {
return DataCommandResult.createRemoveResult(key, null, null, "Error in converting JSON " + e.getMessage(), false);
}
if (region.containsKey(keyObject)) {
Object value = region.remove(keyObject);
if (logger.isDebugEnabled()) {
logger.debug("Removed key {} successfully", key);
}
// return DataCommandResult.createRemoveResult(key, value, null, null);
Object[] array = getJSONForNonPrimitiveObject(value);
DataCommandResult result = DataCommandResult.createRemoveResult(key, array[1], null, null, true);
if (array[0] != null) {
result.setValueClass((String) array[0]);
}
return result;
} else {
return DataCommandResult.createRemoveInfoResult(key, null, null, CliStrings.REMOVE__MSG__KEY_NOT_FOUND_REGION, false);
}
} else {
DataPolicy policy = region.getAttributes().getDataPolicy();
if (!policy.withPartitioning()) {
region.clear();
if (logger.isDebugEnabled()) {
logger.debug("Cleared all keys in the region - {}", regionName);
}
return DataCommandResult.createRemoveInfoResult(key, null, null, CliStrings.format(CliStrings.REMOVE__MSG__CLEARED_ALL_CLEARS, regionName), true);
} else {
return DataCommandResult.createRemoveInfoResult(key, null, null, CliStrings.REMOVE__MSG__CLEAREALL_NOT_SUPPORTED_FOR_PARTITIONREGION, false);
}
}
}
}
Aggregations