@@ -170,12 +170,12 @@ struct TIDBitmap
170170};
171171
172172/*
173- * When iterating over a bitmap in sorted order, a TBMIterator is used to
174- * track our progress. There can be several iterators scanning the same
175- * bitmap concurrently. Note that the bitmap becomes read-only as soon as
176- * any iterator is created.
173+ * When iterating over a backend-local bitmap in sorted order, a
174+ * TBMPrivateIterator is used to track our progress. There can be several
175+ * iterators scanning the same bitmap concurrently. Note that the bitmap
176+ * becomes read-only as soon as any iterator is created.
177177 */
178- struct TBMIterator
178+ struct TBMPrivateIterator
179179{
180180 TIDBitmap * tbm ; /* TIDBitmap we're iterating over */
181181 int spageptr ; /* next spages index */
@@ -213,8 +213,8 @@ typedef struct PTIterationArray
213213} PTIterationArray ;
214214
215215/*
216- * same as TBMIterator , but it is used for joint iteration, therefore this
217- * also holds a reference to the shared state.
216+ * same as TBMPrivateIterator , but it is used for joint iteration, therefore
217+ * this also holds a reference to the shared state.
218218 */
219219struct TBMSharedIterator
220220{
@@ -673,31 +673,32 @@ tbm_is_empty(const TIDBitmap *tbm)
673673}
674674
675675/*
676- * tbm_begin_iterate - prepare to iterate through a TIDBitmap
676+ * tbm_begin_private_iterate - prepare to iterate through a TIDBitmap
677677 *
678- * The TBMIterator struct is created in the caller's memory context.
679- * For a clean shutdown of the iteration, call tbm_end_iterate ; but it's
680- * okay to just allow the memory context to be released, too. It is caller's
681- * responsibility not to touch the TBMIterator anymore once the TIDBitmap
682- * is freed.
678+ * The TBMPrivateIterator struct is created in the caller's memory context.
679+ * For a clean shutdown of the iteration, call tbm_end_private_iterate ; but
680+ * it's okay to just allow the memory context to be released, too. It is
681+ * caller's responsibility not to touch the TBMPrivateIterator anymore once
682+ * the TIDBitmap is freed.
683683 *
684684 * NB: after this is called, it is no longer allowed to modify the contents
685685 * of the bitmap. However, you can call this multiple times to scan the
686686 * contents repeatedly, including parallel scans.
687687 */
688- TBMIterator *
689- tbm_begin_iterate (TIDBitmap * tbm )
688+ TBMPrivateIterator *
689+ tbm_begin_private_iterate (TIDBitmap * tbm )
690690{
691- TBMIterator * iterator ;
691+ TBMPrivateIterator * iterator ;
692692
693693 Assert (tbm -> iterating != TBM_ITERATING_SHARED );
694694
695695 /*
696- * Create the TBMIterator struct, with enough trailing space to serve the
697- * needs of the TBMIterateResult sub-struct.
696+ * Create the TBMPrivateIterator struct, with enough trailing space to
697+ * serve the needs of the TBMIterateResult sub-struct.
698698 */
699- iterator = (TBMIterator * ) palloc (sizeof (TBMIterator ) +
700- MAX_TUPLES_PER_PAGE * sizeof (OffsetNumber ));
699+ iterator = (TBMPrivateIterator * ) palloc (sizeof (TBMPrivateIterator ) +
700+ MAX_TUPLES_PER_PAGE *
701+ sizeof (OffsetNumber ));
701702 iterator -> tbm = tbm ;
702703
703704 /*
@@ -878,7 +879,7 @@ tbm_prepare_shared_iterate(TIDBitmap *tbm)
878879 ptchunks = dsa_get_address (tbm -> dsa , tbm -> ptchunks );
879880
880881 /*
881- * For every shared iterator, referring to pagetable and iterator array,
882+ * For every shared iterator referring to pagetable and iterator array,
882883 * increase the refcount by 1 so that while freeing the shared iterator we
883884 * don't free pagetable and iterator array until its refcount becomes 0.
884885 */
@@ -956,7 +957,7 @@ tbm_advance_schunkbit(PagetableEntry *chunk, int *schunkbitp)
956957}
957958
958959/*
959- * tbm_iterate - scan through next page of a TIDBitmap
960+ * tbm_private_iterate - scan through next page of a TIDBitmap
960961 *
961962 * Returns a TBMIterateResult representing one page, or NULL if there are
962963 * no more pages to scan. Pages are guaranteed to be delivered in numerical
@@ -968,7 +969,7 @@ tbm_advance_schunkbit(PagetableEntry *chunk, int *schunkbitp)
968969 * testing, recheck is always set true when ntuples < 0.)
969970 */
970971TBMIterateResult *
971- tbm_iterate ( TBMIterator * iterator )
972+ tbm_private_iterate ( TBMPrivateIterator * iterator )
972973{
973974 TIDBitmap * tbm = iterator -> tbm ;
974975 TBMIterateResult * output = & (iterator -> output );
@@ -1136,14 +1137,14 @@ tbm_shared_iterate(TBMSharedIterator *iterator)
11361137}
11371138
11381139/*
1139- * tbm_end_iterate - finish an iteration over a TIDBitmap
1140+ * tbm_end_private_iterate - finish an iteration over a TIDBitmap
11401141 *
11411142 * Currently this is just a pfree, but it might do more someday. (For
11421143 * instance, it could be useful to count open iterators and allow the
11431144 * bitmap to return to read/write status when there are no more iterators.)
11441145 */
11451146void
1146- tbm_end_iterate ( TBMIterator * iterator )
1147+ tbm_end_private_iterate ( TBMPrivateIterator * iterator )
11471148{
11481149 pfree (iterator );
11491150}
@@ -1556,3 +1557,66 @@ tbm_calculate_entries(double maxbytes)
15561557
15571558 return nbuckets ;
15581559}
1560+
1561+ /*
1562+ * Create a shared or private bitmap iterator and start iteration.
1563+ *
1564+ * `tbm` is only used to create the private iterator and dsa and dsp are only
1565+ * used to create the shared iterator.
1566+ *
1567+ * Before invoking tbm_begin_iterate() to create a shared iterator, one
1568+ * process must already have invoked tbm_prepare_shared_iterate() to create
1569+ * and set up the TBMSharedIteratorState.
1570+ */
1571+ TBMIterator
1572+ tbm_begin_iterate (TIDBitmap * tbm , dsa_area * dsa , dsa_pointer dsp )
1573+ {
1574+ TBMIterator iterator = {0 };
1575+
1576+ /* Allocate a private iterator and attach the shared state to it */
1577+ if (DsaPointerIsValid (dsp ))
1578+ {
1579+ iterator .shared = true;
1580+ iterator .i .shared_iterator = tbm_attach_shared_iterate (dsa , dsp );
1581+ }
1582+ else
1583+ {
1584+ iterator .shared = false;
1585+ iterator .i .private_iterator = tbm_begin_private_iterate (tbm );
1586+ }
1587+
1588+ return iterator ;
1589+ }
1590+
1591+ /*
1592+ * Clean up shared or private bitmap iterator.
1593+ */
1594+ void
1595+ tbm_end_iterate (TBMIterator * iterator )
1596+ {
1597+ Assert (iterator );
1598+
1599+ if (iterator -> shared )
1600+ tbm_end_shared_iterate (iterator -> i .shared_iterator );
1601+ else
1602+ tbm_end_private_iterate (iterator -> i .private_iterator );
1603+
1604+ * iterator = (TBMIterator )
1605+ {
1606+ 0
1607+ };
1608+ }
1609+
1610+ /*
1611+ * Get the next TBMIterateResult from the shared or private bitmap iterator.
1612+ */
1613+ TBMIterateResult *
1614+ tbm_iterate (TBMIterator * iterator )
1615+ {
1616+ Assert (iterator );
1617+
1618+ if (iterator -> shared )
1619+ return tbm_shared_iterate (iterator -> i .shared_iterator );
1620+ else
1621+ return tbm_private_iterate (iterator -> i .private_iterator );
1622+ }
0 commit comments