MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OpenFiles.hpp
1 /*
2  Copyright (C) 2003, 2005, 2006 MySQL AB
3  All rights reserved. Use is subject to license terms.
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; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #ifndef OPENFILES_H
20 #define OPENFILES_H
21 
22 #include <Vector.hpp>
23 
24 class OpenFiles
25 {
26 public:
27  OpenFiles(){ }
28 
29  /* Get a pointer to the file with id */
30  AsyncFile* find(Uint16 id) const;
31  /* Insert file with id */
32  bool insert(AsyncFile* file, Uint16 id);
33  /* Erase file with id */
34  bool erase(Uint16 id);
35  /* Get number of open files */
36  unsigned size();
37 
38  Uint16 getId(unsigned i);
39  AsyncFile* getFile(unsigned i);
40 
41 
42 private:
43 
44  class OpenFileItem {
45  public:
46  OpenFileItem(): m_file(NULL), m_id(0){};
47 
48  AsyncFile* m_file;
49  Uint16 m_id;
50  };
51 
52  Vector<OpenFileItem> m_files;
53 };
54 
55 
56 //*****************************************************************************
57 inline AsyncFile* OpenFiles::find(Uint16 id) const {
58  for (unsigned i = 0; i < m_files.size(); i++){
59  if (m_files[i].m_id == id){
60  return m_files[i].m_file;
61  }
62  }
63  return NULL;
64 }
65 
66 //*****************************************************************************
67 inline bool OpenFiles::erase(Uint16 id){
68  for (unsigned i = 0; i < m_files.size(); i++){
69  if (m_files[i].m_id == id){
70  m_files.erase(i);
71  return true;
72  }
73  }
74  // Item was not found in list
75  return false;
76 }
77 
78 
79 //*****************************************************************************
80 inline bool OpenFiles::insert(AsyncFile* file, Uint16 id){
81  // Check if file has already been opened
82  for (unsigned i = 0; i < m_files.size(); i++){
83  if(m_files[i].m_file == NULL)
84  continue;
85 
86  if(strcmp(m_files[i].m_file->theFileName.c_str(),
87  file->theFileName.c_str()) == 0)
88  {
89  BaseString names;
90  names.assfmt("open: >%s< existing: >%s<",
91  file->theFileName.c_str(),
92  m_files[i].m_file->theFileName.c_str());
93  ERROR_SET(fatal, NDBD_EXIT_AFS_ALREADY_OPEN, names.c_str(),
94  "OpenFiles::insert()");
95  }
96  }
97 
98  // Insert the file into vector
99  OpenFileItem openFile;
100  openFile.m_id = id;
101  openFile.m_file = file;
102  m_files.push_back(openFile);
103 
104  return true;
105 }
106 
107 //*****************************************************************************
108 inline Uint16 OpenFiles::getId(unsigned i){
109  return m_files[i].m_id;
110 }
111 
112 //*****************************************************************************
113 inline AsyncFile* OpenFiles::getFile(unsigned i){
114  return m_files[i].m_file;
115 }
116 
117 //*****************************************************************************
118 inline unsigned OpenFiles::size(){
119  return m_files.size();
120 }
121 
122 #endif