3

I am trying to use the eieio-persistent class to make persistent objects. My goal is to store many objects in a file.

I am not sure how to achieve this though. If I use this code, I do get a persistent object saved in test.db, but (perhaps predictably) the second save overwrites the first one.

(defclass animal (eieio-persistent)
  ((type :initarg :type)
   (file :initform "/Users/jkitchin/Dropbox/org-mode/test.db")))


(let ((a1 (animal :type 'mammal)))
  (eieio-persistent-save a1))

(let ((a1 (animal :type 'rodent)))
  (eieio-persistent-save a1))

Is there a way to save a list of these "animals" using the eieio framework? I would prefer not to create individual files for each object (I am anticipating thousands of objects later that need to be read in). Ideally, there would be another class that had a list of these animal objects in a records field.

Something like this is close

(defclass animal-db (eieio-named eieio-persistent)
  ((id :initarg :id)
   (file :initform "/Users/jkitchin/Dropbox/org-mode/test.db")
   (records :initarg :records :type listp))
  "Base db for animal class")

but the list in the records doesn't appear to be actual objects, when I read the code back in, but code that needs to be eval'd to get the object.

Any ideas?

John Kitchin
  • 11,555
  • 1
  • 19
  • 41
  • I see an `eieio-persistent-convert-list-to-object` function: perhaps you can convert the list to a single object and save that? Mind you, my eieio knowledge is less than $\epsilon$. – NickD Mar 24 '17 at 15:33
  • That doesn't seem to be the purpose of that function, it seems to take a list of object creation terms, e.g. a class, name and slots. I thought I could use it on each record to create the objects, but it doesn't work for a reason I don't understand yet. – John Kitchin Mar 24 '17 at 16:48
  • Yes, you are right. Reading the eieio-persistent section in the doc seems to indicate that each object is associated with its own file, so you'd have to extend the class. ("IIUC" is implied :-) ) – NickD Mar 24 '17 at 17:05
  • So if you eval the code your problem is solved? Why is that insufficient? – Lyn Headley Dec 29 '17 at 01:27
  • I guess I would not say it is insufficient, just unexpected I guess. Maybe it makes sense, it only reads in the top object, not all the records internally. It looks like it would take a custom reader for something like that. – John Kitchin Dec 30 '17 at 03:08

1 Answers1

0

If you want to store objects by the group rather than per instance, a more natural pattern (with a more out-of-the-box implementation) may be a collector or composite object.

Based only on the examples provided in your question, you may be able to apply eieio-persistent only on some levels of your hierarchy, perhaps adding classes just for grouping purposes. Obviously, this may unacceptably complicate ones modeling.

This uses oset and oref to work with the list but I might take the time to create custom accessors methods if I planned to work with these objects from various points in code.

(defclass animal () ((type :initarg :type)))

(defclass zoo (eieio-persistent)
  ((animals :initarg :menagerie :initform nil)
   (file :initform "~/where-ever-you/test.db")))

(let ((z1 (zoo)))

  (let ((a1 (animal :type 'mammal)))
    (oset z1 animals (append (oref z1 animals) (list a1)))
    (eieio-persistent-save z1))

  (let ((a1 (animal :type 'rodent)))
    ;; sorry pal
    ;;(oset z1 animals (append (oref z1 animals) a1))
    (eieio-persistent-save z1))

  (let ((a1 (animal :type 'unicorn)))
    (oset z1 animals (append (oref z1 animals) (list a1)))
    (eieio-persistent-save z1)))

For me (GNU Emacs 26.3 for Windows 10) this creates a file containing:

;; Object zoo
;; EIEIO PERSISTENT OBJECT
(zoo "zoo"
  :file "test.db"
  :menagerie
  (list
    (animal "animal"
      :type mammal)
    (animal "animal"
      :type unicorn)))