                              (domain/host-shards (domains-info domain)))))
     ))
 
+(defn domain-needs-update? [local-vs remote-vs]
+  (< (.mostRecentVersion local-vs)
+     (.mostRecentVersion remote-vs)))
+
+(defn use-cache-or-update [domain domains-info global-config local-config]
+  (let [fs (filesystem (:hdfs-conf local-config))
+        lfs (local-filesystem)
+        local-dir (:local-dir local-config)
+        local-domain-root (str (path local-dir domain))
+        remote-path (-> global-config (:domains) (get domain))
+        remote-vs (DomainStore. fs remote-path)
+        local-vs (DomainStore. lfs local-domain-root (.getSpec remote-vs))]
+    (if (domain-needs-update? local-vs remote-vs)
+      (do
+        (doto (local-filesystem) (clear-dir local-domain-root))  ;; clear domain dir first
+        (load-domain fs
+                     local-config
+                     local-domain-root
+                     remote-path
+                     (domain/host-shards (domains-info domain))))
+      ;; use cached domain from local-dir (no update needed)
+      (open-domain local-config
+                   (str (path local-dir domain))
+                   (domain/host-shards (domains-info domain)))
+      )))
+
+(defn sync-data-updated [domains-info global-config local-config]
+  (load-and-sync-status domains-info
+                        (fn [domain]
+                          (use-cache-or-update domain domains-info global-config local-config))))
+
+
 (defn- sync-global [global-config local-config token]
   (log-message "Loading remote data")
   (let [fs (filesystem (:hdfs-conf local-config))

    domains-info
    ))

;; should delete any domains that don't exist in config as well 
;; returns map of domain to domain info and launches futures that will fill in the domain info
(defn- sync-data [global-config local-config token]
  (if (cache? global-config token)
    (sync-local global-config local-config)
    (sync-global global-config local-config token)
    ))

(defn- close-lps [domains-info]

     ))
    domains-info ))

(defn sync-updated [global-config local-config token]
  "Only fetch domains from remote if a newer version is available.
Keep the cached versions of any domains that haven't been updated"
  (log-message "Loading remote data if necessary, otherwise loading cached data")
  (let [fs (filesystem (:hdfs-conf local-config))
        domains-info (init-domain-info-map fs global-config)
        cache-config (assoc global-config :token token)]
    (log-message "Domains info: " domains-info)
    (future
      (try
        (sync-data-updated domains-info global-config local-config)
        (log-message "Caching global config " cache-config)
        (cache-global-config! local-config cache-config)
        (log-message "Cached config " (read-cached-global-config local-config))
        (log-message "Finished loading all updated domains from remote")
        (catch Throwable t (log-error t "Error when syncing data") (throw t))
        ))
    domains-info ))

(defn sync-local [global-config local-config]
  (log-message "Loading cached data")
