Skip to content

Commit d182531

Browse files
committed
use inner construct for edge/node to avoid stackoverflow
1 parent 54d73a2 commit d182531

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ db = DB()
2020
- E.g. Nodes 1, 2, and 3: `db[1:3]`
2121
- E.g. Edges from 1 to 2 or 3: `db[1, 2:3]`
2222
- Returned objects are `Node` or `Edge` (or generator if multiple objects queried):
23-
- `Node` and `Edge` are simple structs. By default, `T` will be `String`.
24-
- To read `props` into Julia as a specific type, use `JSON3.read(node.props, MyType)`.
23+
- `Node` and `Edge` are simple structs.
24+
- By default, `T` will be `String`. You can set `T` on construction of the `DB` e.g. `DB(Dict{String,String})`.
2525
```julia
2626
struct Node{T}
2727
id::Int

src/SQLiteGraph.jl

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ end
1818
struct Node{T}
1919
id::Int
2020
props::T
21+
Node(id::Integer, props=nothing) = new{typeof(props)}(id, props)
2122
end
22-
Node(id::Integer, props=nothing) = Node(id, props)
23+
2324
function Base.show(io::IO, o::Node)
2425
print(io, "Node($(o.id)) with props: ")
2526
print(io, o.props)
@@ -30,8 +31,10 @@ struct Edge{T}
3031
source::Int
3132
target::Int
3233
props::T
34+
function Edge(source::Integer, target::Integer, props=nothing)
35+
new{typeof(props)}(source, target, props)
36+
end
3337
end
34-
Edge(source::Integer, target::Integer, props=nothing) = Edge(source, target, props)
3538
Edge(T::DataType, e::Edge{<:AbstractString}) = Edge(e.source, e.target, JSON3.read(e.props, T))
3639
Edge(e::Edge{<:AbstractString}, T::DataType) = Edge(e.source, e.target, JSON3.read(e.props, T))
3740
function Base.show(io::IO, o::Edge)
@@ -42,10 +45,11 @@ end
4245

4346
#-----------------------------------------------------------------------------# DB
4447
"""
45-
DB(file = ":memory")
48+
DB(file = ":memory", T = String)
4649
47-
Create a graph database (in memory by default). Edge and node properties are saved in the database
48-
as `TEXT` (see [https://www.sqlite.org/datatype3.html](https://www.sqlite.org/datatype3.html)) via `JSON3.write(props)`.
50+
Create a graph database (in memory by default).
51+
- Node and edge properties are saved in the database as `TEXT` (see [https://www.sqlite.org/datatype3.html](https://www.sqlite.org/datatype3.html)) via `JSON3.write(props)`.
52+
- Node and edge properties will be interpreted as `T` in Julia: `JSON3.read(props, T)`
4953
5054
# Interal Table Structure
5155
@@ -67,10 +71,10 @@ as `TEXT` (see [https://www.sqlite.org/datatype3.html](https://www.sqlite.org/da
6771
6872
db[1,2] = (z = 4) # edge from 1 → 2
6973
"""
70-
struct DB
74+
struct DB{T}
7175
sqlitedb::SQLite.DB
7276

73-
function DB(file = ":memory:")
77+
function DB(file::String = ":memory:", T::Type = String)
7478
db = SQLite.DB(file)
7579
SQLite.@register db SQLite.regexp
7680
statements = [
@@ -95,11 +99,14 @@ struct DB
9599
map(statements) do x
96100
execute(db, x)
97101
end
98-
new(db)
102+
new{T}(db)
99103
end
100104
end
101-
function Base.show(io::IO, db::DB)
102-
print(io, "SQLiteGraph.DB(\"$(db.sqlitedb.file)\") ($(n_nodes(db)) nodes, $(n_edges(db)) edges)")
105+
DB(T::Type, file::String = ":memory:") = DB(file, T)
106+
DB(T::Type, db::DB) = DB(db.sqlitedb, T)
107+
DB(db::DB, T::Type) = DB(T, db)
108+
function Base.show(io::IO, db::DB{T}) where {T}
109+
print(io, "SQLiteGraph.DB{$T}(\"$(db.sqlitedb.file)\") ($(n_nodes(db)) nodes, $(n_edges(db)) edges)")
103110
end
104111

105112
execute(db::DB, args...; kw...) = execute(db.sqlitedb, args...; kw...)

0 commit comments

Comments
 (0)