Search in sources :

Example 6 with DataCommandResult

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;
    }
}
Also used : InternalCache(org.apache.geode.internal.cache.InternalCache) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) DataCommandResult(org.apache.geode.management.internal.cli.domain.DataCommandResult)

Example 7 with DataCommandResult

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);
    }
}
Also used : InternalCache(org.apache.geode.internal.cache.InternalCache) DataCommandRequest(org.apache.geode.management.internal.cli.domain.DataCommandRequest) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException) IOException(java.io.IOException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) DataCommandResult(org.apache.geode.management.internal.cli.domain.DataCommandResult)

Example 8 with DataCommandResult

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;
    }
}
Also used : DistributedMember(org.apache.geode.distributed.DistributedMember) List(java.util.List) ArrayList(java.util.ArrayList) ResultCollector(org.apache.geode.cache.execute.ResultCollector) DataCommandResult(org.apache.geode.management.internal.cli.domain.DataCommandResult)

Example 9 with DataCommandResult

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);
    }
}
Also used : ArrayList(java.util.ArrayList) InternalCache(org.apache.geode.internal.cache.InternalCache) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) DistributedMember(org.apache.geode.distributed.DistributedMember) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) DataCommandResult(org.apache.geode.management.internal.cli.domain.DataCommandResult)

Example 10 with DataCommandResult

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);
            }
        }
    }
}
Also used : InternalCache(org.apache.geode.internal.cache.InternalCache) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) DataPolicy(org.apache.geode.cache.DataPolicy) DataCommandResult(org.apache.geode.management.internal.cli.domain.DataCommandResult)

Aggregations

DataCommandResult (org.apache.geode.management.internal.cli.domain.DataCommandResult)13 InternalCache (org.apache.geode.internal.cache.InternalCache)8 Region (org.apache.geode.cache.Region)7 DistributedMember (org.apache.geode.distributed.DistributedMember)6 DataCommandRequest (org.apache.geode.management.internal.cli.domain.DataCommandRequest)6 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)4 CliMetaData (org.apache.geode.management.cli.CliMetaData)4 DataCommandFunction (org.apache.geode.management.internal.cli.functions.DataCommandFunction)4 GfJsonObject (org.apache.geode.management.internal.cli.json.GfJsonObject)4 CliCommand (org.springframework.shell.core.annotation.CliCommand)4 ArrayList (java.util.ArrayList)2 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 List (java.util.List)1 DataPolicy (org.apache.geode.cache.DataPolicy)1 ResultCollector (org.apache.geode.cache.execute.ResultCollector)1 FunctionDomainException (org.apache.geode.cache.query.FunctionDomainException)1 NameResolutionException (org.apache.geode.cache.query.NameResolutionException)1 QueryInvalidException (org.apache.geode.cache.query.QueryInvalidException)1