{"id":302,"date":"2004-08-26T16:26:16","date_gmt":"2004-08-26T08:26:16","guid":{"rendered":"http:\/\/foxpro.ntsl119.com\/scr\/?p=302"},"modified":"2007-11-02T13:51:32","modified_gmt":"2007-11-02T05:51:32","slug":"codedump-20040825153618-brute-lee","status":"publish","type":"post","link":"https:\/\/foxpro.ntsl119.com\/scr\/archives\/302","title":{"rendered":"CodeDump-20040825.153618 (Brute Lee)"},"content":{"rendered":"<p>Code Dump: the time where you take a break from your programming related tasks to talk about your programming related tasks.<\/p>\n<p>In<strong> <font face=\"Tahoma, Verdana, sans-serif\">PROJECT::SOPHIEAI<\/font><\/strong> (IUMS: Internet Usage Monitoring System), USAGE.dbf holds the records that stores the log-in\/log-off information of the users of a certain internet lab\/cafe at a given time and space. Along with the IDNO number of the user of course and some other fields.<\/p>\n<p>Violating the second rule of normalization (or was it the third rule), I also included the TotalNumberofHoursUsed per usage and the TotalFee paid. <\/p>\n<p>If you follow strictly the normalization rules, you know that you don&#8217;t have to include in the database fields that can be derived from two or more fields that are already in the database.<\/p>\n<p>For example if your database has fields named Field<font face=\"Tahoma, Verdana, sans-serif\">01<\/font> and Field<font face=\"Tahoma, Verdana, sans-serif\">02<\/font> with sample values <font face=\"Tahoma, Verdana, sans-serif\">10<\/font> and <font face=\"Tahoma, Verdana, sans-serif\">618<\/font> respectively, you don&#8217;t need a Field<font face=\"Tahoma, Verdana, sans-serif\">03<\/font> which will store <font face=\"Tahoma, Verdana, sans-serif\">628, <\/font>the total of Field<font face=\"Tahoma, Verdana, sans-serif\">01<\/font> + Field<font face=\"Tahoma, Verdana, sans-serif\">02<\/font>. Normalization rule will then call you moronic and consider you an outcast of the database programming world.<\/p>\n<p>You deserve to be eaten alive by vultures.<\/p>\n<p>You deserve a punishment such as watching Barney re-runs again and again. <\/p>\n<p>But in some cases, yes&#8230; believe me&#8230; there are times when you have to violate and override this rule. These are the cases when speed-related principles are far more important. Plus&#8230; considering that these days hard disk space is getting cheaper and time (since Mida&#8217;s era) is constantly gold, it justifies the normalization override process.<\/p>\n<p>So back to <strong><font face=\"Tahoma, Verdana, sans-serif\">PROJECT::SOPHIEAI<\/font><\/strong>. <\/p>\n<p>Now because of some &#8216;outside factors&#8217; (again), I need a patch that would make a different database, CRED_NET.dbf, which holds the fields TimeLeft and Credit be synchronized with the data USAGE.dbf holds. <\/p>\n<p>Let&#8217;s say USAGE.dbf records for student <font face=\"Tahoma, Verdana, sans-serif\">0001<\/font> are (let&#8217;s just take the important fields for discussion):<\/p>\n<table width=\"432\" border=\"0\" align=\"center\" cellpadding=\"1\" cellspacing=\"1\">\n<tr bgcolor=\"#CCCCCC\">\n<td width=\"108\"><font face=\"Tahoma, Verdana, sans-serif\">DATE<\/font><\/td>\n<td width=\"108\"><font face=\"Tahoma, Verdana, sans-serif\">IDNO<\/font><\/td>\n<td width=\"108\"><font face=\"Tahoma, Verdana, sans-serif\">TotalTime<\/font><\/td>\n<td width=\"108\"><font face=\"Tahoma, Verdana, sans-serif\">Fee<\/font><\/td>\n<\/tr>\n<tr>\n<td width=\"108\"><font face=\"Tahoma, Verdana, sans-serif\">20040812<\/font><\/td>\n<td width=\"108\"><font face=\"Tahoma, Verdana, sans-serif\">0001<\/font><\/td>\n<td width=\"108\"><font face=\"Tahoma, Verdana, sans-serif\">01:00:00<\/font><\/td>\n<td width=\"108\"><font face=\"Tahoma, Verdana, sans-serif\">25.00<\/font><\/td>\n<\/tr>\n<tr>\n<td><font face=\"Tahoma, Verdana, sans-serif\">20040815<\/font><\/td>\n<td><font face=\"Tahoma, Verdana, sans-serif\">0001<\/font><\/td>\n<td><font face=\"Tahoma, Verdana, sans-serif\">02:00:00<\/font><\/td>\n<td><font face=\"Tahoma, Verdana, sans-serif\">50.00<\/font><\/td>\n<\/tr>\n<\/table>\n<p>Now CRED_NET on the other hand holds how much is left for a student <font face=\"Tahoma, Verdana, sans-serif\">0001<\/font>. There are instances when the values on both databases don&#8217;t synchronize. Yes the existence of these databases violate the normalization rule once again but you have read the reasons above already, right?<\/p>\n<p>And can you stop reminding me about normalization rules so we can go on?<\/p>\n<p>So to correct things you execute this untweaked patch (I&#8217;ll call it the &#8216;brute force approach&#8217;&#8230; &#8216;Brute&#8217;&#8230; like &#8216;Brute Lee&#8217;)<\/p>\n<pre><font size=\"2\" face=\"Tahoma, Verdana, sans-serif\" color=\"#0000FF\">\r\n   select CRED_NET\r\n   set order to CCRENAME\r\n   do while not eof()\r\n      cIDNO = alltrim(CRED_NET.IDNO)\r\n\r\n      **' \u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7 \r\n      **' SECTOR.007.BEGIN\r\n\r\n      select USAGE\r\n      set order to CUSAIDNO\r\n      go TOP\r\n      do while not eof()\r\n         if alltrim(USAGE.IDNO) == cIDNO\r\n            cTimeCount = TimeCalc(cTimeCount, USAGE.TIMETOTAL, 1)\r\n            nCreditsTotal = nCreditsTotal + USAGE.FEENET\r\n         endif\r\n         skip\r\n      enddo\r\n\r\n      **' \u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7 \r\n      **' SECTOR.007.END\r\n\r\n      select CRED_NET\r\n      cTimeReflect = TimeCalc(cTimeCount, '40:00:00', 2)\r\n      nCreditsReflect = 800.00 - nCreditsTotal\r\n \r\n      rlock()\r\n         replace CRED_NET.TIMELEFT with cTimeReflect\r\n         replace CRED_NET.CREDIT with nCreditsReflect\r\n      unlock\r\n\r\n      skip\r\n   enddo\r\n<\/font><\/pre>\n<p>The above code works&#8230; of course, but&#8230; like any process in this world that involves brute force, it is slow.<\/p>\n<p>Now replace the SECTOR.<font face=\"Tahoma, Verdana, sans-serif\">007<\/font> part of the code above with this:<\/p>\n<pre><font size=\"2\" face=\"Tahoma, Verdana, sans-serif\">   select USAGE<br \/>\r\n   set order to CUSAIDNO<br \/>\r\n   set filter to alltrim(USAGE.IDNO) == cIDNO<br \/>\r\n   go top<br \/>\r\n   do while not eof()<br \/>\r\n      cTimeCount = TimeCalc(cTimeCount, USAGE.TIMETOTAL, 1)<br \/>\r\n      nCreditsTotal = nCreditsTotal + USAGE.FEENET<br \/>\r\n      skip<br \/>\r\n   enddo<\/font><\/pre>\n<p>You&#8217;ll see a HUGE difference in terms of execution speed.<\/p>\n<p>TimeCalc is a function by the way that adds, substracts, multiplies and divides two time variables. Something which I&#8217;ll post later. (You can find the basic\/raw version inside the <a href=\"http:\/\/support.microsoft.com\" target=\"_blank\">Microsoft Help and Support Site<\/a> though),<\/p>\n<p>Of course when you&#8217;re doing these kinds of patches, don&#8217;t forget the progress bars. It is a good hypnotic-mechanism for the users to develop this affinity for your software.<\/p>\n<p>CodeDump-<font face=\"Tahoma, Verdana, sans-serif\">20040825.153618<\/font>.End<\/p>\n<p>Now back to work.                <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Code Dump: the time where you take a break from your programming related tasks to talk about your programming related tasks. In PROJECT::SOPHIEAI (IUMS: Internet Usage Monitoring System), USAGE.dbf holds the records that stores the log-in\/log-off information of the users of a certain internet lab\/cafe at a given time and space. Along with the IDNO [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,10],"tags":[],"class_list":["post-302","post","type-post","status-publish","format-standard","hentry","category-visual-foxpro","category-workblog"],"_links":{"self":[{"href":"https:\/\/foxpro.ntsl119.com\/scr\/wp-json\/wp\/v2\/posts\/302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/foxpro.ntsl119.com\/scr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/foxpro.ntsl119.com\/scr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/foxpro.ntsl119.com\/scr\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/foxpro.ntsl119.com\/scr\/wp-json\/wp\/v2\/comments?post=302"}],"version-history":[{"count":0,"href":"https:\/\/foxpro.ntsl119.com\/scr\/wp-json\/wp\/v2\/posts\/302\/revisions"}],"wp:attachment":[{"href":"https:\/\/foxpro.ntsl119.com\/scr\/wp-json\/wp\/v2\/media?parent=302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/foxpro.ntsl119.com\/scr\/wp-json\/wp\/v2\/categories?post=302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/foxpro.ntsl119.com\/scr\/wp-json\/wp\/v2\/tags?post=302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}