Skip to content
Snippets Groups Projects
Commit a1d5f707 authored by Russell Sears's avatar Russell Sears
Browse files

refactor core workload to reduce redundant key and field generation code

parent 2aea7d50
No related branches found
No related tags found
No related merge requests found
...@@ -411,6 +411,33 @@ public class CoreWorkload extends Workload ...@@ -411,6 +411,33 @@ public class CoreWorkload extends Workload
} }
} }
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 * 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 * function must be thread safe. However, avoid synchronized, or the threads will block waiting for each
...@@ -420,19 +447,8 @@ public class CoreWorkload extends Workload ...@@ -420,19 +447,8 @@ public class CoreWorkload extends Workload
public boolean doInsert(DB db, Object threadstate) public boolean doInsert(DB db, Object threadstate)
{ {
int keynum=keysequence.nextInt(); int keynum=keysequence.nextInt();
if (!orderedinserts) String dbkey = buildKeyName(keynum);
{ HashMap<String, ByteIterator> values = buildValues();
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);
}
if (db.insert(table,dbkey,values) == 0) if (db.insert(table,dbkey,values) == 0)
return true; return true;
else else
...@@ -483,12 +499,8 @@ public class CoreWorkload extends Workload ...@@ -483,12 +499,8 @@ public class CoreWorkload extends Workload
} }
while (keynum>transactioninsertkeysequence.lastInt()); while (keynum>transactioninsertkeysequence.lastInt());
if (!orderedinserts) String keyname = buildKeyName(keynum);
{
keynum=Utils.hash(keynum);
}
String keyname="user"+keynum;
HashSet<String> fields=null; HashSet<String> fields=null;
if (!readallfields) if (!readallfields)
...@@ -513,11 +525,7 @@ public class CoreWorkload extends Workload ...@@ -513,11 +525,7 @@ public class CoreWorkload extends Workload
} }
while (keynum>transactioninsertkeysequence.lastInt()); while (keynum>transactioninsertkeysequence.lastInt());
if (!orderedinserts) String keyname = buildKeyName(keynum);
{
keynum=Utils.hash(keynum);
}
String keyname="user"+keynum;
HashSet<String> fields=null; HashSet<String> fields=null;
...@@ -530,24 +538,17 @@ public class CoreWorkload extends Workload ...@@ -530,24 +538,17 @@ public class CoreWorkload extends Workload
fields.add(fieldname); fields.add(fieldname);
} }
HashMap<String,ByteIterator> values=new HashMap<String,ByteIterator>(); HashMap<String,ByteIterator> values;
if (writeallfields) if (writeallfields)
{ {
//new data for all the fields //new data for all the fields
for (int i=0; i<fieldcount; i++) values = buildValues();
{
String fieldname="field"+i;
ByteIterator data = new RandomByteIterator(fieldlengthgenerator.nextInt());
values.put(fieldname,data);
}
} }
else else
{ {
//update a random field //update a random field
String fieldname="field"+fieldchooser.nextString(); values = buildUpdate();
ByteIterator data = new RandomByteIterator(fieldlengthgenerator.nextInt());
values.put(fieldname,data);
} }
//do the transaction //do the transaction
...@@ -573,11 +574,7 @@ public class CoreWorkload extends Workload ...@@ -573,11 +574,7 @@ public class CoreWorkload extends Workload
} }
while (keynum>transactioninsertkeysequence.lastInt()); while (keynum>transactioninsertkeysequence.lastInt());
if (!orderedinserts) String startkeyname = buildKeyName(keynum);
{
keynum=Utils.hash(keynum);
}
String startkeyname="user"+keynum;
//choose a random scan length //choose a random scan length
int len=scanlength.nextInt(); int len=scanlength.nextInt();
...@@ -606,30 +603,19 @@ public class CoreWorkload extends Workload ...@@ -606,30 +603,19 @@ public class CoreWorkload extends Workload
} }
while (keynum>transactioninsertkeysequence.lastInt()); while (keynum>transactioninsertkeysequence.lastInt());
if (!orderedinserts) String keyname=buildKeyName(keynum);
{
keynum=Utils.hash(keynum);
}
String keyname="user"+keynum;
HashMap<String,ByteIterator> values=new HashMap<String,ByteIterator>(); HashMap<String,ByteIterator> values;
if (writeallfields) if (writeallfields)
{ {
//new data for all the fields //new data for all the fields
for (int i=0; i<fieldcount; i++) values = buildValues();
{
String fieldname="field"+i;
ByteIterator data = new RandomByteIterator(fieldlengthgenerator.nextInt());
values.put(fieldname,data);
}
} }
else else
{ {
//update a random field //update a random field
String fieldname="field"+fieldchooser.nextString(); values = buildUpdate();
ByteIterator data = new RandomByteIterator(fieldlengthgenerator.nextInt());
values.put(fieldname,data);
} }
db.update(table,keyname,values); db.update(table,keyname,values);
...@@ -639,19 +625,10 @@ public class CoreWorkload extends Workload ...@@ -639,19 +625,10 @@ public class CoreWorkload extends Workload
{ {
//choose the next key //choose the next key
int keynum=transactioninsertkeysequence.nextInt(); int keynum=transactioninsertkeysequence.nextInt();
if (!orderedinserts)
{ String dbkey = buildKeyName(keynum);
keynum=Utils.hash(keynum);
} HashMap<String, ByteIterator> values = buildValues();
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);
}
db.insert(table,dbkey,values); db.insert(table,dbkey,values);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment