49 const std::string SQLiteBundleStorage::TAG =
"SQLiteBundleStorage";
52 bool SQLiteBundleStorage::TaskIdle::_idle =
false;
54 SQLiteBundleStorage::SQLiteBLOB::SQLiteBLOB(
const ibrcommon::File &path)
59 SQLiteBundleStorage::SQLiteBLOB::~SQLiteBLOB()
65 void SQLiteBundleStorage::SQLiteBLOB::clear()
71 _filestream.open(_file.getPath().c_str(), ios::in | ios::out | ios::trunc | ios::binary );
73 if (!_filestream.is_open())
80 void SQLiteBundleStorage::SQLiteBLOB::open()
85 _filestream.open(_file.getPath().c_str(), ios::in | ios::out | ios::binary );
87 if (!_filestream.is_open())
94 void SQLiteBundleStorage::SQLiteBLOB::close()
105 std::streamsize SQLiteBundleStorage::SQLiteBLOB::__get_size()
116 :
BundleStorage(maxsize), _database(path.get(
"sqlite.db"), *this)
119 if (usePersistentBundleSets)
126 _blockPath = path.
get(
"blocks");
127 _blobPath = path.
get(
"blob");
169 Task *t = _tasks.
getnpop(
true);
172 BlockingTask &btask =
dynamic_cast<BlockingTask&
>(*t);
175 }
catch (
const std::exception&) {
181 }
catch (
const std::bad_cast&) { };
184 std::auto_ptr<Task> killer(t);
186 }
catch (
const std::exception&) { };
240 _database.get(cb, result);
252 _database.
get(
id, bundle, blocks);
254 for (SQLiteDatabase::blocklist::const_iterator iter = blocks.begin(); iter != blocks.end(); ++iter)
257 const int blocktyp = entry.first;
263 std::ifstream is(file.
getPath().c_str(), std::ios::binary | std::ios::in);
268 SQLiteBLOB *blob =
new SQLiteBLOB(_blobPath);
275 blob->_file.remove();
278 if ( ::link(file.
getPath().c_str(), blob->_file.getPath().c_str()) != 0 )
283 std::ofstream fout(blob->_file.getPath().c_str(), std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
288 const std::streamsize length = stream.
size();
301 blob->_file.remove();
324 }
catch (
const std::bad_cast&) { };
360 _database.
store(bundle, size);
387 const SQLiteBLOB &blob =
dynamic_cast<const SQLiteBLOB&
>(*ref);
393 if ( ::link(blob._file.getPath().c_str(), tmpfile.
getPath().c_str()) != 0 )
398 std::ofstream fout(tmpfile.
getPath().c_str(), std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
400 const std::streamsize length = stream.
size();
403 }
catch (
const std::bad_cast&) {
405 std::ofstream fout(tmpfile.
getPath().c_str(), std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
407 const std::streamsize length = stream.
size();
410 }
catch (
const std::bad_cast&) {
417 storedBytes += tmpfile.
size();
420 _database.
store(
id, index, block, tmpfile);
426 std::ofstream filestream(tmpfile.
getPath().c_str(), std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
432 storedBytes += tmpfile.
size();
435 _database.
store(
id, index, block, tmpfile);
482 _database.
get(
id, ret);
525 return _database.
empty();
536 return _database.
count();
552 }
catch (
const std::bad_cast&) { }
561 TaskIdle::_idle =
true;
564 _tasks.push(
new TaskIdle());
570 TaskIdle::_idle =
false;
572 }
catch (
const std::bad_cast&) { }
579 storage._database.
expire(_timestamp);
585 void SQLiteBundleStorage::TaskIdle::run(SQLiteBundleStorage &storage)
601 storage._database.vacuum();
610 if (!TaskIdle::_idle)
return;
614 const std::string SQLiteBundleStorage::getName()
const
616 return "SQLiteBundleStorage";
645 eventBundleRemoved(
id);