SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Bogota
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Mapper (?) Commiting changes at the end of the process

    Hi!

    I'm building some kind of tracking system which consists in 2 parts. First one is analyzing the log files periodically, and the other one is about reacting to the data.

    The log analyzing consists of identifying certain pages in the log and performs an operation to their corresponding object, based on things like:





    • Date of it’s last entry
    • Current total of hits
    • Referrer
    Here’s some code to illustrate:
    PHP Code:
           $entry $log->getNextEntry();
        
    $landingPage $pageFinder->findByUrl($entry->getRequestURL());
        
    $landingPage->addHit($entry->getReferrerID(), $entry->getDate());
        
    $landingPage->process_Hits(); 
    I'm adding a hit per page per referrer id. This will help me calculate some an activity percentil when I run the proce****s() method.

    As you might’ve suspected, there’s a lot of lines in a log and a lot of hits per page. Since the process may be a little bit long, I decided it was worth to have an identity map so I won’t query the database more than once just to get the same object.

    PHP Code:
        Class PageFinder {
      
         function 
    findByUrl ($url) {
             if (
    $page $this->map->get($url)) {
                 return 
    $page;
                   } else {
                 
    $this->_queryByUrl($url); // hit the database
                   
    }
               }
         
    //...
           

    Since there's the possibility that a new landing page might appear in the log (yes, that can happen) I added some extra code to handle it (including the loop):

    PHP Code:
             while ($entry $log->getNextEntry()) {
           if (!
    $landingPage $pageFinder->findByUrl($entry->getRequestURL())) {
          
    $landingPage = new LandingPage($entry->getRequestURL());
             }
       
    $landingPage->addHit($entry->getReferrerID(), $entry->getDate());
         
    $landingPage->process_Hits();
         } 
    Now the fun part. I have to store these thingies into a database. The first thought I had was to modify the finder into a mapper so I could save the object at the end of each loop and the change would be present in the identity map. This had a problem though: I didn’t like the fact that I would be saving the same object several times during the process.

    PHP Code:
      
           $landingPage
    ->process_Hits();
           
    $pageMapper->save($landingPage);
        } 
    // while 
    So I decided to do it at the end of the process. The idea is looping through the IdentityMap and saving the final state for each object.

    Here's the final sketch:
    PHP Code:
        
        $mapper 
    = new LandingPageMapper(); // implements LandingPageFinder
        
    while ($entry $log->getNextEntry()) {
      
             if (!
    $landingPage $mapper->findByUrl($entry->getRequestURL())) {
               
    $landingPage = new LandingPage($entry->getRequestURL());
                
    $mapper->add($landingPage);      
          }
          
    $landingPage->addHit($entry->getReferrerID(), $entry->getDate());
          
    $landingPage->process_Hits();
            }
        
    $mapper->commit(); 
    commit() would check if the object is new or not and insert/update respectively for each object in the identity map.

    My question is regarding the naming conventions... Is this still called a mapper?? I don't think so, but still I cannot find a proper name for this kind of object. I don't like calling it Mapper since I have more mappers in my application and the interface doesn't really fit with them... I'm sure I'm missing something here Any help is greatly appreciated.

    Also, what do you think about the whole design? I'm refactoring my current code to be able to move to the next stages of the project so I'm starting to make decisions. Any feedback?

    Thanks in advance!

    -Andres

    Pd. How can I get the code to indent properly?
    If I have wings, why am I walking?

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this still called a mapper??
    I'm a little bit off track here, but I think it's a UnitOfWork pattern you're describing there ?

    You should take a good long look at the code posted by marcus (lastcraft) in the thread A different slant on persistence ... The naming of those classes may be a bit confusing, but the overall architecture is really nice.

    Pd. How can I get the code to indent properly?
    Don't mix tabs with spaces ... You can set an option somewhere in your texteditor.

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    Bogota
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Quote Originally Posted by kyberfabrikken
    I'm a little bit off track here, but I think it's a UnitOfWork pattern you're describing there ?
    Thanks, I´ll have to read a little more thoroughly about the Unit of Work.

    Quote Originally Posted by kyberfabrikken
    You should take a good long look at the code posted by marcus (lastcraft) in the thread A different slant on persistence ... The naming of those classes may be a bit confusing, but the overall architecture is really nice.
    I looked at it before but didn´t quite get it. Now I think I can make some sense out of it, but still feel awkward with the naming.

    PHP Code:
       $change = &new Change($configuration);
       
       while (
    $entry $log->getNextEntry()) {
           
    $description = &$change->getDescription('LandingPage');
           
    $description->mustEqual('RequestUrl'$entry->getRequestURL());
           
    $landingPage = &$change->localise($description);
       
           if (!
    $landingPage) { // 
               
    $landingPage= &$change->create('Person');
           } 
       
           
    $landingPage->addHit($entry->getReferrerID(), $entry->getDate());
           
    $landingPage->process_Hits();
       }
       
       
    $change->commit(); 
    I think the difference for me is that i´m using a data mapper (which I actully like in my case), so I´m I think I´m too used to have the finder inside the mapper. In Marcus´ library it seems to be located in the Unit of Work. Is it ok to be there?

    Thanks!
    - Andres
    If I have wings, why am I walking?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •