aboutsummaryrefslogtreecommitdiffstats
path: root/fsa/src/vespa/fsamanagers/fsamanager.cpp
blob: a989d2d8b2131357787decf13781bfe87ca2e8f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * @author  Peter Boros
 * @date    2004/08/20
 * @version $Id$
 * @file    fsamanager.cpp
 * @brief
 *
 */

#include "fsamanager.h"

namespace fsa {

// {{{ FSAManager::~FSAManager()

FSAManager::~FSAManager()
{
  for(LibraryIterator it=_library.begin(); it!=_library.end();++it){
    delete it->second;
  }
}

// }}}
// {{{ FSAManager::load()

bool FSAManager::load(const std::string &id, const std::string &url)
{
  std::string file=url;

  if(!url.compare(0,7,"http://"))
  {
    unsigned int pos=url.find_last_of('/');
    if(pos==url.size()-1) return false;
    _cacheLock.lock();
    file=_cacheDir;
    _cacheLock.unlock();
    if(file.size()>0 && file[file.size()-1]!='/') file+='/';
    file+=url.substr(pos+1);
    if(!getUrl(url,file)) return false;
  }

  FSA::Handle *newdict = new FSA::Handle(file);
  if(!newdict->isOk()){
    delete newdict;
    return false;
  }

  _lock.wrLock();
  {
    LibraryIterator it = _library.find(id);
    if(it!=_library.end()){
      delete it->second;
      it->second = newdict;
    }
    else
      _library.insert(Library::value_type(id,newdict));
  }
  _lock.unlock();

  return true;
}

// }}}
// {{{ FSAManager::getUrl()

bool FSAManager::getUrl(const std::string &url, const std::string &file)
{
  (void)url;(void)file;
  return false;
}

// }}}
// {{{ FSAManager::get()

FSA::Handle* FSAManager::get(const std::string &id) const
{
  FSA::Handle *newhandle=NULL;
  _lock.rdLock();
  {
    LibraryConstIterator it = _library.find(id);
    if(it!=_library.end()){
      newhandle = new FSA::Handle(*(it->second));
    }
  }
  _lock.unlock();
  return newhandle;
}

// }}}
// {{{ FSAManager::drop()

void FSAManager::drop(const std::string &id)
{
  _lock.wrLock();
  {
    LibraryIterator it = _library.find(id);
    if(it!=_library.end()){
      delete it->second;
      _library.erase(it);
    }
  }
  _lock.unlock();
}

// }}}
// {{{ FSAManager::clear()

void FSAManager::clear()
{
  _lock.wrLock();
  {
    for(LibraryIterator it = _library.begin(); it!=_library.end(); ++it)
      delete it->second;
    _library.clear();
  }
  _lock.unlock();
}

// }}}
// {{{ FSAManager::setCacheDir()

void FSAManager::setCacheDir(const std::string &dir)
{
  _cacheLock.lock();
  _cacheDir = dir;
  _cacheLock.unlock();
}

// }}}

} // namespace fsa