1/**
2 * @copyright 2013 Couchbase, Inc.
3 *
4 * @author Filipe Manana  <filipe@couchbase.com>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7 * use this file except in compliance with the License. You may obtain a copy of
8 * the License at
9 *
10 *  http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 * License for the specific language governing permissions and limitations under
16 * the License.
17 **/
18
19/**
20 * This is a private header, do not include it in other applications/lirbaries.
21 **/
22
23#ifndef _MAPREDUCE_INTERNAL_H
24#define _MAPREDUCE_INTERNAL_H
25
26#include "mapreduce.h"
27#include <atomic>
28#include <iostream>
29#include <list>
30#include <mutex>
31#include <platform/cb_malloc.h>
32#include <stdint.h>
33#include <stdlib.h>
34#include <string>
35#include <string.h>
36#include <time.h>
37#include <vector>
38#include <v8.h>
39
40
41class MapReduceError;
42
43typedef std::list<mapreduce_json_t>                    json_results_list_t;
44typedef std::list<mapreduce_kv_t>                      kv_list_int_t;
45typedef std::vector< v8::Persistent<v8::Function>* >   function_vector_t;
46
47typedef struct {
48    v8::Persistent<v8::Context> jsContext;
49    v8::Isolate                 *isolate;
50    function_vector_t           *functions;
51    kv_list_int_t               *kvs;
52    std::atomic<time_t>         taskStartTime;
53    std::mutex                  exitMutex;
54} mapreduce_ctx_t;
55
56
57void initContext(mapreduce_ctx_t *ctx,
58                 const std::list<std::string> &function_sources);
59
60void destroyContext(mapreduce_ctx_t *ctx);
61
62void mapDoc(mapreduce_ctx_t *ctx,
63            const mapreduce_json_t &doc,
64            const mapreduce_json_t &meta,
65            mapreduce_map_result_list_t *result);
66
67json_results_list_t runReduce(mapreduce_ctx_t *ctx,
68                              const mapreduce_json_list_t &keys,
69                              const mapreduce_json_list_t &values);
70
71mapreduce_json_t runReduce(mapreduce_ctx_t *ctx,
72                           int reduceFunNum,
73                           const mapreduce_json_list_t &keys,
74                           const mapreduce_json_list_t &values);
75
76mapreduce_json_t runRereduce(mapreduce_ctx_t *ctx,
77                             int reduceFunNum,
78                             const mapreduce_json_list_t &reductions);
79
80void terminateTask(mapreduce_ctx_t *ctx);
81
82
83
84class MapReduceError {
85public:
86    MapReduceError(const mapreduce_error_t error, const char *msg)
87        : _error(error), _msg(msg) {
88    }
89
90    MapReduceError(const mapreduce_error_t error, const std::string &msg)
91        : _error(error), _msg(msg) {
92    }
93
94    mapreduce_error_t getError() const {
95        return _error;
96    }
97
98    const std::string& getMsg() const {
99        return _msg;
100    }
101
102private:
103    const mapreduce_error_t _error;
104    const std::string _msg;
105};
106
107#endif
108