예제 #1
0
arangodb::aql::Graph* arangodb::lookupGraphByName(TRI_vocbase_t* vocbase,
                                                  std::string const& name) {
  SingleCollectionTransaction trx(StandaloneTransactionContext::Create(vocbase),
                                          GRAPHS, TRI_TRANSACTION_READ);

  int res = trx.begin();

  if (res != TRI_ERROR_NO_ERROR) {
    THROW_ARANGO_EXCEPTION_FORMAT(res, "while looking up graph '%s'",
                                  name.c_str());
  }
  VPackBuilder b;
  {
    VPackObjectBuilder guard(&b);
    b.add(StaticStrings::KeyString, VPackValue(name));
  }

  // Default options are enough here
  OperationOptions options;

  OperationResult result = trx.document(GRAPHS, b.slice(), options);

  // Commit or abort.
  res = trx.finish(result.code);

  if (!result.successful()) {
    THROW_ARANGO_EXCEPTION_FORMAT(result.code, "while looking up graph '%s'",
                                  name.c_str());
  }
  if (res != TRI_ERROR_NO_ERROR) {
    THROW_ARANGO_EXCEPTION_FORMAT(res, "while looking up graph '%s'",
                                  name.c_str());
  }

  return new arangodb::aql::Graph(result.slice());
}
예제 #2
0
int Syncer::applyCollectionDumpMarker(
    arangodb::Transaction& trx, std::string const& collectionName,
    TRI_replication_operation_e type, VPackSlice const& old, 
    VPackSlice const& slice, std::string& errorMsg) {

  int res = TRI_ERROR_INTERNAL;

  if (type == REPLICATION_MARKER_DOCUMENT) {
    // {"type":2400,"key":"230274209405676","data":{"_key":"230274209405676","_rev":"230274209405676","foo":"bar"}}

    OperationOptions options;
    options.silent = true;
    options.ignoreRevs = true;
    options.isRestore = true;

    try {
      // try insert first
      OperationResult opRes = trx.insert(collectionName, slice, options); 

      if (opRes.code == TRI_ERROR_ARANGO_UNIQUE_CONSTRAINT_VIOLATED) {
        // perform an update
        opRes = trx.replace(collectionName, slice, options); 
      }
    
      res = opRes.code;
    } catch (arangodb::basics::Exception const& ex) {
      res = ex.code();
    } catch (...) {
      res = TRI_ERROR_INTERNAL;
    }

    if (res != TRI_ERROR_NO_ERROR) {
      errorMsg = "document insert/replace operation failed: " +
                 std::string(TRI_errno_string(res));
    }

    return res;
  }

  else if (type == REPLICATION_MARKER_REMOVE) {
    // {"type":2402,"key":"592063"}
    
    try {
      OperationOptions options;
      options.silent = true;
      options.ignoreRevs = true;
      OperationResult opRes = trx.remove(collectionName, old, options);

      if (opRes.successful() ||
          opRes.code == TRI_ERROR_ARANGO_DOCUMENT_NOT_FOUND) {
        // ignore document not found errors
        return TRI_ERROR_NO_ERROR;
      }

      res = opRes.code;
    } catch (arangodb::basics::Exception const& ex) {
      res = ex.code();
    } catch (...) {
      res = TRI_ERROR_INTERNAL;
    }
    
    if (res != TRI_ERROR_NO_ERROR) {
      errorMsg = "document remove operation failed: " +
                 std::string(TRI_errno_string(res));
    }

    return res;
  }
    
  res = TRI_ERROR_REPLICATION_UNEXPECTED_MARKER;
  errorMsg = "unexpected marker type " + StringUtils::itoa(type);

  return res;
}