src/server/ChunkIterator.cxx

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by www.databasecache.com                           *
00003  *   Contact: praba_tuty@databasecache.com                                 *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015   ***************************************************************************/
00016 #include<Allocator.h>
00017 #include<Database.h>
00018 #include<os.h>
00019 #include<Debug.h>
00020 #include<Config.h>
00021 
00022 //No iterators for variable size allocators
00023 ChunkIterator Chunk::getIterator()
00024 {
00025     ChunkIterator iter;
00026     iter.chunkID_ = chunkID_;
00027     iter.allocSize_ = allocSize_;
00028     iter.allocType_ = allocType_;
00029     iter.iterPage_ = firstPage_;
00030     iter.nodeOffset_ = 0;
00031     iter.noOfNodes_ = os::floor((PAGE_SIZE - sizeof(PageInfo)) / allocSize_);
00032     return iter;
00033 }
00034 
00035 void* ChunkIterator::nextElement()
00036 {
00037     if(NULL == iterPage_)
00038     {
00039         printError(ErrNotExists,"No iter page exists.");
00040         return NULL;
00041     }
00042     //No iterators for variable size allocators
00043     if(0 == allocSize_)
00044     {
00045         printError(ErrNotExists,"Iterators are not for variable size allocators");
00046         return NULL;
00047     }
00048     PageInfo* pageInfo = (PageInfo*)iterPage_;
00049     if (0 == noOfNodes_)
00050     {
00051         //means tuple larger than PAGE_SIZE
00052         iterPage_ = pageInfo->nextPage_;
00053         return (char*)pageInfo + sizeof(PageInfo)+ sizeof(int);
00054     }
00055 
00056     char *data = ((char*)iterPage_) + sizeof(PageInfo) + (nodeOffset_ * allocSize_) ;
00057 
00058     //check whether there are any nodes in the current page
00059     int i = nodeOffset_;
00060     while(nodeOffset_ < noOfNodes_)
00061     {
00062         if (*((int*)data) == 0)
00063         {
00064           //not used, so skip it
00065            data = data + allocSize_;
00066            nodeOffset_++;
00067            if (data >= (char*)iterPage_ + PAGE_SIZE) break; 
00068         }
00069         else
00070         {
00071            //used, return element pointer
00072            nodeOffset_++;
00073            printDebug(DM_Iterator,"ChunkID:%d Returning %x nodeOffset:%d",
00074                                   chunkID_, data + sizeof(int), nodeOffset_);
00075            return data + sizeof(int);
00076         }
00077     }
00078     //go to next page and check till it exhausts
00079     while(pageInfo->nextPage_ != NULL)
00080     {
00081         iterPage_ = pageInfo->nextPage_;
00082         pageInfo = ((PageInfo*)iterPage_);
00083         data = (char*)iterPage_ + sizeof(PageInfo);
00084         nodeOffset_ = 0;
00085         while(nodeOffset_ < noOfNodes_)
00086         {
00087             if (*((int*)data) == 0)
00088             {
00089                //not used, so skip it
00090                data = data + allocSize_;
00091                nodeOffset_++;
00092             }
00093             else
00094             {
00095                 nodeOffset_++;
00096                 printDebug(DM_Iterator,"ChunkID:%d Returning %x Page:%x nodeOffset:%d",
00097                                  chunkID_, data + sizeof(int), pageInfo, nodeOffset_);
00098                 return data +sizeof(int);
00099             }
00100         }
00101     }
00102     return NULL;
00103 }

Generated on Mon Jun 9 22:37:15 2008 for csql by  doxygen 1.4.7