performance fixes
[csql.git] / src / storage / ChunkIterator.cxx
blobbe94ddfc29b4eb909f7f809d706576107f0365fb
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_ = (PageInfo*)firstPage_;
30 iter.nodeOffset_ = 0;
31 iter.noOfNodes_ = os::floor((PAGE_SIZE - sizeof(PageInfo)) / allocSize_);
32 iter.data = NULL;
33 iter.iterPageEnd = ((char*)firstPage_) +PAGE_SIZE;
34 printDebug(DM_Iterator,"ChunkID:%d FirstPage is %x",
35 chunkID_, iter.iterPage_);
36 return iter;
39 void* ChunkIterator::nextElement()
41 /*if(NULL == iterPage_)
43 printError(ErrNotExists,"No iter page exists.");
44 return NULL;
45 }*/
46 //PageInfo* pageInfo = (PageInfo*)iterPage_;
47 if (0 == noOfNodes_)
49 //means tuple larger than PAGE_SIZE
50 char *record = ((char*)iterPage_) +sizeof(PageInfo)+sizeof(int);
51 iterPage_ = (PageInfo*) iterPage_->nextPage_;
52 return record;
55 //check whether there are any nodes in the current page
56 //int i = nodeOffset_;
57 if (!data) {
58 data = ((char*)iterPage_) + sizeof(PageInfo);
59 if ((*(int*)data) == 1)
60 return data + sizeof(int);
62 data = data + allocSize_;
63 while(data < iterPageEnd)
65 if (*((int*)data) == 0)
67 //not used, so skip it
68 data = data + allocSize_;
69 printDebug(DM_Iterator,"ChunkID:%d Moving to next data node %x",
70 chunkID_, data);
72 else
74 //used, return element pointer
75 //nodeOffset_++;
76 printDebug(DM_Iterator,"ChunkID:%d Returning %x nodeOffset:%d",
77 chunkID_, data + sizeof(int), nodeOffset_);
78 return data + sizeof(int);
81 //go to next page and check till it exhausts
82 while(iterPage_->nextPage_ != NULL)
84 iterPage_ = (PageInfo*)iterPage_->nextPage_;
85 data = ((char*)iterPage_) + sizeof(PageInfo);
86 iterPageEnd = ((char*)iterPage_) + PAGE_SIZE;
87 while(data < iterPageEnd)
89 if (*((int*)data) == 0)
91 //not used, so skip it
92 data = data + allocSize_;
93 nodeOffset_++;
95 else
97 //nodeOffset_++;
98 printDebug(DM_Iterator,"ChunkID:%d Returning %x",
99 chunkID_, data + sizeof(int));
100 return data +sizeof(int);
104 return NULL;