libsrc/do1.cxx

Go to the documentation of this file.
00001 /**********************************************************************
00002  * Copyright 2002 Jeff Rush <jrush@taupro.com>
00003  * Original Copyright 1979-2002 Udanax.com
00004  *
00005  * This file is part of the Udanax xanalogical storage system.
00006  *
00007  * Udanax is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * Udanax is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with Udanax; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  **********************************************************************/
00021 
00030 /* Modification History:
00031  * $Log: do1.cxx,v $
00032  * Revision 1.18  2004/09/11 13:59:21  jrush
00033  * Changed all fprintf's to stderr to use the Nana library L() macro.  Also
00034  * removed a 2-3 minor compiler warnings.
00035  *
00036  * Revision 1.17  2004/09/04 16:32:14  jrush
00037  * Fixed Doxygen declarations at the start of each function.
00038  *
00039  * Revision 1.16  2004/09/04 12:52:39  jrush
00040  * Added a few comments to do1.cxx and tweak Doxygen config.
00041  *
00042  * Revision 1.15  2004/09/03 11:38:30  jrush
00043  * Began adding extensive commenting to functions.
00044  *
00045  * Revision 1.14  2002/07/26 04:32:01  jrush
00046  * Replaced gerror() with assert()
00047  *
00048  * Revision 1.13  2002/05/28 04:22:29  jrush
00049  * Adjusted source files to comply with GPL licensing.
00050  *
00051  * Revision 1.12  2002/05/28 02:47:11  jrush
00052  * Cosmetic -- removed some comments.
00053  *
00054  * Revision 1.11  2002/04/12 11:56:42  jrush
00055  * Reorganized include file layout, renamed xanadu.h to udanax.h and
00056  * typecontext/typecrumcontext to C++ class Context/CrumContext.
00057  *
00058  * Revision 1.10  2002/04/10 18:01:54  jrush
00059  * Renamed class typeisa to IStreamAddr.
00060  *
00061  * Revision 1.9  2002/04/09 21:45:46  jrush
00062  * Renamed class 'tumbler' to 'Tumbler', for consistency with Python sources,
00063  * and changed typeisa from typedef to a subclass, in preparation for cleaning
00064  * up the type/class tree.
00065  *
00066  * Revision 1.8  2002/04/08 18:55:56  jrush
00067  * Switched from using global variable 'user' to object 'Session' as a
00068  * connection for tracking open document descriptors in the bert table.
00069  *
00070  * Revision 1.7  2002/04/07 14:04:37  jrush
00071  * Add ptr to Session to checkforopen() and isthisusersdocument() so that we
00072  * have session information available to make the decision.
00073  *
00074  * Revision 1.6  2002/04/06 17:05:57  jrush
00075  * Switched from referring to 'task' for a client connection to 'session',
00076  * and converted the typetask typedef/struct into a Session C++ class.
00077  *
00078  * Revision 1.5  2002/04/06 15:01:17  jrush
00079  * Changed INT to just 'int'.
00080  *
00081  * Revision 1.4  2002/04/06 14:05:24  jrush
00082  * Added functions to graph POOMs via the DOT tool, as well as more debug
00083  * messages as I attempt to get docopy() to work in all cases.
00084  *
00085  * Revision 1.3  2002/04/02 18:44:50  jrush
00086  * Added calls to output graphs of the enfilade trees in docopy(), some debug
00087  * messages and a few cosmetic changes.
00088  *
00089  * Revision 1.2  2002/02/14 09:27:43  jrush
00090  * Cleaned up source:
00091  *
00092  * 1. ran thru the indent tool to achieve a standard look,
00093  * 2. added structured comments at top for use with DOxygen reporting
00094  *    as well as CVS keywords,
00095  * 3. fixed compiler warnings re ambiguous assign/compares,
00096  *    needed casts and unused/uninitialized variables,
00097  * 4. fixed funcs that didnt specify a return type,
00098  * 5. centralized prototypes in protos.h, removing incomplete ones,
00099  * 6. cleaned up use of bool/BOOLEAN type to suit C++ type,
00100  * 7. fixed initializer nesting in tumbler constants,
00101  * 8. renamed vars that conflict with C++ keywords (new, this),
00102  * 9. fixed global/extern confusion re some global vars.
00103  *
00104  */
00105 
00106 #include <stdio.h>
00107 #include <stdlib.h>
00108 #include <fcntl.h>
00109 #include "udanax.h"
00110 
00111 extern void graphwholetree(char *filename, typecorecrum *ptr, char *graphtitle);
00112 
00113 typespec spec, spec2, spec3;
00114 typevstuffset uppervstuffset;
00115 Tumbler fivetumbler = { 0, 0, 0, 0, { 500 /* 100 */ , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
00116 
00125     bool
00126 dofinddocscontaining(
00127     Session     *sess,          
00128     typespecset  specset,       
00129     typelinkset *addresssetptr) 
00130 {
00131     typeispanset ispanset;
00132 
00133     // Convert the specification set into a set of spans into the I-stream.
00134     return specset2ispanset(sess, specset, &ispanset, NOBERTREQUIRED)
00135 
00136     // Search for documents containing those spans
00137             && finddocscontainingsp(sess, ispanset, addresssetptr);
00138 }
00139 
00147     bool
00148 doappend(
00149     Session     *sess,    
00150     IStreamAddr *doc_isa, 
00151     typetextset  textset) 
00152 {
00153     return appendpm(sess, doc_isa, textset);
00154 }
00155 
00168     bool
00169 dorearrange(
00170     Session     *sess,      
00171     IStreamAddr *docisaptr, 
00172     typecutseq  *cutseqptr) 
00173 {
00174     // ORGL element on the Granfilade representing the particular document
00175     typeorgl docorgl;
00176 
00177     // Locate the document's ORGL within the Granfilade and
00178     // obtain a write lock on it.
00179     return findorgl(sess, granf, docisaptr, &docorgl, WRITEBERT)
00180 
00181     // Actually perform the rearrangement of text.
00182             && rearrangepm(sess, docisaptr, docorgl, cutseqptr);
00183 }
00184 
00192     bool
00193 docopy(
00194     Session     *sess,      
00195     IStreamAddr *docisaptr, 
00196     Tumbler     *vsaptr,    
00197     typespecset  specset)   
00198 {
00199     bool f;
00200 
00201     L("(specset2ispanset p=0x%08X)\n", (int) specset); putitemset(sess, (typeitem *) specset);  L(")\n");
00202 
00203     // Convert the specification set into a set of spans into the I-stream.
00204     typeispanset ispanset;
00205     f = specset2ispanset(sess, specset, &ispanset, NOBERTREQUIRED);
00206     if (!f) {
00207         L("(specset2ispanset FAILED)\n");
00208         return false;
00209     }
00210 
00211     L("ispanset is 0x%08X\n", (int) ispanset);
00212     L("(specset2ispanset ispanset=");  putitemset(sess, (typeitem *) ispanset);  L(")\n");
00213     L("(findorgl for docisa=");  puttumbler(stdout, docisaptr);  L(")\n");
00214 
00215     /*
00216      * Walk the granfilade to locate and lock, for writing, the document to receive
00217      * the copied material.
00218      */
00219 
00220     // Locate the document's ORGL within the Granfilade and
00221     // obtain a write lock on it.
00222     typeorgl docorgl;
00223     f = findorgl(sess, granf, docisaptr, &docorgl, WRITEBERT);
00224     if (!f) {
00225         L("(attempt to findorgl FAILED)\n");
00226         return false;
00227     }
00228 
00229     /*
00230      * Validate the V-stream address at which to insert the content for reasonableness.
00231      */ 
00232     L("(acceptablevsa)\n");
00233     f = acceptablevsa(vsaptr, docorgl);
00234     if (!f) {
00235         L("(acceptablevsa FAILED)\n");
00236         return false;
00237     }
00238 
00239     L("(asserttreeisok)\n");
00240     f = asserttreeisok((typecorecrum *) docorgl);
00241     if (!f) {
00242         L("(asserttreeisok FAILED)\n");
00243         return false;
00244     }
00245 
00246 //    L("---- Dump of Entire Granfilade Before Copy ----\n");
00247 //    dumpwholetree((typecorecrum *) granf);
00248     graphwholetree("/var/tmp/granf_before.dot", (typecorecrum *) granf, "Granfilade before Copy");
00249 
00250 //    L("---- End of Dump of Entire Granfilade Before Copy ----\n");
00251 
00252 //    L("---- Dump of Entire Spanfilade Before Copy ----\n");
00253 //    dumpwholetree((typecorecrum *) spanf);
00254     graphwholetree("/var/tmp/spanf_before.dot", (typecorecrum *) spanf, "Spanfilade before Copy");
00255 //    L("---- End of Dump of Entire Spanfilade Before Copy ----\n");
00256 
00257     /*
00258      *
00259      */
00260 
00261 /* the meat of docopy: */
00262     L("(insertpm)\n");
00263     f = insertpm(sess, docisaptr, docorgl, vsaptr, (typesporglitem *) ispanset);
00264     if (!f) {
00265         L("(insertpm FAILED)\n");
00266         return false;
00267     }
00268 
00269     L("(insertspanf)\n");
00270     f = insertspanf(sess, spanf, docisaptr, (typesporglitem *) ispanset, DOCISPAN);
00271     if (!f) {
00272         L("(insertspanf FAILED)\n");
00273         return false;
00274     }
00275 
00276     L("(asserttreeisok)\n");
00277     f = asserttreeisok((typecorecrum *) docorgl);
00278     if (!f) {
00279         L("(asserttreeisok FAILED)\n");
00280         return false;
00281     }
00282 
00283 //    L("---- Dump of Entire Granfilade After Copy ----\n");
00284 //    dumpwholetree((typecorecrum *) granf);
00285     graphwholetree("/var/tmp/granf_after.dot", (typecorecrum *) granf, "Granfilade after Copy");
00286 //    L("---- End of Dump of Entire Granfilade After Copy ----\n");
00287 
00288 //    L("---- Dump of Entire Spanfilade After Copy ----\n");
00289 //    dumpwholetree((typecorecrum *) spanf);
00290     graphwholetree("/var/tmp/spanf_after.dot", (typecorecrum *) spanf, "Spanfilade after Copy");
00291 //    L("---- End of Dump of Entire Spanfilade After Copy ----\n");
00292 
00293     return true;
00294 }
00295 
00303     bool
00304 docopyinternal(
00305     Session     *sess,      
00306     IStreamAddr *docisaptr, 
00307     Tumbler     *vsaptr,    
00308     typespecset  specset)   
00309 {
00310     typeispanset ispanset;
00311 
00312     typeorgl docorgl;
00313 
00314     // Convert the specification set into a set of spans into the I-stream.
00315     return (specset2ispanset(sess, specset, &ispanset, NOBERTREQUIRED)
00316 
00317             && findorgl(sess, granf, docisaptr, &docorgl, NOBERTREQUIRED)
00318             && acceptablevsa(vsaptr, docorgl)
00319             && asserttreeisok((typecorecrum *) docorgl)
00320 
00321 /* the meat of docopy: */
00322             && insertpm(sess, docisaptr, docorgl, vsaptr, (typesporglitem *) ispanset)
00323 
00324             && insertspanf(sess, spanf, docisaptr, (typesporglitem *) ispanset, DOCISPAN)
00325             && asserttreeisok((typecorecrum *) docorgl)
00326             );
00327 }
00328 
00336     bool
00337 doinsert(
00338     Session     *sess,      
00339     IStreamAddr *docisaptr, 
00340     Tumbler     *vsaptr,    
00341     typetextset  textset)   
00342 {
00343     typehint hint;
00344     makehint(DOCUMENT, ATOM, TEXTATOM, docisaptr, &hint);
00345 
00346     typespanset ispanset;
00347     return inserttextingranf(sess, granf, &hint, textset, &ispanset)
00348 
00349            && docopy(sess, docisaptr, vsaptr, (typespec *) ispanset);
00350 }
00351 
00356     void
00357 checkspecandstringbefore()
00358 {
00359     L("--checkspecandstringbefore is a NOP--\n");
00360 }
00361 
00369     bool
00370 dodeletevspan(
00371     Session     *sess,      
00372     IStreamAddr *docisaptr, 
00373     typevspan   *vspanptr)  
00374 {
00375     typeorgl docorgl;
00376 
00377     if (findorgl(sess, granf, docisaptr, &docorgl, WRITEBERT))
00378         return deletevspanpm(sess, docisaptr, docorgl, vspanptr);
00379 
00380     return false;
00381 }
00382 
00390     bool
00391 domakelink(
00392     Session     *sess,        
00393     IStreamAddr *docisaptr,   
00394     typespecset  fromspecset, 
00395     typespecset  tospecset,   
00396     IStreamAddr *linkisaptr)  
00397 {
00398     Tumbler       linkvsa, fromvsa, tovsa;
00399     typespanset   ispanset;
00400     typesporglset fromsporglset;
00401     typesporglset tosporglset;
00402     typeorgl      link;
00403 
00404     typehint hint;
00405     makehint(DOCUMENT, ATOM, LINKATOM, docisaptr, &hint);
00406 
00407     return createorglingranf(sess, granf, &hint, linkisaptr)
00408             && tumbler2spanset(sess, linkisaptr, &ispanset)
00409             && findnextlinkvsa(sess, docisaptr, &linkvsa)
00410             && docopy(sess, docisaptr, &linkvsa, (typespec *) ispanset)
00411             && findorgl(sess, granf, linkisaptr, &link, WRITEBERT)
00412             && specset2sporglset(sess, fromspecset, &fromsporglset, NOBERTREQUIRED)
00413             && specset2sporglset(sess, tospecset, &tosporglset, NOBERTREQUIRED)
00414             && setlinkvsas(&fromvsa, &tovsa, NULL)
00415             && insertendsetsinorgl(sess, linkisaptr, link, &fromvsa, fromsporglset, &tovsa, tosporglset, NULL, NULL)
00416             && insertendsetsinspanf(sess, spanf, linkisaptr, fromsporglset, tosporglset, NULL)
00417             ;
00418 }
00419 
00427     bool
00428 docreatelink(
00429     Session     *sess,         
00430     IStreamAddr *docisaptr,    
00431     typespecset  fromspecset,  
00432     typespecset  tospecset,    
00433     typespecset  threespecset, 
00434     IStreamAddr *linkisaptr)   
00435 {
00436     Tumbler       linkvsa, fromvsa, tovsa, threevsa;
00437     typespanset   ispanset;
00438     typesporglset fromsporglset;
00439     typesporglset tosporglset;
00440     typesporglset threesporglset;
00441     typeorgl      link;
00442 
00443     typehint hint;
00444     makehint(DOCUMENT, ATOM, LINKATOM, docisaptr, &hint);
00445 
00446     return createorglingranf(sess, granf, &hint, linkisaptr)
00447             && tumbler2spanset(sess, linkisaptr, &ispanset)
00448             && findnextlinkvsa(sess, docisaptr, &linkvsa)
00449             && docopy(sess, docisaptr, &linkvsa, (typespec *) ispanset)
00450             && findorgl(sess, granf, linkisaptr, &link, /* WRITEBERT ECH 7-1 */ NOBERTREQUIRED)
00451             && specset2sporglset(sess, fromspecset, &fromsporglset, NOBERTREQUIRED)
00452             && specset2sporglset(sess, tospecset, &tosporglset, NOBERTREQUIRED)
00453             && specset2sporglset(sess, threespecset, &threesporglset, NOBERTREQUIRED)
00454             && setlinkvsas(&fromvsa, &tovsa, &threevsa)
00455             && insertendsetsinorgl(sess, linkisaptr, link, &fromvsa, fromsporglset, &tovsa, tosporglset, &threevsa,
00456                                    threesporglset)
00457             && insertendsetsinspanf(sess, spanf, linkisaptr, fromsporglset, tosporglset, threesporglset)
00458             ;
00459 }
00460 
00465     bool
00466 dofollowlink(
00467     Session     *sess,       
00468     IStreamAddr *linkisaptr, 
00469     typespecset *specsetptr, 
00470     int          whichend)   
00471 {
00472     typesporglset sporglset;
00473 
00474     return link2sporglset(sess, linkisaptr, &sporglset, whichend, NOBERTREQUIRED)
00475             && linksporglset2specset(sess, &((typesporgl *) sporglset)->sporgladdress, sporglset, specsetptr, NOBERTREQUIRED);
00476 }
00477 
00492     bool
00493 docreatenewdocument(
00494     Session     *sess,   
00495     IStreamAddr *isaptr) 
00496 {
00497     typehint hint;
00498     makehint(ACCOUNT, DOCUMENT, 0, &sess->account, &hint);
00499 
00500     return createorglingranf(sess, granf, &hint, isaptr);
00501 }
00502 
00514     bool
00515 docreatenode_or_account(
00516     Session     *sess,   
00517     IStreamAddr *isaptr) 
00518 {
00519     IStreamAddr  isa;
00520     tumblercopy(isaptr, &isa);
00521 
00522     typehint hint;
00523     makehint(NODE, NODE, 0, /* &sess->account */ &isa, &hint);
00524 
00525     return createorglingranf(sess, granf, &hint, &isa);
00526 }
00527 
00533     static bool
00534 doretrievedocvspanfoo(
00535     Session     *sess,      
00536     IStreamAddr *docisaptr, 
00537     typevspan   *vspanptr)  
00538 {
00539     typeorgl docorgl;
00540 
00541     return findorgl(sess, granf, docisaptr, &docorgl, NOBERTREQUIRED)
00542             && retrievedocumentpartofvspanpm(sess, docorgl, vspanptr);
00543 }
00544 
00552     bool
00553 docreatenewversion(
00554     Session     *sess,         
00555     IStreamAddr *isaptr,       
00556     IStreamAddr *wheretoputit, 
00557     IStreamAddr *newisaptr)    
00558 {
00559     IStreamAddr newtp;      /* for internal open */
00560 
00561     // Test for ownership to do the right thing for explicit creation of new
00562     // version of someone else's document
00563     typehint hint;
00564     if (tumbleraccounteq(isaptr, wheretoputit) && isthisusersdocument(sess, isaptr)) {
00565         makehint(DOCUMENT, DOCUMENT, 0, isaptr /* wheretoputit */ , &hint);
00566     } else {
00567         // This does the right thing for a new version of someone else's
00568         // document, as it duplicates the behavior of docreatenewdocument
00569         makehint(ACCOUNT, DOCUMENT, 0, wheretoputit, &hint);
00570     }
00571 
00572     if (!createorglingranf(sess, granf, &hint, newisaptr))
00573         return false;
00574 
00575     // Determine the full span of the parent document, to know the range to
00576     // copy into the newly created version.
00577 
00578     typevspan vspan;
00579     if (!doretrievedocvspanfoo(sess, isaptr, &vspan))
00580         return false;
00581 
00582     typevspec vspec;
00583     vspec.next     = NULL;
00584     vspec.itemid   = VSPECID;
00585     movetumbler(isaptr, &vspec.docisa);
00586     vspec.vspanset = &vspan;
00587 
00588     // BERTMODEONLY to prevent recursive createnewversions
00589     if (!doopen(sess, newisaptr, &newtp, WRITEBERT, BERTMODEONLY))
00590         assert(0); // Couldn't do internal doopen for new doc in docreatenewversion
00591 
00592     // Map (copy virtually) the entire content of the original document into
00593     // V-stream of the newly created version.
00594     docopyinternal(sess, newisaptr, &vspan.stream, (typespec *) &vspec);
00595     doclose(sess, newisaptr);
00596 
00597     return true;
00598 }
00599 
00607     bool
00608 doretrievedocvspan(
00609     Session     *sess,      
00610     IStreamAddr *docisaptr, 
00611     typevspan   *vspanptr)  
00612 {
00613     typeorgl docorgl;
00614 
00615     return findorgl(sess, granf, docisaptr, &docorgl, READBERT)
00616             && retrievevspanpm(sess, docorgl, vspanptr);
00617 }
00618 
00629     bool
00630 doretrievedocvspanset(
00631     Session      *sess,        
00632     IStreamAddr  *docisaptr,   
00633     typevspanset *vspansetptr) 
00634 {
00635     typeorgl docorgl;
00636 
00637     if (findorgl(sess, granf, docisaptr, &docorgl, READBERT)) {
00638         if (!isemptyorgl(docorgl)) {
00639             if (retrievevspansetpm(sess, docorgl, vspansetptr)) {
00640                 return true;
00641             }
00642         }
00643     }
00644     return false;
00645 }
00646 
00653     bool
00654 doretrievev(
00655     Session       *sess,         
00656     typespecset    specset,      
00657     typevstuffset *vstuffsetptr) 
00658 {
00659     typeispanset ispanset;
00660 
00661     // Convert the specification set into a set of spans into the I-stream.
00662     return specset2ispanset(sess, specset, &ispanset, READBERT)
00663             && ispanset2vstuffset(sess, granf, ispanset, vstuffsetptr);
00664 }
00665 
00674     bool
00675 dofindlinksfromtothree(
00676     Session     *sess,          
00677     typespecset  fromvspecset,  
00678     typespecset  tovspecset,    
00679     typespecset  threevspecset, 
00680     typeispan   *orglrangeptr,  
00681     typelinkset *linksetptr)    
00682 {
00683     return findlinksfromtothreesp(sess, spanf, fromvspecset, tovspecset, threevspecset, orglrangeptr, linksetptr);
00684 }
00685 
00694     bool
00695 dofindnumoflinksfromtothree(
00696     Session     *sess,          
00697     typespecset *fromvspecset,  
00698     typespecset *tovspecset,    
00699     typespecset *threevspecset, 
00700     typeispan   *orglrangeptr,  
00701     int         *numptr)        
00702 {
00703     /*BUG: there is some inconsistency re arg 'fromvspecset' as whether it is (typespec *)[fn below] or (typespec **)[passed-in] */
00704     return findnumoflinksfromtothreesp(sess, spanf, (typespec *) fromvspecset, (typespec *) tovspecset, (typespec *) threevspecset, orglrangeptr, numptr);
00705 }
00706 
00716     bool
00717 dofindnextnlinksfromtothree(
00718     Session     *sess,           
00719     typevspec   *fromvspecptr,   
00720     typevspec   *tovspecptr,     
00721     typevspec   *threevspecptr,  
00722     typeispan   *orglrangeptr,   
00723     IStreamAddr *lastlinkisaptr, 
00724     typelinkset *nextlinksetptr, 
00725     int         *nptr)           
00726 {
00727     return findnextnlinksfromtothreesp(sess, (typespec *) fromvspecptr,
00728                (typespec *) tovspecptr, (typespec *) threevspecptr,
00729                orglrangeptr, lastlinkisaptr, nextlinksetptr, nptr);
00730 }
00731 
00736     bool
00737 doretrieveendsets(
00738     Session     *sess,        
00739     typespecset  specset,     
00740     typespecset *fromsetptr,  
00741     typespecset *tosetptr,    
00742     typespecset *threesetptr) 
00743 {
00744     return retrieveendsetsfromspanf(sess, specset, fromsetptr, tosetptr, threesetptr);
00745 }
00746 
00753     bool
00754 doshowrelationof2versions(
00755     Session         *sess,     
00756     typespecset      version1, 
00757     typespecset      version2, 
00758     typespanpairset *relation) 
00759 {
00760     typeispanset version1ispans = NULL;
00761     typeispanset version2ispans = NULL;
00762     typeispanset commonispans   = NULL;
00763 
00764     // Convert the specification set into a set of spans into the I-stream.
00765     return specset2ispanset(sess, version1, &version1ispans, READBERT)
00766             && specset2ispanset(sess, version2, &version2ispans, READBERT)
00767             && intersectspansets(sess, version1ispans, version2ispans, &commonispans, ISPANID)
00768             && ispansetandspecsets2spanpairset(sess, commonispans, version1, version2, relation);
00769 }

Generated on Sun Aug 21 04:18:14 2005 for Udanax-Green by doxygen1.3.4