Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save playerjamesbattleground/208f94fd5ad0468d52e4 to your computer and use it in GitHub Desktop.

Select an option

Save playerjamesbattleground/208f94fd5ad0468d52e4 to your computer and use it in GitHub Desktop.
search algorithm
/*
Class Subject
id
Map<String,Participant> participants
Class Participant
Map<String,List<String>>attributes
*/
//This method is within a DAO class and under Spring context
public List<Subject> lookUp(String entityName) {
Long timer1 = new Date().getTime();
String sql = "select * from xref where entity_name='"+entityName
+"' order by id,participant,attribute,attribute_index";
final List<Subject> results;
final Map<Integer,Subject> subjectIdMap = new HashMap<Integer,Subject>();
jdbcTemplate.query(sql, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
int subjectId = rs.getInt("id");
String participantName = rs.getString("participant");
String attributeName = rs.getString("attribute");
String value = rs.getString("value");
int attributeIndex = rs.getInt("attribute_Index");
if(!subjectIdMap.containsKey(subjectId)) {
List<String> values = new ArrayList<String>();
values.add(value);
Map<String,List<String>> attributes = new HashMap<String,List<String>>();
attributes.put(attributeName, values);
Participant participant = new Participant();
participant.setAttributes(attributes);
Map<String,Participant> participants = new HashMap<String,Participant>();
participants.put(participantName, participant);
Subject subject = new Subject();
subject.setId(subjectId);
subject.setParticipants(participants);
subjectIdMap.put(subjectId, subject);
}else {
Subject subject1 = subjectIdMap.get(subjectId);
Map<String,Participant> participants1 = subject1.getParticipants();
if(!participants1.containsKey(participantName)) {
List<String> values1 = new ArrayList<String>();
values1.add(value);
Map<String,List<String>> attributes1 = new HashMap<String,List<String>>();
attributes1.put(attributeName, values1);
Participant participant1 = new Participant();
participant1.setAttributes(attributes1);
participants1.put(participantName, participant1);
subject1.setParticipants(participants1);
subjectIdMap.put(subjectId, subject1);
}else {
Participant participant2 = participants1.get(participantName);
Map<String,List<String>> attributes2 = participant2.getAttributes();
if(!attributes2.containsKey(attributeName)) {
List<String> values2 = new ArrayList<String>();
values2.add(value);
attributes2.put(attributeName,values2);
participant2.setAttributes(attributes2);
participants1.put(participantName, participant2);
subject1.setParticipants(participants1);
subjectIdMap.put(subjectId,subject1);
}else {
List<String> values3 = attributes2.get(attributeName);
values3.add(value);
attributes2.put(attributeName, values3);
participant2.setAttributes(attributes2);
participants1.put(participantName, participant2);
subject1.setParticipants(participants1);
subjectIdMap.put(subjectId, subject1);
}
}
}//first if
}
});
System.out.println("generated nested map"+subjectIdMap);
Long timer2 = new Date().getTime();
results = new ArrayList<Subject>(subjectIdMap.values());
System.out.println("finished in "+(timer2-timer1));
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment