<commit_before>;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.

(ns containium.systems.cassandra.alia
  "The Alia 2 implementation of the Cassandra system."
  (:require [qbits.alia :as alia]
            [containium.systems :refer (require-system Startable Stoppable)]
            [containium.systems.config :as config :refer (Config)]
            [containium.systems.cassandra :refer (Cassandra cql-statements)]
            [containium.systems.logging :as logging :refer (SystemLogger refer-logging)]))
(refer-logging)


(def ^:dynamic *consistency* nil)

(def ^:dynamic *keywordize* false)


(defn- prepare*
  [{:keys [session]} query-str]
  (alia/prepare session query-str))


(defn- do-prepared*
  [{:keys [session]} statement opts values]
  (let [args (merge {:consistency *consistency*, :keywordize? *keywordize*} opts {:values values})]
    (assert (:consistency opts) "Missing :consistency key and *consistency* not bound.")
    (alia/execute session statement args)))


(defn- has-keyspace*
  [record name]
  (let [pq (prepare* record "SELECT * FROM system.schema_keyspaces WHERE keyspace_name = ?;")]
    (not (empty? (do-prepared* record pq {:consistency :one} [name])))))


(defn- write-schema*
  [record schema-str]
  (doseq [s (cql-statements schema-str)
          :let [ps (prepare* record s)]]
    (do-prepared* record ps {:consistency :one} nil)))


(defrecord AliaCassandra [cluster session logger]
  Cassandra
  (prepare [this query-str]
    (prepare* this query-str))

  (do-prepared [this statement]
    (do-prepared* this statement nil nil))

  (do-prepared [this statement opts-values]
    (cond (sequential? opts-values) (do-prepared* this statement nil opts-values)
          (map? opts-values) (do-prepared* this statement opts-values (:values opts-values))
          :else (throw (IllegalArgumentException.
                        "Parameter opts-values must be a map or sequence."))))

  (do-prepared [this statement opts values]
    (do-prepared* this statement opts values))

  (has-keyspace? [this name]
    (has-keyspace* this name))

  (keyspaced [this name]
    (AliaCassandra. cluster (alia/connect cluster name) logger))

  (write-schema [this schema-str]
    (write-schema* this schema-str))

  Stoppable
  (stop [this]
    (info logger "Stopping Alia 2 system...")
    (alia/shutdown cluster)
    (info logger "Alia 2 system stopped.")))


(defn alia
  "Create a new AliaCassandra Startable, using the specified key to
  lookup the connection details in the Config system."
  [config-key]
  (reify Startable
    (start [_ systems]
      (let [config (config/get-config (require-system Config systems) config-key)
            logger (require-system SystemLogger systems)
            _ (info logger "Starting Alia 2 system, using config:" config)
            cluster (alia/cluster config)
            session (alia/connect cluster)]
        (info logger "Alia 2 system started.")
        (AliaCassandra. cluster session logger)))))
<commit_msg>Update Alia system to disable keywordizing results by default again<commit_after>;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.

(ns containium.systems.cassandra.alia
  "The Alia 2 implementation of the Cassandra system."
  (:require [qbits.alia :as alia]
            [containium.systems :refer (require-system Startable Stoppable)]
            [containium.systems.config :as config :refer (Config)]
            [containium.systems.cassandra :refer (Cassandra cql-statements)]
            [containium.systems.logging :as logging :refer (SystemLogger refer-logging)]))
(refer-logging)


(def ^:dynamic *consistency* nil)

(def ^:dynamic *keywordize* false)


(defn- prepare*
  [{:keys [session]} query-str]
  (alia/prepare session query-str))


(defn- do-prepared*
  [{:keys [session]} statement opts values]
  (let [args (merge {:consistency *consistency*, :string-keys? (not *keywordize*)}
                    (when-let [keys? (:keywordize? opts)] {:string-keys? (not keys?)})
                    opts {:values values})]
    (assert (:consistency opts) "Missing :consistency key and *consistency* not bound.")
    (alia/execute session statement args)))


(defn- has-keyspace*
  [record name]
  (let [pq (prepare* record "SELECT * FROM system.schema_keyspaces WHERE keyspace_name = ?;")]
    (not (empty? (do-prepared* record pq {:consistency :one} [name])))))


(defn- write-schema*
  [record schema-str]
  (doseq [s (cql-statements schema-str)
          :let [ps (prepare* record s)]]
    (do-prepared* record ps {:consistency :one} nil)))


(defrecord AliaCassandra [cluster session logger]
  Cassandra
  (prepare [this query-str]
    (prepare* this query-str))

  (do-prepared [this statement]
    (do-prepared* this statement nil nil))

  (do-prepared [this statement opts-values]
    (cond (sequential? opts-values) (do-prepared* this statement nil opts-values)
          (map? opts-values) (do-prepared* this statement opts-values (:values opts-values))
          :else (throw (IllegalArgumentException.
                        "Parameter opts-values must be a map or sequence."))))

  (do-prepared [this statement opts values]
    (do-prepared* this statement opts values))

  (has-keyspace? [this name]
    (has-keyspace* this name))

  (keyspaced [this name]
    (AliaCassandra. cluster (alia/connect cluster name) logger))

  (write-schema [this schema-str]
    (write-schema* this schema-str))

  Stoppable
  (stop [this]
    (info logger "Stopping Alia 2 system...")
    (alia/shutdown cluster)
    (info logger "Alia 2 system stopped.")))


(defn alia
  "Create a new AliaCassandra Startable, using the specified key to
  lookup the connection details in the Config system."
  [config-key]
  (reify Startable
    (start [_ systems]
      (let [config (config/get-config (require-system Config systems) config-key)
            logger (require-system SystemLogger systems)
            _ (info logger "Starting Alia 2 system, using config:" config)
            cluster (alia/cluster config)
            session (alia/connect cluster)]
        (info logger "Alia 2 system started.")
        (AliaCassandra. cluster session logger)))))

{'fraction_non_alphanumeric': 0.12231893068075847, 'fraction_numerical': 0.0027976375505129004, 'mean_word_length': 3.3216924110141033, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6743368', 'n_tokens_mistral': 2019, 'n_tokens_neox': 1886, 'n_words': 651}