Skip to content

Commit c0af457

Browse files
committed
v7.9.44
1 parent bb3fc03 commit c0af457

4 files changed

Lines changed: 116 additions & 98 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nosflare",
3-
"version": "7.9.43",
3+
"version": "7.9.44",
44
"main": "worker.js",
55
"type": "module",
66
"scripts": {

src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const relayInfo: RelayInfo = {
2323
contact: "lux@fed.wtf",
2424
supported_nips: [1, 2, 4, 5, 9, 11, 12, 13, 15, 16, 17, 20, 22, 25, 28, 33, 40, 42, 57],
2525
software: "https://github.com/Spl0itable/nosflare",
26-
version: "7.9.43",
26+
version: "7.9.44",
2727
icon: "https://raw.githubusercontent.com/Spl0itable/nosflare/main/images/flare.png",
2828

2929
// Optional fields (uncomment as needed):

src/relay-worker.ts

Lines changed: 63 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,69 @@ const CHUNK_SIZE = 500;
3030

3131
// Database initialization
3232
async function initializeDatabase(db: D1Database): Promise<void> {
33+
const dropSession = db.withSession('first-primary');
34+
3335
try {
34-
const session = db.withSession('first-unconstrained');
35-
const initCheck = await session.prepare(
36+
await dropSession.prepare(`
37+
CREATE TABLE IF NOT EXISTS system_config (
38+
key TEXT PRIMARY KEY,
39+
value TEXT NOT NULL,
40+
created_at INTEGER DEFAULT (strftime('%s', 'now'))
41+
)
42+
`).run();
43+
} catch (_) {}
44+
45+
const cleanupDone = await dropSession.prepare(
46+
"SELECT value FROM system_config WHERE key = 'cleanup_v1' LIMIT 1"
47+
).first().catch(() => null);
48+
49+
if (!cleanupDone || cleanupDone.value !== '1') {
50+
const dropIndexes = [
51+
'idx_events_pubkey',
52+
'idx_events_kind',
53+
'idx_events_created_at_kind',
54+
'idx_events_authors_kinds',
55+
'idx_events_tag_p_created_at',
56+
'idx_events_tag_e_created_at',
57+
'idx_events_tag_a_created_at',
58+
'idx_events_tag_t_created_at',
59+
'idx_events_tag_d_created_at',
60+
'idx_events_tag_r_created_at',
61+
'idx_events_tag_L_created_at',
62+
'idx_events_tag_s_created_at',
63+
'idx_events_tag_u_created_at',
64+
'idx_events_kind_tag_p',
65+
'idx_events_kind_tag_e',
66+
'idx_events_kind_tag_a',
67+
'idx_events_kind_tag_t',
68+
'idx_events_kind_tag_L',
69+
'idx_events_kind_tag_s',
70+
'idx_events_reply_to',
71+
'idx_events_root_thread',
72+
'idx_events_kind_created_at_covering',
73+
'idx_events_pubkey_kind_created_at_covering',
74+
'idx_events_created_at_covering',
75+
'idx_events_kind_pubkey_created_at_covering',
76+
'idx_tags_name_value',
77+
'idx_tags_value',
78+
'idx_tags_name_value_event_created',
79+
];
80+
for (const idx of dropIndexes) {
81+
await dropSession.prepare(`DROP INDEX IF EXISTS ${idx}`).run();
82+
}
83+
84+
const dropTables = ['event_tags_cache', 'mv_follow_graph', 'mv_recent_notes', 'mv_timeline_cache'];
85+
for (const tbl of dropTables) {
86+
await dropSession.prepare(`DROP TABLE IF EXISTS ${tbl}`).run();
87+
}
88+
89+
await dropSession.prepare(
90+
"INSERT OR REPLACE INTO system_config (key, value) VALUES ('cleanup_v1', '1')"
91+
).run();
92+
}
93+
94+
try {
95+
const initCheck = await dropSession.prepare(
3696
"SELECT value FROM system_config WHERE key = 'db_initialized' LIMIT 1"
3797
).first().catch(() => null);
3898

@@ -94,8 +154,6 @@ async function initializeDatabase(db: D1Database): Promise<void> {
94154
`CREATE INDEX IF NOT EXISTS idx_tags_name_value_event ON tags(tag_name, tag_value, event_id)`,
95155
`CREATE INDEX IF NOT EXISTS idx_tags_event_id ON tags(event_id)`,
96156

97-
`DROP TABLE IF EXISTS event_tags_cache`,
98-
99157
`CREATE TABLE IF NOT EXISTS event_tags_cache_multi (
100158
event_id TEXT NOT NULL,
101159
pubkey TEXT NOT NULL,
@@ -126,52 +184,13 @@ async function initializeDatabase(db: D1Database): Promise<void> {
126184
)`,
127185
`CREATE INDEX IF NOT EXISTS idx_content_hashes_pubkey ON content_hashes(pubkey)`,
128186
`CREATE INDEX IF NOT EXISTS idx_content_hashes_created_at ON content_hashes(created_at DESC)`,
129-
`CREATE INDEX IF NOT EXISTS idx_content_hashes_pubkey_created ON content_hashes(pubkey, created_at DESC)`,
130-
131-
`DROP TABLE IF EXISTS mv_follow_graph`,
132-
`DROP TABLE IF EXISTS mv_recent_notes`,
133-
`DROP TABLE IF EXISTS mv_timeline_cache`
187+
`CREATE INDEX IF NOT EXISTS idx_content_hashes_pubkey_created ON content_hashes(pubkey, created_at DESC)`
134188
];
135189

136190
for (const statement of statements) {
137191
await session.prepare(statement).run();
138192
}
139193

140-
// Drop unused indexes from existing database
141-
const dropIndexes = [
142-
'idx_events_pubkey',
143-
'idx_events_kind',
144-
'idx_events_created_at_kind',
145-
'idx_events_authors_kinds',
146-
'idx_events_tag_p_created_at',
147-
'idx_events_tag_e_created_at',
148-
'idx_events_tag_a_created_at',
149-
'idx_events_tag_t_created_at',
150-
'idx_events_tag_d_created_at',
151-
'idx_events_tag_r_created_at',
152-
'idx_events_tag_L_created_at',
153-
'idx_events_tag_s_created_at',
154-
'idx_events_tag_u_created_at',
155-
'idx_events_kind_tag_p',
156-
'idx_events_kind_tag_e',
157-
'idx_events_kind_tag_a',
158-
'idx_events_kind_tag_t',
159-
'idx_events_kind_tag_L',
160-
'idx_events_kind_tag_s',
161-
'idx_events_reply_to',
162-
'idx_events_root_thread',
163-
'idx_events_kind_created_at_covering',
164-
'idx_events_pubkey_kind_created_at_covering',
165-
'idx_events_created_at_covering',
166-
'idx_events_kind_pubkey_created_at_covering',
167-
'idx_tags_name_value',
168-
'idx_tags_value',
169-
'idx_tags_name_value_event_created',
170-
];
171-
for (const idx of dropIndexes) {
172-
await session.prepare(`DROP INDEX IF EXISTS ${idx}`).run();
173-
}
174-
175194
await session.prepare("PRAGMA foreign_keys = ON").run();
176195
await session.prepare(
177196
"INSERT OR REPLACE INTO system_config (key, value) VALUES ('db_initialized', '1')"
@@ -1148,7 +1167,6 @@ function buildQuery(filter: NostrFilter): { sql: string; params: any[] } {
11481167
const tagFilter = directTags[0];
11491168
const hasKinds = filter.kinds && filter.kinds.length > 0;
11501169

1151-
// Choose optimal index based on query pattern
11521170
let indexHint = "";
11531171
if (hasKinds && filter.kinds!.length <= 10) {
11541172
indexHint = " INDEXED BY idx_cache_multi_kind_type_value";
@@ -1161,12 +1179,10 @@ function buildQuery(filter: NostrFilter): { sql: string; params: any[] } {
11611179
WHERE m.tag_type = ? AND m.tag_value IN (${tagFilter.values.map(() => '?').join(',')})`;
11621180
params.push(tagFilter.name, ...tagFilter.values);
11631181
} else {
1164-
// Multiple tag types - need to match ALL tag conditions
11651182
const hasKindsMulti = filter.kinds && filter.kinds.length > 0;
11661183
const tagConditions = directTags.map((t, i) => {
11671184
const alias = `m${i}`;
11681185
const placeholders = t.values.map(() => '?').join(',');
1169-
// Add index hint to first alias for better query planning
11701186
const hint = i === 0
11711187
? (hasKindsMulti && filter.kinds!.length <= 10
11721188
? " INDEXED BY idx_cache_multi_kind_type_value"

worker.js

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ var relayInfo = {
8080
contact: "lux@fed.wtf",
8181
supported_nips: [1, 2, 4, 5, 9, 11, 12, 13, 15, 16, 17, 20, 22, 25, 28, 33, 40, 42, 57],
8282
software: "https://github.com/Spl0itable/nosflare",
83-
version: "7.9.43",
83+
version: "7.9.44",
8484
icon: "https://raw.githubusercontent.com/Spl0itable/nosflare/main/images/flare.png",
8585
// Optional fields (uncomment as needed):
8686
// banner: "https://example.com/banner.jpg",
@@ -2928,9 +2928,56 @@ var GLOBAL_MAX_EVENTS = 500;
29282928
var MAX_QUERY_COMPLEXITY = 1e3;
29292929
var CHUNK_SIZE = 500;
29302930
async function initializeDatabase(db) {
2931+
const dropSession = db.withSession("first-primary");
29312932
try {
2932-
const session2 = db.withSession("first-unconstrained");
2933-
const initCheck = await session2.prepare(
2933+
await dropSession.prepare(`
2934+
CREATE TABLE IF NOT EXISTS system_config (
2935+
key TEXT PRIMARY KEY,
2936+
value TEXT NOT NULL,
2937+
created_at INTEGER DEFAULT (strftime('%s', 'now'))
2938+
)
2939+
`).run();
2940+
} catch (_) {
2941+
}
2942+
const dropIndexes = [
2943+
"idx_events_pubkey",
2944+
"idx_events_kind",
2945+
"idx_events_created_at_kind",
2946+
"idx_events_authors_kinds",
2947+
"idx_events_tag_p_created_at",
2948+
"idx_events_tag_e_created_at",
2949+
"idx_events_tag_a_created_at",
2950+
"idx_events_tag_t_created_at",
2951+
"idx_events_tag_d_created_at",
2952+
"idx_events_tag_r_created_at",
2953+
"idx_events_tag_L_created_at",
2954+
"idx_events_tag_s_created_at",
2955+
"idx_events_tag_u_created_at",
2956+
"idx_events_kind_tag_p",
2957+
"idx_events_kind_tag_e",
2958+
"idx_events_kind_tag_a",
2959+
"idx_events_kind_tag_t",
2960+
"idx_events_kind_tag_L",
2961+
"idx_events_kind_tag_s",
2962+
"idx_events_reply_to",
2963+
"idx_events_root_thread",
2964+
"idx_events_kind_created_at_covering",
2965+
"idx_events_pubkey_kind_created_at_covering",
2966+
"idx_events_created_at_covering",
2967+
"idx_events_kind_pubkey_created_at_covering",
2968+
"idx_tags_name_value",
2969+
"idx_tags_value",
2970+
"idx_tags_name_value_event_created"
2971+
];
2972+
for (const idx of dropIndexes) {
2973+
await dropSession.prepare(`DROP INDEX IF EXISTS ${idx}`).run();
2974+
}
2975+
const dropTables = ["event_tags_cache", "mv_follow_graph", "mv_recent_notes", "mv_timeline_cache"];
2976+
for (const tbl of dropTables) {
2977+
await dropSession.prepare(`DROP TABLE IF EXISTS ${tbl}`).run();
2978+
}
2979+
try {
2980+
const initCheck = await dropSession.prepare(
29342981
"SELECT value FROM system_config WHERE key = 'db_initialized' LIMIT 1"
29352982
).first().catch(() => null);
29362983
if (initCheck && initCheck.value === "1") {
@@ -2985,7 +3032,6 @@ async function initializeDatabase(db) {
29853032
)`,
29863033
`CREATE INDEX IF NOT EXISTS idx_tags_name_value_event ON tags(tag_name, tag_value, event_id)`,
29873034
`CREATE INDEX IF NOT EXISTS idx_tags_event_id ON tags(event_id)`,
2988-
`DROP TABLE IF EXISTS event_tags_cache`,
29893035
`CREATE TABLE IF NOT EXISTS event_tags_cache_multi (
29903036
event_id TEXT NOT NULL,
29913037
pubkey TEXT NOT NULL,
@@ -3014,55 +3060,11 @@ async function initializeDatabase(db) {
30143060
)`,
30153061
`CREATE INDEX IF NOT EXISTS idx_content_hashes_pubkey ON content_hashes(pubkey)`,
30163062
`CREATE INDEX IF NOT EXISTS idx_content_hashes_created_at ON content_hashes(created_at DESC)`,
3017-
`CREATE INDEX IF NOT EXISTS idx_content_hashes_pubkey_created ON content_hashes(pubkey, created_at DESC)`,
3018-
`DROP TABLE IF EXISTS mv_follow_graph`,
3019-
`DROP TABLE IF EXISTS mv_recent_notes`,
3020-
`DROP TABLE IF EXISTS mv_timeline_cache`
3063+
`CREATE INDEX IF NOT EXISTS idx_content_hashes_pubkey_created ON content_hashes(pubkey, created_at DESC)`
30213064
];
30223065
for (const statement of statements) {
30233066
await session.prepare(statement).run();
30243067
}
3025-
const dropIndexes = [
3026-
// Redundant single-column prefixes (covered by composite indexes)
3027-
"idx_events_pubkey",
3028-
"idx_events_kind",
3029-
"idx_events_created_at_kind",
3030-
// Unused partial index
3031-
"idx_events_authors_kinds",
3032-
// Denormalized tag column indexes (never queried, superseded by event_tags_cache_multi)
3033-
"idx_events_tag_p_created_at",
3034-
"idx_events_tag_e_created_at",
3035-
"idx_events_tag_a_created_at",
3036-
"idx_events_tag_t_created_at",
3037-
"idx_events_tag_d_created_at",
3038-
"idx_events_tag_r_created_at",
3039-
"idx_events_tag_L_created_at",
3040-
"idx_events_tag_s_created_at",
3041-
"idx_events_tag_u_created_at",
3042-
"idx_events_kind_tag_p",
3043-
"idx_events_kind_tag_e",
3044-
"idx_events_kind_tag_a",
3045-
"idx_events_kind_tag_t",
3046-
"idx_events_kind_tag_L",
3047-
"idx_events_kind_tag_s",
3048-
// Thread indexes (written but never queried)
3049-
"idx_events_reply_to",
3050-
"idx_events_root_thread",
3051-
// Covering indexes (huge storage cost, never referenced by query planner hints)
3052-
"idx_events_kind_created_at_covering",
3053-
"idx_events_pubkey_kind_created_at_covering",
3054-
"idx_events_created_at_covering",
3055-
"idx_events_kind_pubkey_created_at_covering",
3056-
// Redundant tags indexes
3057-
"idx_tags_name_value",
3058-
"idx_tags_value",
3059-
"idx_tags_name_value_event_created"
3060-
// (event_tags_cache indexes removed by DROP TABLE above)
3061-
// (mv_follow_graph indexes removed by DROP TABLE above)
3062-
];
3063-
for (const idx of dropIndexes) {
3064-
await session.prepare(`DROP INDEX IF EXISTS ${idx}`).run();
3065-
}
30663068
await session.prepare("PRAGMA foreign_keys = ON").run();
30673069
await session.prepare(
30683070
"INSERT OR REPLACE INTO system_config (key, value) VALUES ('db_initialized', '1')"

0 commit comments

Comments
 (0)