Demonstrate pagination using range queries.
()
| 130 | |
| 131 | |
| 132 | def demo_pagination_pattern(): |
| 133 | """Demonstrate pagination using range queries.""" |
| 134 | print("\n=== Pagination Pattern ===\n") |
| 135 | |
| 136 | tree = BPlusTreeMap(capacity=16) |
| 137 | |
| 138 | # Create a dataset of products |
| 139 | products = [] |
| 140 | for i in range(100): |
| 141 | product_id = i + 1 |
| 142 | tree[product_id] = { |
| 143 | "name": f"Product {product_id:03d}", |
| 144 | "price": random.randint(10, 500), |
| 145 | "category": random.choice(["Electronics", "Books", "Clothing", "Home"]), |
| 146 | } |
| 147 | |
| 148 | print("Simulating paginated API responses:") |
| 149 | |
| 150 | def get_page(start_id, page_size): |
| 151 | """Get a page of products starting from start_id.""" |
| 152 | results = [] |
| 153 | count = 0 |
| 154 | for product_id, product in tree.range(start_id, None): |
| 155 | results.append((product_id, product)) |
| 156 | count += 1 |
| 157 | if count >= page_size: |
| 158 | break |
| 159 | return results |
| 160 | |
| 161 | # Simulate pagination |
| 162 | page_size = 10 |
| 163 | current_id = 1 |
| 164 | page_num = 1 |
| 165 | |
| 166 | while current_id <= 100 and page_num <= 3: # Show first 3 pages |
| 167 | page_data = get_page(current_id, page_size) |
| 168 | print(f"\n Page {page_num} (starting from ID {current_id}):") |
| 169 | |
| 170 | for product_id, product in page_data: |
| 171 | print(f" {product_id}: {product['name']} - ${product['price']}") |
| 172 | |
| 173 | if page_data: |
| 174 | current_id = page_data[-1][0] + 1 # Next page starts after last item |
| 175 | page_num += 1 |
| 176 | |
| 177 | print( |
| 178 | f" ... (showing only first 3 pages of ~{len(tree) // page_size} total pages)" |
| 179 | ) |
| 180 | |
| 181 | |
| 182 | def demo_performance_comparison(): |
no test coverage detected