store

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package store is the persistence layer for the Douban mirror: a frontier of URLs to crawl, the normalized records harvested from them, crawl cursors, and the raw artifact files captured verbatim.

It uses a pure-Go SQLite (modernc.org/sqlite) so the binary keeps building with CGO_ENABLED=0. The database lives at <dir>/douban.db; raw artifacts go under <dir>/raw/ and JSONL exports under <dir>/export/.

Index

Constants

View Source
const (
	StatusPending = "pending"
	StatusDone    = "done"
	StatusFailed  = "failed"
	StatusBlocked = "blocked"
	StatusSkipped = "skipped"
)

Frontier status values.

View Source
const (
	SourceFrodo  = "frodo"
	SourceHTML   = "html"
	SourceMobile = "mobile"
)

Source values record which surface produced an artifact or record.

Variables

This section is empty.

Functions

This section is empty.

Types

type Frontier

type Frontier struct {
	URL          string
	EntityType   string
	EntityID     string
	Source       string
	Status       string
	Attempts     int
	Priority     int
	HTTPStatus   int
	Error        string
	RawPath      string
	ContentHash  string
	DiscoveredAt time.Time
	FetchedAt    time.Time
}

Frontier is one row of the crawl queue: a URL plus what is known about it.

type Record

type Record struct {
	EntityType string
	EntityID   string
	Source     string
	JSON       json.RawMessage
	FetchedAt  time.Time
}

Record is a normalized structured entity, stored as JSON and exported to JSONL.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store owns the mirror directory and its SQLite database.

func Open

func Open(dir string) (*Store, error)

Open opens (creating if needed) the mirror at dir and applies the schema.

func (*Store) Close

func (s *Store) Close() error

Close closes the database.

func (*Store) Complete

func (s *Store) Complete(url, status string, httpStatus int, errMsg, rawPath, hash string) error

Complete marks a URL with a terminal (or retryable) status, recording the HTTP status, any error, the raw artifact path and hash, and bumping the attempt count. The fetched_at timestamp is set for done/blocked/skipped.

func (*Store) DBPath

func (s *Store) DBPath() string

func (*Store) Dir

func (s *Store) Dir() string

Dir, RawDir, ExportDir, DBPath expose the on-disk layout.

func (*Store) Enqueue

func (s *Store) Enqueue(f Frontier) (bool, error)

Enqueue inserts a frontier URL if it is not already present. It returns true when a new row was added. Re-seeding an existing URL is a no-op (its status and history are preserved).

func (*Store) ExportDir

func (s *Store) ExportDir() string

func (*Store) ExportJSONL

func (s *Store) ExportJSONL(ctx context.Context, entityType string, w io.Writer) (int, error)

ExportJSONL streams records of entityType (empty = all) to w, one JSON object per line. It returns the number of records written.

func (*Store) FrontierCounts

func (s *Store) FrontierCounts() (map[string]map[string]int, error)

FrontierCounts returns frontier row counts grouped by (entity_type, status).

func (*Store) GetMeta

func (s *Store) GetMeta(key string) (value string, ok bool, err error)

GetMeta returns the value for key, or ok=false if unset.

func (*Store) NextPending

func (s *Store) NextPending(limit int) ([]Frontier, error)

NextPending returns up to limit pending frontier rows, highest priority first. Callers coordinate dequeueing (a single feeder goroutine) so the same row is not handed to two workers; rows stay pending while in flight so an interrupted run resumes cleanly.

func (*Store) Pending

func (s *Store) Pending(types []string, source string, limit int) ([]Frontier, error)

Pending returns up to limit pending rows, optionally filtered to the given entity types and/or source, highest priority first. An empty types slice and empty source mean "any". Like NextPending, callers coordinate dequeueing.

func (*Store) PutRecord

func (s *Store) PutRecord(r Record) error

PutRecord upserts a normalized record.

func (*Store) QueueRows

func (s *Store) QueueRows(status, entityType string, limit int) ([]Frontier, error)

QueueRows returns frontier rows filtered by status and/or type, for inspection. Empty status or entityType means "any".

func (*Store) RawDir

func (s *Store) RawDir() string

func (*Store) ReadRaw

func (s *Store) ReadRaw(relPath string) ([]byte, error)

ReadRaw returns the decompressed bytes of a raw artifact given its relative path.

func (*Store) RecordCounts

func (s *Store) RecordCounts() (map[string]int, error)

RecordCounts returns record counts grouped by entity_type.

func (*Store) ResetFailed

func (s *Store) ResetFailed(entityType string) (int64, error)

ResetFailed moves failed rows back to pending so a later crawl retries them. An empty entityType resets every type. It returns the number requeued.

func (*Store) SetMeta

func (s *Store) SetMeta(key, value string) error

SetMeta and GetMeta store and read crawl cursors and counters.

func (*Store) WriteRaw

func (s *Store) WriteRaw(source, entityType, id, ext string, data []byte) (relPath, hash string, err error)

WriteRaw gzips data to raw/<source>/<type>/<shard>/<id>.<ext>.gz and returns the path relative to the mirror dir plus the sha256 of the uncompressed bytes. The shard is the last two digits of the id, capping directory fan-out.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL