MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hp_open.c
1 /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
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 /* open a heap-database */
17 
18 #include "heapdef.h"
19 #include "my_sys.h"
20 
21 /*
22  Open heap table based on HP_SHARE structure
23 
24  NOTE
25  This doesn't register the table in the open table list.
26 */
27 
28 HP_INFO *heap_open_from_share(HP_SHARE *share, int mode)
29 {
30  HP_INFO *info;
31  DBUG_ENTER("heap_open_from_share");
32 
33  if (!(info= (HP_INFO*) my_malloc((uint) sizeof(HP_INFO) +
34  2 * share->max_key_length,
35  MYF(MY_ZEROFILL))))
36  {
37  DBUG_RETURN(0);
38  }
39  share->open_count++;
40  thr_lock_data_init(&share->lock,&info->lock,NULL);
41  info->s= share;
42  info->lastkey= (uchar*) (info + 1);
43  info->recbuf= (uchar*) (info->lastkey + share->max_key_length);
44  info->mode= mode;
45  info->current_record= (ulong) ~0L; /* No current record */
46  info->lastinx= info->errkey= -1;
47 #ifndef DBUG_OFF
48  info->opt_flag= READ_CHECK_USED; /* Check when changing */
49 #endif
50  DBUG_PRINT("exit",("heap: 0x%lx reclength: %d records_in_block: %d",
51  (long) info, share->reclength,
52  share->block.records_in_block));
53  DBUG_RETURN(info);
54 }
55 
56 
57 /*
58  Open heap table based on HP_SHARE structure and register it
59 */
60 
61 HP_INFO *heap_open_from_share_and_register(HP_SHARE *share, int mode)
62 {
63  HP_INFO *info;
64  DBUG_ENTER("heap_open_from_share_and_register");
65 
66  mysql_mutex_lock(&THR_LOCK_heap);
67  if ((info= heap_open_from_share(share, mode)))
68  {
69  info->open_list.data= (void*) info;
70  heap_open_list= list_add(heap_open_list,&info->open_list);
71  /* Unpin the share, it is now pinned by the file. */
72  share->open_count--;
73  }
74  mysql_mutex_unlock(&THR_LOCK_heap);
75  DBUG_RETURN(info);
76 }
77 
78 
84 void heap_release_share(HP_SHARE *share, my_bool internal_table)
85 {
86  /* Couldn't open table; Remove the newly created table */
87  if (internal_table)
88  hp_free(share);
89  else
90  {
91  mysql_mutex_lock(&THR_LOCK_heap);
92  if (--share->open_count == 0)
93  hp_free(share);
94  mysql_mutex_unlock(&THR_LOCK_heap);
95  }
96 }
97 
98 /*
99  Open heap table based on name
100 
101  NOTE
102  This register the table in the open table list. so that it can be
103  found by future heap_open() calls.
104 */
105 
106 HP_INFO *heap_open(const char *name, int mode)
107 {
108  HP_INFO *info;
109  HP_SHARE *share;
110  DBUG_ENTER("heap_open");
111 
112  mysql_mutex_lock(&THR_LOCK_heap);
113  if (!(share= hp_find_named_heap(name)))
114  {
115  my_errno= ENOENT;
116  mysql_mutex_unlock(&THR_LOCK_heap);
117  DBUG_RETURN(0);
118  }
119  if ((info= heap_open_from_share(share, mode)))
120  {
121  info->open_list.data= (void*) info;
122  heap_open_list= list_add(heap_open_list,&info->open_list);
123  }
124  mysql_mutex_unlock(&THR_LOCK_heap);
125  DBUG_RETURN(info);
126 }
127 
128 
129 /* map name to a heap-nr. If name isn't found return 0 */
130 
131 HP_SHARE *hp_find_named_heap(const char *name)
132 {
133  LIST *pos;
134  HP_SHARE *info;
135  DBUG_ENTER("heap_find");
136  DBUG_PRINT("enter",("name: %s",name));
137 
138  for (pos= heap_share_list; pos; pos= pos->next)
139  {
140  info= (HP_SHARE*) pos->data;
141  if (!strcmp(name, info->name))
142  {
143  DBUG_PRINT("exit", ("Old heap_database: 0x%lx", (long) info));
144  DBUG_RETURN(info);
145  }
146  }
147  DBUG_RETURN((HP_SHARE *) 0);
148 }
149 
150