code reorg
[csql.git] / src / server / ChunkIterator.cxx
bloba3a63e873f50b145caa410e95f5f5467009a3fa2
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 if (0 == allocSize_) {
27 printError(ErrNotExists,"Iterators are not for variable size allocators");
28 return iter;
30 iter.chunkID_ = chunkID_;
31 iter.allocSize_ = allocSize_;
32 iter.allocType_ = allocType_;
33 iter.iterPage_ = firstPage_;
34 iter.nodeOffset_ = 0;
35 iter.noOfNodes_ = os::floor((PAGE_SIZE - sizeof(PageInfo)) / allocSize_);
36 return iter;
39 void* ChunkIterator::nextElement()
41 if(NULL == iterPage_)
43 printError(ErrNotExists,"No iter page exists.");
44 return NULL;
46 PageInfo* pageInfo = (PageInfo*)iterPage_;
47 if (0 == noOfNodes_)
49 //means tuple larger than PAGE_SIZE
50 iterPage_ = pageInfo->nextPage_;
51 return (char*)pageInfo + sizeof(PageInfo)+ sizeof(int);
54 char *data = ((char*)iterPage_) + sizeof(PageInfo) + (nodeOffset_ * allocSize_) ;
56 //check whether there are any nodes in the current page
57 int i = nodeOffset_;
58 while(nodeOffset_ < noOfNodes_)
60 if (*((int*)data) == 0)
62 //not used, so skip it
63 data = data + allocSize_;
64 nodeOffset_++;
65 if (data >= (char*)iterPage_ + PAGE_SIZE) break;
67 else
69 //used, return element pointer
70 nodeOffset_++;
71 printDebug(DM_Iterator,"ChunkID:%d Returning %x nodeOffset:%d",
72 chunkID_, data + sizeof(int), nodeOffset_);
73 return data + sizeof(int);
76 //go to next page and check till it exhausts
77 while(pageInfo->nextPage_ != NULL)
79 iterPage_ = pageInfo->nextPage_;
80 pageInfo = ((PageInfo*)iterPage_);
81 data = (char*)iterPage_ + sizeof(PageInfo);
82 nodeOffset_ = 0;
83 while(nodeOffset_ < noOfNodes_)
85 if (*((int*)data) == 0)
87 //not used, so skip it
88 data = data + allocSize_;
89 nodeOffset_++;
91 else
93 nodeOffset_++;
94 printDebug(DM_Iterator,"ChunkID:%d Returning %x Page:%x nodeOffset:%d",
95 chunkID_, data + sizeof(int), pageInfo, nodeOffset_);
96 return data +sizeof(int);
100 return NULL;