<commit_before><commit_msg>Add buffer-store implementation.<commit_after>(ns blocks.store.buffer
  "Logical block storage which buffers new blocks being written to a backing
  store. Reads return a unified view of the existing and buffered blocks. The
  buffer can be _flushed_ to write all the new blocks to the backing store."
  (:require
    (blocks
      [core :as block]
      [util :as util])
    [blocks.store.memory :refer [memory-store]]))


(defrecord BufferStore
  [buffer store]

  block/BlockStore

  (stat
    [this id]
    (or (block/stat buffer id)
        (block/stat store  id)))


  (-list
    [this opts]
    (util/merge-block-lists
      (block/-list buffer opts)
      (block/-list store  opts)))


  (-get
    [this id]
    (or (block/-get buffer id)
        (block/-get store  id)))


  (put!
    [this block]
    (or (block/get store (:id block))
        (block/put! buffer block)))


  (delete!
    [this id]
    (let [buffered? (block/delete! buffer id)
          stored?   (block/delete! store  id)]
      (boolean (or buffered? stored?)))))


(defn buffer-store
  "Creates a new buffering block store. If no buffer store is given, defaults to
  an in-memory store."
  ([store]
   (buffer-store store (memory-store)))
  ([store buffer]
   (BufferStore. store buffer)))


;; Remove automatic constructor functions.
(ns-unmap *ns* '->BufferStore)
(ns-unmap *ns* 'map->BufferStore)
{'fraction_non_alphanumeric': 0.12304409672830725, 'fraction_numerical': 0.0, 'mean_word_length': 2.8337874659400546, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6690266', 'n_tokens_mistral': 457, 'n_tokens_neox': 433, 'n_words': 154}