changes for Gateway with OSYNC Mode.
[csql.git] / src / server / ChunkIterator.cxx
blob7bac2c49460123ce45ff95f57a8b425087b37bc8
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #include<Allocator.h>
17 #include<Database.h>
18 #include<os.h>
19 #include<Debug.h>
20 #include<Config.h>
22 //No iterators for variable size allocators
23 ChunkIterator Chunk::getIterator()
25 ChunkIterator iter;
26 iter.chunkID_ = chunkID_;
27 iter.allocSize_ = allocSize_;
28 iter.allocType_ = allocType_;
29 iter.iterPage_ = firstPage_;
30 iter.nodeOffset_ = 0;
31 iter.noOfNodes_ = os::floor((PAGE_SIZE - sizeof(PageInfo)) / allocSize_);
32 return iter;
35 void* ChunkIterator::nextElement()
37 if(NULL == iterPage_)
39 printError(ErrNotExists,"No iter page exists.");
40 return NULL;
42 //No iterators for variable size allocators
43 if(0 == allocSize_)
45 printError(ErrNotExists,"Iterators are not for variable size allocators");
46 return NULL;
48 PageInfo* pageInfo = (PageInfo*)iterPage_;
49 if (0 == noOfNodes_)
51 //means tuple larger than PAGE_SIZE
52 iterPage_ = pageInfo->nextPage_;
53 return (char*)pageInfo + sizeof(PageInfo)+ sizeof(int);
56 char *data = ((char*)iterPage_) + sizeof(PageInfo) + (nodeOffset_ * allocSize_) ;
58 //check whether there are any nodes in the current page
59 int i = nodeOffset_;
60 while(nodeOffset_ <= noOfNodes_)
62 if (*((int*)data) == 0)
64 //not used, so skip it
65 data = data + allocSize_;
66 nodeOffset_++;
67 if (data >= (char*)iterPage_ + PAGE_SIZE) break;
69 else
71 //used, return element pointer
72 nodeOffset_++;
73 printDebug(DM_Iterator,"ChunkID:%d Returning %x nodeOffset:%d",
74 chunkID_, data + sizeof(int), nodeOffset_);
75 return data + sizeof(int);
78 //go to next page and check till it exhausts
79 while(pageInfo->nextPage_ != NULL)
81 iterPage_ = pageInfo->nextPage_;
82 pageInfo = ((PageInfo*)iterPage_);
83 data = (char*)iterPage_ + sizeof(PageInfo);
84 nodeOffset_ = 0;
85 while(nodeOffset_ < noOfNodes_)
87 if (*((int*)data) == 0)
89 //not used, so skip it
90 data = data + allocSize_;
91 nodeOffset_++;
93 else
95 nodeOffset_++;
96 printDebug(DM_Iterator,"ChunkID:%d Returning %x Page:%x nodeOffset:%d",
97 chunkID_, data + sizeof(int), pageInfo, nodeOffset_);
98 return data +sizeof(int);
102 return NULL;