<commit_before>////////////////////////////////////////////////////////////////////////////
//
// Copyright 2015 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////

#include "shared_realm.hpp"

#include "impl/collection_notifier.hpp"
#include "impl/realm_coordinator.hpp"
#include "impl/transact_log_handler.hpp"

#include "audit.hpp"
#include "binding_context.hpp"
#include "list.hpp"
#include "object.hpp"
#include "object_schema.hpp"
#include "object_store.hpp"
#include "results.hpp"
#include "schema.hpp"
#include "thread_safe_reference.hpp"

#include "util/scheduler.hpp"

#include <realm/db.hpp>
#include <realm/util/scope_exit.hpp>
#include <realm/util/fifo_helper.hpp>

#if REALM_ENABLE_SYNC
#include "sync/impl/sync_file.hpp"
#include "sync/sync_config.hpp"
#include "sync/sync_manager.hpp"

#include <realm/sync/history.hpp>
#include <realm/sync/permissions.hpp>
#include <realm/sync/version.hpp>
#else
namespace realm {
namespace sync {
    struct PermissionsCache {};
    struct TableInfoCache {};
}
}
#endif

using namespace realm;
using namespace realm::_impl;

Realm::Realm(Config config, util::Optional<VersionID> version, std::shared_ptr<_impl::RealmCoordinator> coordinator, MakeSharedTag)
: m_config(std::move(config))
, m_frozen_version(std::move(version))
, m_scheduler(m_config.scheduler)
{
    if (!coordinator->get_cached_schema(m_schema, m_schema_version, m_schema_transaction_version)) {
        m_group = coordinator->begin_read();
        read_schema_from_group_if_needed();
        coordinator->cache_schema(m_schema, m_schema_version, m_schema_transaction_version);
        m_group = nullptr;
    }

    m_coordinator = std::move(coordinator);
}

Realm::~Realm()
{
    if (m_coordinator) {
        m_coordinator->unregister_realm(this);
    }
}

bool Realm::is_partial() const noexcept
{
#if REALM_ENABLE_SYNC
    return m_config.sync_config && m_config.sync_config->is_partial;
#else
    return false;
#endif
}

Group& Realm::read_group()
{
    verify_open();

    if (!m_group)
        begin_read(VersionID{});
    return *m_group;
}

Transaction& Realm::transaction()
{
    REALM_ASSERT(!m_config.immutable());
    return static_cast<Transaction&>(read_group());
}

Transaction& Realm::transaction() const
{<commit_msg>Actually use the frozen version when creating a read group<commit_after>////////////////////////////////////////////////////////////////////////////
//
// Copyright 2015 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////

#include "shared_realm.hpp"

#include "impl/collection_notifier.hpp"
#include "impl/realm_coordinator.hpp"
#include "impl/transact_log_handler.hpp"

#include "audit.hpp"
#include "binding_context.hpp"
#include "list.hpp"
#include "object.hpp"
#include "object_schema.hpp"
#include "object_store.hpp"
#include "results.hpp"
#include "schema.hpp"
#include "thread_safe_reference.hpp"

#include "util/scheduler.hpp"

#include <realm/db.hpp>
#include <realm/util/scope_exit.hpp>
#include <realm/util/fifo_helper.hpp>

#if REALM_ENABLE_SYNC
#include "sync/impl/sync_file.hpp"
#include "sync/sync_config.hpp"
#include "sync/sync_manager.hpp"

#include <realm/sync/history.hpp>
#include <realm/sync/permissions.hpp>
#include <realm/sync/version.hpp>
#else
namespace realm {
namespace sync {
    struct PermissionsCache {};
    struct TableInfoCache {};
}
}
#endif

using namespace realm;
using namespace realm::_impl;

Realm::Realm(Config config, util::Optional<VersionID> version, std::shared_ptr<_impl::RealmCoordinator> coordinator, MakeSharedTag)
: m_config(std::move(config))
, m_frozen_version(std::move(version))
, m_scheduler(m_config.scheduler)
{
    if (!coordinator->get_cached_schema(m_schema, m_schema_version, m_schema_transaction_version)) {
        m_group = coordinator->begin_read();
        read_schema_from_group_if_needed();
        coordinator->cache_schema(m_schema, m_schema_version, m_schema_transaction_version);
        m_group = nullptr;
    }

    m_coordinator = std::move(coordinator);
}

Realm::~Realm()
{
    if (m_coordinator) {
        m_coordinator->unregister_realm(this);
    }
}

bool Realm::is_partial() const noexcept
{
#if REALM_ENABLE_SYNC
    return m_config.sync_config && m_config.sync_config->is_partial;
#else
    return false;
#endif
}

Group& Realm::read_group()
{
    verify_open();

    if (!m_group)
        begin_read(m_frozen_version.value_or(VersionID{}));
    return *m_group;
}

Transaction& Realm::transaction()
{
    REALM_ASSERT(!m_config.immutable());
    return static_cast<Transaction&>(read_group());
}

Transaction& Realm::transaction() const
{
{'fraction_non_alphanumeric': 0.17082899757197365, 'fraction_numerical': 0.002774887270204648, 'mean_word_length': 3.662085691188359, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2841539', 'n_tokens_mistral': 1799, 'n_tokens_neox': 1780, 'n_words': 417}