backends/quartz/btree_util.h

Go to the documentation of this file.
00001 /* btree_util.h: common macros/functions in the Btree implementation.
00002  *
00003  * Copyright 1999,2000,2001 BrightStation PLC
00004  * Copyright 2002,2004,2007,2008 Olly Betts
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License as
00008  * published by the Free Software Foundation; either version 2 of the
00009  * License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
00019  * USA
00020  */
00021 
00022 #ifndef OM_HGUARD_BTREE_UTIL_H
00023 #define OM_HGUARD_BTREE_UTIL_H
00024 
00025 #include "quartz_types.h"
00026 #include "omassert.h"
00027 
00028 #include <string.h>  /* memset */
00029 #include "safeunistd.h" /* for close() */
00030 
00031 #include <string>
00032 
00033 /* The unit of access into the DB files is an unsigned char, which is defined
00034    as 'byte' with a typedef.
00035 
00036    Other integer values are built up from these bytes, either in pairs or fours.
00037    'int2' and 'int4' are signed types which will comfortably accommodate
00038    all 2 byte and 4 byte ranges:
00039 */
00040 
00041 /*
00042    If a signed int cannot hold the full range of two bytes replace 'int' by
00043    'long' throughout the code.
00044 
00045    FIXME: surely if a signed int cannot hold the full range of two bytes,
00046    then the compiler violates ANSI?  Or am I misunderstanding...
00047 */
00048 
00049 // FIXME: 65536 in Asserts below should really be block_size
00050 inline int
00051 GETINT1(const byte *p, int c)
00052 {
00053     Assert(c >= 0);
00054     Assert(c < 65536);
00055     return p[c];
00056 }
00057 
00058 inline void
00059 SETINT1(byte *p, int c, int x)
00060 {
00061     Assert(c >= 0);
00062     Assert(c < 65536);
00063     p[c] = x;
00064 }
00065 
00066 inline int
00067 GETINT2(const byte *p, int c)
00068 {
00069     Assert(c >= 0);
00070     Assert(c < 65536 - 1);
00071     return p[c] << 8 | p[c + 1];
00072 }
00073 
00074 inline void
00075 SETINT2(byte *p, int c, int x)
00076 {
00077     Assert(c >= 0);
00078     Assert(c < 65536 - 1);
00079     p[c] = x >> 8;
00080     p[c + 1] = x;
00081 }
00082 
00083 inline int
00084 get_int4(const byte *p, int c)
00085 {
00086     Assert(c >= 0);
00087     Assert(c < 65536 - 3);
00088     return p[c] << 24 | p[c + 1] << 16 | p[c + 2] << 8 | p[c + 3];
00089 }
00090 
00091 inline void
00092 set_int4(byte *p, int c, int x)
00093 {
00094     Assert(c >= 0);
00095     Assert(c < 65536 - 3);
00096     p[c] = x >> 24;
00097     p[c + 1] = x >> 16;
00098     p[c + 2] = x >> 8;
00099     p[c + 3] = x;
00100 }
00101 
00102 int sys_open_to_read(const std::string & name);
00103 int sys_open_to_read_no_except(const std::string & name);
00104 int sys_open_to_write(const std::string & name);
00105 void sys_unlink_if_exists(const std::string &filename);
00106 // Return true on success
00107 inline bool sys_close(int h) {
00108     return close(h) == 0;
00109 }
00110 
00111 std::string sys_read_all_bytes(int h, size_t max);
00112 void sys_write_string(int h, const std::string &s);
00113 int sys_flush(int h);
00114 
00115 inline byte *zeroed_new(size_t size)
00116 {
00117     byte *temp = new byte[size];
00118     if (temp) memset(temp, 0, size);
00119 
00120     return temp;
00121 }
00122 
00126 class fdcloser {
00127     public:
00128         fdcloser(int fd_) : fd(fd_) {}
00129         ~fdcloser() {
00130             if (fd >= 0) {
00131                 sys_close(fd);
00132             }
00133         }
00134     private:
00135         int fd;
00136 };
00137 
00138 #endif /* OM_HGUARD_BTREE_UTIL_H */

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.