unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Against sqlite3!!! (Was: sqlite3)
@ 2021-12-07  7:44 Qiantan Hong
  0 siblings, 0 replies; 27+ messages in thread
From: Qiantan Hong @ 2021-12-07  7:44 UTC (permalink / raw)
  To: tomas@tuxteam.de; +Cc: emacs-devel@gnu.org

[-- Attachment #1: Type: text/plain, Size: 77 bytes --]

I’ve attached a pure Emacs Lisp implementation of persistent key value store.

[-- Attachment #2: resist!.el --]
[-- Type: application/octet-stream, Size: 1624 bytes --]

;;; resist!.el --- Against SQLite3!  -*- lexical-binding: t; -*-

(defvar kv-store-table)
(defmacro --kv (key value)
  (puthash key value kv-store-table)
  nil)
(cl-defstruct (kv-store (:constructor make-kv-store-1)) path table)
(defun make-kv-store (path)
  (let* ((kv-store (make-kv-store-1 :path path))
         (kv-store-table (make-hash-table :test 'equal))
         need-compactification)
    (when (file-exists-p path)
      (condition-case c
          (load-file path)
        (end-of-file
         ;; We might encounter trailing unbalanced form if Emacs
         ;; crashed in the middle of `kv-put'.  We compact the file
         ;; and fix unbalanced form as a side effect
         (setq need-compactification t)))
      (setf (kv-store-table kv-store) kv-store-table))
    (when need-compactification
      (compact-kv-store kv-store))
    kv-store))
(defsubst kv-put-log (key value kv-store)
  (let ((print-length nil) (print-level nil))
    (prin1 `(--kv ,key ,value) (current-buffer)))
  (insert "\n"))
(defun compact-kv-store (kv-store)
  ;; dump the full content of kv-store-table at once
  ;; to compress the log
  (with-temp-buffer
    (maphash (lambda (key value) (kv-put-log key value kv-store))
             (kv-store-table kv-store))
    (let ((file-precious-flag t))
      (write-file (kv-store-path kv-store)))))
(defun kv-put (key value kv-store)
  (with-temp-buffer
    (kv-put-log key value kv-store)
    (append-to-file nil nil (kv-store-path kv-store)))
  (puthash key value (kv-store-table kv-store)))
(defun kv-get (key kv-store)
  (gethash key (kv-store-table kv-store)))
(provide 'resist!)

^ permalink raw reply	[flat|nested] 27+ messages in thread

end of thread, other threads:[~2021-12-09 19:28 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <MN2PR12MB3391BC76A0D05236AC76C94E946E9@MN2PR12MB3391.namprd12.prod.outlook.com>
2021-12-07  8:13 ` Against sqlite3!!! (Was: sqlite3) Qiantan Hong
2021-12-07  9:14   ` Qiantan Hong
2021-12-07 12:49   ` Against sqlite3!!! Colin Baxter 😺
2021-12-07 13:21   ` Stefan Monnier
2021-12-07 13:55     ` Qiantan Hong
2021-12-07 15:51       ` Tassilo Horn
2021-12-07 16:35         ` Qiantan Hong
2021-12-07 18:43           ` Arthur Miller
2021-12-07 19:13             ` Qiantan Hong
2021-12-07 19:34           ` Tassilo Horn
2021-12-08 10:00             ` Yuri Khan
2021-12-07 19:52           ` Stefan Monnier
2021-12-07 13:45   ` Against sqlite3!!! (Was: sqlite3) Zhu Zihao
2021-12-07 14:50     ` Against sqlite3!!! David Engster
2021-12-07 20:00       ` Lars Ingebrigtsen
2021-12-08  6:11         ` Arthur Miller
2021-12-08  6:20           ` Qiantan Hong
2021-12-08  9:21             ` Arthur Miller
2021-12-08  9:28               ` Qiantan Hong
2021-12-09  7:12           ` Alexandre Garreau
2021-12-09  7:27             ` Qiantan Hong
     [not found]               ` <24465971.J1OoJ6LT5i@galex-713.eu>
2021-12-09  7:50                 ` Qiantan Hong
2021-12-09 19:16                   ` Thierry Volpiatto
2021-12-09 19:24                     ` Qiantan Hong
2021-12-09 19:28                     ` Qiantan Hong
2021-12-09 13:17               ` Stefan Monnier
2021-12-07  7:44 Against sqlite3!!! (Was: sqlite3) Qiantan Hong

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).