<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/.
;;
;; Copyright (c) 2016 <NAME> <<EMAIL>>

(ns storages.misc
  "A local filesystem storage implementation."
  (:require [promesa.core :as p]
            [cuerdas.core :as str]
            [buddy.core.codecs :as codecs]
            [buddy.core.codecs.base64 :as b64]
            [buddy.core.nonce :as nonce]
            [buddy.core.hash :as hash]
            [storages.proto :as pt]
            [storages.impl :as impl])
  (:import java.io.InputStream
           java.io.OutputStream
           java.nio.file.Path
           java.nio.file.Files
           org.apache.commons.io.FilenameUtils
           org.apache.commons.io.IOUtils))

;; --- Prefixed Storage

(defrecord PrefixedPathStorage [storage prefix]
  pt/IPublicStorage
  (-public-uri [_ path]
    (pt/-public-uri storage path))

  pt/IStorage
  (-save [_ path content]
    (let [^Path path (pt/-path path)
          ^Path path (.resolve ^Path prefix path)]
      (pt/-save storage path content)))

  (-delete [_ path]
    (pt/-delete storage path))

  (-exists? [this path]
    (pt/-exists? storage path))

  pt/ILocalStorage
  (-lookup [_ path]
    (pt/-lookup storage path)))

(defn prefixed
  "Create a composed storage instance that automatically prefixes
  the path when content is saved. For the rest of methods it just
  relies to the underlying storage.

  This is usefull for atomatically add sertain prefix to some
  uploads."
  [storage prefix]
  (let [prefix (pt/-path prefix)]
    (->PrefixedPathStorage storage prefix)))

;; --- Hashed Storage

(defn- concat-path
  [path & others]
  (reduce #(FilenameUtils/concat %1 %2) path others))

(defn- generate-path
  [^Path path]<commit_msg>Fix prefixed storage wrong impl.<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/.
;;
;; Copyright (c) 2016 <NAME> <<EMAIL>>

(ns storages.misc
  "A local filesystem storage implementation."
  (:require [promesa.core :as p]
            [cuerdas.core :as str]
            [buddy.core.codecs :as codecs]
            [buddy.core.codecs.base64 :as b64]
            [buddy.core.nonce :as nonce]
            [buddy.core.hash :as hash]
            [storages.proto :as pt]
            [storages.impl :as impl])
  (:import java.io.InputStream
           java.io.OutputStream
           java.nio.file.Path
           java.nio.file.Files))

;; --- Prefixed Storage

(defrecord PrefixedPathStorage [storage ^Path prefix]
  pt/IPublicStorage
  (-public-uri [_ path]
    (let [^Path path (pt/-path [prefix path])]
      (pt/-public-uri storage path)))

  pt/IStorage
  (-save [_ path content]
    (let [^Path path (pt/-path [prefix path])]
      (->> (pt/-save storage path content)
           (p/map (fn [^Path path]
                    (.relativize prefix path))))))

  (-delete [_ path]
    (let [^Path path (pt/-path [prefix path])]
      (pt/-delete storage path)))

  (-exists? [this path]
    (let [^Path path (pt/-path [prefix path])]
      (pt/-exists? storage path)))

  pt/ILocalStorage
  (-lookup [_ path]
    (let [^Path path (pt/-path [prefix path])]
      (pt/-lookup storage path))))

(defn prefixed
  "Create a composed storage instance that automatically prefixes
  the path when content is saved. For the rest of methods it just
  relies to the underlying storage.

  This is usefull for atomatically add sertain prefix to some
  uploads."
  [storage prefix]
  (let [prefix (pt/-path prefix)]
    (->PrefixedPathStorage storage prefix)))

;; --- Hashed Storage

(defn- concat-path
  [path & others]
  (reduce #(FilenameUtils/concat %1 %2) path others))

(defn- generate-path
  [^Path path]
{'fraction_non_alphanumeric': 0.12687515891177217, 'fraction_numerical': 0.007119247393846936, 'mean_word_length': 2.5861440291704647, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '1845720', 'n_tokens_mistral': 1285, 'n_tokens_neox': 1206, 'n_words': 410}