<commit_before>/*
 * Copyright (c) 2016 SnappyData, Inc. All rights reserved.
 *
 * Licensed 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. See accompanying
 * LICENSE file.
 */
package com.pivotal.gemfirexd.internal.engine.distributed;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;

import com.gemstone.gemfire.cache.Declarable;
import com.gemstone.gemfire.cache.execute.Function;
import com.gemstone.gemfire.cache.execute.FunctionContext;
import com.gemstone.gemfire.internal.cache.BucketRegion;
import com.gemstone.gemfire.internal.cache.PartitionedRegion;
import com.pivotal.gemfirexd.internal.engine.Misc;

public class RegionSizeCalculatorFunction implements Function , Declarable {

  public static String ID = "RegionSizeCalculatorFunction";

  @Override
  public void init(Properties props) {

  }

  @Override
  public boolean hasResult() {
    return true;
  }

  @Override
  public void execute(FunctionContext context) {
    HashMap<String, Long> regionSizeInfo = new HashMap<>();

    Set<PartitionedRegion> partitionedRegions = Misc.getGemFireCache().getPartitionedRegions();
    for (PartitionedRegion pr : partitionedRegions) {
      long valueSizeOfRegion = getSizeForAllPrimaryBucketsOfRegion(pr);
      String qualifiedTableName = Misc.getFullTableNameFromRegionPath(pr.getFullPath());
      regionSizeInfo.put(qualifiedTableName, valueSizeOfRegion);
    }
    context.getResultSender().lastResult(regionSizeInfo);
  }

  @Override
  public String getId() {
    return ID;
  }

  @Override
  public boolean optimizeForWrite() {
    return false;
  }

  @Override
  public boolean isHA() {
    return true;
  }


  private long getSizeForAllPrimaryBucketsOfRegion(PartitionedRegion region) {
    long sizeOfRegion = 0;
    Set<BucketRegion> bucketRegions = region.getDataStore().getAllLocalBucketRegions();
    for (BucketRegion br: bucketRegions) {
      sizeOfRegion += br.getTotalBytes();
    }
    return sizeOfRegion;
  }
}
<commit_msg>Fix for  NPE (#69)<commit_after>/*
 * Copyright (c) 2016 SnappyData, Inc. All rights reserved.
 *
 * Licensed 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. See accompanying
 * LICENSE file.
 */
package com.pivotal.gemfirexd.internal.engine.distributed;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;

import com.gemstone.gemfire.cache.Declarable;
import com.gemstone.gemfire.cache.execute.Function;
import com.gemstone.gemfire.cache.execute.FunctionContext;
import com.gemstone.gemfire.internal.cache.BucketRegion;
import com.gemstone.gemfire.internal.cache.PartitionedRegion;
import com.gemstone.gemfire.internal.cache.PartitionedRegionDataStore;
import com.pivotal.gemfirexd.internal.engine.Misc;

public class RegionSizeCalculatorFunction implements Function , Declarable {

  public static String ID = "RegionSizeCalculatorFunction";

  @Override
  public void init(Properties props) {

  }

  @Override
  public boolean hasResult() {
    return true;
  }

  @Override
  public void execute(FunctionContext context) {
    HashMap<String, Long> regionSizeInfo = new HashMap<>();

    Set<PartitionedRegion> partitionedRegions = Misc.getGemFireCache().getPartitionedRegions();
    for (PartitionedRegion pr : partitionedRegions) {
      long valueSizeOfRegion = getSizeForAllPrimaryBucketsOfRegion(pr);
      String qualifiedTableName = Misc.getFullTableNameFromRegionPath(pr.getFullPath());
      regionSizeInfo.put(qualifiedTableName, valueSizeOfRegion);
    }
    context.getResultSender().lastResult(regionSizeInfo);
  }

  @Override
  public String getId() {
    return ID;
  }

  @Override
  public boolean optimizeForWrite() {
    return false;
  }

  @Override
  public boolean isHA() {
    return true;
  }


  private long getSizeForAllPrimaryBucketsOfRegion(PartitionedRegion region) {
    long sizeOfRegion = 0;
    PartitionedRegionDataStore datastore = region.getDataStore();
    if (datastore != null ) {
      Set<BucketRegion> bucketRegions = datastore.getAllLocalBucketRegions();
      for (BucketRegion br : bucketRegions) {
        sizeOfRegion += br.getTotalBytes();
      }
    }
    return sizeOfRegion;
  }
}

{'fraction_non_alphanumeric': 0.0882862088863243, 'fraction_numerical': 0.0038468936333910368, 'mean_word_length': 4.371900826446281, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4447482', 'n_tokens_mistral': 1500, 'n_tokens_neox': 1453, 'n_words': 405}