home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / nastroje / d23456 / SPLBASE.ZIP / Splbase / splbase.txt < prev   
Text File  |  2001-08-05  |  26KB  |  468 lines

  1.  
  2.  
  3. (**************** SplitBase Data Management Systems ****************
  4.  *                                                                 *
  5.  *                Copyright (c) 2001 Leon O. Romain                *
  6.  *                                                                 *
  7.  *                         leon@kafou.com                          *
  8.  *                                                                 *
  9.  *******************************************************************)
  10.  
  11. HISTORY
  12. I conceived SplitBase about 8 years ago as an alternative to the 
  13. available Database Management Programs and also to alleviate some of 
  14. the burden imposed on programmers by Borland's BDE. During that time, 
  15. I have been involved on projects that either did not require database 
  16. programming or those for which the clients requested specific 
  17. systems. I had also hoped that by now the public domain and/or the 
  18. open-source communities would have been flooded with either good or 
  19. excellent alternatives to both the BDE and commercial databases. 
  20. Although there have been many outstanding implementations in that 
  21. direction, none of them had addressed all the issues I am, to this 
  22. day, still concerned with. Now, being faced with the prospect of many 
  23. data management projects in my immediate future, I decided to finally 
  24. start working on my ideas, and that is how I came up with SplitBase.
  25.  
  26. THE CONCEPTS AND PHILOSOPHY
  27. Simplicity, efficiency and speed are the guiding principles behind 
  28. SplitBase, and the reasons I prefer to program in Delphi.  Delphi 
  29. allows the creation of powerful applications with all their 
  30. functionality confined in a single small executable file. In many 
  31. other popular programming environments, a "Hello World" program 
  32. necessitates a complex multi megabytes sized installation program in 
  33. order to run in a user's computer. Dynamic link libraries are a 
  34. wonderful concept that ran out of control. They often create serious 
  35. conflict or even worse, overwriting them sometimes may cause other 
  36. programs to crash or behave erratically. I wanted to develop a 
  37. structure that was simple enough for average programmers to 
  38. comprehend and modify easily according to their needs. That structure 
  39. should be efficient, fast and also powerful enough to be used in real 
  40. world situations. By efficient I mean it should be able to manage 
  41. substantial amount of data (at least one million records) safely and 
  42. accurately. Fast refers to the ability to execute available 
  43. transactions instantaneously  (one fifth of a second or less). 
  44. Efficiency and speed should not rely on the power of today's 
  45. processors and other sort of equipment, but rather should be achieved 
  46. on minimal machines such as the original IBM PC XT and AT with all 
  47. their relative limitations. I believe that with SplitBase I have 
  48. achieved my goals and I humbly present it to your judgment while 
  49. hopping that it might be useful to other programmers and that they 
  50. may improve upon the quality and functionality of SplitBase according 
  51. to the preceding guidelines.
  52.  
  53. DESIGN
  54. In its current incarnation, SplitBase is a multilevel indexed file 
  55. structure. Simply put, it is composed of abstract containers of index 
  56. data or keys as they are more commonly referred to. These keys belong 
  57. to a structure that attaches them to a simple pointer that relates 
  58. them to the corresponding records or other lower level containers. 
  59. When a container reaches its full capacity, it splits into two half 
  60. filled containers. It is very similar to the B-Tree structure used in 
  61. many Database indexes, but it is simpler to manage and store. When 
  62. full, the top-level container, that never splits, indicates that the 
  63. Database has reached its maximum capacity and cannot accommodate any 
  64. more records.
  65.  
  66. In its present phase, SplitBase consists of two levels of key 
  67. containers: a top level with a single container and a secondary level 
  68. with multiple containers. The top level contains a duplicate of the 
  69. first key of each secondary container. Attached to that key is a 
  70. pointer to the physical position in the database file of the related 
  71. secondary level container. When a secondary container splits or when 
  72. its first datum changed, that information is updated in the top 
  73. container. Secondary-container keys are attached to pointers to the 
  74. location of the actual records within the file. The top-level 
  75. container always resides in random access memory (RAM) when a 
  76. database file is active. That is whenever it is opened or created. 
  77. This reduces the number of disk access to only two to locate and 
  78. retrieve any record. To write new records, disk access is also 
  79. limited to an average of two or a maximum of three if the top-level 
  80. container must be updated.
  81.  
  82. At this point, it is important to note that the maximum number of 
  83. records that can be entered in a two level container system as 
  84. described above, is equal to the square of the full capacity of a 
  85. container. In the worst case scenario, when all secondary-level 
  86. containers are only half full and the top-level container reaches 
  87. full capacity, that number is reduced by half, thereby fixing the 
  88. guaranteed minimum number of records that can be entered in the 
  89. SplitBase system. New data such as containers generated by a split 
  90. and all new records are always written to the end of the database 
  91. file and a pointer to their position within that file is attached to 
  92. the corresponding key. A SplitBase file is therefore a moderately 
  93. complex linked list of containers and records. In its original state 
  94. when the file is created, it only contains an empty top container and 
  95. a header that provides the manager with information about the file 
  96. structure such as length of records and of fields or the number of 
  97. deleted records.
  98.  
  99. For the sake of simplicity, I have decided to save all fields in a 
  100. record as strings that can be easily converted, in most programming 
  101. languages, to other data type such as numbers, currency, date etc. 
  102. Data within the containers are automatically sorted in ascending 
  103. order with a simple insert sort algorithm. This allows for extremely 
  104. fast binary search of data in RAM. For every search and insertion 
  105. request, the manager looks up the top container to determine which 
  106. secondary container holds the position for that specific key. It then 
  107. uses the corresponding pointer to locate and load that container into 
  108. RAM. New data are then inserted into their correct position and found 
  109. data provide the pointers to their actual records for retrieval. The 
  110. whole system is then updated if necessary with very minimal disk 
  111. access.
  112.  
  113. The decisive factor in determining container capacity has been 
  114. available memory in our targeted minimal computer, the XT and AT 
  115. version of the IBM PC. Both machines have been plagued by the 64k 
  116. block limit, which was chosen as the maximum physical size of a 
  117. container. On the other hand since only two containers are present in 
  118. memory during data manipulation this will allocate for more than 
  119. enough room in the limited memory of these machines (640k max.) for 
  120. the records, the database manager, the operating system and other 
  121. programs. In our original model, we propose a maximum key length of 
  122. 26 bytes, which is appropriate for the majority of real life 
  123. situations where keys are usually simple and short such as a social 
  124. security number, a last name, a phone number, a zip code etc. This 
  125. choice provides a capacity of 2000 key/pointer combinations yielding 
  126. to databases of a maximum of 4 million records and a minimum of 2 
  127. million records if data was entered sequentially resulting in all 
  128. half full split indexes by the time the top container reaches its 
  129. full capacity.
  130.  
  131. If you did not understand anything in what was said in this section, 
  132. you should not be programming databases anyway. However the functions 
  133. in SplitBase are few and much simpler than with other databases; 
  134. hence you should be able to use it without too much difficulty.
  135.  
  136. IMPLEMENTATION
  137. SplitBase is built around a small set of Boolean functions. It is 
  138. composed of 29 structurally identical functions that return true when 
  139. the function succeeds and false otherwise. Only 20 of those functions 
  140. are necessary for a programmer to know in order to use SplitBase. 
  141. Many of these routines are not structural but complementary and were 
  142. implemented only for the convenience of the programmer and the 
  143. integrity of the system. All the others are mainly basic but 
  144. optimized file manipulation routines. All functions are written in 
  145. simple, object free turbo Pascal syntax with very few exceptions. 
  146. This simplicity makes it extremely easy to port SplitBase to other 
  147. programming environments and early versions of turbo Pascal. It also 
  148. renders the system compatible with current and future releases of 
  149. Delphi with practically no modifications. It could even be adapted as 
  150. easily to run in the original Pascal language but I would not 
  151. recommend anyone to do so simply because Wirth's Pascal did not allow 
  152. the programmer to access files and memory directly. This would result 
  153. in a very slow system and would definitely not meet our previously 
  154. stated standards of speed and efficiency.
  155.  
  156. In this current version, SplitBase is released in two formats. An 
  157. include file (splinc.pas) to be included in Turbo Pascal or Delphi 
  158. programs, and a component (Tspl) that can be added to practically all 
  159. current releases of Delphi and most likely all future implementations 
  160. unless they contain some very dramatic structural modification in 
  161. their basic syntax or functionality.
  162.  
  163. INSTALLATION
  164. For the include-file version, just add the compiler directive {$I 
  165. Splinc.pas} at the top of your Turbo Pascal programs or at the 
  166. beginning of the implementation section of your Delphi projects. For 
  167. the component version, install the file spl.pas as a new component 
  168. according to the methods available in your version of Delphi. You can 
  169. install it in an existing package or a new one preferably Tspl. At 
  170. any rate, if you follow the right steps you should end up with a 
  171. color logo of SplitBase in the "Samples" palette of your Delphi 
  172. environment. Drag and drop that logo into your forms to use the Spl 
  173. component that will provide you with all the functionality of 
  174. SplitBase. The file Splinc.pas is located in the "\Splbase\Include" 
  175. sub directory of the original release of SplitBase. The component 
  176. Spl.pas is located in the "\Splbase\Class" sub directory of the same 
  177. release of SplitBase.
  178.  
  179. PROGRAMMING
  180. SplitBase is very simple to program. The following is a brief 
  181. description of the necessary functions as well as basic principles 
  182. that should be observed to safely and easily implement these 
  183. routines. It is the programmer's responsibility to allocate memory 
  184. space for the system by using InitSpl and ReleaseSpl at the beginning 
  185. and end of your program. You must also initialize system variables by 
  186. using InitBase before creating or opening any database file. You have 
  187. to ensure that your function calls are successful before proceeding 
  188. with any database session. This can simply be achieved by checking 
  189. the return value of all your calls. By following the preceding 
  190. principles you should enjoy a trouble free experience while 
  191. programming and using SplitBase. Here are the main functions:
  192.  
  193. InitSpl: Initializes the Splitbase system by allocating memory to 
  194. accommodate the top and secondary system containers. Initspl must be 
  195. called at the beginning of your programs. If it fails, all other 
  196. calls to the system must be cancelled.
  197.  
  198. ReleaseSpl: Releases allocated memory back to the operating system. 
  199. This function must be called at the end of your programs. Failure to 
  200. do so will prevent other programs to use that portion of memory 
  201. reserved for SplitBase.
  202.  
  203. InitBase: Initializes important variables used by Splitbase. Initbase 
  204. must be called before creating or opening SplitBase files. Failure to 
  205. do so may generate unpredictable errors or otherwise compromise the 
  206. stability of the system.
  207.  
  208. SetSpl: This is a very important function that you must use to tell 
  209. SplitBase the size of each field in a new SplitBase file. It must be 
  210. called before creating a new database and uses a string as parameter. 
  211. This string contains the size of all fields that define the record 
  212. structure. Each field size is described with three characters 
  213. starting with the first field in the record. Fields which sizes are 
  214. less than 100 should be filled with zeroes on the left of its 
  215. allocated portion. For example, a field of length 3 followed by a 
  216. field of length 12 should be written as follows: '003012'.
  217.  
  218. CreateSpl: This function receives a string parameter to create a new 
  219. SplitBase file. The parameter is the name of the file to be created 
  220. without any extension. The system will add the proper extension to 
  221. it, currently (.spd), and create the file if possible. The functions 
  222. InitBase and SetSpl must be called to set the necessary variables 
  223. prior to calling CreateSpl.
  224.  
  225. OpenSpl: This function requires a string parameter to open a 
  226. SplitBase file. The parameter is the name of the file to be opened 
  227. without any extension. The system will add the proper extension to 
  228. the name, currently (.spd), and open the file if possible. A call to 
  229. the InitBase function must be made to set all the necessary variables 
  230. prior to calling OpenSpl.
  231.  
  232. AddField: This function uses two parameters, a string and an integer, 
  233. to insert a field into the record structure of the SplitBase system. 
  234. The string contains the value of the field and the integer its index. 
  235. All fields must be inserted before saving a record into the file.
  236.  
  237. GetField: This function uses an integer parameter to retrieve a field 
  238. from the record structure of SplitBase. The parameter contains the 
  239. index of the field. A record must be retrieved from the data file 
  240. before using this function. Getfield must be called once for each 
  241. field in the current record. If the function fails, SplitBase returns 
  242. an empty string.
  243.  
  244. AddRec: This function saves a record into the SplitBase file. It 
  245. receives the key attached to that record as a string parameter. All 
  246. fields must have been inserted into the record structure prior to 
  247. calling AddRec.
  248.  
  249. GetRec: This function retrieves a record from the data file. It 
  250. receives the key attached to that record as a string parameter. If 
  251. the record is found, all fields are loaded into the record structure 
  252. of SplitBase.
  253.  
  254. DelRec: This function deletes the current record from the data file. 
  255. A record must be active in memory in order to use DelRec. DelRec has 
  256. no parameters.
  257.  
  258. ModRec: This function updates the current record in the data file. It 
  259. actually deletes that record from the file then adds the modified 
  260. record immediately. It uses a string parameter as key.
  261.  
  262. FirstRec: Locates and retrieves the first indexed record.
  263.  
  264. LastRec: Locates and retrieves the last indexed record.
  265.  
  266. NextRec: Locates and retrieves the next indexed record.
  267.  
  268. PrevRec: Locates and retrieves the previous indexed record.
  269.  
  270. RecCount: Returns the total number of records in the data file.
  271.  
  272. ActiveDb: Returns true if a data file is active or opened.
  273.  
  274. ActiveRec: Returns true if a record is active or loaded.
  275.  
  276. DBEmpty: Returns true if the current data file is empty.
  277.  
  278. Many variables are important in the use of SplitBase. Most of them 
  279. should never be manipulated directly by the programmer. However, 
  280. there are a few such as Splerr that the programmer should check 
  281. regularly. Splerr returns the last error number and description. 
  282. These values are returned with the following calls: splerr.recnum and 
  283. splerr.recstr. In the component version the properties ErrorNumber 
  284. and ErrorString must be accessed in lieu of splerr. The variable 
  285. CurDtb or its equivalent CurrentDB contains the name of the current 
  286. data file. The allrec variable contains a lot of valuable information 
  287. such as the number of fields in the current SplitBase record. You may 
  288. read it with allrec.size or with FieldCount from the SplitBase 
  289. component. Finally limrec is a number you can set to limit the number 
  290. of record that may be entered into the data file. For the component, 
  291. use Reclimit instead.
  292.  
  293. RELATED PROGRAMS AND TESTS
  294. The original release of SplitBase comes with two test programs Split 
  295. and Split2. These two programs are identical in looks and 
  296. functionality except that the former uses the include file and the 
  297. other the component. These programs allow a user to test the major 
  298. functions of SplitBase. They contain buttons to create new data file 
  299. and open existing ones. Other buttons also allocate for adding new 
  300. records as well as finding and deleting existing data within a 
  301. splitbase file. They also provide navigation buttons to locate the 
  302. first, last, next and previous records. Finally a generate button 
  303. will automatically insert 1 million records into a specific data 
  304. file. That file is made of a two fields record and is called 
  305. test.spd. It is automatically created and opened if you click the 
  306. related buttons in the program. Generate will automatically add 1 
  307. million even numbers from 2 to 2 million to that file. For fun and to 
  308. simulate data entry overhead, generate will convert all those numbers 
  309. into their English spelling and put the actual number in the first 
  310. field and its spelling in the second before saving the record in the 
  311. data file. These two fields are also output in two edit fields for 
  312. added overhead. An algorithm is used that forces SplitBase to update 
  313. the top container every four records that in real life applications 
  314. should only happen on average every thousand records. Test on a 667 
  315. megahertz custom built computer equipped with a 5400 RPM Ultra ATA/66 
  316. hard drive yield a result of 139 records per second at completion of 
  317. generate. The function can be stopped at any time by clicking on the 
  318. 'Stop' button.
  319.  
  320. FUTURE EXPLORATIONS
  321. Where do we go from here? Well from the basic functionality of the 
  322. system many routes may be considered. One of the more obvious may be 
  323. to increase the number of records that the system can handle. The 
  324. easiest way to do this might be to hold the keys in the top and 
  325. secondary containers into an ANSI string just like 'rechld' that 
  326. holds the fields in any given record. The two-gigabyte theoretical 
  327. limit claimed by Borland is well beyond anything we might need in any 
  328. real world situations.
  329.  
  330. The LongInt and Int64 Problem
  331. The only other hurdle is the 2-gigabyte limit of the longint type 
  332. that is used to determine file sizes and to locate records and 
  333. containers within the data file. This problem may be addressed by 
  334. using a blocking factor greater than 1 for creating and opening data 
  335. files. Two hundred fifty-six (256) and one thousand twenty four 
  336. (1024) may be good values to experiment with. The only problem might 
  337. be in synchronizing container and record size with the chosen 
  338. blocking factor. It would be easy to add fillers like in the good old 
  339. days when Cobol was king. This will certainly add to the waste in a 
  340. data file. But who cares in times when 80-gigabytes hard drives cost 
  341. less than 200 dollars in most major computer stores. An easier way 
  342. would be to use the new int64 type that appeared with version 4 of 
  343. Delphi. But Borland is mute about using it with the reset, rewrite 
  344. and seek procedures. Int64 can hold up an integer the size of 2 to 
  345. the power 63.
  346.  
  347. What about garbage collection? Much like in other databases, deleted 
  348. records and containers are not actually erased from the file. They 
  349. just sit there useless with no actual pointers relating to them. 
  350. Heavy usage of delete transactions tends to increase considerably the 
  351. amount of garbage within the data file. A garbage collection or 
  352. rebuild routine can easily be implemented by using a new data file 
  353. and add all the live records from the previous file to it.
  354.  
  355. Multiple indexes data files may be more easily implemented by saving 
  356. the indexes and the records in different files. It will actually 
  357. allocate for more records if longint is used. This is also the case 
  358. if one would like to emulate the functionality of relational 
  359. databases. The straightforward structure of the data file without the 
  360. containers will make it easy to emulate most general functions of a 
  361. relational database system. In the case of a multi-user environment 
  362. and client/server implementation, a separate manager should be 
  363. written to handle calls from the different users. The most important 
  364. issue in multi-user system is probably the ability to lock records. 
  365. The manager can easily implement this by adding an extra field to 
  366. records in data files that holds the status of that record. A simple 
  367. protocol should also be implemented between the manager and the 
  368. client programs to properly handle transactions.
  369.  
  370. These were a few suggestions for programmers to modify the SplitBase 
  371. system to easily achieve their goals. However, we believe that 
  372. SplitBase is very useful as is and can be adapted to solve a great 
  373. deal of real life problems. Particularly in the retail industry 
  374. Splitbase may be easily and effectively used without modification to 
  375. help manage most problems such as stock, accounting, customers etc... 
  376. How many times did the clerks used complex data analysis to locate 
  377. your record or a specific item in the store? They usually type a 
  378. single key string in order to locate the needed information. Most 
  379. stores even the major chains hold less than 2 million items (I know I 
  380. usually go around and count them :-). Many cities in America and 
  381. around the world have less than two millions citizens, most libraries 
  382. hold less than two million books and the list can go on and on about 
  383. real life situation in which SplitBase may be used without 
  384. modifications.
  385.  
  386. If you come up with important additions to SplitBase you are free to 
  387. publish them along with the original distribution of SplitBase with 
  388. all files present and unchanged. I would recommend that you follow 
  389. the guidelines stated at the beginning of this file. However, if you 
  390. have a REALLY important addition that uses some arcane procedures or 
  391. an extremely complex routine, well go ahead, publish it with the 
  392. restrictions stated above as far as keeping original files intact.
  393.  
  394. COPYRIGHT AND LICENSES
  395. SplitBase is not based on any previous work other than basic computer 
  396. Science and Data Structure principles. It is not a public domain 
  397. material but a copyrighted publication of Leon O. Romain. However, it 
  398. is freeware and is being released under the GNU license agreement. 
  399. See the file licence.txt for more information. You may use the 
  400. SplitBase system as you see fit in your personal, professional or 
  401. commercial programs without paying any loyalty to the author 
  402. providing you relieve him of any and all liabilities that may result 
  403. from the use of SplitBase. In fact SplitBase is released as is. The 
  404. author decline all responsibility and liability from damages either 
  405. direct or incidental that may result from the use of SplitBase and 
  406. its accompanying routines and other software.
  407.  
  408. KNOWN BUGS
  409. There are no known bugs. If any are found, they should be easily 
  410. corrected due to the simplicity of SplitBase and its modular 
  411. structure.
  412.  
  413. ADDENDUM
  414. I checked the int64 type and it worked perfectly with the reset and 
  415. seek functions on files much bigger than 4 gigabytes. This yields the 
  416. possibility of creating SplitBase systems capable of accessing 
  417. billions of records with very little modification to the original 
  418. program when using Delphi version 4 or later. By changing the type of 
  419. the necessary variables from longint to int64 and by modifying the 
  420. index field of the splitbox record to work as an ansistring instead 
  421. of an array is all that it takes for SplitBase to access those 
  422. billions of records. I also added an activex version of SplitBase to 
  423. the original release. It works fine except for the fact that the icon 
  424. does not appear on the form when selected. The necessary files are 
  425. located in the activex subdirectory and the file SplBaseXControl1.ocx 
  426. must be registered before using the control. This opens the door for 
  427. visual basic, c++ and other programming environments to use SplitBase 
  428. before the advent of native implementations of the system. OCX 
  429. controls may be registered using the Regsvr32.exe utility usually 
  430. located in the windows or system directories.
  431.  
  432. COMMUNICATION
  433. For any comments, suggestions, corrections, criticism, bug reports 
  434. and other communications please Email me at leon@kafou.com.
  435.  
  436. SplitBase and the SplitBase logo are trademarks of Leon O. Romain.
  437.  
  438. The SplitBase Data Management Systems and this user guide are 
  439. copyrighted materials of Leon O. Romain.
  440.  
  441. Copyright (c) 2001 Leon O. Romain.
  442.  
  443.  
  444.  
  445.  
  446.                           / \
  447.  
  448.                         /     \
  449.                            S
  450.                       | \     / |
  451.                                  
  452.                       |   \ /   |
  453.                         P     L
  454.                      / \   |   / \
  455.  
  456.                    /     \ | /     \
  457.                       I         T
  458.                  | \     / | \     / |
  459.  
  460.                  |   \ /   |   \ /   |
  461.                    B     A   S     E
  462.                   \   |   / \   |   /
  463.  
  464.                     \ | /     \ | /
  465.  
  466.                 DATA MAMAGEMENT SYSTEMS
  467.                 -----------------------
  468.