~mathiaz/+junk/ceph-new-pkg-review

« back to all changes in this revision

Viewing changes to src/include/ClassLibrary.h

  • Committer: Mathias Gug
  • Date: 2010-07-29 03:10:42 UTC
  • Revision ID: mathias.gug@canonical.com-20100729031042-n9n8kky962qb4onb
Import ceph_0.21-0ubuntu1 from https://launchpad.net/~clint-fewbar/+archive/ceph/+packages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
 
2
// vim: ts=8 sw=2 smarttab
 
3
/*
 
4
 * Ceph - scalable distributed file system
 
5
 *
 
6
 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
 
7
 *
 
8
 * This is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU Lesser General Public
 
10
 * License version 2.1, as published by the Free Software 
 
11
 * Foundation.  See file COPYING.
 
12
 * 
 
13
 */
 
14
 
 
15
#ifndef CEPH_CLASSLIBRARY_H
 
16
#define CEPH_CLASSLIBRARY_H
 
17
 
 
18
#include "include/types.h"
 
19
#include "include/encoding.h"
 
20
 
 
21
#include "common/ClassVersion.h"
 
22
 
 
23
struct ClassImpl {
 
24
  bufferlist binary;
 
25
  utime_t stamp;
 
26
  version_t seq;
 
27
 
 
28
  void encode(bufferlist& bl) const {
 
29
    __u8 v = 0;
 
30
    ::encode(v, bl);
 
31
    ::encode(binary, bl);
 
32
    ::encode(seq, bl);
 
33
  }
 
34
  void decode(bufferlist::iterator& bl) {
 
35
    __u8 v;
 
36
    ::decode(v, bl);
 
37
    ::decode(binary, bl);
 
38
    ::decode(seq, bl);
 
39
  }
 
40
};
 
41
 
 
42
 
 
43
WRITE_CLASS_ENCODER(ClassImpl)
 
44
 
 
45
 
 
46
struct ClassInfo {
 
47
  string name;
 
48
  ClassVersion version;
 
49
 
 
50
  void encode(bufferlist& bl) const {
 
51
    __u8 v = 1;
 
52
    ::encode(v, bl);
 
53
    ::encode(name, bl);
 
54
    ::encode(version, bl);
 
55
  }
 
56
  void decode(bufferlist::iterator& bl) {
 
57
    __u8 v;
 
58
    ::decode(v, bl);
 
59
    ::decode(name, bl);
 
60
    ::decode(version, bl);
 
61
  }
 
62
};
 
63
 
 
64
WRITE_CLASS_ENCODER(ClassInfo)
 
65
 
 
66
typedef enum {
 
67
  CLASS_INC_NOP,
 
68
  CLASS_INC_ADD,
 
69
  CLASS_INC_DEL,
 
70
  CLASS_INC_ACTIVATE,
 
71
} ClassLibraryIncOp;
 
72
 
 
73
struct ClassLibraryIncremental {
 
74
   ClassLibraryIncOp op;
 
75
   bufferlist info;
 
76
   bufferlist impl;
 
77
 
 
78
  void encode(bufferlist& bl) const {
 
79
    __u8 v = 1;
 
80
    ::encode(v, bl);
 
81
    __u32 _op = (__u32)op;
 
82
    ::encode(_op, bl);
 
83
    ::encode(info, bl);
 
84
    ::encode(impl, bl);
 
85
  }
 
86
  void decode(bufferlist::iterator& bl) {
 
87
    __u8 v;
 
88
    ::decode(v, bl);
 
89
    __u32 _op;
 
90
    ::decode(_op, bl);
 
91
    op = (ClassLibraryIncOp)_op;
 
92
    assert( op >= CLASS_INC_NOP && op <= CLASS_INC_ACTIVATE);
 
93
    ::decode(info, bl);
 
94
    ::decode(impl, bl);
 
95
  }
 
96
 
 
97
  void decode_impl(ClassImpl& i) {
 
98
     bufferlist::iterator iter = impl.begin();
 
99
     ::decode(i, iter);
 
100
  }
 
101
 
 
102
  void decode_info(ClassInfo& l) {
 
103
     bufferlist::iterator iter = info.begin();
 
104
     ::decode(l, iter);
 
105
  }
 
106
};
 
107
 
 
108
WRITE_CLASS_ENCODER(ClassLibraryIncremental)
 
109
 
 
110
typedef map<ClassVersion, ClassInfo> tClassVersionMap;
 
111
class ClassVersionMap
 
112
{
 
113
public:
 
114
  tClassVersionMap m;
 
115
  string default_ver;
 
116
 
 
117
  void encode(bufferlist& bl) const {
 
118
    __u8 v = 1;
 
119
    ::encode(v, bl);
 
120
    ::encode(m, bl);
 
121
  }
 
122
  void decode(bufferlist::iterator& bl) {
 
123
    __u8 v;
 
124
    ::decode(v, bl);
 
125
    ::decode(m, bl);
 
126
  }
 
127
 
 
128
  tClassVersionMap::iterator begin() { return m.begin(); }
 
129
  tClassVersionMap::iterator end() { return m.end(); }
 
130
 
 
131
  void add(ClassInfo& library) {
 
132
    m[library.version] = library;
 
133
   if (default_ver.length() == 0)
 
134
    default_ver = library.version.ver;
 
135
  }
 
136
 
 
137
  void remove(ClassInfo& library) {
 
138
    tClassVersionMap::iterator iter;
 
139
    iter = m.find(library.version);
 
140
    if (iter != m.end()) {
 
141
      m.erase(iter);
 
142
    }
 
143
  }
 
144
 
 
145
  ClassInfo *get(ClassVersion& ver);
 
146
  void set_default(string ver) { default_ver = ver; }
 
147
};
 
148
WRITE_CLASS_ENCODER(ClassVersionMap)
 
149
 
 
150
struct ClassLibrary {
 
151
  version_t version;
 
152
  map<string, ClassVersionMap> library_map;
 
153
 
 
154
  ClassLibrary() : version(0) {}
 
155
 
 
156
  void add(const string& name, const ClassVersion& version) {
 
157
    ClassInfo library;
 
158
    library.version = version;
 
159
    library.name = name;
 
160
    add(library);
 
161
  }
 
162
 
 
163
  void add(ClassInfo& library) {
 
164
    ClassVersionMap& vmap = library_map[library.name];
 
165
    vmap.add(library);
 
166
  }
 
167
 
 
168
  void remove(const string& name, const ClassVersion& version) {
 
169
    map<string, ClassVersionMap>::iterator mapiter = library_map.find(name);
 
170
    if (mapiter == library_map.end())
 
171
      return;
 
172
    library_map.erase(mapiter);
 
173
  }
 
174
 
 
175
  bool contains(string& name) {
 
176
    return (library_map.find(name) != library_map.end());
 
177
  }
 
178
  bool get_ver(string& name, ClassVersion& reqver, ClassVersion *ver) {
 
179
    map<string, ClassVersionMap>::iterator mapiter = library_map.find(name);
 
180
    if (mapiter == library_map.end())
 
181
      return false;
 
182
    string ver_str;
 
183
    ClassVersionMap& map = mapiter->second;
 
184
    ClassInfo *info = map.get(reqver);
 
185
    if (info)
 
186
      *ver = info->version;
 
187
    
 
188
    return (info != NULL);
 
189
  }
 
190
  void encode(bufferlist& bl) const {
 
191
    __u8 v = 1;
 
192
    ::encode(v, bl);
 
193
    ::encode(version, bl);
 
194
    ::encode(library_map, bl);
 
195
  }
 
196
  void decode(bufferlist::iterator& bl) {
 
197
    __u8 v;
 
198
    ::decode(v, bl);
 
199
    ::decode(version, bl);
 
200
    ::decode(library_map, bl);
 
201
  }
 
202
};
 
203
WRITE_CLASS_ENCODER(ClassLibrary)
 
204
 
 
205
inline ostream& operator<<(ostream& out, const ClassInfo& e)
 
206
{
 
207
  return out << e.name << " (v" << e.version << ")";
 
208
}
 
209
 
 
210
#endif