Esempio n. 1
0
    // erase
    size_t erase(const KEY_T& key) {
      if (file_mode == READ_ONLY) {
        throw std::runtime_error("Error: erase called in RO mode");
      }

      size_t num_erased = map->erase(key);
      return num_erased;
    }
Esempio n. 2
0
 /** Prune the cache back to a given size.
  *  If the cache is larger than the maximum, the oldest entries
  *  will be deleted.
  * @post size() <= max_size
  * @param max_size  The maximum cache size */
 void prune (size_t max_size)
 {
     while (size_ > max_size)
     {
         map_.erase(list_.back().first);
         list_.pop_back();
         --size_;
     }
 }
Esempio n. 3
0
    /** Remove an element from the cache. */
    void remove (const key_type& k)
    {
        auto found (map_.find(k));
        if (found == map_.end())
            return;

        list_.erase(found->second);
        map_.erase(found);
        --size_;
    }
Esempio n. 4
0
 void prune (size_t max_size, func on_remove)
 {
     while (size_ > max_size)
     {
         pair_t& tbr (list_.back());
         on_remove(tbr.first, tbr.second);
         map_.erase(tbr.first);
         list_.pop_back();
         --size_;
     }
 }
Esempio n. 5
0
    void prune_if (size_t max_size, pred op)
    {
        if (list_.empty())
            return;

        auto i (std::prev(list_.end()));
        while (size_ > max_size)
        {
            if (op(*i))
            {
                map_.erase(i->first);
                i = list_.erase(i);
                --size_;
            }
            if (i == list_.begin())
                return;

            --i;
        }
    }
Esempio n. 6
0
		bool set_fields_if_equals(doid_t do_id, const map_t &equals, map_t &values)
		{
			m_log->trace() << "Setting fields if equals on obj-" << do_id << endl;

			YAML::Node document;
			if(!load(do_id, document))
			{
				values.clear();
				return false;
			}

			// Get current field values from the file
			const Class* dcc = g_dcf->get_class_by_name(document["class"].as<string>());
			ObjectData dbo(dcc->get_id());
			YAML::Node existing = document["fields"];
			for(auto it = existing.begin(); it != existing.end(); ++it)
			{
				const Field* field = dcc->get_field_by_name(it->first.as<string>());
				if(!field)
				{
					m_log->warning() << "Field '" << it->first.as<string>()
					                 << "', loaded from '" << filename(do_id)
					                 << "', does not exist." << endl;
					continue;
				}
				vector<uint8_t> value = read_yaml_field(field, it->second, do_id);
				if(value.size() > 0)
				{
					dbo.fields[field] = value;
				}
			}

			// Check if equals matches current values
			bool fail = false;
			for(auto it = equals.begin(); it != equals.end(); ++it)
			{
				auto found = dbo.fields.find(it->first);
				if(found == dbo.fields.end())
				{
					values.erase(it->first);
					fail = true;
				}
				else if(it->second != found->second)
				{
					values.erase(it->first);
					fail = true;
				}
			}

			// Return current values on failure
			if(fail)
			{
				for(auto it = values.begin(); it != values.end(); ++it)
				{
					it->second = dbo.fields[it->first];
				}
				return false;
			}

			// Update existing values on success
			for(auto it = values.begin(); it != values.end(); ++it)
			{
				dbo.fields[it->first] = it->second;
			}
			write_yaml_object(do_id, dcc, dbo);
			return true;
		}
Esempio n. 7
0
 //Deletes the entry of a key in the resource map.
 //This will call deleter_type to deallocate the resource from memory as well.
 void Free(key_type const &key) noexcept
 {
     map_i i = m_map.find(key);
     m_map.erase(i);
 }