Repository navigation
Expand file tree
/
Copy pathPosts.js
More file actions
101 lines (95 loc) · 2.85 KB
/
Copy pathPosts.js
File metadata and controls
101 lines (95 loc) · 2.85 KB
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
import { DateTime, TimeZone } from "https://code4fukui.github.io/day-es/DateTime.js";
import { JSONLWriter } from "https://code4fukui.github.io/JSONL/JSONLWriter.js";
import { JSONLReader } from "https://code4fukui.github.io/JSONL/JSONLReader.js";
import { TAI64N } from "https://code4fukui.github.io/TAI64N-es/TAI64N.js";
import { CBOR } from "https://js.sabae.cc/CBOR.js";
import { CachedMap } from "https://code4fukui.github.io/CachedMap/CachedMap.js";
/*
posts/
yyyymmdd/
id.cbor
timeline.jsonl // { id }
cache map by id
latest
*/
const basedir = "data/";
const LATEST_N = 50;
const CACHE_N = 1000;
const TIMEZONE = TimeZone.JST;
export class Posts {
constructor(latest = []) {
this.latest = latest;
this.cachedmap = new CachedMap(CACHE_N);
}
static async create() {
const latest = [];
try {
const r = new JSONLReader(basedir + "/timeline.jsonl");
for (;;) {
const rec = await r.readRecord();
if (!rec) break;
latest.unshift(rec.id);
if (latest.length > LATEST_N) {
latest.length = LATEST_N;
}
}
const posts = new Posts(latest);
for (let i = 0; i < latest.length; i++) {
latest[i] = await posts.get(latest[i]);
}
return posts;
} catch (e) {
console.log("no timeline data")
}
return new Posts();
}
async add(post) { // post.data.id as TAI64N
await this.savePost(post);
this.updateLatest(post);
await this.updateTimeline(post);
return true;
}
getPathFromID(id) {
const tai64n = TAI64N.parse(id);
const date = TAI64N.toDate(tai64n);
const dt = new DateTime(date);
const ymd = dt.toLocal(TIMEZONE).day.toStringYMD();
const path = basedir + "/" + ymd;
return path + "/" + id + ".cbor";
}
async savePost(post) {
const fn = this.getPathFromID(post.data.id);
const path = fn.substring(0, fn.lastIndexOf("/"));
await Deno.mkdir(path, { recursive: true });
await Deno.writeFile(fn, CBOR.encode(post));
}
async get(id) {
const post = this.cachedmap.get(id);
if (post) return post;
const fn = this.getPathFromID(id);
const post2 = CBOR.decode(await Deno.readFile(fn));
this.cachedmap.set(id, post2);
return post2;
}
getLatest(lastdt = null) {
if (!lastdt) lastdt = TAI64N.stringify(TAI64N.fromYear(0));
const res = this.latest.filter(i => i.data.id.localeCompare(lastdt) > 0);
if (res.length) {
//console.log(res, res.length, lastdt); // , res[0].data.id)
}
return res;
}
updateLatest(post) {
//if (!post.data.parent) {
this.latest.unshift(post);
if (this.latest.length > LATEST_N) {
this.latest.length = LATEST_N;
}
//}
}
async updateTimeline(post) {
const w = new JSONLWriter(basedir + "/timeline.jsonl", true);
await w.writeRecord({ id: post.data.id });
w.close();
}
}