void MemoryObjectStoreCursor::currentData(IDBGetResult& data)
{
    if (!hasIterators()) {
        m_currentPositionKey = { };
        data = { };
        return;
    }

    auto* set = m_objectStore.orderedKeys();
    ASSERT(set);
    if (m_info.isDirectionForward()) {
        if (!m_forwardIterator || *m_forwardIterator == set->end()) {
            data = { };
            return;
        }

        m_currentPositionKey = **m_forwardIterator;
        data.keyData = **m_forwardIterator;
        data.primaryKeyData = **m_forwardIterator;
        data.valueBuffer = m_objectStore.valueForKeyRange(**m_forwardIterator);
    } else {
        if (!m_reverseIterator || *m_reverseIterator == set->rend()) {
            data = { };
            return;
        }

        m_currentPositionKey = **m_reverseIterator;
        data.keyData = **m_reverseIterator;
        data.primaryKeyData = **m_reverseIterator;
        data.valueBuffer = m_objectStore.valueForKeyRange(**m_reverseIterator);
    }
}
void MemoryObjectStoreCursor::incrementReverseIterator(std::set<IDBKeyData>& set, const IDBKeyData& key, uint32_t count)
{
    // We might need to re-grab the current iterator.
    // e.g. If the record it was pointed to had been deleted.
    bool didResetIterator = false;
    if (!hasIterators()) {
        if (!m_currentPositionKey.isValid())
            return;

        m_remainingRange.upperKey = m_currentPositionKey;
        m_remainingRange.upperOpen = false;
        setFirstInRemainingRange(set);

        didResetIterator = true;
    }

    if (*m_reverseIterator == set.rend())
        return;

    if (key.isValid()) {
        // If iterating to a key, the count passed in must be zero, as there is no way to iterate by both a count and to a key.
        ASSERT(!count);

        if (!m_info.range().containsKey(key))
            return;

        if ((*m_reverseIterator)->compare(key) > 0) {
            m_remainingRange.upperKey = key;
            m_remainingRange.upperOpen = false;

            setFirstInRemainingRange(set);
        }

        return;
    }

    if (!count)
        count = 1;

    // If the reverseIterator was reset because it's previous record had been deleted,
    // we might have already advanced past the current position, eating one one of the iteration counts.
    if (didResetIterator && (*m_reverseIterator)->compare(m_currentPositionKey) < 0)
        --count;

    while (count) {
        --count;

        ++*m_reverseIterator;

        if (*m_reverseIterator == set.rend())
            return;

        if (!m_info.range().containsKey(**m_reverseIterator))
            return;
    }
}
void MemoryObjectStoreCursor::keyAdded(std::set<IDBKeyData>::iterator iterator)
{
    if (hasIterators())
        return;

    if (*iterator != m_currentPositionKey)
        return;

    if (m_info.isDirectionForward())
        m_forwardIterator = iterator;
    else
        m_reverseIterator = --std::set<IDBKeyData>::reverse_iterator(iterator);
}
bool MemoryObjectStoreCursor::hasValidPosition() const
{
    if (!hasIterators())
        return false;

    auto* set = m_objectStore.orderedKeys();
    if (!set)
        return false;

    if (m_info.isDirectionForward())
        return *m_forwardIterator != set->end();

    return *m_reverseIterator != set->rend();
}