summaryrefslogtreecommitdiffstats
path: root/storage/oqgraph/oqgraph_thunk.h
blob: 57c21e8360adc3680ee1a5c4e253f14134f1d061 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* Copyright (C) 2007-2013 Arjen G Lentz & Antony T Curtis for Open Query

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */

/* ======================================================================
   Open Query Graph Computation Engine, based on a concept by Arjen Lentz
   v3 implementation by Antony Curtis, Arjen Lentz, Andrew McDonnell
   For more information, documentation, support, enhancement engineering,
   see http://openquery.com/graph or contact graph@openquery.com
   ======================================================================
*/

#pragma once

#include <list>
#include <queue>
#include <string>
#include <utility>

#include "graphcore-config.h"

#include <boost/intrusive_ptr.hpp>
#include <boost/optional.hpp>
#include <boost/unordered_map.hpp>
#include <boost/unordered_set.hpp>
#include <boost/pending/queue.hpp>
#include <boost/ptr_container/ptr_deque.hpp>

#include "graphcore-types.h"

namespace oqgraph3
{
  typedef open_query::VertexID vertex_id;
  typedef open_query::EdgeWeight weight_t;

  typedef size_t vertices_size_type;
  typedef size_t edges_size_type;
  typedef size_t degree_size_type;

  struct graph;
  struct cursor;

  typedef boost::intrusive_ptr<graph> graph_ptr;

  struct cursor_ptr : public boost::intrusive_ptr<cursor>
  {
    cursor_ptr() : boost::intrusive_ptr<cursor>() { }
    cursor_ptr(cursor* pos) : boost::intrusive_ptr<cursor>(pos) { }

    operator const std::string&() const;
    bool operator==(const cursor_ptr&) const;
    bool operator!=(const cursor_ptr&) const;
  };

  struct edge_info
  {
    cursor_ptr _cursor;

    edge_info() : _cursor(0) { }
    explicit edge_info(const cursor_ptr& pos) : _cursor(pos) { }
    edge_info& operator=(const cursor_ptr& pos) { _cursor= pos; return *this; }

    vertex_id origid() const;
    vertex_id destid() const;
    weight_t weight() const;

    bool operator==(const edge_info&) const;
    bool operator!=(const edge_info&) const;
  };

  struct cursor
  {
    mutable int _ref_count;
    graph_ptr _graph;

    int _index;
    unsigned _parts;
    std::string _key;
    std::string _position;

    int _debugid;

    boost::optional<vertex_id> _origid;
    boost::optional<vertex_id> _destid;

    cursor(const graph_ptr& graph);
    cursor(const cursor& src);

    ~cursor();

    operator bool() const
    { return !_position.empty(); }

    operator edge_info() const
    { return edge_info(const_cast<cursor*>(this)); }

    vertex_id get_origid();
    vertex_id get_destid();
    weight_t get_weight();

    int seek_to(
        boost::optional<vertex_id> origid,
        boost::optional<vertex_id> destid);

    int seek_next();
    int seek_prev();

    void save_position();
    int restore_position();
    const std::string& record_position() const;
    void clear_position();
    int clear_position(int rc) { clear_position(); return rc; }

    bool operator==(const cursor& x) const;
    bool operator!=(const cursor& x) const;

    friend void intrusive_ptr_add_ref(cursor* ptr)
    { ++ptr->_ref_count; }
    friend void intrusive_ptr_release(cursor* ptr)
    { if (!--(ptr->_ref_count)) delete ptr; }
  };

  struct graph
  {
    mutable int _ref_count;
    cursor* _cursor;
    bool _stale;

    cursor_ptr _rnd_cursor;
    size_t _rnd_pos;

    ::TABLE* _table;
    ::Field* _source;
    ::Field* _target;
    ::Field* _weight;

    ::THD* get_table_thd();
    void set_table_thd(::THD* thd);

    graph(
        ::TABLE* table,
        ::Field* source,
        ::Field* target,
        ::Field* weight= 0);
    ~graph();

    edges_size_type num_edges() const;

    friend edges_size_type num_edges(const graph& g)
    { return g.num_edges(); }

    friend void intrusive_ptr_add_ref(graph* ptr)
    { ptr->_ref_count++; }

    friend void intrusive_ptr_release(graph* ptr)
    { ptr->_ref_count--; }
  };

}