emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/ebdb 1a31515 274/350: Fixes to record loading


From: Eric Abrahamsen
Subject: [elpa] externals/ebdb 1a31515 274/350: Fixes to record loading
Date: Mon, 14 Aug 2017 11:46:52 -0400 (EDT)

branch: externals/ebdb
commit 1a3151512475b00bd8476d210a099ba0acf479b3
Author: Eric Abrahamsen <address@hidden>
Commit: Eric Abrahamsen <address@hidden>

    Fixes to record loading
    
    * ebdb.el (ebdb-db-add-record): Don't call `ebdb-db-load-records'
      here. It seemed like a neat way to re-use the loading procedure, but
      the uuid check will raise an error with the record's own self as a
      duplicate. There's no need to use something that deep anyway, this
      method should be kept straightforward. This fixes moving/copying
      records between databases.
      (ebdb-delete-record, ebdb-init-record): Clean up code in here
      somewhat.
    * ebdb-test.el (ebdb-test-database-2): Add a second test database.
      (ebdb-test-with-database): Fix dumb macro mistake.
      (ebdb-load-record-multiple-databases): Test multiple
---
 ebdb-test.el | 28 ++++++++++++++++++++++------
 ebdb.el      | 30 ++++++++++++++----------------
 2 files changed, 36 insertions(+), 22 deletions(-)

diff --git a/ebdb-test.el b/ebdb-test.el
index 7bc43cc..cbedfad 100644
--- a/ebdb-test.el
+++ b/ebdb-test.el
@@ -42,7 +42,7 @@
      ;; Save sets sync-time.
      (ebdb-db-save ,(car db-and-filename))
      ;; Load adds to `ebdb-db-list'.
-     (ebdb-db-load db)
+     (ebdb-db-load ,(car db-and-filename))
      (unwind-protect
         (progn
           ,@body)
@@ -61,6 +61,11 @@
                               "emacs-ebdb-test-db-1-"
                               temporary-file-directory)))
 
+(defvar ebdb-test-database-2 (make-temp-name
+                             (expand-file-name
+                              "emacs-ebdb-test-db-2-"
+                              temporary-file-directory)))
+
 (ert-deftest ebdb-make-database ()
   "Make a database and save it to disk."
   (ebdb-test-with-database (db ebdb-test-database-1)
@@ -77,10 +82,9 @@
 (ert-deftest ebdb-database-unsynced ()
   "Make sure database knows it's unsynced."
   (ebdb-test-with-database (db ebdb-test-database-1)
-    ;; Sync-time doesn't get updated until we load it.
-    (ebdb-db-load db)
-    ;; Apparently the two calls are too close together to register a
-    ;; difference in time, which I find weird.
+    ;; Apparently the call to `ebdb-db-load' and the test are too
+    ;; close together to register a difference in time, which I find
+    ;; weird.
     (sit-for 0.1)
     (append-to-file "\n;; Junk string" nil (slot-value db 'file))
     (should (ebdb-db-unsynced db))))
@@ -97,7 +101,19 @@
       (let ((rec (make-instance 'ebdb-record-person)))
        (should (null (ebdb-record-uuid rec)))
        (ebdb-db-add-record db rec)
-       (should (stringp (ebdb-record-uuid rec)))))))
+       (should (stringp (ebdb-record-uuid rec)))
+       (should (ebdb-gethash (ebdb-record-uuid rec) 'uuid))))))
+
+(ert-deftest ebdb-load-record-multiple-databases ()
+  "Test loading of a record into multiple databases."
+  (ebdb-test-with-records
+    (ebdb-test-with-database (db1 ebdb-test-database-1)
+      (ebdb-test-with-database (db2 ebdb-test-database-2)
+       (let ((rec (make-instance 'ebdb-record-person)))
+         (ebdb-db-add-record db1 rec)
+         (ebdb-db-add-record db2 rec)
+         (should (= 1 (length ebdb-record-tracker)))
+         (should (equal rec (ebdb-gethash (ebdb-record-uuid rec) 'uuid))))))))
 
 ;; Test adding, deleting and changing fields.
 
diff --git a/ebdb.el b/ebdb.el
index b5edf24..d0632fe 100644
--- a/ebdb.el
+++ b/ebdb.el
@@ -2447,23 +2447,19 @@ priority."
      class (plist-put slots :name name))))
 
 (cl-defmethod ebdb-init-record ((record ebdb-record-person))
-  (ebdb-init-field (slot-value record 'name) record)
-  (dolist (aka (slot-value record 'aka))
-    (ebdb-init-field aka record))
-  (dolist (relation (slot-value record 'relations))
-    (ebdb-init-field relation record))
-  (dolist (role (slot-value record 'organizations))
-    (ebdb-init-field role record))
+  (with-slots (name aka relations organizations) record
+    (when name
+      (ebdb-init-field name record))
+    (dolist (f (append aka relations organizations))
+      (ebdb-init-field f record)))
   (cl-call-next-method))
 
 (cl-defmethod ebdb-delete-record ((record ebdb-record-person) &optional _db 
unload)
-  (ebdb-delete-field (slot-value record 'name) record unload)
-  (dolist (a (slot-value record 'aka))
-    (ebdb-delete-field a record unload))
-  (dolist (r (slot-value record 'relations))
-    (ebdb-delete-field r record unload))
-  (dolist (o (slot-value record 'organizations))
-    (ebdb-delete-field o record unload))
+  (with-slots (name aka relations organizations) record
+    (when name
+      (ebdb-delete-field name record unload))
+    (dolist (f (append aka relations organizations))
+      (ebdb-delete-field f record unload)))
   (cl-call-next-method))
 
 (cl-defmethod ebdb-merge ((left ebdb-record-person)
@@ -3246,9 +3242,10 @@ the persistent save, or allow them to propagate."
     (setf (slot-value record 'uuid)
          (make-instance
           'ebdb-field-uuid
-          :uuid (ebdb-make-uuid (slot-value db 'uuid-prefix)))))
+          :uuid (ebdb-make-uuid (slot-value db 'uuid-prefix))))
+    (ebdb-puthash (ebdb-record-uuid record) record))
   (object-add-to-list db 'records record)
-  (ebdb-db-load-records db (list record))
+  (object-add-to-list (ebdb-record-cache record) 'database db)
   (setf (slot-value db 'dirty) t)
   ;; TODO: Is there any need to sort the DB's records after insertion?
   ;; What about sorting ebdb-record-tracker?
@@ -3258,6 +3255,7 @@ the persistent save, or allow them to propagate."
   (object-remove-from-list db 'records record)
   (object-remove-from-list (ebdb-record-cache record)
                           'database db)
+  (setf (slot-value db 'dirty) t)
   record)
 
 (cl-defmethod ebdb-db-add-record-field :before ((db ebdb-db) record _slot 
_field)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]