Trie Implementation
[csql.git] / src / storage / ChunkIterator.cxx
blob80e1a48ac9a81cd4c5bcca076a3f7c776ba5f12a
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 if (allocSize_)
32 iter.noOfNodes_ = os::floor((PAGE_SIZE - sizeof(PageInfo)) / allocSize_);
33 iter.data = NULL;
34 iter.iterPageEnd = ((char*)firstPage_) +PAGE_SIZE;
35 printDebug(DM_Iterator,"ChunkID:%d FirstPage is %x",
36 chunkID_, iter.iterPage_);
37 return iter;
40 void* ChunkIterator::nextElement()
42 if (0 == noOfNodes_)
44 //means tuple larger than PAGE_SIZE
45 if(NULL == iterPage_) return NULL;
46 char *record =NULL;
47 record = ((char*)iterPage_) +sizeof(PageInfo);
48 while(*(InUse*)record != 1)
50 iterPage_ = (PageInfo*) iterPage_->nextPage_;
51 if(NULL == iterPage_) return NULL;
52 record = ((char*)iterPage_) +sizeof(PageInfo);
54 iterPage_ = (PageInfo*) iterPage_->nextPage_;
55 return (record + sizeof(InUse));
58 //check whether there are any nodes in the current page
59 //int i = nodeOffset_;
60 if (!data) {
61 data = ((char*)iterPage_) + sizeof(PageInfo);
62 if ((*(InUse*)data) == 1)
63 return data + sizeof(InUse);
65 data += allocSize_;
66 while(data < iterPageEnd)
68 if (*((InUse*)data) == 0)
70 //not used, so skip it
71 data = data + allocSize_;
72 printDebug(DM_Iterator,"ChunkID:%d Moving to next data node %x",
73 chunkID_, data);
75 else
77 //used, return element pointer
78 //nodeOffset_++;
79 printDebug(DM_Iterator,"ChunkID:%d Returning %x nodeOffset:%d",
80 chunkID_, data + sizeof(InUse), nodeOffset_);
81 return data + sizeof(InUse);
84 //go to next page and check till it exhausts
85 while(iterPage_->nextPage_ != NULL)
87 iterPage_ = (PageInfo*)iterPage_->nextPage_;
88 data = ((char*)iterPage_) + sizeof(PageInfo);
89 iterPageEnd = ((char*)iterPage_) + PAGE_SIZE;
90 while(data < iterPageEnd)
92 if (*((InUse*)data) == 0)
94 //not used, so skip it
95 data = data + allocSize_;
97 else
99 printDebug(DM_Iterator,"ChunkID:%d Returning %x",
100 chunkID_, data + sizeof(InUse));
101 return data +sizeof(InUse);
105 return NULL;
107 void* ChunkIterator::nextElementIntMatch(int value, int offset)
109 //check whether there are any nodes in the current page
110 if (!data) {
111 data = ((char*)iterPage_) + sizeof(PageInfo);
112 if ((*(InUse*)data) == 1 && *(int*)(data+sizeof(InUse)+offset) == value)
113 return data + sizeof(InUse);
115 data += allocSize_;
116 while(data < iterPageEnd)
118 if (*(int*)(data+sizeof(InUse)+offset) == value && *((InUse*)data))
120 printDebug(DM_Iterator,"ChunkID:%d Returning %x nodeOffset:%d",
121 chunkID_, data + sizeof(InUse), nodeOffset_);
122 return data + sizeof(InUse);
124 data = data + allocSize_;
125 printDebug(DM_Iterator,"ChunkID:%d Moving to next data node %x",
126 chunkID_, data);
128 //go to next page and check till it exhausts
129 while(iterPage_->nextPage_ != NULL)
131 iterPage_ = (PageInfo*)iterPage_->nextPage_;
132 data = ((char*)iterPage_) + sizeof(PageInfo);
133 iterPageEnd = ((char*)iterPage_) + PAGE_SIZE;
134 while(data < iterPageEnd)
136 if (*(int*)(data+sizeof(InUse)+offset) == value && *((InUse*)data))
138 printDebug(DM_Iterator,"ChunkID:%d Returning %x",
139 chunkID_, data + sizeof(InUse));
140 return data +sizeof(InUse);
142 data = data + allocSize_;
145 return NULL;