Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

NVActiveDB Class Reference

#include <NVActiveDB.h>

Inheritance diagram for NVActiveDB:

ActiveDB NVHash NVlist NVcontainer List of all members.

Public Member Functions

 NVActiveDB ()
 NVActiveDB (char *dbname)
int lock (int command, int block=Block)
int get_lock (void)
void open (const char *dbname)
int is_open (void)
void close (void)
void setmtime (unsigned long tm, int force=0)
void getmtime (unsigned long *tm)
void clear (void)
int is_empty (void)
void add (GroupInfo &gi)
void set (GroupInfo &gi, int flags=0)
int get (const char *group, GroupInfo *gi)
int hasgroup (const char *group)
void read (std::istream &is, const char *filter, int flags=0)
void write (std::ostream &os, nvtime_t ctime=0, int mode=m_active, const char *filter=NULL)
Iter< GroupInfobegin ()
Iter< GroupInfoend ()

Protected Member Functions

unsigned long hash (const char *strg)
void sset (GroupInfo &gi, int flags=0)

Friends

std::ostream & operator<< (std::ostream &os, NVActiveDB &adb)

Detailed Description

Author:
Thomas Gschwind

Bug:
Documentation is missing.

Definition at line 41 of file NVActiveDB.h.


Constructor & Destructor Documentation

NVActiveDB::NVActiveDB  )  [inline]
 

Definition at line 58 of file NVActiveDB.h.

00058                      :NVHash() {
00059         } NVActiveDB(char *dbname):NVHash(dbname, NVActiveDB_HASHSIZE) {

NVActiveDB::NVActiveDB char *  dbname  )  [inline]
 

Definition at line 59 of file NVActiveDB.h.

00059                                   :NVHash(dbname, NVActiveDB_HASHSIZE) {
00060         }


Member Function Documentation

void NVActiveDB::add GroupInfo gi  )  [virtual]
 

Adds the description of the newsgroup. If the newsgroup already exists in the active database, it adds a duplicate.

Parameters:
gi Description of the newsgroup

Implements ActiveDB.

Definition at line 89 of file NVActiveDB.cc.

References NVHash::add(), GroupInfo::getraw(), hash(), GroupInfo::name(), Logger::p(), slog, and VERB.

00090 {
00091         VERB(slog.p(Logger::Debug) << "NVActiveDB::add(GroupInfo &gi)\n)");
00092         const char *buf;
00093         unsigned int bufsz;
00094         gi.getraw(&buf, &bufsz);
00095         NVHash::add(hash(gi.name()), buf, bufsz);
00096 }

Iter< GroupInfo > NVActiveDB::begin  )  [inline, virtual]
 

Implements ActiveDB.

Definition at line 222 of file NVActiveDB.h.

00223 {
00224         return Iter < GroupInfo > (new NVActiveDB_Iter(this, true));
00225 }

void NVActiveDB::clear void   )  [inline, virtual]
 

Implements ActiveDB.

Definition at line 86 of file NVActiveDB.h.

References NVHash::clear().

Referenced by read().

00086                          {
00087                 NVHash::clear();
00088         }

void NVActiveDB::close void   )  [inline, virtual]
 

Reimplemented from NVcontainer.

Definition at line 75 of file NVActiveDB.h.

References NVcontainer::close().

00075                          {
00076                 NVHash::close();
00077         }

Iter< GroupInfo > NVActiveDB::end  )  [inline, virtual]
 

Implements ActiveDB.

Definition at line 227 of file NVActiveDB.h.

00228 {
00229         return Iter < GroupInfo > (new NVActiveDB_Iter(this, false));
00230 }

int NVActiveDB::get const char *  group,
GroupInfo gi
[virtual]
 

Stores informations of the newsgroup group in gi.

Returns:
0 on success and -1 if the requested group cannot be found

Implements ActiveDB.

Definition at line 98 of file NVActiveDB.cc.

References hash(), lock(), NVlist::o2r(), GroupInfo::setraw(), slog, and VERB.

Referenced by CServer::getgroup(), and CServer::groupinfo().

00099 {
00100         VERB(slog.
00101              p(Logger::Debug) << "NVActiveDB::get(" << group << ",&gi)\n");
00102         lock(ShrdLock);
00103         Record *head = o2r(hashtab[hash(group)]);
00104         Record *r = head;
00105 
00106         while (r) {
00107                 r = o2r(r->next);
00108 
00109                 // The first approach is the cleaner one since it uses 
00110                 // GroupInfo's setraw function. However, this involves 
00111                 // a copy operation
00112 //     gi->setraw(r->datap(),r->szdata);
00113 //     if(strcmp(gi->name(),group)==0) break;
00114 
00115                 // The second approach assumes that the GroupInfo
00116                 // data is stored in the same layout in the file.
00117                 // The last (sizeof(GroupInfo)-r->szdata) bytes don't
00118                 // matter. These are junk anyways.
00119                 if (strcmp(((GroupInfo *) r->datap())->name(), group) == 0) {
00120                         gi->setraw(r->datap(), r->szdata);
00121                         break;
00122                 }
00123                 if (r == head)
00124                         r = NULL;
00125         }
00126         lock(UnLock);
00127 
00128         if (!r)
00129                 return -1;
00130         return 0;
00131 }

int NVActiveDB::get_lock void   )  [inline]
 

Reimplemented from NVcontainer.

Definition at line 65 of file NVActiveDB.h.

References NVcontainer::get_lock().

00065                            {
00066                 return NVHash::get_lock();
00067         }

void NVActiveDB::getmtime unsigned long *  tm  )  [inline, virtual]
 

Implements ActiveDB.

Definition at line 82 of file NVActiveDB.h.

References NVcontainer::getmtime().

Referenced by CServer::active_valid().

00082                                          {
00083                 NVHash::getmtime(tm);
00084         }

int NVActiveDB::hasgroup const char *  group  )  [virtual]
 

Checks whether the group exists in the active database.

Parameters:
group Name of the newsgroup
Returns:
1 if the group exists, otherwise 0
Exceptions:
System System Function Errors
ResponseErr Response Error

Implements ActiveDB.

Definition at line 133 of file NVActiveDB.cc.

References hash(), lock(), NVlist::o2r(), slog, and VERB.

00134 {
00135         VERB(slog.
00136              p(Logger::Info) << "NVActiveDB::hasgroup(" << group << ")\n");
00137         lock(ShrdLock);
00138         Record *head = o2r(hashtab[hash(group)]);
00139         Record *r = head;
00140 
00141         while (r) {
00142                 r = o2r(r->next);
00143 
00144                 // The first approach is the cleaner one since it uses 
00145                 // GroupInfo's setraw function. However, this involves 
00146                 // a copy operation
00147 //     gi->setraw(r->datap(),r->szdata);
00148 //     if(strcmp(gi->name(),group)==0) break;
00149 
00150                 // The second approach assumes that the GroupInfo
00151                 // data is stored in the same layout in the file.
00152                 // The last (sizeof(GroupInfo)-r->szdata) bytes don't
00153                 // matter. These are junk anyways.
00154                 if (strcmp(((GroupInfo *) r->datap())->name(), group) == 0) {
00155                         break;
00156                 }
00157                 if (r == head)
00158                         r = NULL;
00159         }
00160         lock(UnLock);
00161 
00162         if (!r)
00163                 return 0;
00164         return 1;
00165 }

unsigned long NVActiveDB::hash const char *  strg  )  [protected]
 

Definition at line 18 of file NVActiveDB.cc.

References NVActiveDB_HASHSIZE.

Referenced by add(), get(), hasgroup(), and sset().

00019 {
00020         unsigned long h = 1;
00021         const char *p = strg;
00022         while (p[0]) {
00023                 h = (h * (p[0]) + p[1]) % NVActiveDB_HASHSIZE;
00024                 if (!p[1])
00025                         break;
00026                 p += 2;
00027         }
00028         return h;
00029 }

int NVActiveDB::is_empty void   )  [inline, virtual]
 

Implements ActiveDB.

Definition at line 89 of file NVActiveDB.h.

References NVHash::is_empty().

00089                            {
00090                 return NVHash::is_empty();
00091         }

int NVActiveDB::is_open void   )  [inline, virtual]
 

Reimplemented from NVcontainer.

Definition at line 72 of file NVActiveDB.h.

References NVcontainer::is_open().

00072                           {
00073                 return NVHash::is_open();
00074         }

int NVActiveDB::lock int  command,
int  block = Block
[inline]
 

Reimplemented from NVcontainer.

Definition at line 62 of file NVActiveDB.h.

References NVcontainer::lock().

Referenced by CServer::active(), get(), hasgroup(), read(), and set().

00062                                                  {
00063                 return NVHash::lock(command, block);
00064         }

void NVActiveDB::open const char *  dbname  )  [inline]
 

Definition at line 69 of file NVActiveDB.h.

References NVActiveDB_HASHSIZE, and NVHash::open().

00069                                       {
00070                 NVHash::open(dbname, NVActiveDB_HASHSIZE);
00071         }

void NVActiveDB::read std::istream &  is,
const char *  filter,
int  flags = 0
[virtual]
 

Read active database records and store those matching the filter expression in the active database.

Exceptions:
System System Function Errors
ResponseErr Response Error

Implements ActiveDB.

Definition at line 167 of file NVActiveDB.cc.

References clear(), lock(), matchgroup(), nlreadline(), GroupInfo::set(), GroupInfo::setflag(), setmtime(), slog, sset(), and VERB.

00168 {
00169         VERB(slog.
00170              p(Logger::Debug) << "NVActiveDB::read(&is,*filter,flags)\n");
00171 
00172         GroupInfo gd;
00173         string line;
00174         time_t now;
00175 
00176         lock(NVcontainer::ExclLock);
00177         if (flags & F_CLEAR)
00178                 clear();
00179         while (!is.eof()) {
00180                 // Each line has the following form
00181                 // GroupName Last First Flags
00182                 nlreadline(is, line, 0);
00183                 if (line == ".")
00184                         break;
00185 
00186                 if (!filter || matchgroup(filter, line.c_str()) > 0) {
00187                         // Add group
00188                         gd.set(line.c_str());
00189                         if (flags & F_STORE_READONLY)
00190                                 gd.setflag('n');
00191                         sset(gd);
00192                 }
00193         }
00194         time(&now);
00195         setmtime(now);
00196         lock(NVcontainer::UnLock);
00197 }

void NVActiveDB::set GroupInfo gi,
int  flags = 0
[inline, virtual]
 

Implements ActiveDB.

Definition at line 99 of file NVActiveDB.h.

References lock(), and sset().

00099                                                 {
00100                 lock(ExclLock);
00101                 sset(gi, flags);
00102                 lock(UnLock);
00103         }

void NVActiveDB::setmtime unsigned long  tm,
int  force = 0
[inline, virtual]
 

Implements ActiveDB.

Definition at line 79 of file NVActiveDB.h.

References NVcontainer::setmtime().

Referenced by CServer::invalidateActiveDB(), and read().

00079                                                        {
00080                 NVHash::setmtime(tm, force);
00081         }

void NVActiveDB::sset GroupInfo gi,
int  flags = 0
[protected]
 

Set newsgroup information for group gi.name(). If the flag update_only is specified, it is regarded as error, if gi.name() does not already exist. Otherwise, it is added to the active database.

Parameters:
gi Description of the newsgroup.
flags See description above.

Definition at line 31 of file NVActiveDB.cc.

References NVHash::add(), GroupInfo::getraw(), hash(), GroupInfo::name(), nvtime(), nvtime_t, NVlist::o2r(), GroupInfo::setctime(), slog, and VERB.

Referenced by read(), and set().

00032 {
00033         //   GroupInfo cg;
00034         const char *gidata;
00035         unsigned int gisz;
00036         Record *head = o2r(hashtab[hash(gi.name())]);
00037         Record *r = head;
00038 
00039         while (r) {
00040                 r = o2r(r->next);
00041 
00042                 // The first approach is the cleaner one since it uses 
00043                 // GroupInfo's setraw function. However, this involves 
00044                 // a copy operation
00045 //     cg.setraw(r->datap(),r->szdata);
00046 //     if(strcmp(gi.name(),cg.name())==0) {
00047 //       gi.setctime(cg.ctime());
00048                 // The second approach assumes that the GroupInfo
00049                 // data is stored in the same layout in the file.
00050                 // The last (sizeof(GroupInfo)-r->szdata) bytes don't
00051                 // matter. These are junk anyways.
00052                 if (strcmp(((GroupInfo *) r->datap())->name(), gi.name())
00053                     == 0) {
00054                         gi.setctime(((GroupInfo *) r->datap())->ctime());
00055                         gi.getraw(&gidata, &gisz);
00056                         if (gisz != r->szdata) {
00057                                 VERB(slog.
00058                                      p(Logger::
00059                                        Error) <<
00060                                      "NVActiveDB::set: Size of new record not equal original record size. Should have been impossible.\n");
00061                         } else {
00062                                 memcpy(r->datap(), gidata, gisz);
00063                         }
00064                         break;
00065                 }
00066                 if (r == head)
00067                         r = NULL;
00068         }
00069 
00070         if (!r) {
00071                 if (flags & update_only) {
00072                         // Should we throw an exception here?!?
00073                         VERB(slog.
00074                              p(Logger::
00075                                Error) <<
00076                              "NVActiveDB::set: Cannot find requested record\n");
00077                 } else {
00078                         const char *buf;
00079                         unsigned int bufsz;
00080                         nvtime_t now;
00081                         nvtime(&now);
00082                         gi.setctime(now);
00083                         gi.getraw(&buf, &bufsz);
00084                         NVHash::add(hash(gi.name()), buf, bufsz);
00085                 }
00086         }
00087 }

void NVActiveDB::write std::ostream &  os,
nvtime_t  ctime = 0,
int  mode = m_active,
const char *  filter = NULL
[virtual]
 

Implements ActiveDB.

Definition at line 199 of file NVActiveDB.cc.

References GroupInfo::ctime(), NVHashIter::data(), ERROR_LOCATION, NVHashIter::first(), matchgroup(), GroupInfo::name(), NVHashIter::next(), nvtime_t, GroupInfo::setraw(), slog, NVHashIter::valid(), and VERB.

00201 {
00202         VERB(slog.
00203              p(Logger::Debug) << "NVActiveDB::write(&os,ctime,...)\n");
00204 
00205         NVHashIter nvhi(*this);
00206         GroupInfo gd;
00207         const char *buf;
00208         unsigned int bufsz;
00209 
00210         for (nvhi.first(); nvhi.valid(); nvhi.next()) {
00211                 nvhi.data(&buf, &bufsz);
00212                 gd.setraw(buf, bufsz);
00213                 if (((!filter) ||
00214                      ((matchgroup(filter, gd.name()) > 0))) &&
00215                     ctime < gd.ctime()) {
00216                         if (mode == m_active)
00217                                 os << gd << "\r\n";
00218                         else
00219                                 os << gd.name() << " " << gd.
00220                                     ctime() << " news\r\n";
00221                         if (!os.good()) {
00222                                 throw SystemError("Cannot write GroupList",
00223                                                   errno, ERROR_LOCATION);
00224                         }
00225                 }
00226         }                       /* for */
00227 }


Friends And Related Function Documentation

std::ostream& operator<< std::ostream &  os,
NVActiveDB adb
[friend]
 

Definition at line 134 of file NVActiveDB.h.

00135                                                            {
00136                 adb.write(os);
00137                 return os;
00138         }


The documentation for this class was generated from the following files:
Generated on Sun Oct 24 21:08:23 2004 for NewsCache by doxygen 1.3.6-20040222