octocode 0.11.0

AI-powered code indexer with semantic search, GraphRAG knowledge graphs, and MCP server for multi-language codebases
Documentation
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
// Copyright 2025 Muvon Un Limited
//
// 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.

// GraphRAG database operations

use crate::indexer::graphrag::types::{CodeGraph, CodeNode, CodeRelationship};
use crate::indexer::graphrag::utils::cosine_similarity;
use crate::store::Store;
use anyhow::Result;
use arrow::array::Array;
use arrow::datatypes::{DataType, Field, Schema};
use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;

pub struct DatabaseOperations<'a> {
	store: &'a Store,
}

impl<'a> DatabaseOperations<'a> {
	pub fn new(store: &'a Store) -> Self {
		Self { store }
	}

	// Load the existing graph from database
	pub async fn load_graph(&self, _project_root: &Path, quiet: bool) -> Result<CodeGraph> {
		let mut graph = CodeGraph::default();

		// Check if the tables exist
		if !self
			.store
			.tables_exist(&["graphrag_nodes", "graphrag_relationships"])
			.await?
		{
			return Ok(graph); // Return empty graph if tables don't exist
		}

		// Get vector dimension for embedding work
		let vector_dim = self.store.get_code_vector_dim();

		// Get all nodes
		let node_batch = self
			.store
			.search_graph_nodes(&vec![0.0; vector_dim], 10000)
			.await?;
		if node_batch.num_rows() == 0 {
			return Ok(graph); // No nodes found
		}

		if !quiet {
			println!(
				"Loading {} GraphRAG nodes from database...",
				node_batch.num_rows()
			);
		}

		// Process nodes
		let id_array = node_batch
			.column_by_name("id")
			.unwrap()
			.as_any()
			.downcast_ref::<arrow::array::StringArray>()
			.unwrap();
		let name_array = node_batch
			.column_by_name("name")
			.unwrap()
			.as_any()
			.downcast_ref::<arrow::array::StringArray>()
			.unwrap();
		let kind_array = node_batch
			.column_by_name("kind")
			.unwrap()
			.as_any()
			.downcast_ref::<arrow::array::StringArray>()
			.unwrap();
		let path_array = node_batch
			.column_by_name("path")
			.unwrap()
			.as_any()
			.downcast_ref::<arrow::array::StringArray>()
			.unwrap();
		let description_array = node_batch
			.column_by_name("description")
			.unwrap()
			.as_any()
			.downcast_ref::<arrow::array::StringArray>()
			.unwrap();
		let symbols_array = node_batch
			.column_by_name("symbols")
			.unwrap()
			.as_any()
			.downcast_ref::<arrow::array::StringArray>()
			.unwrap();
		let imports_array = node_batch
			.column_by_name("imports")
			.unwrap()
			.as_any()
			.downcast_ref::<arrow::array::StringArray>()
			.unwrap();
		let exports_array = node_batch
			.column_by_name("exports")
			.unwrap()
			.as_any()
			.downcast_ref::<arrow::array::StringArray>()
			.unwrap();
		let hash_array = node_batch
			.column_by_name("hash")
			.unwrap()
			.as_any()
			.downcast_ref::<arrow::array::StringArray>()
			.unwrap();

		// Get the embedding fixed size list array
		let embedding_array = node_batch
			.column_by_name("embedding")
			.unwrap()
			.as_any()
			.downcast_ref::<arrow::array::FixedSizeListArray>()
			.unwrap();

		// Get the values of the embedding array
		let embedding_values = embedding_array
			.values()
			.as_any()
			.downcast_ref::<arrow::array::Float32Array>()
			.unwrap();

		// Process each row
		for i in 0..node_batch.num_rows() {
			let id = id_array.value(i).to_string();
			let name = name_array.value(i).to_string();
			let kind = kind_array.value(i).to_string();
			let path = path_array.value(i).to_string();
			let description = description_array.value(i).to_string();

			// Parse symbols JSON
			let symbols: Vec<String> = if symbols_array.is_null(i) {
				Vec::new()
			} else {
				serde_json::from_str(symbols_array.value(i)).unwrap_or_default()
			};

			// Parse imports JSON
			let imports: Vec<String> = if imports_array.is_null(i) {
				Vec::new()
			} else {
				serde_json::from_str(imports_array.value(i)).unwrap_or_default()
			};

			// Parse exports JSON
			let exports: Vec<String> = if exports_array.is_null(i) {
				Vec::new()
			} else {
				serde_json::from_str(exports_array.value(i)).unwrap_or_default()
			};

			let hash = hash_array.value(i).to_string();

			// Extract the embedding for this node
			let embedding_offset = i * embedding_array.value_length() as usize;
			let embedding_len = embedding_array.value_length() as usize;
			let mut embedding = Vec::with_capacity(embedding_len);

			for j in 0..embedding_len {
				embedding.push(embedding_values.value(embedding_offset + j));
			}

			// Create the node
			let node = CodeNode {
				id,
				name,
				kind,
				path,
				description,
				symbols,
				imports,
				exports,
				functions: Vec::new(), // Default empty for nodes loaded from old schema
				size_lines: 0,         // Default for nodes loaded from old schema
				language: "unknown".to_string(), // Default for nodes loaded from old schema
				hash,
				embedding,
			};

			// Add to graph
			graph.nodes.insert(node.id.clone(), node);
		}

		// Load relationships
		let rel_batch = self.store.get_graph_relationships().await?;
		if rel_batch.num_rows() > 0 {
			if !quiet {
				println!(
					"Loading {} GraphRAG relationships from database...",
					rel_batch.num_rows()
				);
			}

			// Process relationships
			let source_array = rel_batch
				.column_by_name("source")
				.unwrap()
				.as_any()
				.downcast_ref::<arrow::array::StringArray>()
				.unwrap();
			let target_array = rel_batch
				.column_by_name("target")
				.unwrap()
				.as_any()
				.downcast_ref::<arrow::array::StringArray>()
				.unwrap();
			let type_array = rel_batch
				.column_by_name("relation_type")
				.unwrap()
				.as_any()
				.downcast_ref::<arrow::array::StringArray>()
				.unwrap();
			let desc_array = rel_batch
				.column_by_name("description")
				.unwrap()
				.as_any()
				.downcast_ref::<arrow::array::StringArray>()
				.unwrap();
			let conf_array = rel_batch
				.column_by_name("confidence")
				.unwrap()
				.as_any()
				.downcast_ref::<arrow::array::Float32Array>()
				.unwrap();

			// Process each relationship
			for i in 0..rel_batch.num_rows() {
				let relationship = CodeRelationship {
					source: source_array.value(i).to_string(),
					target: target_array.value(i).to_string(),
					relation_type: type_array
						.value(i)
						.parse()
						.unwrap_or(crate::indexer::graphrag::types::RelationType::Imports),
					description: desc_array.value(i).to_string(),
					confidence: conf_array.value(i),
					weight: 1.0, // Default weight for legacy relationships
				};

				// Add to graph
				graph.relationships.push(relationship);
			}
		}

		if !graph.nodes.is_empty() && !quiet {
			println!(
				"Loaded GraphRAG knowledge graph with {} nodes and {} relationships",
				graph.nodes.len(),
				graph.relationships.len()
			);
		}

		Ok(graph)
	}

	// Save just the newly added nodes and relationships in batches
	pub async fn save_graph_incremental(
		&self,
		new_nodes: &[CodeNode],
		new_relationships: &[CodeRelationship],
	) -> Result<()> {
		if new_nodes.is_empty() && new_relationships.is_empty() {
			// Nothing to save
			return Ok(());
		}

		// First, save any new nodes
		if !new_nodes.is_empty() {
			// Create a HashMap for the batch conversion function
			let nodes_map: HashMap<String, CodeNode> = new_nodes
				.iter()
				.map(|node| (node.id.clone(), node.clone()))
				.collect();

			// Convert just these nodes to a RecordBatch
			let nodes_batch = self.nodes_to_batch(&nodes_map).await?;

			// Store the nodes in the database (appending to existing data)
			self.store.store_graph_nodes(nodes_batch).await?;
		}

		// Now save any new relationships
		if !new_relationships.is_empty() {
			// Convert just these relationships to a RecordBatch
			let rel_batch = self.relationships_to_batch(new_relationships).await?;

			// Store the relationships in the database (appending to existing data)
			self.store.store_graph_relationships(rel_batch).await?;
		}

		Ok(())
	}

	// Search for nodes in database
	pub async fn search_nodes_in_database(
		&self,
		query_embedding: &[f32],
		query: &str,
	) -> Result<Vec<CodeNode>> {
		// Check if the tables exist
		if !self.store.tables_exist(&["graphrag_nodes"]).await? {
			return Ok(Vec::new()); // No nodes in database
		}

		// Search for nodes similar to the query (increased limit to 50)
		let node_batch = self.store.search_graph_nodes(query_embedding, 50).await?;
		if node_batch.num_rows() == 0 {
			return Ok(Vec::new());
		}

		// Process the results into CodeNode objects
		let mut nodes = Vec::new();
		let query_lower = query.to_lowercase();

		// Extract columns from the batch
		let id_array = node_batch
			.column_by_name("id")
			.unwrap()
			.as_any()
			.downcast_ref::<arrow::array::StringArray>()
			.unwrap();
		let name_array = node_batch
			.column_by_name("name")
			.unwrap()
			.as_any()
			.downcast_ref::<arrow::array::StringArray>()
			.unwrap();
		let kind_array = node_batch
			.column_by_name("kind")
			.unwrap()
			.as_any()
			.downcast_ref::<arrow::array::StringArray>()
			.unwrap();
		let path_array = node_batch
			.column_by_name("path")
			.unwrap()
			.as_any()
			.downcast_ref::<arrow::array::StringArray>()
			.unwrap();
		let description_array = node_batch
			.column_by_name("description")
			.unwrap()
			.as_any()
			.downcast_ref::<arrow::array::StringArray>()
			.unwrap();
		let symbols_array = node_batch
			.column_by_name("symbols")
			.unwrap()
			.as_any()
			.downcast_ref::<arrow::array::StringArray>()
			.unwrap();
		let hash_array = node_batch
			.column_by_name("hash")
			.unwrap()
			.as_any()
			.downcast_ref::<arrow::array::StringArray>()
			.unwrap();

		// Get the embedding fixed size list array
		let embedding_array = node_batch
			.column_by_name("embedding")
			.unwrap()
			.as_any()
			.downcast_ref::<arrow::array::FixedSizeListArray>()
			.unwrap();

		// Get the values of the embedding array
		let embedding_values = embedding_array
			.values()
			.as_any()
			.downcast_ref::<arrow::array::Float32Array>()
			.unwrap();

		// Process each row
		for i in 0..node_batch.num_rows() {
			let id = id_array.value(i).to_string();
			let name = name_array.value(i).to_string();
			let kind = kind_array.value(i).to_string();
			let path = path_array.value(i).to_string();
			let description = description_array.value(i).to_string();

			// Parse symbols JSON
			let symbols: Vec<String> = if symbols_array.is_null(i) {
				Vec::new()
			} else {
				serde_json::from_str(symbols_array.value(i)).unwrap_or_default()
			};

			let hash = hash_array.value(i).to_string();

			// Extract the embedding for this node
			let embedding_offset = i * embedding_array.value_length() as usize;
			let embedding_len = embedding_array.value_length() as usize;
			let mut embedding = Vec::with_capacity(embedding_len);

			for j in 0..embedding_len {
				embedding.push(embedding_values.value(embedding_offset + j));
			}

			// Calculate semantic similarity
			let similarity = cosine_similarity(query_embedding, &embedding);

			// Check if the query is a substring of various node fields
			let name_contains = name.to_lowercase().contains(&query_lower);
			let kind_contains = kind.to_lowercase().contains(&query_lower);
			let desc_contains = description.to_lowercase().contains(&query_lower);
			let symbols_contain = symbols
				.iter()
				.any(|s| s.to_lowercase().contains(&query_lower));

			// Use a lower threshold for semantic similarity (0.5 instead of 0.6)
			// OR include if the query is a substring of any important field
			if similarity > 0.5
				|| name_contains
				|| kind_contains
				|| desc_contains
				|| symbols_contain
			{
				// Create the node
				let node = CodeNode {
					id,
					name,
					kind,
					path,
					description,
					symbols,
					imports: Vec::new(),   // Default empty for nodes loaded from old schema
					exports: Vec::new(),   // Default empty for nodes loaded from old schema
					functions: Vec::new(), // Default empty for nodes loaded from old schema
					size_lines: 0,         // Default for nodes loaded from old schema
					language: "unknown".to_string(), // Default for nodes loaded from old schema
					hash,
					embedding,
				};

				// Add to results
				nodes.push(node);
			}
		}

		// Sort nodes by relevance (exact matches first, then by similarity)
		nodes.sort_by(|a, b| {
			let a_contains = a.name.to_lowercase().contains(&query_lower)
				|| a.kind.to_lowercase().contains(&query_lower)
				|| a.symbols
					.iter()
					.any(|s| s.to_lowercase().contains(&query_lower));

			let b_contains = b.name.to_lowercase().contains(&query_lower)
				|| b.kind.to_lowercase().contains(&query_lower)
				|| b.symbols
					.iter()
					.any(|s| s.to_lowercase().contains(&query_lower));

			if a_contains && !b_contains {
				std::cmp::Ordering::Less
			} else if !a_contains && b_contains {
				std::cmp::Ordering::Greater
			} else {
				// Both contain or both don't contain, sort by similarity
				let a_sim = cosine_similarity(query_embedding, &a.embedding);
				let b_sim = cosine_similarity(query_embedding, &b.embedding);
				b_sim
					.partial_cmp(&a_sim)
					.unwrap_or(std::cmp::Ordering::Equal)
			}
		});

		Ok(nodes)
	}

	// Convert nodes to a RecordBatch for database storage with updated schema
	async fn nodes_to_batch(
		&self,
		nodes: &HashMap<String, CodeNode>,
	) -> Result<arrow::record_batch::RecordBatch> {
		// Get the vector dimension from the store
		let vector_dim = self.store.get_code_vector_dim();

		// Create updated schema with new fields
		let schema = Arc::new(Schema::new(vec![
			Field::new("id", DataType::Utf8, false),
			Field::new("name", DataType::Utf8, false),
			Field::new("kind", DataType::Utf8, false),
			Field::new("path", DataType::Utf8, false),
			Field::new("description", DataType::Utf8, false),
			Field::new("symbols", DataType::Utf8, true), // JSON serialized
			Field::new("imports", DataType::Utf8, true), // JSON serialized
			Field::new("exports", DataType::Utf8, true), // JSON serialized
			Field::new("functions", DataType::Utf8, true), // JSON serialized
			Field::new("size_lines", DataType::UInt32, false),
			Field::new("language", DataType::Utf8, false),
			Field::new("hash", DataType::Utf8, false),
			Field::new(
				"embedding",
				DataType::FixedSizeList(
					Arc::new(Field::new("item", DataType::Float32, true)),
					vector_dim as i32,
				),
				true,
			),
		]));

		// Prepare arrays
		let nodes_vec: Vec<&CodeNode> = nodes.values().collect();
		if nodes_vec.is_empty() {
			return Err(anyhow::anyhow!("No nodes to convert to batch"));
		}

		let ids: Vec<&str> = nodes_vec.iter().map(|n| n.id.as_str()).collect();
		let names: Vec<&str> = nodes_vec.iter().map(|n| n.name.as_str()).collect();
		let kinds: Vec<&str> = nodes_vec.iter().map(|n| n.kind.as_str()).collect();
		let paths: Vec<&str> = nodes_vec.iter().map(|n| n.path.as_str()).collect();
		let descriptions: Vec<&str> = nodes_vec.iter().map(|n| n.description.as_str()).collect();
		let symbols: Vec<String> = nodes_vec
			.iter()
			.map(|n| serde_json::to_string(&n.symbols).unwrap_or_default())
			.collect();
		let imports: Vec<String> = nodes_vec
			.iter()
			.map(|n| serde_json::to_string(&n.imports).unwrap_or_default())
			.collect();
		let exports: Vec<String> = nodes_vec
			.iter()
			.map(|n| serde_json::to_string(&n.exports).unwrap_or_default())
			.collect();
		let functions: Vec<String> = nodes_vec
			.iter()
			.map(|n| serde_json::to_string(&n.functions).unwrap_or_default())
			.collect();
		let size_lines: Vec<u32> = nodes_vec.iter().map(|n| n.size_lines).collect();
		let languages: Vec<&str> = nodes_vec.iter().map(|n| n.language.as_str()).collect();
		let hashes: Vec<&str> = nodes_vec.iter().map(|n| n.hash.as_str()).collect();

		// Create the embedding fixed size list array
		let mut flattened_embeddings = Vec::with_capacity(nodes_vec.len() * vector_dim);
		for node in &nodes_vec {
			// Ensure embeddings have the correct dimension
			if node.embedding.len() != vector_dim {
				return Err(anyhow::anyhow!(
					"Node embedding has dimension {} but expected {}",
					node.embedding.len(),
					vector_dim
				));
			}
			flattened_embeddings.extend_from_slice(&node.embedding);
		}
		let values = arrow::array::Float32Array::from(flattened_embeddings);

		// Create the fixed size list array
		let embedding_array = arrow::array::FixedSizeListArray::new(
			Arc::new(Field::new("item", DataType::Float32, true)),
			vector_dim as i32,
			Arc::new(values),
			None, // No validity buffer - all values are valid
		);

		// Create record batch
		let batch = arrow::record_batch::RecordBatch::try_new(
			schema,
			vec![
				Arc::new(arrow::array::StringArray::from(ids)),
				Arc::new(arrow::array::StringArray::from(names)),
				Arc::new(arrow::array::StringArray::from(kinds)),
				Arc::new(arrow::array::StringArray::from(paths)),
				Arc::new(arrow::array::StringArray::from(descriptions)),
				Arc::new(arrow::array::StringArray::from(symbols)),
				Arc::new(arrow::array::StringArray::from(imports)),
				Arc::new(arrow::array::StringArray::from(exports)),
				Arc::new(arrow::array::StringArray::from(functions)),
				Arc::new(arrow::array::UInt32Array::from(size_lines)),
				Arc::new(arrow::array::StringArray::from(languages)),
				Arc::new(arrow::array::StringArray::from(hashes)),
				Arc::new(embedding_array),
			],
		)?;

		Ok(batch)
	}

	// Convert relationships to a RecordBatch for database storage
	async fn relationships_to_batch(
		&self,
		relationships: &[CodeRelationship],
	) -> Result<arrow::record_batch::RecordBatch> {
		// Create updated schema with weight field
		let schema = Arc::new(Schema::new(vec![
			Field::new("id", DataType::Utf8, false),
			Field::new("source", DataType::Utf8, false),
			Field::new("target", DataType::Utf8, false),
			Field::new("relation_type", DataType::Utf8, false),
			Field::new("description", DataType::Utf8, false),
			Field::new("confidence", DataType::Float32, false),
			Field::new("weight", DataType::Float32, false),
		]));

		// Generate unique IDs
		let ids: Vec<String> = relationships
			.iter()
			.map(|_| uuid::Uuid::new_v4().to_string())
			.collect();
		let sources: Vec<&str> = relationships.iter().map(|r| r.source.as_str()).collect();
		let targets: Vec<&str> = relationships.iter().map(|r| r.target.as_str()).collect();
		let types: Vec<&str> = relationships
			.iter()
			.map(|r| r.relation_type.as_str())
			.collect();
		let descriptions: Vec<&str> = relationships
			.iter()
			.map(|r| r.description.as_str())
			.collect();
		let confidences: Vec<f32> = relationships.iter().map(|r| r.confidence).collect();
		let weights: Vec<f32> = relationships.iter().map(|r| r.weight).collect();

		// Create record batch
		let batch = arrow::record_batch::RecordBatch::try_new(
			schema,
			vec![
				Arc::new(arrow::array::StringArray::from(ids)),
				Arc::new(arrow::array::StringArray::from(sources)),
				Arc::new(arrow::array::StringArray::from(targets)),
				Arc::new(arrow::array::StringArray::from(types)),
				Arc::new(arrow::array::StringArray::from(descriptions)),
				Arc::new(arrow::array::Float32Array::from(confidences)),
				Arc::new(arrow::array::Float32Array::from(weights)),
			],
		)?;

		Ok(batch)
	}
}