1
0
Fork 0
This commit is contained in:
Jan Steemann 2012-08-28 13:06:04 +02:00
parent 5f6afc3609
commit ed395ab8ce
11 changed files with 176 additions and 95 deletions

View File

@ -64,16 +64,19 @@ describe ArangoDB do
context "known collection name:" do
before do
@cn = "UnitTestsCollectionEdge"
@cid = ArangoDB.create_collection(@cn, true, 3) # type 3 = edge collection
@ce = "UnitTestsCollectionEdge"
@eid = ArangoDB.create_collection(@ce, true, 3) # type 3 = edge collection
@cv = "UnitTestsCollectionVertex"
@vid = ArangoDB.create_collection(@cv, true, 2) # type 2 = document collection
end
after do
ArangoDB.drop_collection(@cn)
ArangoDB.drop_collection(@ce)
ArangoDB.drop_collection(@cv)
end
it "creating an edge" do
cmd = "/_api/document?collection=#{@cid}"
cmd = "/_api/document?collection=#{@vid}"
# create first vertex
body = "{ \"a\" : 1 }"
@ -96,7 +99,7 @@ describe ArangoDB do
id2 = doc.parsed_response['_id']
# create edge
cmd = "/_api/edge?collection=#{@cid}&from=#{id1}&to=#{id2}"
cmd = "/_api/edge?collection=#{@eid}&from=#{id1}&to=#{id2}"
body = "{}"
doc = ArangoDB.log_post("#{prefix}-create-edge", cmd, :body => body)
@ -117,7 +120,7 @@ describe ArangoDB do
doc.headers['content-type'].should eq("application/json; charset=utf-8")
# create another edge
cmd = "/_api/edge?collection=#{@cid}&from=#{id1}&to=#{id2}"
cmd = "/_api/edge?collection=#{@eid}&from=#{id1}&to=#{id2}"
body = "{ \"e\" : 1 }"
doc = ArangoDB.log_post("#{prefix}-create-edge", cmd, :body => body)
@ -139,7 +142,7 @@ describe ArangoDB do
doc.headers['content-type'].should eq("application/json; charset=utf-8")
# create third edge
cmd = "/_api/edge?collection=#{@cid}&from=#{id2}&to=#{id1}"
cmd = "/_api/edge?collection=#{@eid}&from=#{id2}&to=#{id1}"
body = "{ \"e\" : 2 }"
doc = ArangoDB.log_post("#{prefix}-create-edge", cmd, :body => body)
@ -161,7 +164,7 @@ describe ArangoDB do
doc.headers['content-type'].should eq("application/json; charset=utf-8")
# check ANY edges
cmd = "/_api/edges/#{@cid}?vertex=#{id1}"
cmd = "/_api/edges/#{@eid}?vertex=#{id1}"
doc = ArangoDB.log_get("#{prefix}-read-edges-any", cmd);
doc.code.should eq(200)
@ -169,7 +172,7 @@ describe ArangoDB do
doc.parsed_response['edges'].length.should be(3)
# check IN edges
cmd = "/_api/edges/#{@cid}?vertex=#{id1}&direction=in"
cmd = "/_api/edges/#{@eid}?vertex=#{id1}&direction=in"
doc = ArangoDB.log_get("#{prefix}-read-edges-in", cmd);
doc.code.should eq(200)
@ -177,7 +180,7 @@ describe ArangoDB do
doc.parsed_response['edges'].length.should be(1)
# check OUT edges
cmd = "/_api/edges/#{@cid}?vertex=#{id1}&direction=out"
cmd = "/_api/edges/#{@eid}?vertex=#{id1}&direction=out"
doc = ArangoDB.log_get("#{prefix}-read-edges-out", cmd);
doc.code.should eq(200)

View File

@ -264,10 +264,9 @@ bool RestDocumentHandler::createDocument () {
}
// find and load collection given by name or identifier
int res = useCollection(collection, create);
int res = useCollection(collection, getCollectionType(), create);
if (res != TRI_ERROR_NO_ERROR) {
releaseCollection();
return false;
}
@ -287,7 +286,7 @@ bool RestDocumentHandler::createDocument () {
// outside write transaction
// .............................................................................
// release collection and free json
// release collection
releaseCollection();
// generate result
@ -408,11 +407,10 @@ bool RestDocumentHandler::readSingleDocument (bool generateBody) {
string collection = suffix[0];
string did = suffix[1];
// find and load collection given by name oder identifier
int res = useCollection(collection);
// find and load collection given by name or identifier
int res = useCollection(collection, getCollectionType());
if (res != TRI_ERROR_NO_ERROR) {
releaseCollection();
return false;
}
@ -497,16 +495,14 @@ bool RestDocumentHandler::readSingleDocument (bool generateBody) {
////////////////////////////////////////////////////////////////////////////////
bool RestDocumentHandler::readAllDocuments () {
// extract the cid
bool found;
string collection = _request->value("collection", found);
// find and load collection given by name oder identifier
int res = useCollection(collection);
// find and load collection given by name or identifier
int res = useCollection(collection, getCollectionType());
if (res != TRI_ERROR_NO_ERROR) {
releaseCollection();
return false;
}
@ -753,11 +749,10 @@ bool RestDocumentHandler::modifyDocument (bool isPatch) {
// extract or chose the update policy
TRI_doc_update_policy_e policy = extractUpdatePolicy();
// find and load collection given by name oder identifier
int res = useCollection(collection);
// find and load collection given by name or identifier
int res = useCollection(collection, getCollectionType());
if (res != TRI_ERROR_NO_ERROR) {
releaseCollection();
return false;
}
@ -934,11 +929,10 @@ bool RestDocumentHandler::deleteDocument () {
return false;
}
// find and load collection given by name oder identifier
int res = useCollection(collection);
// find and load collection given by name or identifier
int res = useCollection(collection, getCollectionType());
if (res != TRI_ERROR_NO_ERROR) {
releaseCollection();
return false;
}

View File

@ -117,6 +117,14 @@ namespace triagens {
protected:
////////////////////////////////////////////////////////////////////////////////
/// @brief get collection type
////////////////////////////////////////////////////////////////////////////////
virtual TRI_col_type_e getCollectionType () const {
return TRI_COL_TYPE_SIMPLE_DOCUMENT;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief creates a document
////////////////////////////////////////////////////////////////////////////////
@ -170,6 +178,7 @@ namespace triagens {
////////////////////////////////////////////////////////////////////////////////
virtual bool checkDocument ();
};
}
}

View File

@ -159,10 +159,9 @@ bool RestEdgeHandler::createDocument () {
}
// find and load collection given by name or identifier
int res = useCollection(collection, create);
int res = useCollection(collection, getCollectionType(), create);
if (res != TRI_ERROR_NO_ERROR) {
releaseCollection();
return false;
}

View File

@ -77,6 +77,29 @@ namespace triagens {
// --SECTION-- protected methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup ArangoDB
/// @{
////////////////////////////////////////////////////////////////////////////////
protected:
////////////////////////////////////////////////////////////////////////////////
/// @brief get collection type
////////////////////////////////////////////////////////////////////////////////
virtual TRI_col_type_e getCollectionType () const {
return TRI_COL_TYPE_SIMPLE_EDGE;
}
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- private methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup ArangoDB
/// @{

View File

@ -199,11 +199,9 @@ bool RestImportHandler::createByArray () {
bool reuseId = found ? StringUtils::boolean(valueStr) : false;
// find and load collection given by name or identifier
int res = useCollection(collection, create);
int res = useCollection(collection, TRI_COL_TYPE_SIMPLE_DOCUMENT, create);
if (res != TRI_ERROR_NO_ERROR) {
releaseCollection();
// error is already generated by useCollection!
return false;
}
@ -381,11 +379,9 @@ bool RestImportHandler::createByList () {
}
// find and load collection given by name or identifier
int res = useCollection(collection, create);
int res = useCollection(collection, TRI_COL_TYPE_SIMPLE_DOCUMENT, create);
if (res != TRI_ERROR_NO_ERROR) {
releaseCollection();
if (keys) {
TRI_FreeJson(TRI_UNKNOWN_MEM_ZONE, keys);
}

View File

@ -492,48 +492,6 @@ TRI_doc_update_policy_e RestVocbaseBaseHandler::extractUpdatePolicy () {
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief uses a collection, loading or manifesting and locking it
////////////////////////////////////////////////////////////////////////////////
int RestVocbaseBaseHandler::useCollection (string const& name, bool create) {
_collection = 0;
_documentCollection = 0;
// sanity check
if (name.empty()) {
generateError(HttpResponse::BAD,
TRI_ERROR_HTTP_CORRUPTED_JSON,
"collection identifier is empty");
return TRI_set_errno(TRI_ERROR_HTTP_CORRUPTED_JSON);
}
// try to find the collection
if (isdigit(name[0])) {
TRI_voc_cid_t id = StringUtils::uint64(name);
_collection = TRI_LookupCollectionByIdVocBase(_vocbase, id);
}
else {
_collection = TRI_FindCollectionByNameVocBase(_vocbase, name.c_str(), create);
}
if (_collection == 0) {
generateCollectionNotFound(name);
return TRI_set_errno(TRI_ERROR_ARANGO_COLLECTION_NOT_FOUND);
}
// and use the collection
int res = TRI_UseCollectionVocBase(_vocbase, const_cast<TRI_vocbase_col_s*>(_collection));
if (res == TRI_ERROR_NO_ERROR) {
_documentCollection = _collection->_collection;
assert(_documentCollection != 0);
}
return res;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief releases a collection
////////////////////////////////////////////////////////////////////////////////
@ -646,6 +604,57 @@ int RestVocbaseBaseHandler::parseDocumentId (string const& handle,
return TRI_errno();
}
////////////////////////////////////////////////////////////////////////////////
/// @brief uses a collection, loading or manifesting and locking it
////////////////////////////////////////////////////////////////////////////////
int RestVocbaseBaseHandler::useCollection (string const& name,
TRI_col_type_e type,
bool create) {
_collection = 0;
_documentCollection = 0;
// sanity check
if (name.empty()) {
generateError(HttpResponse::BAD,
TRI_ERROR_HTTP_CORRUPTED_JSON,
"collection identifier is empty");
return TRI_set_errno(TRI_ERROR_HTTP_CORRUPTED_JSON);
}
// try to find the collection
if (isdigit(name[0])) {
TRI_voc_cid_t id = StringUtils::uint64(name);
_collection = TRI_LookupCollectionByIdVocBase(_vocbase, id);
}
else {
if (type == TRI_COL_TYPE_SIMPLE_DOCUMENT) {
_collection = TRI_FindDocumentCollectionByNameVocBase(_vocbase, name.c_str(), create);
}
else if (type == TRI_COL_TYPE_SIMPLE_EDGE) {
_collection = TRI_FindEdgeCollectionByNameVocBase(_vocbase, name.c_str(), create);
}
}
if (_collection == 0) {
generateCollectionNotFound(name);
return TRI_set_errno(TRI_ERROR_ARANGO_COLLECTION_NOT_FOUND);
}
// and use the collection
int res = TRI_UseCollectionVocBase(_vocbase, const_cast<TRI_vocbase_col_s*>(_collection));
if (res == TRI_ERROR_NO_ERROR) {
assert(_collection != 0);
_documentCollection = _collection->_collection;
assert(_documentCollection != 0);
}
return res;
}
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////

View File

@ -252,10 +252,10 @@ namespace triagens {
TRI_doc_update_policy_e extractUpdatePolicy ();
////////////////////////////////////////////////////////////////////////////////
/// @brief creates or loads a collection
/// @brief creates or loads an edge collection
////////////////////////////////////////////////////////////////////////////////
int useCollection (string const& name, bool create = false);
int useEdgeCollection (string const& name, bool create = false);
////////////////////////////////////////////////////////////////////////////////
/// @brief releases a collection
@ -283,6 +283,12 @@ namespace triagens {
TRI_voc_cid_t& cid,
TRI_voc_did_t& did);
////////////////////////////////////////////////////////////////////////////////
/// @brief creates or loads a collection
////////////////////////////////////////////////////////////////////////////////
int useCollection (string const& name, TRI_col_type_e type, bool create = false);
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////

View File

@ -69,7 +69,7 @@ static TRI_doc_mptr_t CreateJson (TRI_doc_collection_t* collection,
TRI_doc_mptr_t result;
TRI_voc_did_t did = 0;
TRI_voc_rid_t rid = 0;
shaped = TRI_ShapedJsonJson(collection->_shaper, json);
if (shaped == 0) {

View File

@ -641,7 +641,9 @@ static int ScanPath (TRI_vocbase_t* vocbase, char const* path) {
/// @brief bears a new collection or returns an existing one by name
////////////////////////////////////////////////////////////////////////////////
static TRI_vocbase_col_t* BearCollectionVocBase (TRI_vocbase_t* vocbase, char const* name) {
static TRI_vocbase_col_t* BearCollectionVocBase (TRI_vocbase_t* vocbase,
char const* name,
TRI_col_type_e type) {
union { void const* v; TRI_vocbase_col_t* c; } found;
TRI_vocbase_col_t* collection;
TRI_col_parameter_t parameter;
@ -685,7 +687,7 @@ static TRI_vocbase_col_t* BearCollectionVocBase (TRI_vocbase_t* vocbase, char co
}
// create a new collection
collection = AddCollection(vocbase, TRI_COL_TYPE_SIMPLE_DOCUMENT, name, TRI_NewTickVocBase(), NULL);
collection = AddCollection(vocbase, type, name, TRI_NewTickVocBase(), NULL);
if (collection == NULL) {
TRI_WRITE_UNLOCK_COLLECTIONS_VOCBASE(vocbase);
@ -781,6 +783,33 @@ static int ManifestCollectionVocBase (TRI_vocbase_t* vocbase, TRI_vocbase_col_t*
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief finds a collection by name or creates it
////////////////////////////////////////////////////////////////////////////////
static TRI_vocbase_col_t* FindCollectionByNameVocBase (TRI_vocbase_t* vocbase,
char const* name,
bool bear,
TRI_col_type_e type) {
union { void const* v; TRI_vocbase_col_t* c; } found;
TRI_READ_LOCK_COLLECTIONS_VOCBASE(vocbase);
found.v = TRI_LookupByKeyAssociativePointer(&vocbase->_collectionsByName, name);
TRI_READ_UNLOCK_COLLECTIONS_VOCBASE(vocbase);
if (found.v != NULL) {
return found.c;
}
if (! bear) {
TRI_set_errno(TRI_ERROR_ARANGO_COLLECTION_NOT_FOUND);
return NULL;
}
return BearCollectionVocBase(vocbase, name, type);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief loads an existing (document) collection
///
@ -1320,26 +1349,27 @@ TRI_vocbase_col_t* TRI_LookupCollectionByIdVocBase (TRI_vocbase_t* vocbase, TRI_
}
////////////////////////////////////////////////////////////////////////////////
/// @brief finds a (document) collection by name
/// @brief finds a collection by name
////////////////////////////////////////////////////////////////////////////////
TRI_vocbase_col_t* TRI_FindCollectionByNameVocBase (TRI_vocbase_t* vocbase, char const* name, bool bear) {
union { void const* v; TRI_vocbase_col_t* c; } found;
return TRI_FindDocumentCollectionByNameVocBase(vocbase, name, bear);
}
TRI_READ_LOCK_COLLECTIONS_VOCBASE(vocbase);
found.v = TRI_LookupByKeyAssociativePointer(&vocbase->_collectionsByName, name);
TRI_READ_UNLOCK_COLLECTIONS_VOCBASE(vocbase);
////////////////////////////////////////////////////////////////////////////////
/// @brief finds a document collection by name
////////////////////////////////////////////////////////////////////////////////
if (found.v != NULL) {
return found.c;
}
TRI_vocbase_col_t* TRI_FindDocumentCollectionByNameVocBase (TRI_vocbase_t* vocbase, char const* name, bool bear) {
return FindCollectionByNameVocBase(vocbase, name, bear, TRI_COL_TYPE_SIMPLE_DOCUMENT);
}
if (! bear) {
TRI_set_errno(TRI_ERROR_ARANGO_COLLECTION_NOT_FOUND);
return NULL;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief finds an edge collection by name
////////////////////////////////////////////////////////////////////////////////
return BearCollectionVocBase(vocbase, name);
TRI_vocbase_col_t* TRI_FindEdgeCollectionByNameVocBase (TRI_vocbase_t* vocbase, char const* name, bool bear) {
return FindCollectionByNameVocBase(vocbase, name, bear, TRI_COL_TYPE_SIMPLE_EDGE);
}
////////////////////////////////////////////////////////////////////////////////

View File

@ -494,11 +494,23 @@ TRI_vocbase_col_t* TRI_LookupCollectionByNameVocBase (TRI_vocbase_t*, char const
TRI_vocbase_col_t* TRI_LookupCollectionByIdVocBase (TRI_vocbase_t*, TRI_voc_cid_t);
////////////////////////////////////////////////////////////////////////////////
/// @brief finds a (document) collection by name
/// @brief finds a collection by name
////////////////////////////////////////////////////////////////////////////////
TRI_vocbase_col_t* TRI_FindCollectionByNameVocBase (TRI_vocbase_t*, char const*, bool bear);
////////////////////////////////////////////////////////////////////////////////
/// @brief finds a document collection by name
////////////////////////////////////////////////////////////////////////////////
TRI_vocbase_col_t* TRI_FindDocumentCollectionByNameVocBase (TRI_vocbase_t*, char const*, bool bear);
////////////////////////////////////////////////////////////////////////////////
/// @brief finds an edge collection by name
////////////////////////////////////////////////////////////////////////////////
TRI_vocbase_col_t* TRI_FindEdgeCollectionByNameVocBase (TRI_vocbase_t*, char const*, bool bear);
////////////////////////////////////////////////////////////////////////////////
/// @brief creates a new (document) collection from parameter set
////////////////////////////////////////////////////////////////////////////////