 (ns appengine-magic.services.datastore
   (:import [com.google.appengine.api.datastore DatastoreService DatastoreServiceFactory
-            Key KeyFactory KeyRange
+            DatastoreServiceConfig DatastoreServiceConfig$Builder
+            ReadPolicy ReadPolicy$Consistency ImplicitTransactionManagementPolicy
+            Key KeyFactory
             Entity
             FetchOptions$Builder
             Query Query$FilterOperator Query$SortDirection]))


(defonce *datastore-service* (atom nil))
(defonce *current-transaction* nil)


(defonce *filter-operator-map*

  (when (nil? @*datastore-service*)
    (reset! *datastore-service* (DatastoreServiceFactory/getDatastoreService)))
  @*datastore-service*)


(defn init-datastore-service [& {:keys [deadline read-policy implicit-transaction-policy]}]
  (let [datastore-config-object (DatastoreServiceConfig$Builder/withDefaults)]
    (when deadline
      (.deadline datastore-config-object deadline))
    (when read-policy
      (.readPolicy
       datastore-config-object
       (ReadPolicy. (get *datastore-read-policy-map* read-policy))))
    (when implicit-transaction-policy
      (.implicitTransactionManagementPolicy
       datastore-config-object
       (get *datastore-implicit-transaction-policy-map* implicit-transaction-policy)))
    (reset! *datastore-service*
            (DatastoreServiceFactory/getDatastoreService datastore-config-object))
    @*datastore-service*))


(defprotocol EntityProtocol
