@@ -1291,18 +1291,17 @@ _compute_data_dsa(const DataEntry *entry)
1291
1291
bool
1292
1292
aqo_data_store (uint64 fs , int fss , OkNNrdata * data , List * reloids )
1293
1293
{
1294
- DataEntry * entry ;
1295
- bool found ;
1296
- data_key key = {.fs = fs , .fss = fss };
1297
- fs_list neighbour_list = {0 };
1298
- int i ;
1299
- char * ptr ;
1300
- ListCell * lc ;
1301
- size_t size ;
1302
- bool tblOverflow ;
1303
- HASHACTION action ;
1304
- bool result ;
1305
- uint64 * prev_fs ;
1294
+ DataEntry * entry ;
1295
+ bool found ;
1296
+ data_key key = {.fs = fs , .fss = fss };
1297
+ int i ;
1298
+ char * ptr ;
1299
+ ListCell * lc ;
1300
+ size_t size ;
1301
+ bool tblOverflow ;
1302
+ HASHACTION action ;
1303
+ bool result ;
1304
+ NeighboursEntry * prev_fs ;
1306
1305
1307
1306
Assert (!LWLockHeldByMe (& aqo_state -> data_lock ));
1308
1307
@@ -1392,25 +1391,23 @@ aqo_data_store(uint64 fs, int fss, OkNNrdata *data, List *reloids)
1392
1391
1393
1392
LWLockAcquire (& aqo_state -> neighbours_lock , LW_EXCLUSIVE );
1394
1393
1395
- prev_fs = (uint64 * ) hash_search (fss_neighbours , & fss , HASH_ENTER , & found );
1394
+ prev_fs = (NeighboursEntry * ) hash_search (fss_neighbours , & fss , HASH_ENTER , & found );
1396
1395
if (!found )
1397
1396
{
1398
- //* prev_fs = fs;
1399
- memcpy ( prev_fs , & entry -> key . fs , sizeof ( uint64 )) ;
1397
+ entry -> list . prev_fs = fs ;
1398
+ entry -> list . next_fs = fs ;
1400
1399
}
1401
1400
else
1402
1401
{
1403
- data_key prev_key = {.fs = * prev_fs , .fss = fss };
1402
+ data_key prev_key = {.fs = prev_fs -> fs , .fss = fss };
1404
1403
DataEntry * prev ;
1405
1404
1406
1405
prev = (DataEntry * ) hash_search (data_htab , & prev_key , HASH_FIND , NULL );
1407
- // prev->list.next_fs = fs;
1408
- memcpy ( & prev -> list .next_fs , & fs , sizeof ( uint64 )) ;
1409
- neighbour_list .prev_fs = prev -> key .fs ;
1406
+ prev -> list .next_fs = fs ;
1407
+ entry -> list .next_fs = fs ;
1408
+ entry -> list .prev_fs = prev -> key .fs ;
1410
1409
}
1411
-
1412
- prev_fs = (uint64 * ) hash_search (fss_neighbours , & fss , HASH_FIND , & found );
1413
- elog (NOTICE , "%d 🗿🔫" , found );
1410
+ prev_fs -> fs = entry -> key .fs ;
1414
1411
1415
1412
LWLockRelease (& aqo_state -> neighbours_lock );
1416
1413
@@ -1421,8 +1418,6 @@ aqo_data_store(uint64 fs, int fss, OkNNrdata *data, List *reloids)
1421
1418
1422
1419
memcpy (ptr , & key , sizeof (data_key )); /* Just for debug */
1423
1420
ptr += sizeof (data_key );
1424
- memcpy (ptr , & neighbour_list , sizeof (fs_list ));
1425
- ptr += sizeof (fs_list );
1426
1421
if (entry -> cols > 0 )
1427
1422
{
1428
1423
for (i = 0 ; i < entry -> rows ; i ++ )
@@ -2170,9 +2165,52 @@ cleanup_aqo_database(bool gentle, int *fs_num, int *fss_num)
2170
2165
/* Remove junk records from aqo_data */
2171
2166
foreach (lc , junk_fss )
2172
2167
{
2173
- data_key key = {.fs = entry -> fs , .fss = lfirst_int (lc )};
2174
- //TODO fix fs list
2175
- hash_search (fss_neighbours , & lfirst_int (lc ), HASH_REMOVE , NULL );
2168
+ data_key key = {.fs = entry -> fs , .fss = lfirst_int (lc )};
2169
+ bool found ;
2170
+ bool has_prev_fs = false;
2171
+ bool has_next_fs = false;
2172
+ DataEntry * current_entry ;
2173
+ DataEntry * prev_entry ;
2174
+ DataEntry * next_entry ;
2175
+ NeighboursEntry * fss_htab_entry ;
2176
+
2177
+ /* fix fs list */
2178
+ current_entry = (DataEntry * ) hash_search (data_htab , & key , HASH_FIND , & found );
2179
+ if (found )
2180
+ {
2181
+ data_key neighbour_key = {.fs = current_entry -> list .prev_fs , .fss = key .fss };
2182
+
2183
+ if (key .fs != current_entry -> list .prev_fs )
2184
+ {
2185
+ prev_entry = (DataEntry * ) hash_search (data_htab , & neighbour_key , HASH_FIND , & has_prev_fs );
2186
+ }
2187
+
2188
+ neighbour_key .fs = current_entry -> list .next_fs ;
2189
+ if (key .fs != current_entry -> list .next_fs )
2190
+ {
2191
+ next_entry = (DataEntry * ) hash_search (data_htab , & neighbour_key , HASH_FIND , & has_next_fs );
2192
+ }
2193
+
2194
+ if (has_prev_fs )
2195
+ prev_entry -> list .next_fs = has_next_fs ? current_entry -> list .next_fs : prev_entry -> key .fs ;
2196
+ if (has_next_fs )
2197
+ next_entry -> list .prev_fs = has_prev_fs ? current_entry -> list .prev_fs : next_entry -> key .fs ;
2198
+
2199
+ }
2200
+
2201
+ /* Fix or remove neighbours htab entry*/
2202
+ fss_htab_entry = (NeighboursEntry * ) hash_search (fss_neighbours , & key .fss , HASH_FIND , & found );
2203
+ if (found && fss_htab_entry -> fs == key .fs )
2204
+ {
2205
+ if (has_prev_fs )
2206
+ {
2207
+ fss_htab_entry -> fs = prev_entry -> key .fs ;
2208
+ }
2209
+ else
2210
+ {
2211
+ hash_search (fss_neighbours , & key .fss , HASH_REMOVE , NULL );
2212
+ }
2213
+ }
2176
2214
(* fss_num ) += (int ) _aqo_data_remove (& key );
2177
2215
}
2178
2216
0 commit comments