00001
00006 #include "User.h"
00007 #include <fstream>
00008
00009 using namespace std;
00010
00011 User::User(Cache* cache, const string& login, unsigned quota) :
00012 cache(cache), login(login), quota(quota), declaredQuota(0) {
00013 }
00014
00015 User::~User() {
00016 for (map<string, Space *>::iterator it = spaceHashMap.begin(); it != spaceHashMap.end(); ++it) {
00017 cache->gc->removeObservedSpace(it->second);
00018 it->second->destroySpace();
00019 delete it->second;
00020 }
00021 spaceHashMap.clear();
00022 }
00023
00024 Space * User::createSpace(const string& name, unsigned softQuota, unsigned hardQuota) {
00025 Space * space = NULL;
00026 std::map<string, Space *>::iterator it = spaceHashMap.find(name);
00027 if (it == spaceHashMap.end()) {
00028 space = new Space(name, softQuota, hardQuota, cache, this);
00029 spaceHashMap.insert(pair<string, Space *> (name, space));
00030 } else
00031 space = it->second;
00032
00033 cache->logger.logCreateSpace(name, login, softQuota, hardQuota);
00034
00035 cache->gc->addObservedSpace(space);
00036
00037 return space;
00038 }
00039
00040 Space * User::loadSpace(const string& name) {
00041 ifstream input;
00042 input.open(("space_dump/" + name).c_str(), ios::in | ios::binary);
00043
00044 if (!input.is_open())
00045 return NULL;
00046
00047 if (Space * prevSpace = selectSpace(name)) {
00048 cache->gc->removeObservedSpace(prevSpace);
00049 delete prevSpace;
00050 }
00051
00052 Stats stats;
00053
00054 input.read((char*) &stats.softQuota, sizeof(stats.softQuota));
00055 input.read((char*) &stats.hardQuota, sizeof(stats.hardQuota));
00056
00057 unsigned recordCount = 0;
00058 input.read((char*) &recordCount, sizeof(unsigned));
00059
00060 cout << "READ:" << recordCount << " SOFT:" << stats.softQuota << " HARD:" << stats.hardQuota << endl;
00061
00062 Space * space = selectSpace(name);
00063
00064 if (!space) {
00065 space = new Space(name, stats.softQuota, stats.hardQuota, cache, this);
00066 }
00067
00068 for (unsigned i = 0; i < recordCount; i++) {
00069
00070 unsigned valueSize;
00071 unsigned keyLength;
00072 time_t expiryTime;
00073 unsigned flag;
00074
00075 input.read((char*) &keyLength, sizeof(keyLength));
00076
00077 char key[keyLength];
00078 input.read((char*) &key, keyLength);
00079 input.read((char*) &valueSize, sizeof(valueSize));
00080
00081 char val[valueSize];
00082 input.read(val, valueSize);
00083
00084 input.read((char*) &expiryTime, sizeof(expiryTime));
00085
00086 input.read((char*) &flag, sizeof(flag));
00087
00088 unsigned timeout = expiryTime - Cache::getCurrentTime();
00089
00090 cout << "KEY LENGTH:" << keyLength << " KEY:" << key << " SIZE:" << valueSize << " VALUE:" << val << " EXP:"
00091 << expiryTime << endl;
00092
00093 if (timeout >= 0 && (space->get(LightString(key, keyLength))).isNull()) {
00094 space->set(LightString(key, keyLength), Value((void*) val, valueSize, flag), timeout);
00095 }
00096 }
00097
00098 cache->gc->addObservedSpace(space);
00099
00100 cache->logger.logLoadSpace(name, "space_dump/" + name);
00101
00102 return space;
00103 }
00104
00105 Space * User::selectSpace(const string& name) {
00106 cache->logger.logSelectSpace(name);
00107 return spaceHashMap.find(name)->second;
00108 }