My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.softwaretrajectory.msr.stack.data;

import java.io.FileInputStream;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import org.hackystat.utilities.tstamp.Tstamp;
import com.softwaretrajectory.msr.stack.db.StackDB;
import com.softwaretrajectory.msr.stack.db.StackDBManager;
import com.softwaretrajectory.msr.stack.persistence.PostHistory;
import com.softwaretrajectory.msr.stack.persistence.Vote;

/**
* Parse the challenge change XML and load it into database.
*
* @author Pavel Senin
*
*/
public class LoadPostHistory {

/** The file location. */
// private static final String fileLocation =
// "/media/backup/XML/home/users/ponza/stack_overflow_201208/votes.xml";
private static final String fileLocation = "/media/Stock/home/users/ponza/stack_overflow_201208/posthistory_noC.xml";

private static final String ROOT_ELEMENT = "posthistory";

private static final String ROW_ELEMENT = "row";

private static StackDB db;

/**
* Main runnable class.
*
* @param args None used.
* @throws Exception if error occurs.
*/
public static void main(String[] args) throws Exception {

db = StackDBManager.getProductionInstance();

// get the XML file handler
//
FileInputStream fileInputStream = new FileInputStream(fileLocation);
XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(
fileInputStream);

// set-up time tracking
//
int recordCounter = 0;
XMLGregorianCalendar startTstamp = Tstamp.makeTimestamp();

// run begins
//
while (xmlStreamReader.hasNext()) {

int eventCode = xmlStreamReader.next();

if ((XMLStreamConstants.START_ELEMENT == eventCode)
&& xmlStreamReader.getLocalName().equalsIgnoreCase(ROOT_ELEMENT)) {

// parse the stream looking for the start of something within change
//
while (xmlStreamReader.hasNext()) {

eventCode = xmlStreamReader.next();

if ((XMLStreamConstants.END_ELEMENT == eventCode)
&& xmlStreamReader.getLocalName().equalsIgnoreCase(ROOT_ELEMENT)) {
break;
}
else {

if ((XMLStreamConstants.START_ELEMENT == eventCode)
&& xmlStreamReader.getLocalName().equalsIgnoreCase(ROW_ELEMENT)) {

PostHistory record = new PostHistory();

int attributesCount = xmlStreamReader.getAttributeCount();
for (int i = 0; i < attributesCount; i++) {
record.setAttribute(xmlStreamReader.getAttributeLocalName(i),
xmlStreamReader.getAttributeValue(i));
}
db.savePostHistory(record);
recordCounter++;

if (recordCounter % 20000 == 0) {
db.commit();

XMLGregorianCalendar cTime = Tstamp.makeTimestamp();
System.err.println("+20K posthistory records loaded and saved, total records: "
+ recordCounter);

System.err.println(" .. . time from start: " + timeToString(startTstamp, cTime));
}
}
}
}
}
}

db.shutDown();

}

@SuppressWarnings("unused")
private static String timeToString(XMLGregorianCalendar start, XMLGregorianCalendar finish) {
long l1 = start.toGregorianCalendar().getTimeInMillis();
long l2 = finish.toGregorianCalendar().getTimeInMillis();
long diff = l2 - l1;

long secondInMillis = 1000;
long minuteInMillis = secondInMillis * 60;
long hourInMillis = minuteInMillis * 60;
long dayInMillis = hourInMillis * 24;
long yearInMillis = dayInMillis * 365;

long elapsedYears = diff / yearInMillis;
diff = diff % yearInMillis;
long elapsedDays = diff / dayInMillis;
diff = diff % dayInMillis;
long elapsedHours = diff / hourInMillis;
diff = diff % hourInMillis;
long elapsedMinutes = diff / minuteInMillis;
diff = diff % minuteInMillis;
long elapsedSeconds = diff / secondInMillis;

return elapsedHours + "h " + elapsedMinutes + "m " + elapsedSeconds + "s";
}
}

Change log

r1062 by seninp on Oct 17, 2012   Diff
PostHistory loader works.
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 4247 bytes, 131 lines

File properties

svn:mime-type
text/plain
Powered by Google Project Hosting