---
title: llarp/nodedb.hpp

---

# llarp/nodedb.hpp



## Namespaces

| Name           |
| -------------- |
| **[llarp](Namespaces/namespacellarp.md)** <br>[crypto.hpp] |

## Classes

|                | Name           |
| -------------- | -------------- |
| class | **[llarp::NodeDB](Classes/classllarp_1_1NodeDB.md)**  |




## Source code

```cpp
#pragma once

#include "router_contact.hpp"
#include "router_id.hpp"
#include "util/common.hpp"
#include "util/fs.hpp"
#include "util/thread/threading.hpp"
#include "util/thread/annotations.hpp"
#include "dht/key.hpp"
#include "crypto/crypto.hpp"

#include <set>
#include <optional>
#include <unordered_set>
#include <unordered_map>
#include <utility>
#include <atomic>
#include <algorithm>

namespace llarp
{
  class NodeDB
  {
    struct Entry
    {
      const RouterContact rc;
      llarp_time_t insertedAt;
      explicit Entry(RouterContact rc);
    };
    using NodeMap = std::unordered_map<RouterID, Entry>;

    NodeMap m_Entries;

    const fs::path m_Root;

    const std::function<void(std::function<void()>)> disk;

    llarp_time_t m_NextFlushAt;

    mutable util::NullMutex m_Access;

    void
    AsyncRemoveManyFromDisk(std::unordered_set<RouterID> idents) const;

    fs::path
    GetPathForPubkey(RouterID pk) const;

   public:
    explicit NodeDB(fs::path rootdir, std::function<void(std::function<void()>)> diskCaller);

    NodeDB();

    void
    LoadFromDisk();

    void
    SaveToDisk() const;

    size_t
    NumLoaded() const;

    void
    Tick(llarp_time_t now);

    RouterContact
    FindClosestTo(dht::Key_t location) const;

    std::vector<RouterContact>
    FindManyClosestTo(dht::Key_t location, uint32_t numRouters) const;

    bool
    Has(RouterID pk) const;

    std::optional<RouterContact>
    Get(RouterID pk) const;

    template <typename Filter>
    std::optional<RouterContact>
    GetRandom(Filter visit) const
    {
      util::NullLock lock{m_Access};

      std::vector<const decltype(m_Entries)::value_type*> entries;
      for (const auto& entry : m_Entries)
        entries.push_back(&entry);

      std::shuffle(entries.begin(), entries.end(), llarp::CSRNG{});

      for (const auto entry : entries)
      {
        if (visit(entry->second.rc))
          return entry->second.rc;
      }

      return std::nullopt;
    }

    template <typename Visit>
    void
    VisitAll(Visit visit) const
    {
      util::NullLock lock{m_Access};
      for (const auto& item : m_Entries)
      {
        visit(item.second.rc);
      }
    }

    template <typename Visit>
    void
    VisitInsertedBefore(Visit visit, llarp_time_t insertedBefore)
    {
      util::NullLock lock{m_Access};
      for (const auto& item : m_Entries)
      {
        if (item.second.insertedAt < insertedBefore)
          visit(item.second.rc);
      }
    }

    void
    Remove(RouterID pk);

    template <typename Filter>
    void
    RemoveIf(Filter visit)
    {
      util::NullLock lock{m_Access};
      std::unordered_set<RouterID> removed;
      auto itr = m_Entries.begin();
      while (itr != m_Entries.end())
      {
        if (visit(itr->second.rc))
        {
          removed.insert(itr->second.rc.pubkey);
          itr = m_Entries.erase(itr);
        }
        else
          ++itr;
      }
      if (not removed.empty())
        AsyncRemoveManyFromDisk(std::move(removed));
    }

    void
    RemoveStaleRCs(std::unordered_set<RouterID> keep, llarp_time_t cutoff);

    void
    PutIfNewer(RouterContact rc);

    void
    Put(RouterContact rc);
  };
}  // namespace llarp
```


-------------------------------

Updated on 2023-03-06 at 20:36:35 +0000
