Permalink
Browse files

refactor core workload to reduce redundant key and field generation code

  • Loading branch information...
Russell Sears
Russell Sears committed Nov 19, 2011
1 parent 2aea7d5 commit a1d5f7077b0a6593607bbe800b905b00a3829073
Showing with 44 additions and 67 deletions.
  1. +44 −67 src/com/yahoo/ycsb/workloads/CoreWorkload.java
@@ -411,6 +411,33 @@ else if (scanlengthdistrib.compareTo("zipfian")==0)
}
}
+ public String buildKeyName(int keynum) {
+ if (!orderedinserts)
+ {
+ keynum=Utils.hash(keynum);
+ }
+ return "user"+keynum;
+ }
+ HashMap<String, ByteIterator> buildValues() {
+ HashMap<String,ByteIterator> values=new HashMap<String,ByteIterator>();
+
+ for (int i=0; i<fieldcount; i++)
+ {
+ String fieldkey="field"+i;
+ ByteIterator data= new RandomByteIterator(fieldlengthgenerator.nextInt());
+ values.put(fieldkey,data);
+ }
+ return values;
+ }
+ HashMap<String, ByteIterator> buildUpdate() {
+ //update a random field
+ HashMap<String, ByteIterator> values=new HashMap<String,ByteIterator>();
+ String fieldname="field"+fieldchooser.nextString();
+ ByteIterator data = new RandomByteIterator(fieldlengthgenerator.nextInt());
+ values.put(fieldname,data);
+ return values;
+ }
+
/**
* Do one insert operation. Because it will be called concurrently from multiple client threads, this
* function must be thread safe. However, avoid synchronized, or the threads will block waiting for each
@@ -420,19 +447,8 @@ else if (scanlengthdistrib.compareTo("zipfian")==0)
public boolean doInsert(DB db, Object threadstate)
{
int keynum=keysequence.nextInt();
- if (!orderedinserts)
- {
- keynum=Utils.hash(keynum);
- }
- String dbkey="user"+keynum;
- HashMap<String,ByteIterator> values=new HashMap<String,ByteIterator>();
-
- for (int i=0; i<fieldcount; i++)
- {
- String fieldkey="field"+i;
- ByteIterator data= new RandomByteIterator(fieldlengthgenerator.nextInt());
- values.put(fieldkey,data);
- }
+ String dbkey = buildKeyName(keynum);
+ HashMap<String, ByteIterator> values = buildValues();
if (db.insert(table,dbkey,values) == 0)
return true;
else
@@ -483,12 +499,8 @@ public void doTransactionRead(DB db)
}
while (keynum>transactioninsertkeysequence.lastInt());
- if (!orderedinserts)
- {
- keynum=Utils.hash(keynum);
- }
- String keyname="user"+keynum;
-
+ String keyname = buildKeyName(keynum);
+
HashSet<String> fields=null;
if (!readallfields)
@@ -513,11 +525,7 @@ public void doTransactionReadModifyWrite(DB db)
}
while (keynum>transactioninsertkeysequence.lastInt());
- if (!orderedinserts)
- {
- keynum=Utils.hash(keynum);
- }
- String keyname="user"+keynum;
+ String keyname = buildKeyName(keynum);
HashSet<String> fields=null;
@@ -530,24 +538,17 @@ public void doTransactionReadModifyWrite(DB db)
fields.add(fieldname);
}
- HashMap<String,ByteIterator> values=new HashMap<String,ByteIterator>();
+ HashMap<String,ByteIterator> values;
if (writeallfields)
{
//new data for all the fields
- for (int i=0; i<fieldcount; i++)
- {
- String fieldname="field"+i;
- ByteIterator data = new RandomByteIterator(fieldlengthgenerator.nextInt());
- values.put(fieldname,data);
- }
+ values = buildValues();
}
else
{
//update a random field
- String fieldname="field"+fieldchooser.nextString();
- ByteIterator data = new RandomByteIterator(fieldlengthgenerator.nextInt());
- values.put(fieldname,data);
+ values = buildUpdate();
}
//do the transaction
@@ -573,11 +574,7 @@ public void doTransactionScan(DB db)
}
while (keynum>transactioninsertkeysequence.lastInt());
- if (!orderedinserts)
- {
- keynum=Utils.hash(keynum);
- }
- String startkeyname="user"+keynum;
+ String startkeyname = buildKeyName(keynum);
//choose a random scan length
int len=scanlength.nextInt();
@@ -606,30 +603,19 @@ public void doTransactionUpdate(DB db)
}
while (keynum>transactioninsertkeysequence.lastInt());
- if (!orderedinserts)
- {
- keynum=Utils.hash(keynum);
- }
- String keyname="user"+keynum;
+ String keyname=buildKeyName(keynum);
- HashMap<String,ByteIterator> values=new HashMap<String,ByteIterator>();
+ HashMap<String,ByteIterator> values;
if (writeallfields)
{
//new data for all the fields
- for (int i=0; i<fieldcount; i++)
- {
- String fieldname="field"+i;
- ByteIterator data = new RandomByteIterator(fieldlengthgenerator.nextInt());
- values.put(fieldname,data);
- }
+ values = buildValues();
}
else
{
//update a random field
- String fieldname="field"+fieldchooser.nextString();
- ByteIterator data = new RandomByteIterator(fieldlengthgenerator.nextInt());
- values.put(fieldname,data);
+ values = buildUpdate();
}
db.update(table,keyname,values);
@@ -639,19 +625,10 @@ public void doTransactionInsert(DB db)
{
//choose the next key
int keynum=transactioninsertkeysequence.nextInt();
- if (!orderedinserts)
- {
- keynum=Utils.hash(keynum);
- }
- String dbkey="user"+keynum;
-
- HashMap<String,ByteIterator> values=new HashMap<String,ByteIterator>();
- for (int i=0; i<fieldcount; i++)
- {
- String fieldkey="field"+i;
- ByteIterator data = new RandomByteIterator(fieldlengthgenerator.nextInt());
- values.put(fieldkey,data);
- }
+
+ String dbkey = buildKeyName(keynum);
+
+ HashMap<String, ByteIterator> values = buildValues();
db.insert(table,dbkey,values);
}
}

0 comments on commit a1d5f70

Please sign in to comment.