@@ -1395,19 +1395,18 @@ aqo_data_store(uint64 fs, int fss, OkNNrdata *data, List *reloids)
1395
1395
LWLockAcquire (& aqo_state -> neighbours_lock , LW_EXCLUSIVE );
1396
1396
1397
1397
prev = (NeighboursEntry * ) hash_search (fss_neighbours , & key .fss , HASH_ENTER , & found );
1398
- if (! found )
1399
- {
1400
- entry -> list . prev = NULL ;
1401
- entry -> list . next = NULL ;
1402
- }
1403
- else
1398
+
1399
+ /* A new element contains backward link to the first element list and
1400
+ * the first element contains toward link to the new element.
1401
+ * The new element has become the first element of the list.
1402
+ */
1403
+ if ( found )
1404
1404
{
1405
- prev -> data -> list .next = entry ;
1406
- entry -> list .next = NULL ;
1407
- entry -> list .prev = prev -> data ;
1405
+ prev -> data -> neighbour_refs .next = entry ;
1406
+ entry -> neighbour_refs .prev = prev -> data ;
1408
1407
}
1409
1408
prev -> data = entry ;
1410
-
1409
+ aqo_state -> neighbours_changed = true;
1411
1410
LWLockRelease (& aqo_state -> neighbours_lock );
1412
1411
}
1413
1412
@@ -1590,6 +1589,9 @@ load_aqo_data(uint64 fs, int fss, OkNNrdata *data, List **reloids,
1590
1589
neighbour_entry = (NeighboursEntry * ) hash_search (fss_neighbours , & fss , HASH_FIND , & found );
1591
1590
entry = found ? neighbour_entry -> data : NULL ;
1592
1591
1592
+ /*
1593
+ * Go through the list, starting from the first element and go to back.
1594
+ */
1593
1595
while (entry != NULL )
1594
1596
{
1595
1597
List * tmp_oids = NIL ;
@@ -1620,7 +1622,11 @@ load_aqo_data(uint64 fs, int fss, OkNNrdata *data, List **reloids,
1620
1622
1621
1623
build_knn_matrix (data , temp_data );
1622
1624
1623
- entry = entry -> list .prev ;
1625
+ /*
1626
+ * We have a backward oriented list since the first element always stay
1627
+ * the last element of list, so go to the next element on the backward link.
1628
+ */
1629
+ entry = entry -> neighbour_refs .prev ;
1624
1630
}
1625
1631
LWLockRelease (& aqo_state -> neighbours_lock );
1626
1632
}
@@ -1758,15 +1764,15 @@ _aqo_data_clean(uint64 fs)
1758
1764
entry -> data_dp = InvalidDsaPointer ;
1759
1765
1760
1766
/* fix neighbour list */
1761
- if (entry -> list .next )
1767
+ if (entry -> neighbour_refs .next )
1762
1768
has_next = true;
1763
- if (entry -> list .prev )
1769
+ if (entry -> neighbour_refs .prev )
1764
1770
has_prev = true;
1765
1771
1766
1772
if (has_prev )
1767
- entry -> list .prev -> list .next = has_next ? entry -> list .next : NULL ;
1773
+ entry -> neighbour_refs .prev -> neighbour_refs .next = has_next ? entry -> neighbour_refs .next : NULL ;
1768
1774
if (has_next )
1769
- entry -> list .next -> list .prev = has_prev ? entry -> list .prev : NULL ;
1775
+ entry -> neighbour_refs .next -> neighbour_refs .prev = has_prev ? entry -> neighbour_refs .prev : NULL ;
1770
1776
1771
1777
/* Fix or remove neighbours htab entry*/
1772
1778
LWLockAcquire (& aqo_state -> neighbours_lock , LW_EXCLUSIVE );
@@ -1775,15 +1781,20 @@ _aqo_data_clean(uint64 fs)
1775
1781
{
1776
1782
if (has_prev )
1777
1783
{
1778
- fss_htab_entry -> data = entry -> list .prev ;
1784
+ fss_htab_entry -> data = entry -> neighbour_refs .prev ;
1779
1785
}
1780
1786
else
1781
1787
{
1782
1788
hash_search (fss_neighbours , & entry -> key .fss , HASH_REMOVE , NULL );
1783
1789
}
1790
+ /*
1791
+ * We found element in Neibours hash table and made change:
1792
+ * either delete element of table or replace its value.
1793
+ */
1794
+ aqo_state -> neighbours_changed = true;
1784
1795
}
1785
1796
LWLockRelease (& aqo_state -> neighbours_lock );
1786
-
1797
+
1787
1798
1788
1799
if (hash_search (data_htab , & entry -> key , HASH_REMOVE , NULL ) == NULL )
1789
1800
elog (ERROR , "[AQO] hash table corrupted" );
@@ -2324,15 +2335,15 @@ cleanup_aqo_database(bool gentle, int *fs_num, int *fss_num)
2324
2335
entry = (DataEntry * ) hash_search (data_htab , & key , HASH_FIND , & found );
2325
2336
if (found )
2326
2337
{
2327
- if (entry -> list .next )
2338
+ if (entry -> neighbour_refs .next )
2328
2339
has_next = true;
2329
- if (entry -> list .prev )
2340
+ if (entry -> neighbour_refs .prev )
2330
2341
has_prev = true;
2331
2342
2332
2343
if (has_prev )
2333
- entry -> list .prev -> list .next = has_next ? entry -> list .next : NULL ;
2344
+ entry -> neighbour_refs .prev -> neighbour_refs .next = has_next ? entry -> neighbour_refs .next : NULL ;
2334
2345
if (has_next )
2335
- entry -> list .next -> list .prev = has_prev ? entry -> list .prev : NULL ;
2346
+ entry -> neighbour_refs .next -> neighbour_refs .prev = has_prev ? entry -> neighbour_refs .prev : NULL ;
2336
2347
2337
2348
/* Fix or remove neighbours htab entry*/
2338
2349
LWLockAcquire (& aqo_state -> neighbours_lock , LW_EXCLUSIVE );
@@ -2341,12 +2352,17 @@ cleanup_aqo_database(bool gentle, int *fs_num, int *fss_num)
2341
2352
{
2342
2353
if (has_prev )
2343
2354
{
2344
- fss_htab_entry -> data = entry -> list .prev ;
2355
+ fss_htab_entry -> data = entry -> neighbour_refs .prev ;
2345
2356
}
2346
2357
else
2347
2358
{
2348
2359
hash_search (fss_neighbours , & key .fss , HASH_REMOVE , NULL );
2349
2360
}
2361
+ /*
2362
+ * We found element in Neibours hash table and made change:
2363
+ * either delete element of table or replace its value.
2364
+ */
2365
+ aqo_state -> neighbours_changed = true;
2350
2366
}
2351
2367
LWLockRelease (& aqo_state -> neighbours_lock );
2352
2368
}
0 commit comments