|
| 1 | +# Git-Reticulator: Database Setup |
| 2 | + |
| 3 | +To support topological queries and hierarchical LOD (Level of Detail) zooming, Git-Reticulator uses **PostgreSQL** with the **pgRouting** extension. |
| 4 | + |
| 5 | +## 🐘 Prerequisites |
| 6 | +- PostgreSQL 14+ |
| 7 | +- PostGIS |
| 8 | +- pgRouting |
| 9 | + |
| 10 | +## 🛠️ Schema Initialization |
| 11 | + |
| 12 | +Run the following SQL to set up the semantic lattice environment: |
| 13 | + |
| 14 | +```sql |
| 15 | +-- Enable necessary extensions |
| 16 | +CREATE EXTENSION IF NOT EXISTS postgis; |
| 17 | +CREATE EXTENSION IF NOT EXISTS pgrouting; |
| 18 | +CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; |
| 19 | + |
| 20 | +-- 1. Keywords Table (Nodes in the Lattice) |
| 21 | +-- Stores semantic identities at multiple LOD levels. |
| 22 | +CREATE TABLE IF NOT EXISTS keywords ( |
| 23 | + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), |
| 24 | + name TEXT NOT NULL, |
| 25 | + file_path TEXT NOT NULL, |
| 26 | + level TEXT NOT NULL CHECK (level IN ('Module', 'File', 'Definition', 'Block')), |
| 27 | + parent_id UUID REFERENCES keywords(id), |
| 28 | + embedding VECTOR(1536), -- Optional: requires pgvector for semantic search |
| 29 | + cluster TEXT, |
| 30 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, |
| 31 | + UNIQUE(name, file_path, level) |
| 32 | +); |
| 33 | + |
| 34 | +-- Index for hierarchical lookups (Zooming) |
| 35 | +CREATE INDEX idx_keywords_parent ON keywords(parent_id); |
| 36 | +CREATE INDEX idx_keywords_level ON keywords(level); |
| 37 | + |
| 38 | +-- 2. Relationships Table (Edges in the Lattice) |
| 39 | +-- Optimized for pgRouting topological queries. |
| 40 | +CREATE TABLE IF NOT EXISTS relationships ( |
| 41 | + id SERIAL PRIMARY KEY, |
| 42 | + source_id UUID NOT NULL REFERENCES keywords(id), |
| 43 | + target_id UUID NOT NULL REFERENCES keywords(id), |
| 44 | + weight FLOAT DEFAULT 1.0, |
| 45 | + rel_type TEXT NOT NULL, -- e.g., 'calls', 'contains', 'depends_on' |
| 46 | + |
| 47 | + -- pgRouting required columns for topology |
| 48 | + source INTEGER, |
| 49 | + target INTEGER, |
| 50 | + cost FLOAT DEFAULT 1.0, |
| 51 | + reverse_cost FLOAT DEFAULT -1.0 -- -1 means one-way |
| 52 | +); |
| 53 | + |
| 54 | +-- 3. Topology Generation |
| 55 | +-- After inserting relationships, run pgr_createTopology to populate source/target integers. |
| 56 | +-- This is handled by the Git-Reticulator storage engine. |
| 57 | +``` |
| 58 | + |
| 59 | +## 🔍 Example pgRouting Query (Shortest Semantic Path) |
| 60 | + |
| 61 | +To find the most direct semantic relationship between two nodes: |
| 62 | + |
| 63 | +```sql |
| 64 | +SELECT * FROM pgr_dijkstra( |
| 65 | + 'SELECT id, source, target, cost FROM relationships', |
| 66 | + 1, -- Start node ID (integer) |
| 67 | + 50, -- End node ID (integer) |
| 68 | + directed := true |
| 69 | +); |
| 70 | +``` |
0 commit comments