 (ns elephantdb.shard
-  (:use [elephantdb util config])
+  (:use [elephantdb util config log])
   (:import [elephantdb Utils]))
 
 (defstruct shard-index ::hosts-to-shards ::shards-to-hosts)



(defn compute-host-to-shards [domain hosts numshards replication]
  (when (> replication (count hosts))
    (throw (IllegalArgumentException. "Replication greater than number of servers")))
  (let [shards (repeat-seq replication (range numshards))

    ))

(defn shard-domain [domain hosts numshards replication]
  (log-message "sharding domain " domain hosts numshards replication)
  (let [hosts-to-shards (compute-host-to-shards domain hosts numshards replication)]
      (struct shard-index hosts-to-shards (map-mapvals set (reverse-multimap hosts-to-shards)))
    ))

(defn shard-domains [fs global-config]
  (log-message "shard-domains" global-config)
  (into {}
        (dofor [[domain remote-location] (:domains global-config)]
               (let [domain-spec (read-domain-spec fs remote-location)]
                 (println "shard-domains domain-spec" domain-spec)
                 [domain (shard-domain
                          domain
                          (:hosts global-config)
                          (:num-shards domain-spec)
                          (:replication global-config))]
                 ))))

(defn host-shards [index host]
  ((::hosts-to-shards index) host))
