-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
StackExchangePostsDb.java
171 lines (136 loc) · 5.6 KB
/
StackExchangePostsDb.java
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package nu.marginalia.integration.stackexchange.sqlite;
import com.github.luben.zstd.Zstd;
import gnu.trove.list.TIntList;
import gnu.trove.list.array.TIntArrayList;
import gnu.trove.map.TIntIntMap;
import gnu.trove.map.hash.TIntIntHashMap;
import lombok.SneakyThrows;
import nu.marginalia.integration.stackexchange.xml.StackExchangeXmlPostReader;
import org.apache.commons.compress.compressors.zstandard.ZstdUtils;
import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.function.Predicate;
public class StackExchangePostsDb {
@SneakyThrows
public static void create(Path sqliteFile,
Path stackExchange7zFile) {
if (Files.exists(sqliteFile))
Files.delete(sqliteFile);
String connStr = "jdbc:sqlite:" + sqliteFile;
try (var connection = DriverManager.getConnection(connStr);
var stream = ClassLoader.getSystemResourceAsStream("db/stackexchange.sql");
var updateStmt = connection.createStatement()
) {
var sql = new String(stream.readAllBytes());
String[] sqlParts = sql.split(";");
for (var part : sqlParts) {
if (part.isBlank()) {
continue;
}
updateStmt.executeUpdate(part);
}
updateStmt.execute("PRAGMA synchronous = OFF");
var postReader = new StackExchangeXmlPostReader(
stackExchange7zFile
);
var insertPost = connection.prepareStatement("""
INSERT INTO post(id, threadId, postYear, title, body, origSize, tags)
VALUES (?, ?, ?, ?, ?, ?, ?)
""");
var iter = postReader.iterator();
int cnt = 0;
while (iter.hasNext()) {
var post = iter.next();
insertPost.setInt(1, post.id());
if (post.parentId() == null) insertPost.setInt(2, post.id());
else insertPost.setInt(2, post.parentId());
insertPost.setInt(3, post.year());
if (post.title() == null)
insertPost.setString(4, "");
else
insertPost.setString(4, post.title());
byte[] bodyBytes = post.body().getBytes();
insertPost.setBytes(5, Zstd.compress(bodyBytes));
insertPost.setInt(6, bodyBytes.length);
insertPost.setString(7, String.join(",", post.tags()));
insertPost.addBatch();
if (++cnt > 100) {
insertPost.executeBatch();
cnt = 0;
}
}
if (cnt > 0) {
insertPost.executeBatch();
}
}
catch (IOException | SQLException | XMLStreamException e) {
e.printStackTrace();
}
}
@SneakyThrows
public static void forEachPost(
Path sqliteFile,
Predicate<CombinedPostModel> consumer) {
String connStr = "jdbc:sqlite:" + sqliteFile;
try (var connection = DriverManager.getConnection(connStr);
var selectThreadIds = connection.prepareStatement("SELECT DISTINCT(threadId) FROM post");
var queryPostContents = connection.prepareStatement("""
SELECT postYear, title, body, origSize, tags
FROM post
WHERE threadId = ?
""")
) {
TIntList threadIds = new TIntArrayList(10_000);
ResultSet rs = selectThreadIds.executeQuery();
while (rs.next()) {
threadIds.add(rs.getInt(1));
}
System.out.println("Got " + threadIds.size() + " IDs");
var idIterator = threadIds.iterator();
int ordinal = 0;
while (idIterator.hasNext()) {
queryPostContents.setInt(1, idIterator.next());
rs = queryPostContents.executeQuery();
List<String> parts = new ArrayList<>();
String title = "";
int year = 2023;
List<Future<String>> partWork = new ArrayList<>();
var commonPool = ForkJoinPool.commonPool();
while (rs.next()) {
String maybeTitle = rs.getString("title");
if (maybeTitle != null && !maybeTitle.isBlank())
title = maybeTitle;
int origSize = rs.getInt("origSize");
year = Math.min(year, rs.getInt("postYear"));
byte[] bytes = rs.getBytes("body");
partWork.add(commonPool.submit(
() -> new String(Zstd.decompress(bytes, origSize)
)));
}
for (var workItem : partWork) {
parts.add(workItem.get());
}
if (!consumer.test(new CombinedPostModel(ordinal++, title, year, parts)))
break;
}
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
public record CombinedPostModel(int ordinal,
String title,
int year,
List<String> bodies)
{
}
}