rippled
DatabaseNodeImp.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/app/ledger/Ledger.h>
21 #include <ripple/nodestore/impl/DatabaseNodeImp.h>
22 #include <ripple/protocol/HashPrefix.h>
23 
24 namespace ripple {
25 namespace NodeStore {
26 
27 void
29  NodeObjectType type,
30  Blob&& data,
31  uint256 const& hash,
32  std::uint32_t seq)
33 {
34  auto nObj = NodeObject::createObject(type, std::move(data), hash);
35  pCache_->canonicalize_replace_cache(hash, nObj);
36  backend_->store(nObj);
37  nCache_->erase(hash);
38  storeStats(nObj->getData().size());
39 }
40 
41 bool
43  uint256 const& hash,
44  std::uint32_t seq,
46 {
47  // See if the object is in cache
48  object = pCache_->fetch(hash);
49  if (object || nCache_->touch_if_exists(hash))
50  return true;
51  // Otherwise post a read
53  return false;
54 }
55 
56 void
58 {
59  pCache_->setTargetSize(size);
60  pCache_->setTargetAge(age);
61  nCache_->setTargetSize(size);
62  nCache_->setTargetAge(age);
63 }
64 
65 void
67 {
68  pCache_->sweep();
69  nCache_->sweep();
70 }
71 
72 } // namespace NodeStore
73 } // namespace ripple
ripple::NodeStore::DatabaseNodeImp::backend_
std::shared_ptr< Backend > backend_
Definition: DatabaseNodeImp.h:142
ripple::NodeStore::DatabaseNodeImp::store
void store(NodeObjectType type, Blob &&data, uint256 const &hash, std::uint32_t seq) override
Store the object.
Definition: DatabaseNodeImp.cpp:28
std::shared_ptr< NodeObject >
ripple::KeyCache::erase
bool erase(key_type const &key)
Remove the specified cache entry.
Definition: KeyCache.h:253
std::vector< unsigned char >
ripple::NodeObjectType
NodeObjectType
The types of node objects.
Definition: NodeObject.h:32
ripple::NodeObject::createObject
static std::shared_ptr< NodeObject > createObject(NodeObjectType type, Blob &&data, uint256 const &hash)
Create an object from fields.
Definition: NodeObject.cpp:37
std::chrono::seconds
ripple::NodeStore::DatabaseNodeImp::tune
void tune(int size, std::chrono::seconds age) override
Set the maximum number of entries and maximum cache age for both caches.
Definition: DatabaseNodeImp.cpp:57
ripple::KeyCache::touch_if_exists
bool touch_if_exists(KeyComparable const &key)
Refresh the last access time on a key if present.
Definition: KeyCache.h:234
ripple::base_uint< 256 >
ripple::NodeStore::DatabaseNodeImp::pCache_
std::shared_ptr< TaggedCache< uint256, NodeObject > > pCache_
Definition: DatabaseNodeImp.h:136
ripple::NodeStore::Database::storeStats
void storeStats(size_t sz)
Definition: Database.h:250
std::uint32_t
ripple::KeyCache::sweep
void sweep()
Remove stale entries from the cache.
Definition: KeyCache.h:267
ripple::NodeStore::DatabaseNodeImp::asyncFetch
bool asyncFetch(uint256 const &hash, std::uint32_t seq, std::shared_ptr< NodeObject > &object) override
Fetch an object without waiting.
Definition: DatabaseNodeImp.cpp:42
ripple::NodeStore::DatabaseNodeImp::nCache_
std::shared_ptr< KeyCache< uint256 > > nCache_
Definition: DatabaseNodeImp.h:139
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::KeyCache::setTargetSize
void setTargetSize(size_type s)
Definition: KeyCache.h:177
ripple::NodeStore::DatabaseNodeImp::sweep
void sweep() override
Remove expired entries from the positive and negative caches.
Definition: DatabaseNodeImp.cpp:66
ripple::NodeStore::Database::asyncFetch
virtual bool asyncFetch(uint256 const &hash, std::uint32_t seq, std::shared_ptr< NodeObject > &object)=0
Fetch an object without waiting.
ripple::KeyCache::setTargetAge
void setTargetAge(std::chrono::seconds s)
Definition: KeyCache.h:184