changed: DirContainer - uses std::list instead of std::vector as the main container

we do not have to rebuild indexes after deleting some items
added:   DirContainer::DelById(long id)
changed: Dirs::CheckRootDir() addes the root dir if there is no one
added:   Dirs::DeleteDir(long id) - deletes specified directory (and its contents)
         (from the cache and the database)
added:   Db::DelDirById(long id) - deletes a dir from the database 
         and its first children
added:   standard function rm can delete directories


git-svn-id: svn://ttmath.org/publicrep/cmslu/trunk@487 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
2009-02-06 13:12:03 +00:00
parent 7d73d048c8
commit 951d0e8653
18 changed files with 303 additions and 102 deletions

View File

@@ -986,6 +986,46 @@ return result;
Error Db::DelDirById(long id)
{
Error result = Error::ok;
PGresult * r = 0;
const char * crows;
try
{
AssertConnection();
std::ostringstream query, query2;
query << "delete from core.content where content.id in (select content_id from core.item where parent_id='" << id << "');";
r = AssertQuery( query.str() );
AssertResultStatus(r, PGRES_COMMAND_OK);
crows = PQcmdTuples(r);
if( crows )
log << log2 << "Db: deleted " << atol(crows) << " rows from core.content" << logend;
query2 << "delete from core.item where id='" << id << "' or parent_id='" << id << "';";
r = AssertQuery( query2.str() );
AssertResultStatus(r, PGRES_COMMAND_OK);
crows = PQcmdTuples(r);
if( crows )
log << log1 << "Db: deleted dir: " << id << " (deleted: " << atol(crows) << " rows)" << logend;
}
catch(const Error & e)
{
result = e;
}
ClearResult(r);
return result;
}
bool Db::DelItemDelItem(const Item & item)
{

View File

@@ -53,6 +53,8 @@ public:
bool GetPriv(Item & item, long id);
Error EditPrivById(Item & item, long id);
Error DelDirById(long id);
bool DelItem(const Item & item);
void GetDirs(DirContainer & dir_table);
void GetUsers(UGContainer<User> & user_table);

View File

@@ -22,8 +22,8 @@ DirContainer::Iterator DirContainer::GetRoot()
{
if( !is_root )
return table.end();
return table.begin() + root_index;
return root_iter;
}
@@ -54,31 +54,26 @@ bool DirContainer::Empty()
void DirContainer::PushBack(const Item & item)
{
bool rebuild_indexes = false;
if( table.size() == table.capacity() )
rebuild_indexes = true;
if( item.parent_id == -1 )
if( item.parent_id == -1 && is_root )
{
if( is_root )
log << log1 << "DirCont: more than one root dir - skipped, id: " << item.id << logend;
else
{
is_root = true;
root_index = table.size();
}
log << log1 << "DirCont: more than one root dir - skipped, id: " << item.id << logend;
return;
}
table.push_back(item);
log << log2 << "DirCont: added dir, url: " << item.url << ", id: " << item.id << ", parent_id: " << item.parent_id << logend;
Iterator last_iter = table.insert(table.end(), item);
if( rebuild_indexes )
RebuildIndexes();
else
AddIndexes( --table.end() );
if( item.parent_id == -1 )
{
is_root = true;
root_iter = last_iter;
}
log << log2 << "DirCont: added dir, url: " << item.url << ", id: " << item.id << ", parent_id: " << item.parent_id << logend;
table_id.insert( std::make_pair(last_iter->id, last_iter) );
table_parent.insert( std::make_pair(last_iter->parent_id, last_iter) );
log << log3 << "DirCont: added indexes to dir, id: " << last_iter->id << ", parent_id: " << last_iter->parent_id << logend;
}
@@ -90,6 +85,7 @@ void DirContainer::Clear()
table.clear();
table_id.clear();
table_parent.clear();
is_root = false;
}
@@ -107,34 +103,6 @@ return i->second;
void DirContainer::AddIndexes(Iterator item)
{
table_id.insert( std::make_pair(item->id, item) );
table_parent.insert( std::make_pair(item->parent_id, item) );
log << log2 << "DirCont: added indexes to dir, id: " << item->id << ", parent_id: " << item->parent_id << logend;
}
void DirContainer::RebuildIndexes()
{
Iterator i;
log << log2 << "DirCont: rebuilding indexes" << logend;
table_id.clear();
table_parent.clear();
for(i=table.begin() ; i!=table.end() ; ++i)
AddIndexes( i );
log << log2 << "DirCont: indexes rebuilt, table.size: " << table.size() << ", table_id.size: " << table_id.size() << ", table_parent.size: " << table_parent.size() << logend;
}
DirContainer::ParentIterator DirContainer::ParentBegin()
{
return table_parent.begin();
@@ -184,3 +152,56 @@ DirContainer::ParentIterator DirContainer::NextParent(ParentIterator i)
return i;
}
bool DirContainer::DelById(long id)
{
TableId::iterator i = table_id.find(id);
if( i == table_id.end() )
{
log << log1 << "DirCont: delete: there is no directory with id: " << id << logend;
return false;
}
long parent_id = i->second->parent_id;
TableParent::iterator z = table_parent.lower_bound(parent_id);
bool found = false;
for( ; z != table_parent.end() && z->first == parent_id ; ++z )
{
if( z->second == i->second )
{
log << log2 << "DirCont: deleted directory id: " << id << ", url: " << i->second->url;
if( i->second->parent_id == -1 )
{
log << log2 << " (root directory)";
is_root = false;
}
log << log2 << logend;
table.erase(i->second);
log << log3 << "DirCont: deleted indexes into directory id: " << id << logend;
table_id.erase(i);
table_parent.erase(z);
found = true;
break;
}
}
if( !found )
{
log << log1 << "DirCont: can't find an index_parent into directory id: " << id << ", url: " << i->second->url << " (deleting skipped)" << logend;
return false;
}
return true;
}

View File

@@ -10,7 +10,7 @@
#ifndef headerfiledircontainer
#define headerfiledircontainer
#include <vector>
#include <list>
#include <map>
#include "item.h"
#include "log.h"
@@ -20,9 +20,9 @@ class DirContainer
{
public:
typedef std::vector<Item> Table;
typedef Table::iterator Iterator;
typedef Table::size_type SizeType;
typedef std::list<Item> Table;
typedef Table::iterator Iterator;
typedef Table::size_type SizeType;
typedef std::map<long, Iterator> TableId;
typedef std::multimap<long, Iterator> TableParent;
@@ -43,6 +43,8 @@ public:
Iterator FindId(long id);
bool DelById(long id);
ParentIterator ParentBegin();
ParentIterator ParentEnd();
ParentSizeType ParentSize();
@@ -53,17 +55,14 @@ public:
private:
void AddIndexes(Iterator item);
void RebuildIndexes();
// main table with dirs
Table table;
// true if there is a root dir in the table
bool is_root;
// root dir
SizeType root_index;
// root
Iterator root_iter;
// indexes
TableId table_id;

View File

@@ -19,23 +19,27 @@ void Dirs::Clear()
void Dirs::CheckRootDir()
{
DirContainer::ParentIterator i;
long roots = 0;
for( i=dir_table.FindFirstParent(-1) ; i!=dir_table.ParentEnd() ; i=dir_table.NextParent(i) )
++roots;
if( roots == 0 )
DirContainer::Iterator i = dir_table.GetRoot();
if( i != dir_table.End() )
return;
log << log1 << "Dirs: there is no a root dir in the database (creating one)" << logend;
Item root;
root.type = Item::dir;
root.parent_id = -1;
root.user_id = -1;
root.group_id = -1;
root.privileges = 0755;
root.default_item = -1;
// !! upewnic sie ze baza nie zmieni url (gdyby wczesniej juz byl w bazie pusty url)
// !! zrobic jakis wyjatek do wprowadzania roota?
if( db.AddItem(root) == Error::ok )
{
log << log1 << "Dirs: there is no a root dir in the database" << logend;
Clear();
}
else
if( roots > 1 )
{
log << log1 << "Dirs: there are more than one root dir in the database" << logend;
Clear();
dir_table.PushBack(root);
}
}
@@ -372,3 +376,30 @@ void Dirs::SplitPath(const std::string & path, std::string & dir, std::string &
if( i < path.size() - 1 )
file.assign(path, i+1, path.size() - i - 1);
}
void Dirs::DeleteDir(long id)
{
DirContainer::ParentIterator pnext, p = dir_table.FindFirstParent(id);
for( ; p != dir_table.ParentEnd() ; p = pnext )
{
// this iterator p will be deleted by the next DeleteDir(p->second->id)
// (the next iterator we must calculate beforehand)
pnext = dir_table.NextParent(p);
DeleteDir(p->second->id);
}
if( db.DelDirById(id) == Error::ok )
dir_table.DelById(id);
}

View File

@@ -24,19 +24,6 @@
class Dirs
{
private:
public: // !! temporarily
DirContainer dir_table;
bool ExtractName(const char * & s, std::string & name);
void CheckRootDir();
public:
void Clear();
@@ -86,6 +73,20 @@ public:
static void SplitPath(const std::string & path, std::string & dir, std::string & file);
void DeleteDir(long id);
void CheckRootDir();
private:
DirContainer dir_table;
bool ExtractName(const char * & s, std::string & name);
};

View File

@@ -23,6 +23,7 @@ public:
added_item,
edited_item,
deleted_item,
deleted_dir,
privileged_item,
added_dir,
defaulted_dir,

View File

@@ -43,7 +43,7 @@ public:
no_function, // !! zamienic na no_function
item_required, // !! zamienic na no_item (i usunac db_no_item)
unknown_param,
unknown = 1000

View File

@@ -33,6 +33,7 @@ void FunctionParser::ParseDirectories()
return;
}
while( true )
{
request.dir_table.push_back( pdir );

View File

@@ -31,6 +31,8 @@ void Session::Clear()
done_status = Error::ok;
item.Clear();
done_timer = 0;
dir_old.clear();
}
@@ -72,5 +74,8 @@ void Session::CheckTimers()
}
}
void Session::IncrementTimersIfExist()
{
if( done_timer != 0 )
++done_timer;
}

View File

@@ -49,6 +49,8 @@ struct Session
Item item;
std::string dir_old;
// -------------------
Session();
@@ -57,6 +59,7 @@ struct Session
bool operator<(const Session & s) const;
bool DecTimer(int & timer);
void CheckTimers();
void IncrementTimersIfExist();
};