Browse Source

Worked on performance issues

pull/1/head
Vincent Truchseß 10 months ago
parent
commit
d90f28dff4
3 changed files with 38 additions and 23 deletions
  1. +2
    -1
      src/saljut/sbot.clj
  2. +2
    -2
      src/saljut/server.clj
  3. +34
    -20
      src/saljut/util/server.clj

+ 2
- 1
src/saljut/sbot.clj View File

@@ -3,6 +3,7 @@
(:require [saljut.util.io :as sio])
(:require [saljut.util.sbot :as ut])
(:require [clojure.java.io :as io])
(:require [clojure.string :as st])
(:gen-class))

(defn get-latest-msg [pubkey domain]
@@ -56,6 +57,6 @@
(if (get als id)
als
(if (and al (= id about))
(assoc als id al)
(assoc als id (st/lower-case al))
als))))
{} (ut/get-messages-by-type "about")))

+ 2
- 2
src/saljut/server.clj View File

@@ -16,7 +16,7 @@
path (if (= "/" (:uri request))
"/index.html"
(:uri request))
file (us/serve-file domain key32 path)]
content (us/serve-file domain key32 path)]
{:status 200
:headers {"ContentType" (m/case-re path
#".*\.html$" "text/html"
@@ -25,7 +25,7 @@
#".*\.xml" "application/xml"
#".*" "")
"Connection" "close"}
:body (new java.io.FileInputStream file)})
:body (new java.io.ByteArrayInputStream content)})
;; Handle domain.alias.ssb hostnames
#"^([a-zA-Z0-9]+\.){2}ssb$"
(let [[domain al] (st/split hostname #"\.")


+ 34
- 20
src/saljut/util/server.clj View File

@@ -1,18 +1,22 @@
(ns saljut.util.server
(:require [clojure.java.io :as io]
[saljut.sbot :as sbt]
[saljut.util.sbot :as sbtu]
[saljut.util.codec :as co]
[clojure.string :as st])
(:gen-class))

(def inventory (atom {}))

(def name-table (atom {:timestamp 0}))

(def content (atom {}))

(defn serve-file [domain key32 path]
"Ensures the most resent version of \"domain.key32.b32.ssb\" is present in the
cache-dir. Returns a file-object at path from the webroot"
(let [site-dir (io/file "/tmp/saljut" key32 domain)
content-dir (io/file site-dir "content")
metafile (io/file site-dir "meta.edn")
metadata (if (.exists metafile)
(read-string (slurp metafile))
(let [metadata (if-let [m (get-in @inventory [key32 domain])]
m
{:timestamp 0,
:sequence 0,
:manifest {}})
@@ -23,22 +27,32 @@
(doseq [[k v] (filter (fn [[k v]] (not (= (get (:manifest metadata) k) v)))
(:manifest msg))]
(println (str "Downloading " k))
(sbt/download-file v (io/file content-dir k))))
(spit metafile (str (assoc msg :timestamp timestamp))))))
(io/file content-dir (st/replace path #"^/" ""))))
(swap! content assoc-in [key32 domain k] (sbtu/get-blob v))))
(swap! inventory assoc-in [key32 domain]
(assoc msg :timestamp timestamp)))))
(get-in @content [key32 domain (st/replace path #"^/" "")])))

(defn reverse-index [forward-index]
(reduce (fn [index [k n]]
(if-let [ks (get index n)]
(assoc index n (conj ks k))
(assoc index n [k])))
{:timestamp (quot (System/currentTimeMillis) 1000)} forward-index))


(defn refresh-name-table []
(let [forward-index (sbt/gen-alias-map)]
(swap! name-table (fn [o n] n)
(reduce (fn [index [k n]]
(if-let [ks (get index n)]
(assoc index n (conj ks k))
(assoc index n [k])))
{:timestamp (quot (System/currentTimeMillis) 1000)} forward-index))))


(defn alias-lookup [s]
"Returns a seq of strings containing all SSB-IDs found with s as current
self-assigned alias"
(let [timestamp (quot (System/currentTimeMillis) 1000)
alias-file (io/file "/tmp/saljut/alias.edn")
alias-data (if (.exists alias-file)
(read-string (slurp alias-file))
{:timestamp timestamp
:aliases (sbt/gen-alias-map)})
alias-data (if (< 3600 (- timestamp (:timestamp alias-data)))
(assoc alias-data :aliases (sbt/gen-alias-map))
alias-data)]
(.mkdirs (io/file (.getParent (io/file alias-file))))
(spit alias-file alias-data)
(map first (filter (fn [[_ al]] (= (st/lower-case s) (st/lower-case al))) (:aliases alias-data)))))
(if (< 3600 (- (quot (System/currentTimeMillis) 1000) (:timestamp @name-table)))
(refresh-name-table))
(get @name-table s))

Loading…
Cancel
Save