00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef _terimber_memdb_h_
00029 #define _terimber_memdb_h_
00030
00031 #include "db/db.h"
00032 #include "memdb/memdbacc.h"
00033
00034 #include "base/vector.h"
00035 #include "base/list.h"
00036 #include "base/map.h"
00037 #include "base/common.h"
00038
00039
00040 BEGIN_TERIMBER_NAMESPACE
00041 #pragma pack(4)
00042
00045 class terimber_index_column_info_ex : public terimber_index_column_info
00046 {
00047 public:
00048 dbtypes _type;
00049 };
00050
00053 class terimber_db_value_vector_impl : public terimber_db_value_vector
00054 {
00055 public:
00057 terimber_db_value_vector_impl(size_t count
00058 );
00059
00061 virtual
00062 ~terimber_db_value_vector_impl();
00063
00065 virtual
00066 size_t
00067 get_size() const;
00068
00071 virtual
00072 bool
00073 set_value_as_null(size_t index,
00074 dbtypes type
00075 );
00077 virtual
00078 bool
00079 set_value_as_bool(size_t index,
00080 bool val
00081 );
00083 virtual
00084 bool
00085 set_value_as_char(size_t index,
00086 sb1_t val
00087 );
00089 virtual
00090 bool
00091 set_value_as_byte(size_t index,
00092 ub1_t val
00093 );
00095 virtual
00096 bool
00097 set_value_as_short(size_t index,
00098 sb2_t val
00099 );
00101 virtual
00102 bool
00103 set_value_as_word(size_t index,
00104 ub2_t val
00105 );
00107 virtual
00108 bool
00109 set_value_as_long(size_t index,
00110 sb4_t val
00111 );
00113 virtual
00114 bool
00115 set_value_as_dword(size_t index,
00116 ub4_t val
00117 );
00119 virtual
00120 bool
00121 set_value_as_float(size_t index,
00122 float val
00123 );
00125 virtual
00126 bool
00127 set_value_as_double(size_t index,
00128 double val
00129 );
00131 virtual
00132 bool
00133 set_value_as_long64(size_t index,
00134 sb8_t val
00135 );
00137 virtual
00138 bool
00139 set_value_as_dword64(size_t index,
00140 ub8_t val
00141 );
00143 virtual
00144 bool
00145 set_value_as_guid(size_t index,
00146 const guid_t& val
00147 );
00148
00149
00150
00151
00152
00153
00155 virtual
00156 bool
00157 set_value_as_numeric(size_t index,
00158 const char* val,
00159 char delimeter
00160 );
00162 virtual
00163 bool
00164 set_value_as_decimal(size_t index,
00165 const char* val,
00166 char delimeter
00167 );
00168
00169
00170
00171
00172
00173
00174
00175
00176
00178 virtual
00179 bool
00180 set_value_as_date(size_t index,
00181 ub4_t year,
00182 ub1_t month,
00183 ub1_t day,
00184 ub1_t hour,
00185 ub1_t minute,
00186 ub1_t second,
00187 ub2_t millisec
00188 );
00189
00192 virtual
00193 bool
00194 set_value_as_string(size_t index,
00195 const char* val,
00196 size_t len
00197 );
00198
00204 virtual
00205 bool
00206 set_value_as_string_ptr(size_t index,
00207 const char* val
00208 );
00209
00212 virtual
00213 bool
00214 set_value_as_wstring(size_t index,
00215 const wchar_t* val,
00216 size_t len
00217 );
00222 virtual
00223 bool
00224 set_value_as_wstring_ptr(size_t index,
00225 const wchar_t* val
00226 );
00229 virtual
00230 bool
00231 set_value_as_binary(size_t index,
00232 const ub1_t* val,
00233 size_t len
00234 );
00243 virtual
00244 bool
00245 set_value_as_binary_ptr(size_t index,
00246 const ub1_t* val
00247 );
00248
00249
00252 virtual
00253 bool
00254 get_value_is_null(size_t index
00255 ) const;
00256
00258 virtual
00259 bool
00260 get_value_as_bool(size_t index
00261 ) const;
00263 virtual
00264 sb1_t
00265 get_value_as_char(size_t index
00266 ) const;
00268 virtual
00269 ub1_t
00270 get_value_as_byte(size_t index
00271 ) const;
00273 virtual
00274 sb2_t
00275 get_value_as_short(size_t index
00276 ) const;
00278 virtual
00279 ub2_t
00280 get_value_as_word(size_t index
00281 ) const;
00283 virtual
00284 sb4_t
00285 get_value_as_long(size_t index
00286 ) const;
00288 virtual
00289 ub4_t
00290 get_value_as_dword(size_t index
00291 ) const;
00293 virtual
00294 float
00295 get_value_as_float(size_t index
00296 ) const;
00298 virtual
00299 double
00300 get_value_as_double(size_t index
00301 ) const;
00303 virtual
00304 sb8_t
00305 get_value_as_long64(size_t index
00306 ) const;
00308 virtual
00309 ub8_t
00310 get_value_as_dword64(size_t index
00311 ) const;
00314 virtual
00315 bool
00316 get_value_as_guid(size_t index,
00317 guid_t& val
00318 ) const;
00319
00321 virtual
00322 const char*
00323 get_value_as_numeric(size_t index,
00324 char delimeter
00325 ) const;
00326
00328 virtual
00329 const char*
00330 get_value_as_decimal(size_t index,
00331 char delimeter
00332 ) const;
00333
00335 virtual
00336 bool
00337 get_value_as_date(size_t index,
00338 ub4_t& year,
00339 ub1_t& month,
00340 ub1_t& day,
00341 ub1_t& hour,
00342 ub1_t& minute,
00343 ub1_t& second,
00344 ub2_t& millisec,
00345 ub1_t& wday,
00346 ub2_t& yday
00347 ) const;
00348
00351 virtual
00352 const char*
00353 get_value_as_string(size_t index
00354 ) const;
00357 virtual
00358 const wchar_t*
00359 get_value_as_wstring(size_t index
00360 ) const;
00361
00365 virtual
00366 const ub1_t*
00367 get_value_as_binary(size_t index,
00368 size_t& len
00369 ) const;
00375 virtual
00376 const ub1_t*
00377 get_value_as_binary_ptr(size_t index
00378 ) const;
00379
00380 private:
00382 terimber_xml_value
00383 get_value_as_value(size_t index,
00384 vt_types type
00385 ) const;
00386
00387 public:
00388 mutable byte_allocator _tmp_allocator;
00389 vector< binder > _value;
00390 };
00391
00394 class memdb_row
00395 {
00396 public:
00397 _vector< terimber_db_value > _row;
00398 terimber_db_row_status _status;
00399 };
00400
00403 typedef list< memdb_row > memdb_rowset_t;
00406 typedef memdb_rowset_t::iterator memdb_rowset_iterator_t;
00409 typedef memdb_rowset_t::const_iterator memdb_rowset_citerator_t;
00412 typedef vector< terimber_index_column_info_ex > terimber_index_column_array_t;
00413
00414
00416 static
00417 inline
00418 int
00419 compare_db_value( dbtypes type,
00420 const terimber_db_value& first,
00421 const terimber_db_value second,
00422 bool case_insensitive
00423 );
00424
00427 class memdb_rowset_less
00428 {
00429 public:
00431 memdb_rowset_less(const terimber_index_column_array_t& info
00432 ) :
00433 _info(info)
00434 {
00435 }
00437 inline
00438 bool
00439 operator()(const memdb_rowset_citerator_t& first, const memdb_rowset_citerator_t& second) const;
00441 inline
00442 const terimber_index_column_array_t&
00443 get_info() const
00444 {
00445 return _info;
00446 }
00447 private:
00448 terimber_index_column_array_t _info;
00449 };
00450
00453 typedef map< memdb_rowset_citerator_t, memdb_rowset_citerator_t, memdb_rowset_less, true > memdb_index_t;
00456 typedef memdb_index_t::const_iterator memdb_index_citer_t;
00457
00458
00459 class memindex;
00460 class memlookup;
00461
00464 class memtable : public terimber_memtable
00465 {
00468 typedef list< memindex* > list_indexes_t;
00471 typedef list< terimber_db_value_vector_impl* > list_values_t;
00472 public:
00474 memtable();
00477 virtual
00478 ~memtable();
00479
00481 virtual
00482 const char*
00483 get_last_error();
00484
00497 virtual
00498 bool
00499 populate( dbserver* server,
00500 size_t start_row,
00501 size_t max_rows
00502 );
00503
00506 virtual
00507 bool
00508 create( size_t columns,
00509 const terimber_table_column_desc desc[]
00510 );
00511
00512
00515 virtual
00516 void
00517 refresh();
00519 virtual
00520 size_t
00521 get_row_count();
00522
00524
00526 virtual
00527 size_t
00528 get_column_count() const;
00530 virtual
00531 dbtypes
00532 get_column_type(size_t index
00533 ) const;
00535 virtual
00536 const char*
00537 get_column_name(size_t index
00538 ) const;
00540 virtual
00541 bool
00542 get_column_nullable(size_t index
00543 ) const;
00545 virtual
00546 size_t
00547 get_column_scale(size_t index
00548 ) const;
00550 virtual
00551 size_t
00552 get_column_precision(size_t index
00553 ) const;
00555 virtual
00556 size_t
00557 get_column_max_length(size_t index
00558 ) const;
00559
00562
00570 virtual
00571 terimber_memindex*
00572 add_index( size_t columns,
00573 const terimber_index_column_info info[]
00574 );
00575
00579 virtual
00580 bool
00581 remove_index( terimber_memindex* obj
00582 );
00583
00584
00586 virtual
00587 terimber_db_value_vector*
00588 allocate_db_values(size_t count
00589 );
00590
00592 virtual
00593 void
00594 destroy_db_values(terimber_db_value_vector* obj
00595 );
00596
00597 public:
00599 inline
00600 const memdb_rowset_t&
00601 get_rowset() const
00602 {
00603 return _rowset;
00604 }
00606 inline
00607 const binders_t&
00608 get_cols() const
00609 {
00610 return _cols;
00611 }
00616 bool
00617 insert_row( const terimber_db_value_vector* info
00618 );
00620 bool
00621 update_row( memdb_rowset_citerator_t iter,
00622 const terimber_db_value_vector* info
00623 );
00627 bool
00628 delete_row( memdb_rowset_citerator_t iter
00629 );
00630
00631 private:
00633 void
00634 uninit();
00635 private:
00636 byte_allocator _allocator;
00637 binders_t _cols;
00638 memdb_rowset_t _rowset;
00639 string_t _error;
00640 mutex _mtx;
00641 list_indexes_t _indexes;
00642 list_values_t _values;
00643 };
00644
00647 class memindex : public terimber_memindex,
00648 public terimber_log_helper
00649 {
00651 friend class memtable;
00654 typedef list< memlookup* > list_lookups_t;
00655 protected:
00657 memindex( memtable& parent,
00658 const memdb_rowset_less& pred
00659 );
00662 virtual
00663 ~memindex();
00664 public:
00665
00676 virtual
00677 terimber_memlookup*
00678 add_lookup( const terimber_db_value_vector* info
00679 );
00680
00682 virtual
00683 bool
00684 remove_lookup( terimber_memlookup* obj
00685 );
00686
00687
00688 public:
00690 inline
00691 const memtable&
00692 get_table() const
00693 {
00694 return _parent;
00695 }
00697 inline
00698 memtable&
00699 get_table()
00700 {
00701 return _parent;
00702 }
00704 inline
00705 const memdb_index_t&
00706 get_index()
00707 {
00708 return _index;
00709 }
00710
00711 private:
00713 bool
00714 construct();
00716 void
00717 notify( memdb_rowset_citerator_t iter,
00718 bool insert_or_delete
00719 );
00720
00721 private:
00722 memtable& _parent;
00723 memdb_index_t _index;
00724 mutex _mtx;
00725 list_lookups_t _lookups;
00726 };
00727
00730 class memlookup : public terimber_memlookup,
00731 public terimber_log_helper
00732 {
00734 friend class memindex;
00735 protected:
00737 memlookup( memindex& parent
00738 );
00740 virtual
00741 ~memlookup();
00743 bool
00744 construct( const terimber_db_value_vector_impl* info
00745 );
00746 public:
00749 virtual
00750 bool
00751 assign( const terimber_memlookup& x
00752 );
00753
00755
00758 virtual
00759 bool
00760 reset( const terimber_db_value_vector* info
00761 );
00762
00765 virtual
00766 bool
00767 next() const;
00768
00771 virtual
00772 bool
00773 prev() const;
00774
00783
00785 virtual
00786 bool
00787 get_value_is_null(size_t index
00788 ) const;
00790 virtual
00791 bool
00792 get_value_as_bool(size_t index
00793 ) const;
00795 virtual
00796 sb1_t
00797 get_value_as_char(size_t index
00798 ) const;
00800 virtual
00801 ub1_t
00802 get_value_as_byte(size_t index
00803 ) const;
00805 virtual
00806 sb2_t
00807 get_value_as_short(size_t index
00808 ) const;
00810 virtual
00811 ub2_t
00812 get_value_as_word(size_t index
00813 ) const;
00815 virtual
00816 sb4_t
00817 get_value_as_long(size_t index
00818 ) const;
00820 virtual
00821 ub4_t
00822 get_value_as_dword(size_t index
00823 ) const;
00825 virtual
00826 float
00827 get_value_as_float(size_t index
00828 ) const;
00830 virtual
00831 double
00832 get_value_as_double(size_t index
00833 ) const;
00835 virtual
00836 sb8_t
00837 get_value_as_long64(size_t index
00838 ) const;
00840 virtual
00841 ub8_t
00842 get_value_as_dword64(size_t index
00843 ) const;
00844
00847 virtual
00848 bool
00849 get_value_as_guid(size_t index,
00850 guid_t& val
00851 ) const;
00854 virtual
00855 const char*
00856 get_value_as_numeric(size_t index,
00857 char delimeter
00858 ) const;
00861 virtual
00862 const char*
00863 get_value_as_decimal(size_t index,
00864 char delimeter
00865 ) const;
00868 virtual
00869 bool
00870 get_value_as_date(size_t index,
00871 ub4_t& year,
00872 ub1_t& month,
00873 ub1_t& day,
00874 ub1_t& hour,
00875 ub1_t& minute,
00876 ub1_t& second,
00877 ub2_t& millisec,
00878 ub1_t& wday,
00879 ub2_t& yday
00880 ) const;
00883 virtual
00884 const char*
00885 get_value_as_string(size_t index
00886 ) const;
00889 virtual
00890 const wchar_t*
00891 get_value_as_wstring(size_t index
00892 ) const;
00893
00897 virtual
00898 const ub1_t*
00899 get_value_as_binary(size_t index,
00900 size_t& len
00901 ) const;
00907 virtual
00908 const ub1_t*
00909 get_value_as_binary_ptr(size_t index
00910 ) const;
00911
00912
00919
00924 virtual
00925 bool
00926 insert_row( const terimber_db_value_vector* info
00927 );
00928
00929
00933 virtual
00934 bool
00935 delete_row();
00936
00940 virtual
00941 bool
00942 update_row( const terimber_db_value_vector* info
00943 );
00944
00950
00955 virtual
00956 terimber_db_row_status
00957 get_row_status();
00958
00959 private:
00961 void
00962 notify( memdb_index_citer_t iter,
00963 bool insert_or_delete
00964 );
00966 terimber_xml_value
00967 get_value_as_value(size_t index,
00968 vt_types type
00969 ) const;
00970
00971 private:
00972 mutable byte_allocator _tmp_allocator;
00973 memindex& _parent;
00974 memdb_index_citer_t _low_bounder;
00975 memdb_index_citer_t _upper_bounder;
00976 mutable memdb_index_citer_t _current_iter;
00977 byte_allocator _condition_allocator;
00978 memdb_rowset_t _condition_rowset;
00979 };
00980
00981
00982 #pragma pack()
00983 END_TERIMBER_NAMESPACE
00984
00985 #endif // _terimber_memdb_h_
00986