From d6f61f7db485de5e9cee3cbf686a72d0c57cc26d Mon Sep 17 00:00:00 2001
From: danielpoltx <daniel.pol@hpe.com>
Date: Sat, 16 Apr 2016 09:55:13 -0400
Subject: [PATCH] Added check for valid insertstart and insertcount Fixed nits
 suggested by @busbey

---
 .../main/java/com/yahoo/ycsb/Workload.java    | 18 ++---
 .../ycsb/generator/SequentialGenerator.java   | 79 +++++++++----------
 .../yahoo/ycsb/workloads/CoreWorkload.java    | 47 ++++++-----
 3 files changed, 73 insertions(+), 71 deletions(-)

diff --git a/core/src/main/java/com/yahoo/ycsb/Workload.java b/core/src/main/java/com/yahoo/ycsb/Workload.java
index 89356f31..3b66478d 100644
--- a/core/src/main/java/com/yahoo/ycsb/Workload.java
+++ b/core/src/main/java/com/yahoo/ycsb/Workload.java
@@ -28,7 +28,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
  * done by init().
  * 
  * If you extend this class, you should support the "insertstart" property. This 
- * allows the load phase to proceed from multiple clients on different machines, in case
+ * allows the Client to proceed from multiple clients on different machines, in case
  * the client is the bottleneck. For example, if we want to load 1 million records from
  * 2 machines, the first machine should have insertstart=0 and the second insertstart=500000. Additionally,
  * the "insertcount" property, which is interpreted by Client, can be used to tell each instance of the
@@ -36,13 +36,13 @@ import java.util.concurrent.atomic.AtomicBoolean;
  */
 public abstract class Workload
 {
-	public static final String INSERT_START_PROPERTY="insertstart";
-	public static final String INSERT_COUNT_PROPERTY="insertcount";
-	
-	public static final String INSERT_START_PROPERTY_DEFAULT="0";
-	
-	private volatile AtomicBoolean stopRequested = new AtomicBoolean(false);
-	
+  public static final String INSERT_START_PROPERTY = "insertstart";
+  public static final String INSERT_COUNT_PROPERTY = "insertcount";
+  
+  public static final String INSERT_START_PROPERTY_DEFAULT = "0";
+  
+  private volatile AtomicBoolean stopRequested = new AtomicBoolean(false);
+  
       /**
        * Initialize the scenario. Create any generators and other shared objects here.
        * Called once, in the main client thread, before any operations are started.
@@ -65,7 +65,7 @@ public abstract class Workload
        */
       public Object initThread(Properties p, int mythreadid, int threadcount) throws WorkloadException
       {
-	 return null;
+   return null;
       }
       
       /**
diff --git a/core/src/main/java/com/yahoo/ycsb/generator/SequentialGenerator.java b/core/src/main/java/com/yahoo/ycsb/generator/SequentialGenerator.java
index 7e2bfa84..e30115e3 100644
--- a/core/src/main/java/com/yahoo/ycsb/generator/SequentialGenerator.java
+++ b/core/src/main/java/com/yahoo/ycsb/generator/SequentialGenerator.java
@@ -1,5 +1,5 @@
 /**                                                                                                                                                                                
- * Copyright (c) 2010 Yahoo! Inc. All rights reserved.                                                                                                                             
+ * Copyright (c) 2016 YCSB Contributors 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                                                                                                                
@@ -22,46 +22,41 @@ import java.util.concurrent.atomic.AtomicInteger;
 /**
  * Generates a sequence of integers 0, 1, ...
  */
-public class SequentialGenerator extends NumberGenerator
-{
-	final AtomicInteger counter;
-	int _interval,_countstart;
+public class SequentialGenerator extends NumberGenerator {
+  final AtomicInteger counter;
+  int _interval,_countstart;
 
-	/**
-	 * Create a counter that starts at countstart
-	 */
-	public SequentialGenerator(int countstart, int countend)
-	{
-		counter=new AtomicInteger();
-		setLastValue(counter.get());
-		_countstart=countstart;
-		_interval=countend-countstart+1;
-	}
-	
-	/**
-	 * If the generator returns numeric (integer) values, return the next value as an int. Default is to return -1, which
-	 * is appropriate for generators that do not return numeric values.
-	 */
-	public int nextInt() 
-	{
-		int ret = _countstart + counter.getAndIncrement()%_interval;
-		setLastValue(ret);
-		return ret;
-	}
-	@Override
-	public Number nextValue() 
-	{
-		int ret = _countstart + counter.getAndIncrement()%_interval;
-		setLastValue(ret);
-		return ret;
-	}
-	@Override
-	public Number lastValue()
-	{
-	                return counter.get() + 1;
-	}
-	@Override
-	public double mean() {
-		throw new UnsupportedOperationException("Can't compute mean of non-stationary distribution!");
-	}
+  /**
+   * Create a counter that starts at countstart
+   */
+  public SequentialGenerator(int countstart, int countend) {
+    counter = new AtomicInteger();
+    setLastValue(counter.get());
+    _countstart = countstart;
+    _interval = countend - countstart + 1;
+  }
+  
+  /**
+   * If the generator returns numeric (integer) values, return the next value as an int. Default is to return -1, which
+   * is appropriate for generators that do not return numeric values.
+   */
+  public int nextInt() {
+    int ret = _countstart + counter.getAndIncrement()%_interval;
+    setLastValue(ret);
+    return ret;
+  }
+  @Override
+  public Number nextValue() {
+    int ret = _countstart + counter.getAndIncrement()%_interval;
+    setLastValue(ret);
+    return ret;
+  }
+  @Override
+  public Number lastValue() {
+                  return counter.get() + 1;
+  }
+  @Override
+  public double mean() {
+    throw new UnsupportedOperationException("Can't compute mean of non-stationary distribution!");
+  }
 }
diff --git a/core/src/main/java/com/yahoo/ycsb/workloads/CoreWorkload.java b/core/src/main/java/com/yahoo/ycsb/workloads/CoreWorkload.java
index bdbb52a8..5f38cffb 100644
--- a/core/src/main/java/com/yahoo/ycsb/workloads/CoreWorkload.java
+++ b/core/src/main/java/com/yahoo/ycsb/workloads/CoreWorkload.java
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
+ * Copyright (c) 2010 Yahoo! Inc., 2016 YCSB Contributors 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
@@ -21,27 +21,28 @@ import java.util.Properties;
 
 import com.yahoo.ycsb.*;
 import com.yahoo.ycsb.generator.AcknowledgedCounterGenerator;
+import com.yahoo.ycsb.generator.ConstantIntegerGenerator;
 import com.yahoo.ycsb.generator.CounterGenerator;
 import com.yahoo.ycsb.generator.DiscreteGenerator;
 import com.yahoo.ycsb.generator.ExponentialGenerator;
-import com.yahoo.ycsb.generator.ConstantIntegerGenerator;
-import com.yahoo.ycsb.generator.HotspotIntegerGenerator;
 import com.yahoo.ycsb.generator.HistogramGenerator;
+import com.yahoo.ycsb.generator.HotspotIntegerGenerator;
 import com.yahoo.ycsb.generator.NumberGenerator;
 import com.yahoo.ycsb.generator.ScrambledZipfianGenerator;
+import com.yahoo.ycsb.generator.SequentialGenerator;
 import com.yahoo.ycsb.generator.SkewedLatestGenerator;
 import com.yahoo.ycsb.generator.UniformIntegerGenerator;
 import com.yahoo.ycsb.generator.ZipfianGenerator;
-import com.yahoo.ycsb.generator.SequentialGenerator;
 import com.yahoo.ycsb.measurements.Measurements;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.Vector;
 import java.util.List;
 import java.util.Map;
-import java.util.ArrayList;
+import java.util.Vector;
+
 
 /**
  * The core benchmark scenario. Represents a set of clients doing simple CRUD operations. The
@@ -415,12 +416,18 @@ public class CoreWorkload extends Workload {
     String scanlengthdistrib =
         p.getProperty(SCAN_LENGTH_DISTRIBUTION_PROPERTY, SCAN_LENGTH_DISTRIBUTION_PROPERTY_DEFAULT);
 
-	int insertstart =
-		Integer.parseInt(p.getProperty(INSERT_START_PROPERTY,INSERT_START_PROPERTY_DEFAULT));
-	int insertcount =
-		Integer.parseInt(p.getProperty(INSERT_COUNT_PROPERTY,String.valueOf(recordcount-insertstart)));
-	zeropadding =
-		Integer.parseInt(p.getProperty(ZERO_PADDING_PROPERTY,ZERO_PADDING_PROPERTY_DEFAULT));
+  int insertstart =
+    Integer.parseInt(p.getProperty(INSERT_START_PROPERTY,INSERT_START_PROPERTY_DEFAULT));
+  int insertcount =
+    Integer.parseInt(p.getProperty(INSERT_COUNT_PROPERTY,String.valueOf(recordcount-insertstart)));
+  // Confirm valid values for insertstart and insertcount in relation to recordcount
+  if (recordcount < (insertstart + insertcount) ) {
+      System.err.println("Invalid combination of insertstart, insertcount and recordcount.");
+      System.err.println("recordcount must be bigger than insertstart + insertcount.");
+      System.exit(-1);
+  }
+  zeropadding =
+    Integer.parseInt(p.getProperty(ZERO_PADDING_PROPERTY,ZERO_PADDING_PROPERTY_DEFAULT));
 
     readallfields = Boolean.parseBoolean(
         p.getProperty(READ_ALL_FIELDS_PROPERTY, READ_ALL_FIELDS_PROPERTY_DEFAULT));
@@ -479,9 +486,9 @@ public class CoreWorkload extends Workload {
     transactioninsertkeysequence = new AcknowledgedCounterGenerator(recordcount);
     if (requestdistrib.compareTo("uniform") == 0) {
       keychooser=new UniformIntegerGenerator(insertstart,insertstart+insertcount-1);
-    } else if (requestdistrib.compareTo("sequential")==0)	{
-			keychooser=new SequentialGenerator(insertstart,insertstart+insertcount-1);
-	}else if (requestdistrib.compareTo("zipfian") == 0) {
+    } else if (requestdistrib.compareTo("sequential")==0) {
+      keychooser=new SequentialGenerator(insertstart,insertstart+insertcount-1);
+  }else if (requestdistrib.compareTo("zipfian") == 0) {
       // it does this by generating a random "next key" in part by taking the modulus over the
       // number of keys.
       // If the number of keys changes, this would shift the modulus, and we don't want that to
@@ -530,11 +537,11 @@ public class CoreWorkload extends Workload {
     if (!orderedinserts) {
       keynum = Utils.hash(keynum);
     }
-	String value=Long.toString(keynum);
-	int fill=zeropadding - value.length();
-	String prekey="user";
-	for(int i=0; i<fill;i++) {prekey+='0';}
-	return prekey+value;
+  String value = Long.toString(keynum);
+  int fill = zeropadding - value.length();
+  String prekey = "user";
+  for(int i=0; i<fill;i++) {prekey += '0';}
+  return prekey + value;
   }
 
   /**
-- 
GitLab