-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathengine.c
More file actions
1316 lines (1052 loc) · 39.7 KB
/
engine.c
File metadata and controls
1316 lines (1052 loc) · 39.7 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "../include/gs_engine.h"
#include "../include/gs_memory.h"
#include "../include/gs_vertex.h"
#include "../include/gs_edge.h"
#include "../include/gs_planner_internal.h"
#include "../include/gs_cache.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <assert.h>
// Provider registration entry
typedef struct {
char* name;
gs_generate_edges_fn outgoing_generator;
gs_generate_incoming_edges_fn incoming_generator;
void* user_data;
bool is_enabled;
bool supports_incoming; // Whether this provider has incoming edge support
} Provider;
// Engine structure
struct GraphserverEngine {
Provider* providers;
size_t provider_count;
size_t provider_capacity;
GraphserverEngineConfig config;
// Edge cache for vertex expansion acceleration
EdgeCache* edge_cache;
// Statistics
GraphserverPlanStats last_plan_stats;
// Future: thread pool for concurrent expansion
// ThreadPool* thread_pool;
};
// Path structure (basic implementation for now)
struct GraphserverPath {
GraphserverEdge** edges;
size_t num_edges;
double* total_cost;
size_t cost_vector_size;
};
// Path list structure
struct GraphserverPathList {
GraphserverPath** paths;
size_t num_paths;
size_t capacity;
};
// Helper function to duplicate string
static char* duplicate_string(const char* str) {
if (!str) return NULL;
size_t len = strlen(str);
char* copy = malloc(len + 1);
if (!copy) return NULL;
memcpy(copy, str, len + 1);
return copy;
}
// Helper function to find provider by name
static Provider* find_provider(GraphserverEngine* engine, const char* name) {
if (!engine || !name) return NULL;
for (size_t i = 0; i < engine->provider_count; i++) {
if (strcmp(engine->providers[i].name, name) == 0) {
return &engine->providers[i];
}
}
return NULL;
}
// Helper function to ensure provider capacity
static GraphserverResult ensure_provider_capacity(GraphserverEngine* engine, size_t min_capacity) {
if (engine->provider_capacity >= min_capacity) return GS_SUCCESS;
size_t new_capacity = engine->provider_capacity == 0 ? 4 : engine->provider_capacity * 2;
while (new_capacity < min_capacity) {
new_capacity *= 2;
}
Provider* new_providers = realloc(engine->providers, sizeof(Provider) * new_capacity);
if (!new_providers) return GS_ERROR_OUT_OF_MEMORY;
engine->providers = new_providers;
engine->provider_capacity = new_capacity;
return GS_SUCCESS;
}
// Get default configuration
GraphserverEngineConfig gs_engine_get_default_config(void) {
GraphserverEngineConfig config = {0};
config.default_arena_size = GS_DEFAULT_ARENA_SIZE;
config.max_memory_limit = 0; // No limit
config.default_timeout_seconds = 30.0; // 30 seconds
config.enable_concurrent_expansion = false; // Not implemented yet
config.max_worker_threads = 4;
config.enable_edge_caching = false; // Disabled by default
return config;
}
// Engine lifecycle
GraphserverEngine* gs_engine_create(void) {
GraphserverEngineConfig config = gs_engine_get_default_config();
return gs_engine_create_with_config(&config);
}
GraphserverEngine* gs_engine_create_with_config(const GraphserverEngineConfig* config) {
if (!config) return NULL;
GraphserverEngine* engine = malloc(sizeof(GraphserverEngine));
if (!engine) return NULL;
engine->providers = NULL;
engine->provider_count = 0;
engine->provider_capacity = 0;
engine->config = *config;
// Initialize edge cache if enabled
if (config->enable_edge_caching) {
engine->edge_cache = edge_cache_create();
if (!engine->edge_cache) {
free(engine);
return NULL;
}
} else {
engine->edge_cache = NULL;
}
// Initialize stats
memset(&engine->last_plan_stats, 0, sizeof(GraphserverPlanStats));
return engine;
}
void gs_engine_destroy(GraphserverEngine* engine) {
if (!engine) return;
// Free provider names
for (size_t i = 0; i < engine->provider_count; i++) {
free(engine->providers[i].name);
}
// Destroy edge cache
if (engine->edge_cache) {
edge_cache_destroy(engine->edge_cache);
}
free(engine->providers);
free(engine);
}
// Internal helper function to clear the cache and reset statistics (used internally)
static void gs_engine_clear_cache_internal(GraphserverEngine* engine) {
if (!engine) return;
// Clear the cache if caching is enabled and cache exists
if (engine->config.enable_edge_caching && engine->edge_cache) {
edge_cache_clear(engine->edge_cache);
// Reset cache statistics
engine->last_plan_stats.cache_hits = 0;
engine->last_plan_stats.cache_misses = 0;
engine->last_plan_stats.cache_puts = 0;
}
}
// Public cache invalidation functions
GraphserverResult gs_engine_invalidate_vertex_cache(
GraphserverEngine* engine,
const GraphserverVertex* vertex) {
if (!engine || !vertex) {
return GS_ERROR_NULL_POINTER;
}
// Only proceed if caching is enabled and cache exists
if (!engine->config.enable_edge_caching || !engine->edge_cache) {
return GS_SUCCESS; // Nothing to invalidate
}
return edge_cache_invalidate(engine->edge_cache, vertex);
}
GraphserverResult gs_engine_invalidate_vertices_cache(
GraphserverEngine* engine,
const GraphserverVertex** vertices,
size_t count) {
if (!engine || !vertices) {
return GS_ERROR_NULL_POINTER;
}
// Only proceed if caching is enabled and cache exists
if (!engine->config.enable_edge_caching || !engine->edge_cache) {
return GS_SUCCESS; // Nothing to invalidate
}
return edge_cache_invalidate_batch(engine->edge_cache, vertices, count);
}
GraphserverResult gs_engine_clear_cache(GraphserverEngine* engine) {
if (!engine) {
return GS_ERROR_NULL_POINTER;
}
// Only proceed if caching is enabled and cache exists
if (!engine->config.enable_edge_caching || !engine->edge_cache) {
return GS_SUCCESS; // Nothing to clear
}
edge_cache_invalidate_all(engine->edge_cache);
// Reset cache statistics
engine->last_plan_stats.cache_hits = 0;
engine->last_plan_stats.cache_misses = 0;
engine->last_plan_stats.cache_puts = 0;
return GS_SUCCESS;
}
// Provider management
GraphserverResult gs_engine_register_provider(
GraphserverEngine* engine,
const char* provider_name,
gs_generate_edges_fn generator_func,
void* user_data) {
if (!engine || !provider_name || !generator_func) {
return GS_ERROR_NULL_POINTER;
}
// Check if provider already exists
if (find_provider(engine, provider_name) != NULL) {
return GS_ERROR_INVALID_ARGUMENT; // Provider already exists
}
// Ensure capacity
GraphserverResult result = ensure_provider_capacity(engine, engine->provider_count + 1);
if (result != GS_SUCCESS) return result;
// Add new provider
Provider* provider = &engine->providers[engine->provider_count];
provider->name = duplicate_string(provider_name);
if (!provider->name) return GS_ERROR_OUT_OF_MEMORY;
provider->outgoing_generator = generator_func;
provider->incoming_generator = NULL; // Legacy providers only support outgoing
provider->user_data = user_data;
provider->is_enabled = true;
provider->supports_incoming = false;
engine->provider_count++;
// Clear cache since adding a provider changes graph topology
gs_engine_clear_cache_internal(engine);
return GS_SUCCESS;
}
GraphserverResult gs_engine_register_bidirectional_provider(
GraphserverEngine* engine,
const char* provider_name,
gs_generate_edges_fn outgoing_generator,
gs_generate_incoming_edges_fn incoming_generator,
void* user_data) {
if (!engine || !provider_name) {
return GS_ERROR_NULL_POINTER;
}
// At least one generator must be provided
if (!outgoing_generator && !incoming_generator) {
return GS_ERROR_INVALID_ARGUMENT;
}
// Check if provider already exists
if (find_provider(engine, provider_name) != NULL) {
return GS_ERROR_INVALID_ARGUMENT; // Provider already exists
}
// Ensure capacity
GraphserverResult result = ensure_provider_capacity(engine, engine->provider_count + 1);
if (result != GS_SUCCESS) return result;
// Add new provider
Provider* provider = &engine->providers[engine->provider_count];
provider->name = duplicate_string(provider_name);
if (!provider->name) return GS_ERROR_OUT_OF_MEMORY;
provider->outgoing_generator = outgoing_generator;
provider->incoming_generator = incoming_generator;
provider->user_data = user_data;
provider->is_enabled = true;
provider->supports_incoming = (incoming_generator != NULL);
engine->provider_count++;
// Clear cache since adding a provider changes graph topology
if (engine->edge_cache) {
edge_cache_clear(engine->edge_cache);
}
return GS_SUCCESS;
}
GraphserverResult gs_engine_unregister_provider(
GraphserverEngine* engine,
const char* provider_name) {
if (!engine || !provider_name) return GS_ERROR_NULL_POINTER;
// Find provider
for (size_t i = 0; i < engine->provider_count; i++) {
if (strcmp(engine->providers[i].name, provider_name) == 0) {
// Free the name
free(engine->providers[i].name);
// Shift remaining providers down
for (size_t j = i; j < engine->provider_count - 1; j++) {
engine->providers[j] = engine->providers[j + 1];
}
engine->provider_count--;
// Clear cache since removing a provider changes graph topology
gs_engine_clear_cache_internal(engine);
return GS_SUCCESS;
}
}
return GS_ERROR_KEY_NOT_FOUND;
}
GraphserverResult gs_engine_set_provider_enabled(
GraphserverEngine* engine,
const char* provider_name,
bool enabled) {
if (!engine || !provider_name) return GS_ERROR_NULL_POINTER;
Provider* provider = find_provider(engine, provider_name);
if (!provider) return GS_ERROR_KEY_NOT_FOUND;
provider->is_enabled = enabled;
// Clear cache since enabling/disabling a provider changes graph topology
gs_engine_clear_cache_internal(engine);
return GS_SUCCESS;
}
GraphserverResult gs_engine_list_providers(
const GraphserverEngine* engine,
GraphserverProviderInfo** out_provider_info,
size_t* out_count) {
if (!engine || !out_provider_info || !out_count) {
return GS_ERROR_NULL_POINTER;
}
if (engine->provider_count == 0) {
*out_provider_info = NULL;
*out_count = 0;
return GS_SUCCESS;
}
GraphserverProviderInfo* info = malloc(sizeof(GraphserverProviderInfo) * engine->provider_count);
if (!info) return GS_ERROR_OUT_OF_MEMORY;
for (size_t i = 0; i < engine->provider_count; i++) {
info[i].name = engine->providers[i].name;
info[i].generator = engine->providers[i].outgoing_generator;
info[i].user_data = engine->providers[i].user_data;
info[i].is_enabled = engine->providers[i].is_enabled;
}
*out_provider_info = info;
*out_count = engine->provider_count;
return GS_SUCCESS;
}
size_t gs_engine_get_provider_count(const GraphserverEngine* engine) {
return engine ? engine->provider_count : 0;
}
bool gs_engine_has_provider(const GraphserverEngine* engine, const char* provider_name) {
return find_provider((GraphserverEngine*)engine, provider_name) != NULL;
}
// Attempt to load edges from the cache. Returns GS_SUCCESS on cache hit and
// GS_ERROR_KEY_NOT_FOUND on miss. Other errors are propagated so callers can
// decide how to proceed.
static GraphserverResult try_cache(
GraphserverEngine* engine,
const GraphserverVertex* vertex,
GraphserverEdgeList* out_edges)
{
if (!engine || !vertex || !out_edges) {
return GS_ERROR_NULL_POINTER;
}
if (!engine->config.enable_edge_caching || !engine->edge_cache) {
return GS_ERROR_KEY_NOT_FOUND;
}
GraphserverEdgeList* cached_edges = NULL;
GraphserverResult cache_result = edge_cache_get(engine->edge_cache, vertex, &cached_edges);
if (cache_result == GS_SUCCESS && cached_edges) {
size_t cached_edge_count = gs_edge_list_get_count(cached_edges);
for (size_t i = 0; i < cached_edge_count; i++) {
GraphserverEdge* edge;
if (gs_edge_list_get_edge(cached_edges, i, &edge) == GS_SUCCESS && edge) {
GraphserverEdge* edge_clone = gs_edge_clone(edge);
if (edge_clone) {
gs_edge_list_add_edge(out_edges, edge_clone);
}
}
}
engine->last_plan_stats.cache_hits++;
engine->last_plan_stats.edges_generated += cached_edge_count;
gs_edge_list_destroy(cached_edges);
return GS_SUCCESS;
}
if (cache_result == GS_ERROR_KEY_NOT_FOUND) {
engine->last_plan_stats.cache_misses++;
}
if (cached_edges) {
gs_edge_list_destroy(cached_edges);
}
return cache_result;
}
// Invoke all enabled providers to generate edges for a vertex.
// Returns GS_SUCCESS unless a memory allocation failure occurs.
static GraphserverResult call_providers(
GraphserverEngine* engine,
const GraphserverVertex* vertex,
GraphserverEdgeList* out_edges)
{
if (!engine || !vertex || !out_edges) {
return GS_ERROR_NULL_POINTER;
}
GraphserverResult overall_result = GS_SUCCESS;
for (size_t i = 0; i < engine->provider_count; i++) {
Provider* provider = &engine->providers[i];
if (!provider->is_enabled) {
continue;
}
GraphserverEdgeList* provider_edges = gs_edge_list_create();
if (!provider_edges) {
overall_result = GS_ERROR_OUT_OF_MEMORY;
break;
}
// Use outgoing generator for normal expansion
int provider_result = provider->outgoing_generator ?
provider->outgoing_generator(vertex, provider_edges, provider->user_data) : -1;
if (provider_result == 0) {
size_t provider_edge_count = gs_edge_list_get_count(provider_edges);
for (size_t j = 0; j < provider_edge_count; j++) {
GraphserverEdge* edge;
if (gs_edge_list_get_edge(provider_edges, j, &edge) == GS_SUCCESS && edge) {
gs_edge_list_add_edge(out_edges, edge);
}
}
engine->last_plan_stats.providers_called++;
engine->last_plan_stats.edges_generated += provider_edge_count;
}
gs_edge_list_destroy(provider_edges);
}
return overall_result;
}
// Graph expansion
GraphserverResult gs_engine_expand_vertex(
GraphserverEngine* engine,
const GraphserverVertex* vertex,
GraphserverEdgeList* out_edges) {
if (!engine || !vertex || !out_edges) return GS_ERROR_NULL_POINTER;
// Clear the output edge list
gs_edge_list_clear(out_edges);
// Try retrieving from cache first
GraphserverResult cache_result = try_cache(engine, vertex, out_edges);
if (cache_result == GS_SUCCESS) {
return GS_SUCCESS;
}
// Cache miss or error - generate edges using providers
GraphserverResult overall_result = call_providers(engine, vertex, out_edges);
// Store results in cache if caching is enabled and providers succeeded
if (engine->config.enable_edge_caching && engine->edge_cache && overall_result == GS_SUCCESS) {
(void)edge_cache_put(engine->edge_cache, vertex, out_edges);
engine->last_plan_stats.cache_puts++;
}
return overall_result;
}
// Invoke all enabled providers to generate incoming edges for a vertex.
// Returns GS_SUCCESS unless a memory allocation failure occurs.
static GraphserverResult call_providers_incoming(
GraphserverEngine* engine,
const GraphserverVertex* vertex,
GraphserverEdgeList* in_edges)
{
if (!engine || !vertex || !in_edges) {
return GS_ERROR_NULL_POINTER;
}
GraphserverResult overall_result = GS_SUCCESS;
for (size_t i = 0; i < engine->provider_count; i++) {
Provider* provider = &engine->providers[i];
if (!provider->is_enabled || !provider->incoming_generator) {
continue;
}
GraphserverEdgeList* provider_edges = gs_edge_list_create();
if (!provider_edges) {
overall_result = GS_ERROR_OUT_OF_MEMORY;
break;
}
int provider_result = provider->incoming_generator(vertex, provider_edges, provider->user_data);
if (provider_result == 0) {
size_t provider_edge_count = gs_edge_list_get_count(provider_edges);
for (size_t j = 0; j < provider_edge_count; j++) {
GraphserverEdge* edge;
if (gs_edge_list_get_edge(provider_edges, j, &edge) == GS_SUCCESS && edge) {
gs_edge_list_add_edge(in_edges, edge);
}
}
engine->last_plan_stats.edges_generated += provider_edge_count;
}
engine->last_plan_stats.providers_called++;
gs_edge_list_destroy(provider_edges);
}
return overall_result;
}
GraphserverResult gs_engine_expand_vertex_incoming(
GraphserverEngine* engine,
const GraphserverVertex* vertex,
GraphserverEdgeList* in_edges) {
if (!engine || !vertex || !in_edges) return GS_ERROR_NULL_POINTER;
// Clear the output edge list
gs_edge_list_clear(in_edges);
// For now, incoming edges are not cached (could be added later with separate cache)
// Generate edges using providers directly
GraphserverResult overall_result = call_providers_incoming(engine, vertex, in_edges);
return overall_result;
}
// Configuration
GraphserverResult gs_engine_set_config(
GraphserverEngine* engine,
const GraphserverEngineConfig* config) {
if (!engine || !config) return GS_ERROR_NULL_POINTER;
// Check if caching configuration is changing
bool caching_was_enabled = engine->config.enable_edge_caching;
bool caching_will_be_enabled = config->enable_edge_caching;
// Update configuration
engine->config = *config;
// Handle cache state changes
if (caching_was_enabled && !caching_will_be_enabled) {
// Caching is being disabled - destroy the cache
if (engine->edge_cache) {
edge_cache_destroy(engine->edge_cache);
engine->edge_cache = NULL;
}
// Reset cache statistics
engine->last_plan_stats.cache_hits = 0;
engine->last_plan_stats.cache_misses = 0;
engine->last_plan_stats.cache_puts = 0;
} else if (!caching_was_enabled && caching_will_be_enabled) {
// Caching is being enabled - create the cache
engine->edge_cache = edge_cache_create();
if (!engine->edge_cache) {
return GS_ERROR_OUT_OF_MEMORY;
}
} else if (caching_was_enabled && caching_will_be_enabled) {
// Caching remains enabled - clear the cache as configuration might affect edge generation
gs_engine_clear_cache_internal(engine);
}
return GS_SUCCESS;
}
GraphserverResult gs_engine_get_config(
const GraphserverEngine* engine,
GraphserverEngineConfig* out_config) {
if (!engine || !out_config) return GS_ERROR_NULL_POINTER;
*out_config = engine->config;
return GS_SUCCESS;
}
GraphserverResult gs_engine_get_stats(
const GraphserverEngine* engine,
GraphserverPlanStats* out_stats) {
if (!engine || !out_stats) return GS_ERROR_NULL_POINTER;
*out_stats = engine->last_plan_stats;
return GS_SUCCESS;
}
// Basic path management (will be expanded when planners are implemented)
GraphserverPath* gs_path_create(size_t cost_vector_size) {
GraphserverPath* path = malloc(sizeof(GraphserverPath));
if (!path) return NULL;
path->edges = NULL;
path->num_edges = 0;
path->cost_vector_size = cost_vector_size;
if (cost_vector_size > 0) {
path->total_cost = calloc(cost_vector_size, sizeof(double));
if (!path->total_cost) {
free(path);
return NULL;
}
} else {
path->total_cost = NULL;
}
return path;
}
void gs_path_destroy(GraphserverPath* path) {
if (!path) return;
// Destroy individual edges
if (path->edges) {
for (size_t i = 0; i < path->num_edges; i++) {
if (path->edges[i]) {
gs_edge_destroy(path->edges[i]);
}
}
free(path->edges);
}
free(path->total_cost);
free(path);
}
size_t gs_path_get_num_edges(const GraphserverPath* path) {
return path ? path->num_edges : 0;
}
const GraphserverEdge* gs_path_get_edge(const GraphserverPath* path, size_t index) {
if (!path || index >= path->num_edges) return NULL;
return path->edges[index];
}
const double* gs_path_get_total_cost(const GraphserverPath* path) {
return path ? path->total_cost : NULL;
}
size_t gs_path_get_cost_vector_size(const GraphserverPath* path) {
return path ? path->cost_vector_size : 0;
}
char* gs_path_to_string(const GraphserverPath* path) {
if (!path) return NULL;
// Simple implementation for now
char* buffer = malloc(256);
if (!buffer) return NULL;
snprintf(buffer, 256, "Path{edges: %zu, cost_size: %zu}",
path->num_edges, path->cost_vector_size);
return buffer;
}
// Path list management
GraphserverPathList* gs_pathlist_create(void) {
GraphserverPathList* list = malloc(sizeof(GraphserverPathList));
if (!list) return NULL;
list->paths = NULL;
list->num_paths = 0;
list->capacity = 0;
return list;
}
void gs_pathlist_destroy(GraphserverPathList* path_list) {
if (!path_list) return;
for (size_t i = 0; i < path_list->num_paths; i++) {
gs_path_destroy(path_list->paths[i]);
}
free(path_list->paths);
free(path_list);
}
size_t gs_pathlist_get_count(const GraphserverPathList* path_list) {
return path_list ? path_list->num_paths : 0;
}
GraphserverPath* gs_pathlist_get_path(const GraphserverPathList* path_list, size_t index) {
if (!path_list || index >= path_list->num_paths) return NULL;
return path_list->paths[index];
}
// Planning function implementations using Dijkstra algorithm
GraphserverPathList* gs_plan(
GraphserverEngine* engine,
const GraphserverPlanOptions* options,
GraphserverPlanStats* out_stats) {
if (!engine || !options) return NULL;
// Preserve existing stats (e.g., from precaching) and only reset planning-specific counters
// Keep: cache_hits, cache_misses, cache_puts, providers_called, edges_generated from precaching
// Reset: vertices_expanded (will be set by planner)
engine->last_plan_stats.vertices_expanded = 0;
// Create arena for planning operations
GraphserverArena* arena = gs_arena_create(engine->config.default_arena_size);
if (!arena) return NULL;
GraphserverPath* path = NULL;
GraphserverPlanStats stats = {0};
// Use Dijkstra planner (currently only single-objective supported)
GraphserverResult result = gs_plan_dijkstra(
engine,
options->start_vertex,
options->is_goal_fn,
options->is_goal_user_data,
options->timeout_seconds,
arena,
&path,
&stats
);
// Only create path list if we have a valid path
// Return NULL for no path found (fixes memory leak)
if (result != GS_SUCCESS || !path) {
gs_arena_destroy(arena);
return NULL;
}
// Create path list result for successful path
GraphserverPathList* path_list = gs_pathlist_create();
if (!path_list) {
gs_path_destroy(path);
gs_arena_destroy(arena);
return NULL;
}
// Expand path list capacity and add the path
path_list->capacity = 1;
path_list->paths = malloc(sizeof(GraphserverPath*) * path_list->capacity);
if (!path_list->paths) {
gs_path_destroy(path);
gs_pathlist_destroy(path_list);
gs_arena_destroy(arena);
return NULL;
}
path_list->paths[0] = path;
path_list->num_paths = 1;
// Merge planner stats with existing engine stats (preserving precaching stats)
engine->last_plan_stats.vertices_expanded = stats.vertices_expanded;
// Keep existing cache stats and add any new ones from planning
engine->last_plan_stats.cache_hits += stats.cache_hits;
engine->last_plan_stats.cache_misses += stats.cache_misses;
engine->last_plan_stats.cache_puts += stats.cache_puts;
engine->last_plan_stats.providers_called += stats.providers_called;
engine->last_plan_stats.edges_generated += stats.edges_generated;
if (out_stats) {
*out_stats = stats;
}
gs_arena_destroy(arena);
return path_list;
}
GraphserverPath* gs_plan_simple(
GraphserverEngine* engine,
const GraphserverVertex* start_vertex,
gs_goal_predicate_fn is_goal,
void* goal_user_data,
GraphserverPlanStats* out_stats) {
if (!engine || !start_vertex || !is_goal) return NULL;
// Create arena for planning operations
GraphserverArena* arena = gs_arena_create(engine->config.default_arena_size);
if (!arena) return NULL;
GraphserverPath* path = NULL;
GraphserverPlanStats stats = {0};
// Use Dijkstra planner directly
GraphserverResult result = gs_plan_dijkstra(
engine,
start_vertex,
is_goal,
goal_user_data,
engine->config.default_timeout_seconds,
arena,
&path,
&stats
);
// Merge planner stats with existing engine stats (preserving precaching stats)
engine->last_plan_stats.vertices_expanded = stats.vertices_expanded;
// Keep existing cache stats and add any new ones from planning
engine->last_plan_stats.cache_hits += stats.cache_hits;
engine->last_plan_stats.cache_misses += stats.cache_misses;
engine->last_plan_stats.cache_puts += stats.cache_puts;
engine->last_plan_stats.providers_called += stats.providers_called;
engine->last_plan_stats.edges_generated += stats.edges_generated;
if (out_stats) {
*out_stats = stats;
}
gs_arena_destroy(arena);
if (result == GS_SUCCESS) {
return path;
} else {
if (path) gs_path_destroy(path);
return NULL;
}
}
GraphserverPath* gs_plan_with_planner(
GraphserverEngine* engine,
const GraphserverVertex* start_vertex,
gs_goal_predicate_fn is_goal,
void* goal_user_data,
const char* planner_name,
GraphserverPlanStats* out_stats) {
if (!engine || !start_vertex || !is_goal || !planner_name) return NULL;
// Create arena for planning operations
GraphserverArena* arena = gs_arena_create(engine->config.default_arena_size);
if (!arena) return NULL;
GraphserverPath* path = NULL;
GraphserverPlanStats stats = {0};
GraphserverResult result = GS_ERROR_INVALID_ARGUMENT;
if (strcmp(planner_name, "dijkstra") == 0) {
// Use Dijkstra planner
result = gs_plan_dijkstra(
engine,
start_vertex,
is_goal,
goal_user_data,
engine->config.default_timeout_seconds,
arena,
&path,
&stats
);
} else if (strcmp(planner_name, "astar") == 0) {
// Use A* planner with geographic heuristic
result = gs_plan_astar(
engine,
start_vertex,
is_goal,
goal_user_data,
geographic_distance_heuristic,
goal_user_data, // Use same goal data for heuristic
engine->config.default_timeout_seconds,
arena,
&path,
&stats
);
}
// Merge planner stats with existing engine stats
engine->last_plan_stats.vertices_expanded = stats.vertices_expanded;
engine->last_plan_stats.cache_hits += stats.cache_hits;
engine->last_plan_stats.cache_misses += stats.cache_misses;
engine->last_plan_stats.cache_puts += stats.cache_puts;
engine->last_plan_stats.providers_called += stats.providers_called;
engine->last_plan_stats.edges_generated += stats.edges_generated;
if (out_stats) {
*out_stats = stats;
}
gs_arena_destroy(arena);
if (result == GS_SUCCESS) {
return path;
} else {
if (path) gs_path_destroy(path);
return NULL;
}
}
// Utility functions
const char* gs_get_error_message(GraphserverResult result) {
switch (result) {
case GS_SUCCESS: return "Success";
case GS_ERROR_NULL_POINTER: return "Null pointer";
case GS_ERROR_INVALID_ARGUMENT: return "Invalid argument";
case GS_ERROR_OUT_OF_MEMORY: return "Out of memory";
case GS_ERROR_KEY_NOT_FOUND: return "Key not found";
case GS_ERROR_TYPE_MISMATCH: return "Type mismatch";
case GS_ERROR_TIMEOUT: return "Timeout";
case GS_ERROR_NO_PATH_FOUND: return "No path found";
default: return "Unknown error";
}
}
const char* gs_get_version(void) {
return "2.0.0";
}
GraphserverResult gs_initialize(void) {
// Currently no global initialization needed
return GS_SUCCESS;
}
void gs_cleanup(void) {
// Currently no global cleanup needed
}
// Forward declaration for BFS precaching helper
static GraphserverResult bfs_precache(
GraphserverEngine* engine,
Provider* provider,
GraphserverVertex** seed_vertices,
size_t num_seeds,
size_t max_depth,
size_t max_vertices
);
// Edge list node for tracking edge lists to destroy
typedef struct EdgeListNode {
GraphserverEdgeList* edges;
struct EdgeListNode* next;
} EdgeListNode;
// BFS precaching state structure
typedef struct {
PriorityQueue* queue;
VertexSet* visited;
GraphserverArena* arena;