/*
 * 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;
  }
}

@@ -1082,32 +1082,103 @@
 titionedRegion;%0A
+import com.gemstone.gemfire.internal.cache.PartitionedRegionDataStore;%0A
 import com.pivot
@@ -2339,16 +2339,114 @@
 on = 0;%0A
+    PartitionedRegionDataStore datastore = region.getDataStore();%0A    if (datastore != null ) %7B%0A  
     Set%3C
@@ -2475,37 +2475,25 @@
 gions = 
-region.getDataS
+datas
 tore
-()
 .getAllL
@@ -2505,32 +2505,34 @@
 ucketRegions();%0A
+  
     for (BucketR
@@ -2539,16 +2539,17 @@
 egion br
+ 
 : bucket
@@ -2555,24 +2555,26 @@
 tRegions) %7B%0A
+  
       sizeOf
@@ -2599,24 +2599,32 @@
 talBytes();%0A
+      %7D%0A
     %7D%0A    re

Fix for NPE (#69)