<commit_before>;;; pastebin-db.el --- backend databases for the pastebin package

;; This is free and unencumbered software released into the public domain.

;;; Commentary:

;; This is a pluggable database API with two implementations:

;; * hash-table (no on-disk storage)
;; * flat-file

;; New implementations only need to create a class that implements the
;; two generic functions, `pastebin-db-get' and `pastebin-db-put'.

;;; Code:

(require 'json)
(require 'eieio)

(defstruct db-entry
  "Represents a single, immutable database entry."
  (content :read-only t)
  (language :read-only t)
  (expiration :read-only t)
  (title :read-only t)
  (parent :read-only t)
  (time :read-only t))

(defun db-entry-to-json (entry)
  "Encode a DB entry into a JSON string."
  (json-encode `((content . ,(db-entry-content entry))
                 (language . ,(db-entry-language entry))
                 (expiration . ,(db-entry-expiration entry))
                 (title . ,(db-entry-title entry))
                 (parent . ,(db-entry-parent entry))
                 (time . ,(db-entry-time entry)))))

(defun db-entry-from-json (string)
  "Turn a JSON expression into a DB entry."
  (let ((json (json-read-from-string string)))
    (macrolet ((ref (slot) `(cdr (assoc (quote ,slot) json))))
      (make-db-entry :content (ref content)
                     :language (ref language)
                     :expiration (+ (float-time) (ref expiration))
                     :title (ref title)
                     :parent (ref parent)
                     :time (float-time)))))

(defun db-entry-alive-p (entry)
  "Return T if the entry has not expired."
  (< (float-time) (db-entry-expiration entry)))

(defgeneric pastebin-db-get (db id)
  "Get paste entry ID from database DB. Returns NIL if ID doesn't
exist. The ID passed to this method should already be sanitized.")

(defgeneric pastebin-db-put (db id entry)
  "Put a paste entry into database DB, returning ENTRY. The ID<commit_msg>Oops, set default values for entries.<commit_after>;;; pastebin-db.el --- backend databases for the pastebin package

;; This is free and unencumbered software released into the public domain.

;;; Commentary:

;; This is a pluggable database API with two implementations:

;; * hash-table (no on-disk storage)
;; * flat-file

;; New implementations only need to create a class that implements the
;; two generic functions, `pastebin-db-get' and `pastebin-db-put'.

;;; Code:

(require 'json)
(require 'eieio)

(defstruct db-entry
  "Represents a single, immutable database entry."
  (content "" :read-only t)
  (language "" :read-only t)
  (expiration 0 :read-only t)
  (title "" :read-only t)
  (parent nil :read-only t)
  (time 0 :read-only t))

(defun db-entry-to-json (entry)
  "Encode a DB entry into a JSON string."
  (json-encode `((content . ,(db-entry-content entry))
                 (language . ,(db-entry-language entry))
                 (expiration . ,(db-entry-expiration entry))
                 (title . ,(db-entry-title entry))
                 (parent . ,(db-entry-parent entry))
                 (time . ,(db-entry-time entry)))))

(defun db-entry-from-json (string)
  "Turn a JSON expression into a DB entry."
  (let ((json (json-read-from-string string)))
    (macrolet ((ref (slot) `(cdr (assoc (quote ,slot) json))))
      (make-db-entry :content (ref content)
                     :language (ref language)
                     :expiration (+ (float-time) (ref expiration))
                     :title (ref title)
                     :parent (ref parent)
                     :time (float-time)))))

(defun db-entry-alive-p (entry)
  "Return T if the entry has not expired."
  (< (float-time) (db-entry-expiration entry)))

(defgeneric pastebin-db-get (db id)
  "Get paste entry ID from database DB. Returns NIL if ID doesn't
exist. The ID passed to this method should already be sanitized.")

(defgeneric pastebin-db-put (db id entry)
  "Put a paste entry into database DB, returning ENTRY. The ID
{'fraction_non_alphanumeric': 0.13027202395807339, 'fraction_numerical': 0.0004991265285749938, 'mean_word_length': 2.6469517743403093, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1147233', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1209, 'n_words': 429}