1 /* Copyright (C) 2007-2013 Arjen G Lentz & Antony T Curtis for Open Query
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License as published by
5    the Free Software Foundation; version 2 of the License, or
6    (at your option) any later version.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
16 
17 /* ======================================================================
18    Open Query Graph Computation Engine, based on a concept by Arjen Lentz
19    v3 implementation by Antony Curtis, Arjen Lentz, Andrew McDonnell
20    For more information, documentation, support, enhancement engineering,
21    see http://openquery.com/graph or contact graph@openquery.com
22    ======================================================================
23 */
24 
25 #ifndef oq_graphcore_h_
26 #define oq_graphcore_h_
27 
28 /* #define GRAPHCORE_INTERNAL __attribute__((visibility("hidden"))) */
29 #define GRAPHCORE_INTERNAL
30 
31 #include "graphcore-types.h"
32 
33 namespace open_query
34 {
35   class oqgraph_share;
36   class oqgraph_cursor;
37 
38   struct row
39   {
40     bool latch_indicator;
41     bool orig_indicator;
42     bool dest_indicator;
43     bool weight_indicator;
44     bool seq_indicator;
45     bool link_indicator;
46 
47     int latch;
48     const char* latchStringValue; // workaround for when latch is a Varchar
49     int latchStringValueLen;
50     VertexID orig;
51     VertexID dest;
52     EdgeWeight weight;
53     unsigned seq;
54     VertexID link;
55   };
56 
57   class oqgraph
58   {
59     oqgraph_share *const share;
60     oqgraph_cursor *cursor;
61     row row_info;
62 
63     inline oqgraph(oqgraph_share*) throw();
64     inline ~oqgraph() throw();
65   public:
66 
67     // Integer operation flags
68     enum {
69       NO_SEARCH = 0,
70       DIJKSTRAS = 1,
71       BREADTH_FIRST = 2,
72       NUM_SEARCH_OP = 3,
73       LEAVES = 4,
74 
75       ALGORITHM = 0x0ffff,
76       HAVE_ORIG = 0x10000,
77       HAVE_DEST = 0x20000,
78       };
79 
80     enum error_code
81     {
82       OK= 0,
83       NO_MORE_DATA,
84       EDGE_NOT_FOUND,
85       INVALID_WEIGHT,
86       DUPLICATE_EDGE,
87       CANNOT_ADD_VERTEX,
88       CANNOT_ADD_EDGE,
89       MISC_FAIL
90     };
91 
92     struct current_row_st {};
current_row()93     static inline current_row_st current_row()
94     { return current_row_st(); }
95 
96     unsigned vertices_count() const throw();
97     unsigned edges_count() const throw();
98 
99     int delete_all(void) throw();
100 
101     int insert_edge(VertexID, VertexID, EdgeWeight, bool=0) throw();
102     int modify_edge(VertexID, VertexID, EdgeWeight) throw();
103     int delete_edge(VertexID, VertexID) throw();
104 
105     int modify_edge(current_row_st,
106                     VertexID*, VertexID*, EdgeWeight*, bool=0) throw();
107     int delete_edge(current_row_st) throw();
108 
replace_edge(VertexID orig,VertexID dest,EdgeWeight weight)109     int replace_edge(VertexID orig, VertexID dest, EdgeWeight weight) throw()
110     { return insert_edge(orig, dest, weight, true); }
111 
112     // Update the retained latch string value, for later retrieval by
113     // fetch_row() as a workaround for making sure we return the correct
114     // string to match the latch='' clause
115     // (This is a hack for mariadb mysql compatibility)
116     // IT SHOULD ONLY BE CALLED IMMEIDATELY BEFORE search)(
117     void retainLatchFieldValue(const char *retainedLatch);
118 
119     int search(int*, VertexID*, VertexID*) throw();
120     int random(bool) throw();
121 
122     int fetch_row(row&) throw();
123     int fetch_row(row&, const void*) throw();
124     void row_ref(void*) throw();
125     void init_row_ref(void*) throw();
126 
127     static oqgraph* create(oqgraph_share*) throw();
128     static oqgraph_share *create(TABLE*,Field*,Field*,Field*) throw();
129 
130     THD* get_thd();
131     void set_thd(THD*);
132 
133     static void free(oqgraph*) throw();
134     static void free(oqgraph_share*) throw();
135 
136     void release_cursor() throw();
137 
138     static const size_t sizeof_ref;
139   private:
140     char *lastRetainedLatch;
141   };
142 
143 }
144 #endif
145