Spaces:
Sleeping
Sleeping
File size: 6,562 Bytes
57272d3 | 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 | """SQLite database layer for paper caching.
Provides a lightweight persistence layer using SQLAlchemy with SQLite.
Papers are cached locally so repeated queries are fast and the corpus
grows over time for trend detection and citation analysis.
"""
import json
import os
import threading
from datetime import datetime, timezone
from pathlib import Path
from sqlalchemy import (
Column,
DateTime,
Float,
Integer,
String,
Text,
UniqueConstraint,
create_engine,
event,
tuple_,
)
from sqlalchemy.orm import Session, declarative_base, sessionmaker
Base = declarative_base()
_CACHE_DIR = Path(os.environ.get(
"RESEARCH_MCP_CACHE_DIR",
os.path.expanduser("~/.research-papers-mcp"),
))
class Paper(Base):
__tablename__ = "papers"
__table_args__ = (
UniqueConstraint("source", "source_id", name="uq_source_paper"),
)
id = Column(Integer, primary_key=True)
title = Column(String, nullable=False)
abstract = Column(Text)
authors = Column(String)
publication_date = Column(DateTime)
source = Column(String, nullable=False)
source_id = Column(String, nullable=False)
url = Column(String)
doi = Column(String)
topics_json = Column(Text, default="[]")
citation_count = Column(Integer)
influential_citation_count = Column(Integer)
impact_score = Column(Float)
created_at = Column(DateTime, default=lambda: datetime.now(timezone.utc))
updated_at = Column(DateTime, default=lambda: datetime.now(timezone.utc),
onupdate=lambda: datetime.now(timezone.utc))
@property
def topics(self):
"""Topic names only, normalised from either on-disk schema shape."""
from .topics.schema import to_names
try:
raw = json.loads(self.topics_json or "[]")
except (json.JSONDecodeError, TypeError):
return []
return to_names(raw)
@property
def topic_entries(self):
"""Full topic entries: ``[{"name", "confidence", "source"}, ...]``."""
from .topics.schema import to_entries
try:
raw = json.loads(self.topics_json or "[]")
except (json.JSONDecodeError, TypeError):
return []
return to_entries(raw)
@topics.setter
def topics(self, value):
self.topics_json = json.dumps(value or [])
def to_dict(self, compact: bool = False):
d = {
"id": self.id,
"title": self.title,
"authors": self.authors,
"publication_date": (
self.publication_date.isoformat() if self.publication_date else None
),
"source": self.source,
"url": self.url,
"topics": self.topics,
"citation_count": self.citation_count,
"impact_score": self.impact_score,
}
if not compact:
d["abstract"] = self.abstract
d["source_id"] = self.source_id
d["doi"] = self.doi
d["influential_citation_count"] = self.influential_citation_count
d["updated_at"] = (
self.updated_at.isoformat() if self.updated_at else None
)
return d
_engine = None
_SessionLocal = None
_init_lock = threading.Lock()
def _enable_wal(dbapi_conn, connection_record):
cursor = dbapi_conn.cursor()
cursor.execute("PRAGMA journal_mode=WAL")
cursor.close()
def get_engine():
global _engine
if _engine is None:
with _init_lock:
if _engine is None:
_CACHE_DIR.mkdir(parents=True, exist_ok=True)
db_path = _CACHE_DIR / "papers.db"
_engine = create_engine(
f"sqlite:///{db_path}",
connect_args={"check_same_thread": False},
)
event.listen(_engine, "connect", _enable_wal)
Base.metadata.create_all(_engine)
return _engine
def get_session() -> Session:
global _SessionLocal
if _SessionLocal is None:
with _init_lock:
if _SessionLocal is None:
_SessionLocal = sessionmaker(bind=get_engine())
return _SessionLocal()
def upsert_papers(papers: list[dict]) -> int:
"""Insert papers, skipping duplicates. Returns count of new papers."""
if not papers:
return 0
session = get_session()
new_count = 0
try:
# Batch-fetch existing papers to avoid N+1 queries
keys = [(p["source"], p["source_id"]) for p in papers]
existing_rows = (
session.query(Paper)
.filter(
tuple_(Paper.source, Paper.source_id).in_(keys)
)
.all()
)
existing_map = {(r.source, r.source_id): r for r in existing_rows}
for p in papers:
key = (p["source"], p["source_id"])
existing = existing_map.get(key)
if existing:
updated = False
if p.get("citation_count") is not None:
existing.citation_count = p["citation_count"]
updated = True
if p.get("influential_citation_count") is not None:
existing.influential_citation_count = p["influential_citation_count"]
updated = True
if p.get("impact_score") is not None:
existing.impact_score = p["impact_score"]
updated = True
if p.get("abstract") and not existing.abstract:
existing.abstract = p["abstract"]
updated = True
if updated:
existing.updated_at = datetime.now(timezone.utc)
continue
paper = Paper(
title=p["title"],
abstract=p.get("abstract"),
authors=p.get("authors"),
publication_date=p.get("publication_date"),
source=p["source"],
source_id=p["source_id"],
url=p.get("url"),
doi=p.get("doi"),
citation_count=p.get("citation_count"),
influential_citation_count=p.get("influential_citation_count"),
impact_score=p.get("impact_score"),
)
paper.topics = p.get("topics", [])
session.add(paper)
new_count += 1
session.commit()
except Exception:
session.rollback()
raise
finally:
session.close()
return new_count
|