mirror of https://github.com/trapexit/mergerfs.git
2 changed files with 0 additions and 212 deletions
@ -1,151 +0,0 @@ |
|||||
/*
|
|
||||
ISC License |
|
||||
|
|
||||
Copyright (c) 2026, Antonio SJ Musumeci <trapexit@spawn.link> |
|
||||
|
|
||||
Permission to use, copy, modify, and/or distribute this software for any |
|
||||
purpose with or without fee is hereby granted, provided that the above |
|
||||
copyright notice and this permission notice appear in all copies. |
|
||||
|
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
||||
*/ |
|
||||
|
|
||||
#include "policy_cache.hpp"
|
|
||||
|
|
||||
#include "mutex.hpp"
|
|
||||
|
|
||||
#include <cstdlib>
|
|
||||
#include <map>
|
|
||||
#include <string>
|
|
||||
|
|
||||
#include <time.h>
|
|
||||
|
|
||||
using std::map; |
|
||||
using std::string; |
|
||||
|
|
||||
static const uint64_t DEFAULT_TIMEOUT = 0; |
|
||||
|
|
||||
namespace l |
|
||||
{ |
|
||||
static |
|
||||
uint64_t |
|
||||
get_time(void) |
|
||||
{ |
|
||||
uint64_t rv; |
|
||||
|
|
||||
rv = ::time(NULL); |
|
||||
|
|
||||
return rv; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
|
|
||||
PolicyCache::Value::Value() |
|
||||
: time(0), |
|
||||
paths() |
|
||||
{ |
|
||||
|
|
||||
} |
|
||||
|
|
||||
PolicyCache::PolicyCache(void) |
|
||||
: timeout(DEFAULT_TIMEOUT) |
|
||||
{ |
|
||||
mutex_init(&_lock); |
|
||||
} |
|
||||
|
|
||||
void |
|
||||
PolicyCache::erase(const char *fusepath_) |
|
||||
{ |
|
||||
if(timeout == 0) |
|
||||
return; |
|
||||
|
|
||||
mutex_lock(&_lock); |
|
||||
|
|
||||
_cache.erase(fusepath_); |
|
||||
|
|
||||
mutex_unlock(&_lock); |
|
||||
} |
|
||||
|
|
||||
void |
|
||||
PolicyCache::cleanup(const int prob_) |
|
||||
{ |
|
||||
uint64_t now; |
|
||||
map<string,Value>::iterator i; |
|
||||
|
|
||||
if(timeout == 0) |
|
||||
return; |
|
||||
|
|
||||
if(rand() % prob_) |
|
||||
return; |
|
||||
|
|
||||
now = l::get_time(); |
|
||||
|
|
||||
mutex_lock(&_lock); |
|
||||
|
|
||||
i = _cache.begin(); |
|
||||
while(i != _cache.end()) |
|
||||
{ |
|
||||
if((now - i->second.time) >= timeout) |
|
||||
_cache.erase(i++); |
|
||||
else |
|
||||
++i; |
|
||||
} |
|
||||
|
|
||||
mutex_unlock(&_lock); |
|
||||
} |
|
||||
|
|
||||
void |
|
||||
PolicyCache::clear(void) |
|
||||
{ |
|
||||
mutex_lock(&_lock); |
|
||||
|
|
||||
_cache.clear(); |
|
||||
|
|
||||
mutex_unlock(&_lock); |
|
||||
} |
|
||||
|
|
||||
int |
|
||||
PolicyCache::operator()(const Policy::Search &policy_, |
|
||||
const Branches &branches_, |
|
||||
const char *fusepath_, |
|
||||
std::vector<Branch*> &paths_) |
|
||||
{ |
|
||||
int rv; |
|
||||
Value *v; |
|
||||
uint64_t now; |
|
||||
|
|
||||
if(timeout == 0) |
|
||||
return policy_(branches_,fusepath_,paths_); |
|
||||
|
|
||||
now = l::get_time(); |
|
||||
|
|
||||
mutex_lock(&_lock); |
|
||||
v = &_cache[fusepath_]; |
|
||||
|
|
||||
if((now - v->time) >= timeout) |
|
||||
{ |
|
||||
mutex_unlock(&_lock); |
|
||||
|
|
||||
rv = policy_(branches_,fusepath_,paths_); |
|
||||
if(rv == -1) |
|
||||
return -1; |
|
||||
|
|
||||
mutex_lock(&_lock); |
|
||||
v->time = now; |
|
||||
v->paths = paths_; |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
paths_ = v->paths; |
|
||||
} |
|
||||
|
|
||||
mutex_unlock(&_lock); |
|
||||
|
|
||||
return 0; |
|
||||
} |
|
||||
@ -1,61 +0,0 @@ |
|||||
/*
|
|
||||
ISC License |
|
||||
|
|
||||
Copyright (c) 2026, Antonio SJ Musumeci <trapexit@spawn.link> |
|
||||
|
|
||||
Permission to use, copy, modify, and/or distribute this software for any |
|
||||
purpose with or without fee is hereby granted, provided that the above |
|
||||
copyright notice and this permission notice appear in all copies. |
|
||||
|
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
||||
*/ |
|
||||
|
|
||||
#pragma once
|
|
||||
|
|
||||
#include "policy.hpp"
|
|
||||
|
|
||||
#include <cstdint>
|
|
||||
#include <string>
|
|
||||
#include <map>
|
|
||||
|
|
||||
#include <pthread.h>
|
|
||||
|
|
||||
|
|
||||
class PolicyCache |
|
||||
{ |
|
||||
public: |
|
||||
struct Value |
|
||||
{ |
|
||||
Value(); |
|
||||
|
|
||||
uint64_t time; |
|
||||
std::vector<Branch*> paths; |
|
||||
}; |
|
||||
|
|
||||
public: |
|
||||
PolicyCache(void); |
|
||||
|
|
||||
public: |
|
||||
void erase(const char *fusepath); |
|
||||
void cleanup(const int prob = 1); |
|
||||
void clear(void); |
|
||||
|
|
||||
public: |
|
||||
int operator()(const Policy::Search &policy, |
|
||||
const Branches &branches, |
|
||||
const char *fusepath, |
|
||||
std::vector<Branch*> &paths); |
|
||||
|
|
||||
public: |
|
||||
uint64_t timeout; |
|
||||
|
|
||||
private: |
|
||||
pthread_mutex_t _lock; |
|
||||
std::map<std::string,Value> _cache; |
|
||||
}; |
|
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue