コード例 #1
0
ファイル: ldbcache.cpp プロジェクト: ashang/pepper
// Adds the revision to the cache
void LdbCache::put(const std::string &id, const Revision &rev)
{
	if (!m_db) opendb();

	MOStream rout;
	rev.write(rout);
	std::vector<char> data(rout.data());
	leveldb::Status s = m_db->Put(leveldb::WriteOptions(), id, std::string(data.begin(), data.end()));
	if (!s.ok()) {
		throw PEX(str::printf("Error writing to cache: %s", s.ToString().c_str()));
	}
}
コード例 #2
0
ファイル: cache.cpp プロジェクト: pombredanne/pepper
// Adds the revision to the cache
void Cache::put(const std::string &id, const Revision &rev)
{
	if (!m_loaded) {
		load();
	}

	// Defer any signals while writing to the cache
	SIGBLOCK_DEFER();

	// Add revision to cache
	std::string dir = m_opts.cacheDir() + "/" + uuid(), path;
	if (m_cout == NULL) {
		m_coindex = 0;
		do {
			path = str::printf("%s/cache.%u", dir.c_str(), m_coindex);
			if (!sys::fs::fileExists(path) || sys::fs::filesize(path) < MAX_CACHEFILE_SIZE) {
				break;
			}
			++m_coindex;
		} while (true);

		delete m_cout;
		m_cout = new BOStream(path, true);
	} else if (m_cout->tell() >= MAX_CACHEFILE_SIZE) {
		delete m_cout;
		path = str::printf("%s/cache.%u", dir.c_str(), ++m_coindex);
		m_cout = new BOStream(path, true);
	}

	uint32_t offset = m_cout->tell();
	MOStream rout;
	rev.write03(rout);
	std::vector<char> compressed = utils::compress(rout.data());
	*m_cout << compressed;

	// Add revision to index
	if (m_iout == NULL) {
		if (sys::fs::exists(dir + "/index")) {
			m_iout = new GZOStream(dir + "/index", true);
		} else {
			m_iout = new GZOStream(dir + "/index", false);
			// Version number
			*m_iout << CACHE_VERSION;
		}
	}
	*m_iout << id;
	*m_iout << m_coindex << offset << utils::crc32(compressed);

	// Update cached index
	m_index[id] = std::pair<uint32_t, uint32_t>(m_coindex, offset);
}