Skip to content

Instantly share code, notes, and snippets.

@jouderianjr
Last active August 29, 2015 14:06
Show Gist options
  • Save jouderianjr/d371753d272918c106ae to your computer and use it in GitHub Desktop.
Save jouderianjr/d371753d272918c106ae to your computer and use it in GitHub Desktop.
HashJoin.java
import java.util.ArrayList;
import br.com.unifor.models.beans.Cd;
import br.com.unifor.models.beans.Gravadora;
import br.com.unifor.models.beans.Joined;
public class HashJoin {
private int id;
private int qntBuckets;
private ArrayList<Cd> cds;
private ArrayList<Gravadora> gravadoras;
private ArrayList<Cd>[] bucketsCds;
private ArrayList<Gravadora>[] bucketsGravadoras;
public HashJoin(int id, ArrayList<Cd> cds, ArrayList<Gravadora> gravadoras, int qntBuckets){
this.id = id;
this.cds = cds;
this.gravadoras = gravadoras;
this.qntBuckets = qntBuckets;
this.bucketsCds = new ArrayList[qntBuckets];
this.bucketsGravadoras = new ArrayList[qntBuckets];
}
public ArrayList hashJoin(ArrayList<Cd> cds, ArrayList<Gravadora> gravadoras, int qntBuckets, int id){
return null;
}
public ArrayList<Joined> getJoin(){
ArrayList<Joined> result = new ArrayList<Joined>();
applyHashFunction();
int hashOfQueryId = hashFunction(id);
ArrayList<Cd> bucketCds = bucketsCds[hashOfQueryId];
ArrayList<Gravadora> bucketGravadoras = bucketsGravadoras[hashOfQueryId];
for (Gravadora gravadora : bucketGravadoras) {
for (Cd cd : bucketCds) {
if(cd.getGravadoraId() == gravadora.getId()){
result.add(new Joined(gravadora, cd));
}
}
}
return result;
}
private void applyHashFunction(){
for (Cd cd : cds) {
int hash = hashFunction(cd.getGravadoraId());
bucketsCds[hash].add(cd);
}
for (Gravadora gravadora : gravadoras) {
int hash = hashFunction(gravadora.getId());
bucketsGravadoras[hash].add(gravadora);
}
}
private int hashFunction(int id){
return (id % qntBuckets);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment