1
0
Fork 0
arangodb/arangod/RestServer/VocbaseManager.h

375 lines
14 KiB
C++

////////////////////////////////////////////////////////////////////////////////
/// @brief vocbase manager
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2004-2013 triAGENS GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is triAGENS GmbH, Cologne, Germany
///
/// @author Dr. Frank Celler
/// @author Copyright 2011-2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#ifndef TRIAGENS_REST_SERVER_VOCBASE_MANAGER_H
#define TRIAGENS_REST_SERVER_VOCBASE_MANAGER_H 1
#ifdef _WIN32
#include "BasicsC/win-utils.h"
#endif
#include "Basics/Mutex.h"
#include "Basics/ReadLocker.h"
#include "Basics/WriteLocker.h"
#include "Rest/HttpRequest.h"
#include "Rest/HttpResponse.h"
#include "VocBase/vocbase.h"
#include <map>
#include <string>
#include <vector>
#include "v8.h"
struct TRI_vector_pointer_s;
namespace triagens {
namespace rest {
class ApplicationEndpointServer;
}
}
namespace triagens {
namespace arango {
class VocbaseContext;
class JSLoader;
// -----------------------------------------------------------------------------
// --SECTION-- class ArangoServer
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup ArangoDB
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief ArangoDB VocbaseManager
////////////////////////////////////////////////////////////////////////////////
class VocbaseManager {
private:
VocbaseManager () :
_vocbase(0),
_vocbases(),
_endpoints(),
_creationLock(),
_rwLock(),
_authCache(),
_startupLoader(0),
_endpointServer(0),
_freeCollections(0),
_freeVocbases() {
// allocate some space for collection pointers that we need to remove later
_freeCollections = (TRI_vector_pointer_t*) TRI_Allocate(TRI_CORE_MEM_ZONE, sizeof(TRI_vector_pointer_t), false);
if (_freeCollections != 0) {
TRI_InitVectorPointer(_freeCollections, TRI_CORE_MEM_ZONE);
}
};
~VocbaseManager () {
// if we have buffered some collections, we must now free them
if (_freeCollections != 0) {
TRI_FreeCollectionsVocBase(_freeCollections);
TRI_FreeVectorPointer(TRI_CORE_MEM_ZONE, _freeCollections);
}
// on shutdown, we'll free all vocbases we had collected
for (size_t i = 0; i < _freeVocbases.size(); ++i) {
TRI_Free(TRI_UNKNOWN_MEM_ZONE, _freeVocbases[i]);
}
}
VocbaseManager (const VocbaseManager&);
VocbaseManager& operator= (const VocbaseManager&);
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- public methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup ArangoDB
/// @{
////////////////////////////////////////////////////////////////////////////////
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief add system vocbase
////////////////////////////////////////////////////////////////////////////////
void addSystemVocbase (TRI_vocbase_t*);
////////////////////////////////////////////////////////////////////////////////
/// @brief get system vocbase
////////////////////////////////////////////////////////////////////////////////
TRI_vocbase_t* getSystemVocbase () {
return _vocbase;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief grab an exclusive lock for creation
////////////////////////////////////////////////////////////////////////////////
void lockCreation ();
////////////////////////////////////////////////////////////////////////////////
/// @brief release the exclusive lock for creation
////////////////////////////////////////////////////////////////////////////////
void unlockCreation ();
////////////////////////////////////////////////////////////////////////////////
/// @brief add user vocbase
////////////////////////////////////////////////////////////////////////////////
void addUserVocbase (TRI_vocbase_t*);
////////////////////////////////////////////////////////////////////////////////
/// @brief close user vocbases
////////////////////////////////////////////////////////////////////////////////
void closeUserVocbases ();
////////////////////////////////////////////////////////////////////////////////
/// @brief lookup vocbase by name
////////////////////////////////////////////////////////////////////////////////
TRI_vocbase_t* lookupVocbaseByName (std::string const&);
////////////////////////////////////////////////////////////////////////////////
/// @brief check name and path
////////////////////////////////////////////////////////////////////////////////
int canAddVocbase (std::string const&,
std::string const&,
bool);
////////////////////////////////////////////////////////////////////////////////
/// @brief check if a collection name is valid
////////////////////////////////////////////////////////////////////////////////
bool isValidName (std::string const&) const;
////////////////////////////////////////////////////////////////////////////////
/// @brief remove a vocbase by name
////////////////////////////////////////////////////////////////////////////////
int deleteVocbase (string const&);
////////////////////////////////////////////////////////////////////////////////
/// @brief add the startup loader
////////////////////////////////////////////////////////////////////////////////
void setStartupLoader (JSLoader* startupLoader) {
_startupLoader = startupLoader;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief add the application enpoint server
////////////////////////////////////////////////////////////////////////////////
void setApplicationEndpointServer (triagens::rest::ApplicationEndpointServer* endpointServer) {
_endpointServer = endpointServer;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief run version check
/// @return bool returns false if the version check fails
////////////////////////////////////////////////////////////////////////////////
bool runVersionCheck (TRI_vocbase_t*,
v8::Handle<v8::Context>);
////////////////////////////////////////////////////////////////////////////////
/// @brief initialize foxx
////////////////////////////////////////////////////////////////////////////////
void initializeFoxx (TRI_vocbase_t*,
v8::Handle<v8::Context>);
////////////////////////////////////////////////////////////////////////////////
/// @brief add an endpoint
////////////////////////////////////////////////////////////////////////////////
bool addEndpoint (std::string const&,
std::vector<std::string> const&);
////////////////////////////////////////////////////////////////////////////////
/// @brief lookup vocbase by http request
////////////////////////////////////////////////////////////////////////////////
TRI_vocbase_t* lookupVocbaseByHttpRequest (triagens::rest::HttpRequest*);
////////////////////////////////////////////////////////////////////////////////
/// @brief authenticate a request
////////////////////////////////////////////////////////////////////////////////
rest::HttpResponse::HttpResponseCode authenticate (TRI_vocbase_t*,
triagens::rest::HttpRequest*);
////////////////////////////////////////////////////////////////////////////////
/// @brief reload auth info
////////////////////////////////////////////////////////////////////////////////
bool reloadAuthInfo (TRI_vocbase_t*);
////////////////////////////////////////////////////////////////////////////////
/// @brief get list of database names
////////////////////////////////////////////////////////////////////////////////
vector<TRI_vocbase_t*> vocbases ();
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- public static methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup ArangoDB
/// @{
////////////////////////////////////////////////////////////////////////////////
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief the single vocbase manager
////////////////////////////////////////////////////////////////////////////////
static VocbaseManager manager;
////////////////////////////////////////////////////////////////////////////////
/// @brief set the request context
////////////////////////////////////////////////////////////////////////////////
static bool setRequestContext (triagens::rest::HttpRequest*);
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- private variables
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup ArangoDB
/// @{
////////////////////////////////////////////////////////////////////////////////
private:
////////////////////////////////////////////////////////////////////////////////
/// @brief system vocbase
////////////////////////////////////////////////////////////////////////////////
TRI_vocbase_t* _vocbase;
////////////////////////////////////////////////////////////////////////////////
/// @brief user vocbase
////////////////////////////////////////////////////////////////////////////////
std::map<std::string, TRI_vocbase_t*> _vocbases;
////////////////////////////////////////////////////////////////////////////////
/// @brief endpoints to database names mapping
////////////////////////////////////////////////////////////////////////////////
std::map<std::string, std::vector<std::string> > _endpoints;
////////////////////////////////////////////////////////////////////////////////
/// @brief exclusive lock for creating new databases
////////////////////////////////////////////////////////////////////////////////
basics::Mutex _creationLock;
////////////////////////////////////////////////////////////////////////////////
/// @brief authentication lock for cache
////////////////////////////////////////////////////////////////////////////////
basics::ReadWriteLock _rwLock;
////////////////////////////////////////////////////////////////////////////////
/// @brief authentication cache
////////////////////////////////////////////////////////////////////////////////
std::map<TRI_vocbase_t*, std::map<std::string, std::string> > _authCache;
////////////////////////////////////////////////////////////////////////////////
/// @brief the javascript startup file loader (used for the version check)
////////////////////////////////////////////////////////////////////////////////
JSLoader* _startupLoader;
////////////////////////////////////////////////////////////////////////////////
/// @brief the application enpoint server
////////////////////////////////////////////////////////////////////////////////
triagens::rest::ApplicationEndpointServer* _endpointServer;
////////////////////////////////////////////////////////////////////////////////
/// @brief a vector of collections that we own and must free at the end
////////////////////////////////////////////////////////////////////////////////
struct TRI_vector_pointer_s* _freeCollections;
////////////////////////////////////////////////////////////////////////////////
/// @brief a vector of vocbases that we own and must free at the end
////////////////////////////////////////////////////////////////////////////////
std::vector<TRI_vocbase_s*> _freeVocbases;
};
}
}
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
#endif
// -----------------------------------------------------------------------------
// --SECTION-- END-OF-FILE
// -----------------------------------------------------------------------------
// Local Variables:
// mode: outline-minor
// outline-regexp: "/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|/// @page\\|// --SECTION--\\|/// @\\}"
// End: