all repos — homestead @ 477702a8fedd215586025ba7f27f9eed9970dfac

Code for my website

internal/storage/sqlite/writer.go (view raw)

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
package sqlite

import (
	"database/sql"
	"fmt"
	"hash/fnv"
	"io"
	"mime"
	"net/http"
	"path/filepath"
	"time"

	"github.com/andybalholm/brotli"
	"github.com/klauspost/compress/gzip"
	"github.com/klauspost/compress/zstd"
	"go.alanpearce.eu/homestead/internal/buffer"
	"go.alanpearce.eu/homestead/internal/content"
	"go.alanpearce.eu/homestead/internal/storage"
	"go.alanpearce.eu/x/log"

	"gitlab.com/tozd/go/errors"
	_ "modernc.org/sqlite" // import registers db/SQL driver
)

var encodings = []string{"gzip", "br", "zstd"}

type Writer struct {
	db *sql.DB

	options *Options
	log     *log.Logger
	queries struct {
		insertURL     *sql.Stmt
		insertFile    *sql.Stmt
		insertContent *sql.Stmt
	}
}

type Options struct {
	Compress bool
}

func OpenDB(dbPath string) (*sql.DB, errors.E) {
	db, err := sql.Open(
		"sqlite",
		fmt.Sprintf(
			"file:%s?mode=%s&_pragma=foreign_keys(1)&_pragma=mmap_size(%d)",
			dbPath,
			"rwc",
			16*1024*1024,
		),
	)
	if err != nil {
		return nil, errors.WithStack(err)
	}

	return db, nil
}

func NewWriter(db *sql.DB, logger *log.Logger, opts *Options) (*Writer, errors.E) {
	_, err := db.Exec(`
		CREATE TABLE IF NOT EXISTS url (
			url_id INTEGER PRIMARY KEY,
			path TEXT NOT NULL
		);
		CREATE UNIQUE INDEX IF NOT EXISTS url_path
			ON url (path);

		CREATE TABLE IF NOT EXISTS file (
			file_id INTEGER PRIMARY KEY,
			url_id INTEGER NOT NULL,
			content_type TEXT NOT NULL,
			last_modified INTEGER NOT NULL,
			title TEXT NOT NULL,
			etag TEXT NOT NULL,
			style_hash TEXT NOT NULL,
			FOREIGN KEY (url_id) REFERENCES url (url_id)
		);
		CREATE UNIQUE INDEX IF NOT EXISTS file_url_content_type
			ON file (url_id, content_type);

		CREATE TABLE IF NOT EXISTS content (
			content_id INTEGER PRIMARY KEY,
			file_id INTEGER NOT NULL,
			encoding TEXT NOT NULL,
			body BLOB NOT NULL,
			FOREIGN KEY (file_id) REFERENCES file (file_id)
		);
		CREATE UNIQUE INDEX IF NOT EXISTS file_content
			ON content (file_id, encoding);
	`)
	if err != nil {
		return nil, errors.WithMessage(err, "creating tables")
	}

	w := &Writer{
		db:      db,
		log:     logger,
		options: opts,
	}

	w.queries.insertURL, err = db.Prepare(`INSERT INTO url (path) VALUES (?)`)
	if err != nil {
		return nil, errors.WithMessage(err, "preparing insert URL statement")
	}

	w.queries.insertFile, err = db.Prepare(`
		INSERT INTO file (url_id, content_type, last_modified, etag, style_hash, title)
		VALUES (:url_id, :content_type, :last_modified, :etag, :style_hash, :title)
	`)
	if err != nil {
		return nil, errors.WithMessage(err, "preparing insert file statement")
	}

	w.queries.insertContent, err = db.Prepare(`
		INSERT INTO content (file_id, encoding, body)
		VALUES (:file_id, :encoding, :body)
	`)
	if err != nil {
		return nil, errors.WithMessage(err, "preparing insert content statement")
	}

	return w, nil
}

func (s *Writer) Mkdirp(string) errors.E {
	return nil
}

func (s *Writer) storeURL(path string) (int64, errors.E) {
	r, err := s.queries.insertURL.Exec(path)
	if err != nil {
		return 0, errors.WithMessagef(err, "inserting URL %s into database", path)
	}

	id, err := r.LastInsertId()
	if err != nil {
		return 0, errors.WithStack(err)
	}

	return id, nil
}

func (s *Writer) storeFile(urlID int64, file *storage.File) (int64, errors.E) {
	if file.ContentType == "" {
		file.ContentType = http.DetectContentType(file.Encodings["identity"].Bytes())
		s.log.Warn(
			"file has no content type, sniffing",
			"path",
			file.Path,
			"sniffed",
			file.ContentType,
		)
	}
	r, err := s.queries.insertFile.Exec(
		sql.Named("url_id", urlID),
		sql.Named("content_type", file.ContentType),
		sql.Named("last_modified", file.LastModified.Unix()),
		sql.Named("etag", file.Etag),
		sql.Named("style_hash", file.StyleHash),
		sql.Named("title", file.Title),
	)
	if err != nil {
		return 0, errors.WithMessage(err, "inserting file into database")
	}

	id, err := r.LastInsertId()
	if err != nil {
		return 0, errors.WithStack(err)
	}

	return id, nil
}

func (s *Writer) storeEncoding(fileID int64, encoding string, data []byte) errors.E {
	_, err := s.queries.insertContent.Exec(
		sql.Named("file_id", fileID),
		sql.Named("encoding", encoding),
		sql.Named("body", data),
	)
	if err != nil {
		return errors.WithMessagef(
			err,
			"inserting encoding into database file_id: %d encoding: %s",
			fileID,
			encoding,
		)
	}

	return nil
}

func etag(content []byte) (string, errors.E) {
	hash := fnv.New64a()
	hash.Write(content)

	return fmt.Sprintf(`W/"%x"`, hash.Sum(nil)), nil
}

func contentType(pathname string) string {
	return mime.TypeByExtension(filepath.Ext(pathNameToFileName(pathname)))
}

func (s *Writer) NewFileFromPost(post *content.Post) *storage.File {
	file := &storage.File{
		Title:        post.Title,
		Path:         post.URL,
		LastModified: post.Date,
		Encodings:    map[string]*buffer.Buffer{},
	}

	return file
}

func (s *Writer) WritePost(post *content.Post, content *buffer.Buffer) errors.E {
	s.log.Debug("storing post", "title", post.Title)

	return s.WriteFile(s.NewFileFromPost(post), content)
}

func (s *Writer) Write(pathname string, title string, content *buffer.Buffer) errors.E {
	file := &storage.File{
		Title:        title,
		Path:         pathname,
		LastModified: time.Now(),
		Encodings:    map[string]*buffer.Buffer{},
	}

	return s.WriteFile(file, content)
}

func (s *Writer) WriteFile(file *storage.File, content *buffer.Buffer) errors.E {
	s.log.Debug("storing content", "pathname", file.Path)

	urlID, err := s.storeURL(file.Path)
	if err != nil {
		return errors.WithMessage(err, "storing URL")
	}

	if file.Encodings == nil {
		file.Encodings = map[string]*buffer.Buffer{}
	}
	file.Encodings["identity"] = content

	if file.ContentType == "" {
		file.ContentType = contentType(file.Path)
	}

	if file.Etag == "" {
		file.Etag, err = etag(content.Bytes())
		if err != nil {
			return errors.WithMessage(err, "could not calculate file etag")
		}
	}

	err = file.CalculateStyleHash()
	if err != nil {
		return errors.WithMessage(err, "calculating file hash")
	}

	fileID, err := s.storeFile(urlID, file)
	if err != nil {
		return errors.WithMessage(err, "storing file")
	}

	err = s.storeEncoding(fileID, "identity", content.Bytes())
	if err != nil {
		return err
	}

	if s.options.Compress {
		for _, enc := range encodings {
			compressed, err := compress(enc, content)
			if err != nil {
				return errors.WithMessage(err, "compressing file")
			}

			err = s.storeEncoding(fileID, enc, compressed.Bytes())
			if err != nil {
				return err
			}

		}
	}

	return nil
}

func compress(encoding string, content *buffer.Buffer) (*buffer.Buffer, errors.E) {
	var w io.WriteCloser
	compressed := new(buffer.Buffer)
	switch encoding {
	case "gzip":
		w = gzip.NewWriter(compressed)
	case "br":
		w = brotli.NewWriter(compressed)
	case "zstd":
		var err error
		w, err = zstd.NewWriter(compressed)
		if err != nil {
			return nil, errors.WithMessage(err, "could not create zstd writer")
		}
	}
	defer w.Close()

	if err := content.SeekStart(); err != nil {
		return nil, errors.WithMessage(err, "seeking to start of content buffer")
	}
	if _, err := io.Copy(w, content); err != nil {
		return nil, errors.WithMessage(err, "compressing file")
	}

	return compressed, nil
}