Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

65 lines (57 sloc) 2.25 kB
(ns foreclojure.mongo
(:use somnium.congomongo
[ :only [load-problems]]
[foreclojure.config :only [config]]
[foreclojure.problems :only [total-solved get-problem-list]]
[foreclojure.users :only [get-users]]))
(defn connect-to-db []
(let [{:keys [db-user db-pwd db-host]} config]
:host (or db-host "localhost")
:db "mydb")
(when (and db-user db-pwd)
(authenticate db-user db-pwd))))
(defn prepare-problems []
(when-not (fetch-one :problems)
(add-index! :problems [:solved]))
(defn prepare-seqs []
(update! :seqs
{:_id "problems"}
{:$set {:seq (->> (fetch :problems :only [:_id])
(map :_id)
(apply max)
;; make it easier to get off the ground by marking contributors automatically
;; useful since some "in-development" features aren't enabled for all users
(defn prepare-users []
(add-index! :users [:user] :unique true)
(add-index! :users [[:solved -1]])
(update! :users
{:user {:$in ["amalloy" "dbyrne" "raynes" "cmeier" "devn"
"citizen428" "daviddavis" "clinteger"]}}
{:$set {:contributor true}}
:upsert false
:multiple true))
(defn prepare-solutions []
(add-index! :solutions [:user :problem]))
(defn reconcile-solved-count
"Overwrites the times-solved field in the problems collection based on data from the users collection. Should only be called on server startup since it isn't a safe operation. Also updates the total-solved agent."
(let [total (->> (get-users)
(mapcat :solved)
(reduce (fn [sum [id solved]]
(update! :problems
{:_id id}
{:$set {:times-solved solved}})
(+ sum solved))
(send total-solved (constantly total))))
(defn prepare-mongo []
Jump to Line
Something went wrong with that request. Please try again.