~ubuntu-branches/ubuntu/precise/mysql-5.5/precise-201203300109

« back to all changes in this revision

Viewing changes to storage/ndb/src/kernel/vm/Rope.cpp

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2011-11-08 11:31:13 UTC
  • Revision ID: package-import@ubuntu.com-20111108113113-3ulw01fvi4vn8m25
Tags: upstream-5.5.17
ImportĀ upstreamĀ versionĀ 5.5.17

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2006 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
15
 
 
16
#include "Rope.hpp"
 
17
 
 
18
#define DEBUG_ROPE 0
 
19
 
 
20
void
 
21
ConstRope::copy(char* buf) const {
 
22
  char * ptr = buf;
 
23
  if(DEBUG_ROPE)
 
24
    ndbout_c("ConstRope::copy() head = [ %d 0x%x 0x%x ]",
 
25
             head.used, head.firstItem, head.lastItem);
 
26
  Uint32 left = head.used;
 
27
  Ptr<Segment> curr;
 
28
  curr.i = head.firstItem;
 
29
  while(left > 4 * getSegmentSize()){
 
30
    thePool.getPtr(curr);
 
31
    memcpy(buf, curr.p->data, 4 * getSegmentSize());
 
32
    curr.i = curr.p->nextPool;
 
33
    left -= 4 * getSegmentSize();
 
34
    buf += 4 * getSegmentSize();
 
35
  }
 
36
  if(left > 0){
 
37
    thePool.getPtr(curr);
 
38
    memcpy(buf, curr.p->data, left);
 
39
  }
 
40
  
 
41
  if(DEBUG_ROPE)
 
42
    ndbout_c("ConstRope::copy()-> %s", ptr);
 
43
}
 
44
 
 
45
int
 
46
ConstRope::compare(const char * str, size_t len) const {
 
47
  if(DEBUG_ROPE)
 
48
    ndbout_c("ConstRope[ %d  0x%x  0x%x ]::compare(%s, %d)", 
 
49
             head.used, head.firstItem, head.lastItem, str, (int) len);
 
50
  Uint32 left = head.used > len ? len : head.used;
 
51
  Ptr<Segment> curr;
 
52
  curr.i = head.firstItem;
 
53
  while(left > 4 * getSegmentSize()){
 
54
    thePool.getPtr(curr);
 
55
    int res = memcmp(str, (const char*)curr.p->data, 4 * getSegmentSize());
 
56
    if(res != 0){
 
57
      if(DEBUG_ROPE)
 
58
        ndbout_c("ConstRope::compare(%s, %d, %s) -> %d", str, left, 
 
59
                 (const char*)curr.p->data, res);
 
60
      return res;
 
61
    }
 
62
    curr.i = curr.p->nextPool;
 
63
    left -= 4 * getSegmentSize();
 
64
    str += 4 * getSegmentSize();
 
65
  }
 
66
  
 
67
  if(left > 0){
 
68
    thePool.getPtr(curr);
 
69
    int res = memcmp(str, (const char*)curr.p->data, left);
 
70
    if(res){
 
71
      if(DEBUG_ROPE)
 
72
        ndbout_c("ConstRope::compare(%s, %d, %s) -> %d", 
 
73
                 str, left, (const char*)curr.p->data, res);
 
74
      return res;
 
75
    }
 
76
  }
 
77
  if(DEBUG_ROPE)
 
78
    ndbout_c("ConstRope::compare(%s, %d) -> %d", str, (int) len, head.used > len);
 
79
  return head.used > len;
 
80
}
 
81
 
 
82
void
 
83
Rope::copy(char* buf) const {
 
84
  char * ptr = buf;
 
85
  if(DEBUG_ROPE)
 
86
    ndbout_c("Rope::copy() head = [ %d 0x%x 0x%x ]",
 
87
             head.used, head.firstItem, head.lastItem);
 
88
  Uint32 left = head.used;
 
89
  Ptr<Segment> curr;
 
90
  curr.i = head.firstItem;
 
91
  while(left > 4 * getSegmentSize()){
 
92
    thePool.getPtr(curr);
 
93
    memcpy(buf, curr.p->data, 4 * getSegmentSize());
 
94
    curr.i = curr.p->nextPool;
 
95
    left -= 4 * getSegmentSize();
 
96
    buf += 4 * getSegmentSize();
 
97
  }
 
98
  if(left > 0){
 
99
    thePool.getPtr(curr);
 
100
    memcpy(buf, curr.p->data, left);
 
101
  }
 
102
  if(DEBUG_ROPE)
 
103
    ndbout_c("Rope::copy()-> %s", ptr);
 
104
}
 
105
 
 
106
int
 
107
Rope::compare(const char * str, size_t len) const {
 
108
  if(DEBUG_ROPE)
 
109
    ndbout_c("Rope::compare(%s, %d)", str, (int) len);
 
110
  Uint32 left = head.used > len ? len : head.used;
 
111
  Ptr<Segment> curr;
 
112
  curr.i = head.firstItem;
 
113
  while(left > 4 * getSegmentSize()){
 
114
    thePool.getPtr(curr);
 
115
    int res = memcmp(str, (const char*)curr.p->data, 4 * getSegmentSize());
 
116
    if(res != 0){
 
117
      if(DEBUG_ROPE)
 
118
        ndbout_c("Rope::compare(%s, %d, %s) -> %d", str, (int) len, 
 
119
                 (const char*)curr.p->data, res);
 
120
      return res;
 
121
    }
 
122
    
 
123
    curr.i = curr.p->nextPool;
 
124
    left -= 4 * getSegmentSize();
 
125
    str += 4 * getSegmentSize();
 
126
  }
 
127
  
 
128
  if(left > 0){
 
129
    thePool.getPtr(curr);
 
130
    int res = memcmp(str, (const char*)curr.p->data, left);
 
131
    if(res){
 
132
      if(DEBUG_ROPE)
 
133
        ndbout_c("Rope::compare(%s, %d) -> %d", str, (int) len, res);
 
134
      return res;
 
135
    }
 
136
  }
 
137
  if(DEBUG_ROPE)
 
138
    ndbout_c("Rope::compare(%s, %d) -> %d", str, (int) len, head.used > len);
 
139
  return head.used > len;
 
140
}
 
141
 
 
142
bool
 
143
Rope::assign(const char * s, size_t len, Uint32 hash){
 
144
  if(DEBUG_ROPE)
 
145
    ndbout_c("Rope::assign(%s, %d, 0x%x)", s, (int) len, hash);
 
146
  m_hash = hash;
 
147
  head.used = (head.used + 3) / 4;
 
148
  release();
 
149
  if(append((const Uint32*)s, len >> 2)){
 
150
    if(len & 3){
 
151
      Uint32 buf = 0;
 
152
      const char * src = (const char*)(((Uint32*)s)+(len >> 2));
 
153
      char* dst = (char*)&buf;
 
154
      size_t left = len & 3;
 
155
      while(left){
 
156
        * dst ++ = * src++;
 
157
        left--;
 
158
      }
 
159
      if(!append(&buf, 1))
 
160
        return false;
 
161
    }
 
162
    head.used = len;
 
163
    if(DEBUG_ROPE)
 
164
      ndbout_c("Rope::assign(...) head = [ %d 0x%x 0x%x ]",
 
165
               head.used, head.firstItem, head.lastItem);
 
166
    return true;
 
167
  }
 
168
  return false;
 
169
}
 
170
 
 
171
void
 
172
Rope::erase(){
 
173
  head.used = (head.used + 3) / 4;
 
174
  release();
 
175
}
 
176
 
 
177
Uint32
 
178
Rope::hash(const char * p, Uint32 len){
 
179
  if(DEBUG_ROPE)
 
180
    ndbout_c("Rope::hash(%s, %d)", p, len);
 
181
  Uint32 h = 0;
 
182
  for (; len > 0; len--)
 
183
    h = (h << 5) + h + (* p++);
 
184
  if(DEBUG_ROPE)
 
185
    ndbout_c("Rope::hash(...) -> 0x%x", h);
 
186
  return h;
 
187
}
 
188