<commit_before>////////////////////////////////////////////////////////////////////////////////
/// @brief V8 Traverser
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2014-2015 ArangoDB GmbH, Cologne, Germany
/// Copyright 2004-2014 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 ArangoDB GmbH, Cologne, Germany
///
/// @author <NAME>
/// @author Copyright 2014-2015, ArangoDB GmbH, Cologne, Germany
/// @author Copyright 2012-2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////

#include "v8.h"
#include "V8/v8-conv.h"
#include "V8/v8-utils.h"
#include "V8Server/v8-vocbaseprivate.h"
#include "V8Server/v8-wrapshapedjson.h"
#include "V8Server/v8-vocindex.h"
#include "V8Server/v8-collection.h"
#include "Utils/transactions.h"
#include "Utils/V8ResolverGuard.h"
#include "Utils/CollectionNameResolver.h"
#include "Traverser.h"
#include "VocBase/key-generator.h"

using namespace std;
using namespace triagens::basics;
using namespace triagens::arango;

class SimpleEdgeExpander {

  private:
    TRI_edge_direction_e forwardDirection;
    TRI_edge_direction_e backwardDirection;
    TRI_document_collection_t* edgeCollection;
    CollectionNameResolver const* resolver;

  public: 
    void operator() ( Traverser::VertexId source,
                      Traverser::Direction dir,
                      vector<Traverser::Neighbor>& result
                    ) {
      std::vector<TRI_doc_mptr_copy_t> edges;
      // Process Vertex Id!
      size_t split;
      char const* str = source.c_str();
      if (!TRI_ValidateDocumentIdKeyGenerator(str, &split)) {
        // TODO Error Handling
        return;
      }
      std::unique_ptr<char[]>& key;
      string& collectionName = string(str, split);
      auto const length = str->length() - split - 1;
      auto buffer = new char[length + 1];
      memcpy(buffer, *str + split + 1, length);
      buffer[length] = '\0';
      key.reset(buffer);

      auto col = resolver->getCollectionStruct(collectionName);
      if (col == nullptr) {
        // collection not found
        throw TRI_ERROR_ARANGO_COLLECTION_NOT_FOUND;
      }
      auto collectionCId = col->_cid;
      if (dir == Traverser::FORWARD) {
        edges = TRI_LookupEdgesDocumentCollection(edgeCollection, forwardDirection, collectionCId, key.get());
      } else {
        edges = TRI_LookupEdgesDocumentCollection(edgeCollection, backwardDirection, collectionCId, key.get());
      }

      // TODO Build result
    }
    SimpleEdgeExpander(
      TRI_edge_direction_e edgeDirection,
      TRI_document_collection_t* edgeCollection,
      CollectionNameResolver const* resolver
    ) : 
      edgeCollection(edgeCollection),
      resolver(resolver)
    {
      if (edgeDirection == TRI_EDGE_OUT) {
        forwardDirection = TRI_EDGE_OUT;
        backwardDirection = TRI_EDGE_IN;
      } else if (edgeDirection == TRI_EDGE_IN) {
        forwardDirection = TRI_EDGE_IN;
        backwardDirection = TRI_EDGE_OUT;
      } else {
        forwardDirection = TRI_EDGE_ANY;
        backwardDirection = TRI_EDGE_ANY;
      }
    };
}


struct LocalCollectionGuard {
  LocalCollectionGuard (TRI_vocbase_col_t* collection)
    : _collection(collection) {
  }
<commit_msg>Further implementation of simple expander. Also included Path -> JSON converter.<commit_after>////////////////////////////////////////////////////////////////////////////////
/// @brief V8 Traverser
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2014-2015 ArangoDB GmbH, Cologne, Germany
/// Copyright 2004-2014 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 ArangoDB GmbH, Cologne, Germany
///
/// @author <NAME>
/// @author Copyright 2014-2015, ArangoDB GmbH, Cologne, Germany
/// @author Copyright 2012-2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////

#include "v8.h"
#include "V8/v8-conv.h"
#include "V8/v8-utils.h"
#include "V8Server/v8-vocbaseprivate.h"
#include "V8Server/v8-wrapshapedjson.h"
#include "V8Server/v8-vocindex.h"
#include "V8Server/v8-collection.h"
#include "Utils/transactions.h"
#include "Utils/V8ResolverGuard.h"
#include "Utils/CollectionNameResolver.h"
#include "VocBase/document-collection.h"
#include "Traverser.h"
#include "VocBase/key-generator.h"

using namespace std;
using namespace triagens::basics;
using namespace triagens::arango;

class SimpleEdgeExpander {

  private:
    TRI_edge_direction_e forwardDirection;
    TRI_edge_direction_e backwardDirection;
    TRI_document_collection_t* edgeCollection;
    string edgeIdPrefix;
    CollectionNameResolver const* resolver;
    bool usesDist;

  public: 

    Traverser::VertexId extractFromId(TRI_doc_mptr_copy_t ptr) {
      char const* key = TRI_EXTRACT_MARKER_FROM_KEY(ptr);
      TRI_voc_cid_t cid = TRI_EXTRACT_MARKER_FROM_CID(ptr);
      string col = resolver->getCollectionName(cid);
      col.append("/");
      col.append(key);
      return col;
    };

    Traverser::VertexId extractToId(TRI_doc_mptr_copy_t ptr) {
      char const* key = TRI_EXTRACT_MARKER_TO_KEY(ptr);
      TRI_voc_cid_t cid = TRI_EXTRACT_MARKER_TO_CID(ptr);
      string col = resolver->getCollectionName(cid);
      col.append("/");
      col.append(key);
      return col;
    };

    Traverser::EdgeId extractEdgeId(TRI_doc_mptr_copy_t ptr) {
      char const* key = TRI_EXTRACT_MARKER_KEY(ptr);
      // AppendAndCopy
      return edgeIdPrefix.append(key);
    };

    void operator() ( Traverser::VertexId source,
                      Traverser::Direction dir,
                      vector<Traverser::Neighbor>& result
                    ) {
      std::vector<TRI_doc_mptr_copy_t> edges;
      // Process Vertex Id!
      size_t split;
      char const* str = source.c_str();
      if (!TRI_ValidateDocumentIdKeyGenerator(str, &split)) {
        // TODO Error Handling
        return;
      }
      string collectionName = string(str, split);
      auto const length = strlen(str) - split - 1;
      auto buffer = new char[length + 1];
      memcpy(buffer, str + split + 1, length);
      buffer[length] = '\0';

      auto col = resolver->getCollectionStruct(collectionName);
      if (col == nullptr) {
        // collection not found
        throw TRI_ERROR_ARANGO_COLLECTION_NOT_FOUND;
      }
      auto collectionCId = col->_cid;
      if (dir == Traverser::FORWARD) {
        edges = TRI_LookupEdgesDocumentCollection(edgeCollection, forwardDirection, collectionCId, buffer);
      } else {
        edges = TRI_LookupEdgesDocumentCollection(edgeCollection, backwardDirection, collectionCId, buffer);
      }
      std::unordered_map<Traverser::VertexId, Traverser::Neighbor> candidates;
      Traverser::VertexId from;
      Traverser::VertexId to;
      std::unordered_map<Traverser::VertexId, Traverser::Neighbor>::const_iterator cand;
      if (usesDist) {
        for (size_t j = 0;  j < edges.size();  ++j) {
          from = extractFromId(edges[j]);
          to = extractFromId(edges[j]);
          if (from != source) {
            candidates.find(from);
            if (cand == candidates.end()) {
              // Add weight
              candidates.emplace(from, Traverser::Neighbor(from, edges[j]._id, 1));
            } else {
              // Compare weight
            }
          } else if (to != source) {
            candidates.find(to);
            if (cand == candidates.end()) {
              // Add weight
              candidates.emplace(to, Traverser::Neighbor(to, edges[j]._id, 1));
            } else {
              // Compare weight
            }
          }
        }
      } else {
        for (size_t j = 0;  j < edges.size();  ++j) {
          from = extractFromId(edges[j]);
          to = extractFromId(edges[j]);
          if (from != source) {
            candidates.find(from);
            if (cand == candidates.end()) {
              candidates.emplace(from, Traverser::Neighbor(from, edges[j]._id, 1));
            }
          } else if (to != source) {
            candidates.find(to);
            if (cand == candidates.end()) {
              candidates.emplace(to, Traverser::Neighbor(to, edges[j]._id, 1));
            }
          }
        }
      }
      for (auto it = candidates.begin(); it != candidates.end(); ++it) {
        result.push_back(it->second);
      }
    }
    SimpleEdgeExpander(
      TRI_edge_direction_e edgeDirection,
      TRI_document_collection_t* edgeCollection,
      CollectionNameResolver const* resolver
    ) : 
      edgeCollection(edgeCollection),
      resolver(resolver)
    {
      usesDist = false;
      if (edgeDirection == TRI_EDGE_OUT) {
        forwardDirection = TRI_EDGE_OUT;
        backwardDirection = TRI_EDGE_IN;
      } else if (edgeDirection == TRI_EDGE_IN) {
        forwardDirection = TRI_EDGE_IN;
        backwardDirection = TRI_EDGE_OUT;
      } else {
        forwardDirection = TRI_EDGE_ANY;
        backwardDirection = TRI_EDGE_ANY;
      }
    };
};

static <v8::Handle<v8::Value> pathToV8(v8::Isolate* isolate, Traverser::Path p) {
  v8::Handle<v8::Value> result;

  uint32_t const vn = static_cast<uint32_t>(p.vertices.size());
  v8::Handle<v8::Array> vertices = v8::Array::New(isolate, static_cast<int>(vn));

  for (size_t j = 0;  j < vn;  ++j) {
    vertices->Set(static_cast<uint32_t>(j), TRI_V8_ASCII_STRING(p.vertices[j]));
  }
  result->Set("vertices", vertices);

  uint32_t const en = static_cast<uint32_t>(p.edges.size());
  v8::Handle<v8::Array> edges = v8::Array::New(isolate, static_cast<int>(en));

  for (size_t j = 0;  j < en;  ++j) {
    edges->Set(static_cast<uint32_t>(j), TRI_V8_ASCII_STRING(p.edges[j]));
  }
  result->Set("edges", edges);

  return result;
};

struct LocalCollectionGuard {
  LocalCollectionGuard (TRI_vocbase_col_t* collection)
    : _collection(collection) {
  }

{'fraction_non_alphanumeric': 0.14558689717925385, 'fraction_numerical': 0.013102820746132848, 'mean_word_length': 2.5284109149277687, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '905506', 'n_tokens_mistral': 3471, 'n_tokens_neox': 3331, 'n_words': 768}