hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
AStorageHandler.h
Go to the documentation of this file.
1 #ifndef ASTORAGEHANDLER_H
2 #define ASTORAGEHANDLER_H
3 #include <stdio.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <stdarg.h>
8 #include <string.h>
9 #include <math.h>
10 #include <new>
11 #include <BaseHandler.h>
12 #include <THash.h>
13 #include <CRC32.h>
14 #include <String.h>
15 #include <Thread_t.h>
16 #include <FileWriter.h>
17 
19 {
20  public:
21  StorageData():data(NULL),type(NULL),count(0){}
22  StorageData(const StorageData &storageData)
23  {
24  count = storageData.count;
25  data = (_Data*)malloc(sizeof(_Data) * count);
26  type = (DataType*)malloc(sizeof(DataType) * count);
27  memcpy(data, storageData.data, sizeof(_Data) * count);
28  memcpy(type, storageData.type, sizeof(DataType) * count);
29  }
31  {
32  if(data)free(data);
33  if(type)free(type);
34  }
35  void init(int count)
36  {
37  this->count = count;
38  data = (_Data*)malloc(sizeof(_Data) * count);
39  type = (DataType*)malloc(sizeof(DataType) * count);
40  for(int i = 0; i < count; i++)type[i] = EMPTY;
41  }
42  void resize(int count)
43  {
44  data = (_Data*)realloc(data, sizeof(_Data) * count);
45  type = (DataType*)realloc(type, sizeof(DataType) * count);
46  for(int i = this->count; i < count; i++)type[i] = EMPTY;
47  this->count = count;
48  }
50  {
51  if(this->data)free(this->data);
52  if(this->type)free(this->type);
53  init(storageData.count);
54  memcpy(data, storageData.data, sizeof(_Data) * count);
55  memcpy(type, storageData.type, sizeof(DataType) * count);
56  return *this;
57  }
58  enum DataType
59  {
61  };
62  union _Data
63  {
64  long long integer;
65  double real;
66  char *string;
67  }*data;
69  int count;
70 };
71 
72 class StorageHash: public THash<StorageData, String>
73 {
74  public:
75  StorageHash(unsigned long count):THash<StorageData, String>(count){}
76  int dump(const char* filename);
77  bool load(const char* filename);
78 
79  private:
80  unsigned long hashFunc(String name){return crc.calc((char*)name.c_str(), name.length());}
81  void destroyFunc(String name, StorageData data)
82  {
83  for(int i = 0; i < data.count; i++)
84  {
85  if(data.type[i] == StorageData::STRING)free(data.data[i].string);
86  }
87  }
88  CRC32 crc;
89 };
90 
92 {
93  public:
94  AStorageHandler():BaseHandler(102), hash(113){}
95  ~AStorageHandler(){hash.destroy();}
96  virtual void *handle(char code, ...);
97  void setValue(const char *name, long long value, int slot = 0);
98  void setValue(const char *name, double value, int slot = 0);
99  void setValue(const char *name, const char *value, int slot = 0);
100  bool addToValue(const char *name, long long delta, int slot = 0);
101  bool addToValue(const char *name, double delta, int slot = 0);
102  long long getInteger(const char *name, int slot = 0);
103  double getFloat(const char *name, int slot = 0);
104  char* getString(const char *name, int slot = 0);
105  int dump(const char* filename){return hash.dump(filename);}
106  bool load(const char* filename)
107  {
108  long long oldStartDate = getInteger("StartDate");
109  bool ok = hash.load(filename);
110  if(oldStartDate)
111  {
112  setValue("StartDate", oldStartDate);
113  }
114  return ok;
115  }
116 
117  protected:
118  void *handle(char code, va_list &list);
119 
120  private:
121  StorageHash hash;
122  Mutex mutex;
123 };
124 #endif