Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp (C++)

Code before:
    // 5. Let range be cursor’s range.
    auto range = cursor->range();

    // 6. Let position be cursor’s position.
    auto position = cursor->position();

    // 7. Let object store position be cursor’s object store position.
    auto object_store_position = cursor->object_store_position();

    // 8. If count is not given, let count be 1.
    // NOTE: This is handled by the default parameter

    auto next_requirements = [&](Variant<ObjectStoreRecord, IndexRecord> const& record) -> bool {
        bool satisfies = true;
        auto retain_satisfaction = [&](bool condition) -> bool {
            if (satisfies)
                satisfies = condition;

            return satisfies;
        };

        // * If key is defined:
        if (key) {
            // * The record’s key is greater than or equal to key.
            auto is_greater_than_or_equal = record.visit(
                [](Empty) { VERIFY_NOT_REACHED(); },
                [key](auto const& inner_record) {
                    return Key::greater_than_or_equal(inner_record.key, *key);
                });

            retain_satisfaction(is_greater_than_or_equal);
        }

        // * If primaryKey is defined:
        if (primary_key) {
            auto const& inner_record = record.get<IndexRecord>();

            // * If the record’s key is equal to key:
            if (Key::equals(inner_record.key, *key)) {
                // * The record’s value is greater than or equal to primaryKey
                retain_satisfaction(Key::greater_than_or_equal(inner_record.value, *primary_key));
            }
            // * Else:
            else {
                // * The record’s key is greater than key.
                retain_satisfaction(Key::greater_than(inner_record.key, *key));
            }
        }

        // * If position is defined and source is an object store:
        if (position && source.has<GC::Ref<ObjectStore>>()) {

Review comment (suggestion):
I'm confused by all these `Empty` branches. The `record` is a `Variant<ObjectStoreRecord, IndexRecord>`, it does not contain an `Empty` type. Can't every one of these just be:
```c++
auto is_greater_than_or_equal = record.visit([&](auto const& record) {
    return Key::greater_than_or_equal(record.key, *key);
});
```

Code after:
    // 5. Let range be cursor’s range.
    auto range = cursor->range();

    // 6. Let position be cursor’s position.
    auto position = cursor->position();

    // 7. Let object store position be cursor’s object store position.
    auto object_store_position = cursor->object_store_position();

    // 8. If count is not given, let count be 1.
    // NOTE: This is handled by the default parameter

    auto next_requirements = [&](Variant<ObjectStoreRecord, IndexRecord> const& record) -> bool {
        // * If key is defined:
        if (key) {
            // * The record’s key is greater than or equal to key.
            if (!record.visit([&](auto const& inner_record) {
                    return Key::greater_than_or_equal(inner_record.key, *key);
                }))
                return false;
        }

        // * If primaryKey is defined:
        if (primary_key) {
            auto const& inner_record = record.get<IndexRecord>();

            // * If the record’s key is equal to key:
            if (Key::equals(inner_record.key, *key)) {
                // * The record’s value is greater than or equal to primaryKey
                if (!Key::greater_than_or_equal(inner_record.value, *primary_key))
                    return false;
            }
            // * Else:
            else {
                // * The record’s key is greater than key.
                if (!Key::greater_than(inner_record.key, *key))
                    return false;
            }
        }

        // * If position is defined and source is an object store:
        if (position && source.has<GC::Ref<ObjectStore>>()) {
            auto const& inner_record = record.get<ObjectStoreRecord>();

            // * The record’s key is greater than position.
            if (!Key::greater_than(inner_record.key, *position))
                return false;
        }

        // * If position is defined and source is an index:
        if (position && source.has<GC::Ref<Index>>()) {