changelog shortlog graph tags branches changeset files file revisions raw help

Mercurial > core / annotate lisp/lib/obj/db.lisp

changeset 479: ff3b057402d1
parent: 8252ee515756
child: 686748796f08
author: Richard Westhaver <ellis@rwest.io>
date: Tue, 25 Jun 2024 22:28:30 -0400
permissions: -rw-r--r--
description: light cleanup
373
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
1
 ;;; lib/obj/db/proto.lisp --- Database Protocol
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
2
 
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
3
 ;;
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
4
 
387
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
5
 ;;; Commentary:
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
6
 
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
7
 ;; This set of 
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
8
 
373
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
9
 ;;; Code:
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
10
 (in-package :obj/db)
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
11
 
387
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
12
 ;;; Vars
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
13
 (declaim (sb-kernel:type-specifier *default-database-type* *default-database-collection-type*))
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
14
 (defparameter *default-database-type* 'vector)
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
15
 (defparameter *default-database-collection-type* 'list)
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
16
 
373
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
17
 ;;; Database
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
18
 (defgeneric db (self)
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
19
   (:documentation "Return the Database associated with SELF."))
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
20
 
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
21
 (defclass database ()
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
22
   ((db :initarg :db :accessor db)))
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
23
 
387
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
24
 (defclass database-collection () ())
373
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
25
 
387
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
26
 ;; TODO 2024-05-30: maybe make into a macro?
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
27
 (defgeneric make-db (engine &rest initargs &key &allow-other-keys)
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
28
   (:documentation "'make-instance' for databases. An ENGINE must be supplied, which is usually a key such as :ROCKSDB or :SQLITE."))
373
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
29
 
387
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
30
 (defgeneric connect-db (db &key &allow-other-keys)
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
31
   (:documentation "Connect the database DB."))
373
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
32
 
479
ff3b057402d1 light cleanup
Richard Westhaver <ellis@rwest.io>
parents: 387
diff changeset
33
 (defgeneric query-db (db query &key &allow-other-keys)
387
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
34
   (:documentation "Execute QUERY against DB."))
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
35
 
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
36
 (defgeneric db-get (db key &key &allow-other-keys)
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
37
   (:documentation "Return the value associated with KEY from DB."))
373
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
38
 
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
39
 (defgeneric (setf db-get) (db key val &key &allow-other-keys))
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
40
 
387
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
41
 (defgeneric close-db (db &key &allow-other-keys)
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
42
   (:documentation "Close the database DB."))
373
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
43
 
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
44
 (defgeneric open-db (self))
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
45
 
387
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
46
 (defgeneric destroy-db (self)
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
47
   (:documentation "Destroy all traces of a database, deleting any on-disk data and shutting down
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
48
 in-memory objects."))
373
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
49
 
387
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
50
 (defgeneric find-db (dbs name &key &allow-other-keys)
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
51
   (:documentation "Return the db by NAME, from a collection of databases DBS."))
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
52
 
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
53
 (defgeneric insert-db (dbs name &key &allow-other-keys)
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
54
   (:documentation "Inserts a database by NAME into the database-collection DBS."))
373
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
55
 
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
56
 ;;; Common
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
57
 (defun slot-val (instance slot-name)
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
58
   (if (and instance
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
59
            (slot-boundp instance slot-name))
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
60
       (slot-value instance slot-name)))
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
61
 
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
62
 (defgeneric get-val (object element &optional data-type)
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
63
   (:documentation "Returns the value in a object based on the supplied element name and possible
387
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
64
 type hints.")
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
65
   (:method (object element &optional data-type)
373
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
66
   (when object
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
67
     (typecase (or data-type object)
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
68
       (hash-table
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
69
        (gethash element object))
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
70
       (standard-object
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
71
        (slot-val object element))
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
72
       (t
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
73
        (if data-type
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
74
            (cond 
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
75
              ((equal 'alist data-type)
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
76
               (second (assoc element object :test #'equal)))
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
77
              ((equal 'plist data-type)
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
78
               (get object element))
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
79
              (t
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
80
               (error "Does not handle this type of object. Implement your own get-val method.")))
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
81
            (if (listp object)
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
82
                (second (assoc element object :test #'equal))
387
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
83
                (error "Does not handle this type of object. Implement your own get-val method."))))))))
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
84
     
373
9eb2c112aa16 refactor db stuff
Richard Westhaver <ellis@rwest.io>
parents:
diff changeset
85
 
387
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
86
 (defgeneric (setf get-val) (new-value object element &optional data-type)
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
87
   (:documentation "Set the value in a object based on the supplied element name and possible type
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
88
 hints.")
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
89
   (:method (new-value object element &optional data-type)
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
90
     (typecase (or data-type object)
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
91
       (hash-table (setf (gethash element object) new-value))
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
92
       (standard-object (setf (slot-value object element) new-value))
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
93
       (t
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
94
        (if data-type
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
95
            (cond ((equal 'alist data-type)
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
96
                   (replace object (list (list element new-value))))
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
97
                  ((equal 'plist data-type)
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
98
                   ;;TODO: Implement this properly.
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
99
                   (get object element ))
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
100
                  (t
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
101
                   (error "Does not handle this type of object. Implement your own get-val method.")))
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
102
            (if (listp object)
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
103
                (replace object (list (list element new-value)))
8252ee515756 db and readtables
Richard Westhaver <ellis@rwest.io>
parents: 373
diff changeset
104
                (error "Does not handle this type of object. Implement your own get-val method.")))))))