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)
122 _blockPath = path.
get(
"blocks");
123 _blobPath = path.
get(
"blob");
136 Task *t = _tasks.
getnpop(
true);
139 BlockingTask &btask =
dynamic_cast<BlockingTask&
>(*t);
142 }
catch (
const std::exception&) {
148 }
catch (
const std::bad_cast&) { };
151 std::auto_ptr<Task> killer(t);
153 }
catch (
const std::exception&) { };
234 _database.get(cb, result);
246 _database.
get(
id, bundle, blocks);
248 for (SQLiteDatabase::blocklist::const_iterator iter = blocks.begin(); iter != blocks.end(); ++iter)
251 const int blocktyp = entry.first;
257 std::ifstream is(file.
getPath().c_str(), std::ios::binary | std::ios::in);
262 SQLiteBLOB *blob =
new SQLiteBLOB(_blobPath);
269 blob->_file.remove();
272 if ( ::link(file.
getPath().c_str(), blob->_file.getPath().c_str()) != 0 )
277 std::ofstream fout(blob->_file.getPath().c_str(), std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
282 const std::streamsize length = stream.
size();
290 blob->_file.remove();
313 }
catch (
const std::bad_cast&) { };
342 _database.
store(bundle);
368 const SQLiteBLOB &blob =
dynamic_cast<const SQLiteBLOB&
>(*ref);
374 if ( ::link(blob._file.getPath().c_str(), tmpfile.
getPath().c_str()) != 0 )
379 std::ofstream fout(tmpfile.
getPath().c_str(), std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
381 const std::streamsize length = stream.
size();
384 }
catch (
const std::bad_cast&) {
386 std::ofstream fout(tmpfile.
getPath().c_str(), std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
388 const std::streamsize length = stream.
size();
391 }
catch (
const std::bad_cast&) {
398 storedBytes += tmpfile.
size();
401 _database.
store(
id, index, block, tmpfile);
407 std::ofstream filestream(tmpfile.
getPath().c_str(), std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
413 storedBytes += tmpfile.
size();
416 _database.
store(
id, index, block, tmpfile);
492 return _database.
empty();
503 return _database.
count();
519 }
catch (
const std::bad_cast&) { }
528 TaskIdle::_idle =
true;
531 _tasks.push(
new TaskIdle());
537 TaskIdle::_idle =
false;
539 }
catch (
const std::bad_cast&) { }
546 storage._database.
expire(_timestamp);
552 void SQLiteBundleStorage::TaskIdle::run(SQLiteBundleStorage &storage)
568 storage._database.vacuum();
577 if (!TaskIdle::_idle)
return;
581 const std::string SQLiteBundleStorage::getName()
const
583 return "SQLiteBundleStorage";
609 eventBundleRemoved(
id);