First version
[csql.git] / src / storage / ChunkIterator.cxx
blobe91d1206101491ce2a472fdb4f9186c39310995e
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 if(NULL == iterPage_) return NULL;
51 char *record =NULL;
52 record = ((char*)iterPage_) +sizeof(PageInfo);
53 while(*(int*)record != 1)
55 iterPage_ = (PageInfo*) iterPage_->nextPage_;
56 if(NULL == iterPage_) return NULL;
57 record = ((char*)iterPage_) +sizeof(PageInfo);
59 iterPage_ = (PageInfo*) iterPage_->nextPage_;
60 return (record + sizeof(int));
63 //check whether there are any nodes in the current page
64 //int i = nodeOffset_;
65 if (!data) {
66 data = ((char*)iterPage_) + sizeof(PageInfo);
67 if ((*(int*)data) == 1)
68 return data + sizeof(int);
70 data = data + allocSize_;
71 while(data < iterPageEnd)
73 if (*((int*)data) == 0)
75 //not used, so skip it
76 data = data + allocSize_;
77 printDebug(DM_Iterator,"ChunkID:%d Moving to next data node %x",
78 chunkID_, data);
80 else
82 //used, return element pointer
83 //nodeOffset_++;
84 printDebug(DM_Iterator,"ChunkID:%d Returning %x nodeOffset:%d",
85 chunkID_, data + sizeof(int), nodeOffset_);
86 return data + sizeof(int);
89 //go to next page and check till it exhausts
90 while(iterPage_->nextPage_ != NULL)
92 iterPage_ = (PageInfo*)iterPage_->nextPage_;
93 data = ((char*)iterPage_) + sizeof(PageInfo);
94 iterPageEnd = ((char*)iterPage_) + PAGE_SIZE;
95 while(data < iterPageEnd)
97 if (*((int*)data) == 0)
99 //not used, so skip it
100 data = data + allocSize_;
101 nodeOffset_++;
103 else
105 //nodeOffset_++;
106 printDebug(DM_Iterator,"ChunkID:%d Returning %x",
107 chunkID_, data + sizeof(int));
108 return data +sizeof(int);
112 return NULL;