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
|
(ns db-populate
(:import java.lang.Math)
(:require [clojure.contrib.generic.collection :as gc])
(:use clojure.contrib.sql
clojure.contrib.probabilities.finite-distributions
clojure.contrib.probabilities.monte-carlo
clojure.contrib.probabilities.random-numbers
utils))
(def *num-users* 500)
(def *mean-posts-per-user* 200)
(def *std-dev-posts-per-user* 1.5)
(def *image-msg-frequency* 0.5)
(def *nick-prefix* "user")
(def *sample-message-sentences*
["Aim at new love victories",
"Bomb her womb from your huge battleship",
"Face your new mate without fear",
"Get armed for a new love battle",
"Upgrade your weapon used to make love"])
(defn sample-unit []
(first (gc/seq rand-stream)))
(defn sample-lognormal [mean sigma]
(first (gc/seq (random-stream (lognormal (Math/log mean) sigma)
rand-stream))))
(defn rand-elt [coll]
(nth coll (rand-int (count coll))))
(defn nick-func [i]
(str *nick-prefix* "-" i "-" (System/currentTimeMillis)))
(defn hash-func [i]
(str "HASH"))
(defn email-func [i]
(str *nick-prefix* "-" i "@" i ".com"))
(defn make-user [label]
(println "Creating user" label)
(let [qry (str "INSERT INTO users (nick, hash, email) "
"VALUES (?, ?, ?) RETURNING user_id")
nick (nick-func label)
hash (hash-func label)
email (email-func label)
res (do-select [qry nick hash email])]
((first res) :user_id)))
(defn sample-text-msg []
(rand-elt *sample-message-sentences*))
(defn sample-image-msg []
"http://localhost:8080/images/20100310/1268271054246-abc-b7413897775a39087737d54768adca55d3c97cf4.jpeg")
(defn sample-msg-contents []
(if (<= *image-msg-frequency* (sample-unit))
[(sample-image-msg) true]
[(sample-text-msg) false]))
(defn sample-room-id []
1)
(defn make-messages [user-id]
(let [num-msgs (int (sample-lognormal *mean-posts-per-user*
*std-dev-posts-per-user*))]
(println "Creating" num-msgs "messages for userid" user-id)
(doseq [i (range num-msgs)]
(let [[content is-image] (sample-msg-contents)
room-id (sample-room-id)
qry (str "INSERT INTO messages "
"(user_id, room_id, content, is_image) "
"VALUES (?, ?, ?, ?) RETURNING message_id")]
(do-select [qry user-id room-id content is-image])))))
(defn populate [n-str]
(let [n (Integer/parseInt n-str)]
(doall (map (comp make-messages make-user) (range n)))))
(defn clear []
(println "Clearing all users starting with" *nick-prefix*)
(let [params ["nick LIKE ?" (str *nick-prefix* "%")]
cnt (first (do-delete "users" params))]
(println "Cleared" cnt "users")))
(def *command-map* {"populate" populate "clear" clear})
(defn parse-command-args
([] (prn "Usage: db_populate.clj command num"))
([cmd-str & more]
(if-let [cmd (*command-map* cmd-str)]
(apply cmd more)
(println "Unknown command '" cmd-str "'"))))
(apply parse-command-args (rest *command-line-args*))
|