home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cslio205.zip / INCLUDE / CSBUFF.H < prev    next >
C/C++ Source or Header  |  1997-01-21  |  8KB  |  243 lines

  1. /***********************************************************************
  2.  
  3.                       CSDB Library, Free Evaluation Version 2.0.5 
  4.                                        Release: January 22th 1997 
  5.  
  6.        Header file of the BUFFER class.
  7.  
  8.        This class buffers the IO done by the database classes.
  9.        BUFFER neither opens nor closes a stream. Its open function accept
  10.        a file pointer to an already opened stream. Similar, the close()
  11.        function stops the buffering process, but doesn't close the stream.
  12.  
  13.                                            Copyright(c) 1994-1997 
  14.                                                           ComBits 
  15.                                                   The Netherlands 
  16. ***********************************************************************/
  17.  
  18. #ifndef __CSBUFF_H
  19. #define __CSBUFF_H
  20.  
  21. #include "csstr.h"
  22. #include "cstable.h"
  23. #include "cstypes.h"
  24.  
  25.  
  26. class BUFFER: public TABLE
  27. {
  28.  
  29. private:
  30.  
  31.    FILE *fipo;
  32.  
  33.    csSTR b_name;    // Name of the buffer system
  34.    int     is_open;
  35.    int     bReadOnly;
  36.  
  37. protected:
  38.  
  39.  
  40.    U32 bufbit;
  41.  
  42.  
  43.    enum { ST_READ=0, ST_DIRTY=1 };
  44.  
  45.    typedef struct
  46.    {
  47.       void *prev;
  48.       void *next;
  49.       void **key_add;
  50.       U32  key;
  51.       int  stat;
  52.       int  prior;
  53.    } bcp;          // Buffer Chain Pointers
  54.  
  55.  
  56.    U32     lbsc_k;      // Load Buffer Short Cut Key
  57.    int     lbsc_p;      // Load Buffer Short Cut Priority
  58.    int     lbsc_s;      // Load Buffer Short Cut Status
  59.    void *lbsc_a;      // Load Buffer Short Cut Address
  60.  
  61.    U32    *bupo;
  62.    U32    *bupn;
  63.    bcp    *buch;          // Buffer chains
  64.    U16    *buma;          // Max number of buffer in chain
  65.    U16    *buco;          // Number of buffers currently in chain
  66.  
  67.  
  68.    U32    reco;          // Number of reads from file
  69.    U32    wrco;          // Number of writes to file.
  70.    U32    fbco;          // Number of times a 'force-buffer' was needed.
  71.    U32    llco;          // Locate and Load count;
  72.  
  73.    U16    bs;          // Buffer Size
  74.    U16    nb;          // Maximum Number of Buffers
  75.    U16    nbm;          // Minimum Number of Buffers
  76.    U16    na;          // Number of buffers currently Allocated from OS
  77.    U16    bas;          // Buffer Allocation Size (bs+sizeof(bcp))
  78.  
  79.    U16    buf_lim_nr;   // Limits the index blocks down.
  80.    U16    headpagesize; // Size of the first page, containing the header.
  81.    S32    headoff;      // Offset to the first page in file. (Can be negative)
  82.    int    enoug_mem;    // Short-cut in memory allocation.
  83.  
  84.    int    echo_on;
  85.  
  86.  
  87.  
  88. protected:
  89.  
  90. ////////////////////////// Core functions //////////////////////////////////////
  91. //   Chain
  92.    void connect(bcp &left,bcp &right)    { left.next=&right; right.prev=&left; }
  93.    void connect(void *left,void *right) { connect(*(bcp *)left,*(bcp *)right); }
  94.    void end_ic(bcp &c,bcp *p)        { connect(*(bcp *)c.prev,*p); connect(*p,c); }
  95.    bcp *init_bcp(void *d,U32 key,void **add,int prior,int stat);
  96.  
  97. //   Buffers
  98.    void *i2b(void *p)           { return ((csCHAR *)p-bs); }
  99.    void *b2i(void *p)           { return ((csCHAR *)p+bs); }
  100.    void  force_buff(U32 key,void *& data,void ** addr,int prior,int stat);
  101.  
  102. //   I/O
  103.    U32     pos_infile(U32 pagnr)          { return headoff+pagnr*bs; }
  104.    void  file_2_buffer(U32 key,void *buf);
  105.    void  buffer_2_file(U32 key,void *buf);
  106.  
  107.  
  108.  
  109. protected:
  110.  
  111.    void free_chain(bcp *p);
  112.    void init_allocs(void);
  113.    void init_vars(void);
  114.    void init_defaults(void);
  115.  
  116. ////////////////////////// Miscellaneous ///////////////////////////////////////
  117.    void set_num_page(U32 n);
  118.  
  119.    int    close2(void);
  120.    void overflow_buf(int prior);
  121.    void buf_show(void);
  122.  
  123.    void buco_min(int prior)   { buco[prior]--; }
  124.  
  125.    void buco_plus(int prior)
  126.    {
  127.      if(  buco[prior]==buma[prior]) overflow_buf(prior);
  128.      else buco[prior]++;
  129.    }
  130.  
  131.  
  132.  
  133. ////////////////////////////////////////////////////////////////////////////////
  134. ////////////////////////                ////////////////////////////
  135. ////////////////////////     P U B L I C        ////////////////////////////
  136. ////////////////////////                ////////////////////////////
  137. ////////////////////////////////////////////////////////////////////////////////
  138.  
  139.  
  140. public:
  141.  
  142.    virtual
  143.   ~BUFFER(void);
  144.    BUFFER(void);
  145.  
  146.    void echo_IO(int TF) { echo_on=TF; }
  147.  
  148. ////////////////////////// Setup functions /////////////////////////////////////
  149.  
  150.    void index_limit(U16  nr);
  151.    void header_page_size(U16  n);
  152.  
  153.    void buffer_size(U16  n);
  154.    void number_buff(U16  maxi,U16  mini=2);
  155.  
  156.    void number_buff_min(U16  mini);
  157.  
  158. ////////////////// Compatibility ////////////////////////////////////
  159.    void    name(csCHAR *s)       { e_name(s);       }
  160.    csCHAR *name(void)           { return e_name(); }
  161.  
  162.  
  163. /////////////////////////////////////////////////////////////////////
  164. /////////////////////////////////////////////////////////////////////
  165.  
  166.    void    e_name(csCHAR *s)       { b_name=s; TABLE::name(s); }
  167.    csCHAR *e_name(void)        { return (csCHAR *)b_name; }
  168.  
  169.  
  170.  
  171. ////////////////////////// Open & Close ////////////////////////////////////////
  172.  
  173.  
  174.    void read_only(void)     { read_only(TRUE);    }
  175.    void read_only(int ToF)  { bReadOnly=ToF;    }
  176.    int    is_read_only(void)  { return bReadOnly; }
  177.  
  178.  
  179.    void open(FILE *p);
  180.    int    open(void)     { return IsOpen(); }
  181.  
  182.    int    IsOpen(void)     { return is_open;  }
  183.    void IsOpen(int ToF)  { is_open=ToF;     }
  184.  
  185.    int    close(void);
  186. //   void zap(void);
  187.    void empty(void);
  188.  
  189. ////////////////////////// Number of Buffers ///////////////////////////////
  190.    U16    number_buff_max(void)     { return nb;  }
  191.    U16    number_buff_min(void)     { return nbm; }
  192.  
  193.  
  194. ////////////////////////// Writing back to disk ////////////////////////////////
  195. // void flush(void);
  196.    void save(void);
  197.  
  198. ////////////////////////// Locate & Load functions /////////////////////////////
  199.    void *locate_buff(U32 key,int prior,int rwl);
  200.    void *load_buff(U32 key,int prior,int rwl);
  201.  
  202.    void *locate_buff(U32 key)       { return locate_buff(key,1,ST_READ);  }
  203.    void *load_buff(U32 key)       { return load_buff(key,2,ST_READ);     }
  204.    void *locate_buff_d(U32 key)    { return locate_buff(key,3,ST_DIRTY); }
  205.    void *load_buff_d(U32 key)       { return load_buff(key,4,ST_DIRTY);     }
  206.    void *load_buff_ld(U32 key)       { return load_buff(key,31,ST_DIRTY);  }
  207.    void *locate_buff_ld(U32 key)   { return locate_buff(key,31,ST_DIRTY);}
  208.    void *load_buff_l(U32 key)       { return load_buff(key,31,ST_READ);     }
  209.    void *locate_buff_l(U32 key)    { return locate_buff(key,31,ST_READ); }
  210.  
  211.    int    in_ram(U32 key);
  212.    int    in_ram(U32 key,void * &buf);
  213.  
  214. ////////////////////////// Changing Status & Priority //////////////////////////
  215.    void change_stat(void *buf,int prior,int stat);
  216.    void change_stat(void *buf,int prior);
  217.    void change_stat(U32 key,int prior);
  218.    int    priority(void *buf)        { return ((bcp *)b2i(buf))->prior; }
  219.    void max_with_priority(int prior,U16  number) { buma[prior]=number; }
  220.    int    max_with_priority(int prior)         { return buma[prior]; }
  221.  
  222. ////////////////////////// Set Clean/Dirty flag ////////////////////////////////
  223.    int    is_dirty(void *buf)       { return ((bcp *)b2i(buf))->stat;   }
  224.    void dirty(void *buf)       { ((bcp *)b2i(buf))->stat=ST_DIRTY; }
  225.    void clean(void *buf)       { ((bcp *)b2i(buf))->stat=ST_READ;  }
  226.  
  227.    int    is_dirty(U32 key);
  228.    void dirty(U32 key)       { void *buf; if(in_ram(key,buf)) dirty(buf); }
  229.    void clean(U32 key)       { void *buf; if(in_ram(key,buf)) clean(buf); }
  230.  
  231.    int    has_been_updated(void);
  232.  
  233. ///////////////////////// Report writing //////////////////////////////////////
  234.    int    report(csCHAR *name,int sub=1);
  235.    void report(FILE *fipo,int sub);
  236.  
  237.  
  238. ////////////////////////// Background processing ///////////////////////////////
  239.    int    background(void);
  240.  
  241. };
  242. #endif
  243.