~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/ndb/src/kernel/blocks/ndbfs/OpenFiles.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#ifndef OPENFILES_H
 
17
#define OPENFILES_H
 
18
 
 
19
#include <Vector.hpp>
 
20
 
 
21
class OpenFiles
 
22
{
 
23
public:
 
24
   OpenFiles(){ }
 
25
   
 
26
  /* Get a pointer to the file with id */
 
27
  AsyncFile* find(Uint16 id) const;
 
28
  /* Insert file with id */
 
29
  bool insert(AsyncFile* file, Uint16 id);
 
30
  /* Erase file with id */
 
31
  bool erase(Uint16 id);
 
32
  /* Get number of open files */
 
33
  unsigned size();
 
34
 
 
35
  Uint16 getId(unsigned i);
 
36
  AsyncFile* getFile(unsigned i);
 
37
  
 
38
   
 
39
private:
 
40
 
 
41
  class OpenFileItem {
 
42
  public:
 
43
    OpenFileItem():  m_file(NULL), m_id(0){};
 
44
 
 
45
    AsyncFile* m_file;      
 
46
    Uint16 m_id;
 
47
  };
 
48
 
 
49
  Vector<OpenFileItem> m_files;
 
50
};
 
51
 
 
52
 
 
53
//*****************************************************************************
 
54
inline AsyncFile* OpenFiles::find(Uint16 id) const {
 
55
  for (unsigned i = 0; i < m_files.size(); i++){
 
56
    if (m_files[i].m_id == id){
 
57
      return m_files[i].m_file;
 
58
    }
 
59
  }
 
60
  return NULL;
 
61
}
 
62
 
 
63
//*****************************************************************************
 
64
inline bool OpenFiles::erase(Uint16 id){
 
65
  for (unsigned i = 0; i < m_files.size(); i++){
 
66
    if (m_files[i].m_id == id){
 
67
      m_files.erase(i);
 
68
      return true;
 
69
    }
 
70
  }
 
71
  // Item was not found in list
 
72
  return false;
 
73
}
 
74
 
 
75
 
 
76
//*****************************************************************************
 
77
inline bool OpenFiles::insert(AsyncFile* file, Uint16 id){
 
78
  // Check if file has already been opened
 
79
  for (unsigned i = 0; i < m_files.size(); i++){
 
80
    if(m_files[i].m_file == NULL)
 
81
      continue;
 
82
 
 
83
    if(strcmp(m_files[i].m_file->theFileName.c_str(), 
 
84
              file->theFileName.c_str()) == 0)
 
85
    {
 
86
      BaseString names;
 
87
      names.assfmt("open: >%s< existing: >%s<",
 
88
                   file->theFileName.c_str(),
 
89
                   m_files[i].m_file->theFileName.c_str());
 
90
      ERROR_SET(fatal, NDBD_EXIT_AFS_ALREADY_OPEN, names.c_str(),
 
91
                "OpenFiles::insert()");    
 
92
    }
 
93
  }
 
94
  
 
95
  // Insert the file into vector
 
96
  OpenFileItem openFile;
 
97
  openFile.m_id = id;
 
98
  openFile.m_file = file;
 
99
  m_files.push_back(openFile);
 
100
  
 
101
  return true;
 
102
}
 
103
 
 
104
//*****************************************************************************
 
105
inline Uint16 OpenFiles::getId(unsigned i){
 
106
  return m_files[i].m_id; 
 
107
}
 
108
 
 
109
//*****************************************************************************
 
110
inline AsyncFile* OpenFiles::getFile(unsigned i){
 
111
  return m_files[i].m_file; 
 
112
}
 
113
 
 
114
//*****************************************************************************
 
115
inline unsigned OpenFiles::size(){
 
116
  return m_files.size(); 
 
117
}
 
118
 
 
119
#endif