<commit_before>(ns wordbook.datastore
  (:require [com.ashafa.clutch :as clutch]))

(def db-name (or (System/getenv "WB_DATABASE_NAME") "wordbook"))
(def design-doc-name db-name)
(def db (clutch/get-database db-name))

(defn sync-views []
  (let [design-doc-id (str "_design/" design-doc-name)
        design-doc (or (clutch/get-document db design-doc-id)
                       {:_id design-doc-id})]
    (clutch/put-document db
      (merge design-doc
             {:views
              {:users-by-email
               {:map "function(doc) {
                       if (doc.type === 'user') {
                         emit(doc.email, null)
                       }
                      }"}
               :words-by-update-time
               {:map "function(doc) {
                        if (doc.type === 'word') {
                          emit(doc.updated, null)
                        }
                      }"}
               :words-by-word
               {:map "function(doc) {
                        if (doc.type === 'word') {
                          emit(doc.word, null)
                        }
                      }"}}}))))

(defn get-user-by-email [email]
  (let [users (clutch/get-view db design-doc-name :users-by-email
                               {:key email :include_docs true})]
    (:doc (first users))))

(defn put-word [word-data]
  (let [word-entity (merge {:type "word"} word-data)]
    (clutch/put-document db word-entity)))

(defn get-latest-words [limit]
  (map #(:doc %) (clutch/get-view db design-doc-name :words-by-update-time
                                  {:limit limit
                                   :include_docs true
                                   :descending true})))<commit_msg>Simplify map.<commit_after>(ns wordbook.datastore
  (:require [com.ashafa.clutch :as clutch]))

(def db-name (or (System/getenv "WB_DATABASE_NAME") "wordbook"))
(def design-doc-name db-name)
(def db (clutch/get-database db-name))

(defn sync-views []
  (let [design-doc-id (str "_design/" design-doc-name)
        design-doc (or (clutch/get-document db design-doc-id)
                       {:_id design-doc-id})]
    (clutch/put-document db
      (merge design-doc
             {:views
              {:users-by-email
               {:map "function(doc) {
                       if (doc.type === 'user') {
                         emit(doc.email, null)
                       }
                      }"}
               :words-by-update-time
               {:map "function(doc) {
                        if (doc.type === 'word') {
                          emit(doc.updated, null)
                        }
                      }"}
               :words-by-word
               {:map "function(doc) {
                        if (doc.type === 'word') {
                          emit(doc.word, null)
                        }
                      }"}}}))))

(defn get-user-by-email [email]
  (let [users (clutch/get-view db design-doc-name :users-by-email
                               {:key email :include_docs true})]
    (:doc (first users))))

(defn put-word [word-data]
  (let [word-entity (merge {:type "word"} word-data)]
    (clutch/put-document db word-entity)))

(defn get-latest-words [limit]
  (map :doc (clutch/get-view db design-doc-name :words-by-update-time
                             {:limit limit
                              :include_docs true
                              :descending true})))
{'fraction_non_alphanumeric': 0.13933951332560834, 'fraction_numerical': 0.0, 'mean_word_length': 1.2553886348791639, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5084664', 'n_tokens_mistral': 1028, 'n_tokens_neox': 987, 'n_words': 210}