Commits

llibra  committed d946f79

Initial import.

  • Participants

Comments (0)

Files changed (6)

+.*~$
+^test.lisp$
+(in-package :cl-user)
+(defpackage :lvdb.asdf (:use :cl :asdf))
+(in-package :lvdb.asdf)
+
+(defsystem :lvdb
+  :version "0.0"
+  :author "Manabu Takayama <learn.libra@gmail.com>"
+  :license "MIT License"
+  :depends-on (:alexandria :cffi)
+  :components ((:module "src"
+                        :serial t
+                        :components ((:file "packages")
+                                     (:file "ffi")
+                                     (:file "options")
+                                     (:file "db")))))
+(in-package :lvdb.db)
+
+(defun open (opt name)
+  (with-foreign-string (name-ptr name)
+    (with-foreign-object (err-ptr :pointer)
+      (setf (mem-aref err-ptr :pointer) (null-pointer))
+      (let ((db (leveldb-open opt name-ptr err-ptr)))
+        (if (null-pointer-p db)
+            (let ((ptr (mem-aref err-ptr :pointer)))
+              (unwind-protect (error (foreign-string-to-lisp ptr))
+                (free ptr)))
+            db)))))
+
+(defun close (db)
+  (leveldb-close db))

File src/ffi.lisp

+(in-package :lvdb.ffi)
+
+(define-foreign-library libleveldb
+  (:unix "libleveldb.so.0"))
+
+(use-foreign-library libleveldb)
+
+(defcfun "leveldb_options_create" :pointer)
+
+(defcfun "leveldb_options_destroy" :void
+  (options :pointer))
+
+(defcfun "leveldb_options_set_create_if_missing" :void
+  (opt :pointer)
+  (v :uchar))
+
+(defcfun "leveldb_open" :pointer
+  (options :pointer)
+  (name :pointer)
+  (errptr :pointer))
+
+(defcfun "leveldb_close" :void
+  (db :pointer))
+
+(defcfun "free" :void
+  (ptr :pointer))

File src/options.lisp

+(in-package :lvdb.opt)
+
+(defun set (opt
+            &key
+            (create-if-missing nil create-if-missing-p))
+  (when create-if-missing-p
+    (let ((v (convert-to-foreign create-if-missing :boolean)))
+      (leveldb-options-set-create-if-missing opt v))))
+
+(defun create (&rest args)
+  (let ((opt (leveldb-options-create)))
+    (apply #'set opt args)
+    opt))
+
+(defun destroy (opt)
+  (leveldb-options-destroy opt))
+
+(defmacro with-options ((var &rest args) &body body)
+  `(let ((,var (create ,@args)))
+     (unwind-protect (progn ,@body)
+       (destroy ,var))))

File src/packages.lisp

+(defpackage :leveldb.ffi
+  (:nicknames :lvdb.ffi)
+  (:use :cl :cffi)
+  (:export :free
+
+           :leveldb-options-create :leveldb-options-destroy
+           :leveldb-options-set-create-if-missing
+           :leveldb-open :leveldb-close))
+
+(defpackage :leveldb.options
+  (:nicknames :lvdb.opt)
+  (:use :cl :cffi :lvdb.ffi)
+  (:shadow :set)
+  (:export :set :create :destroy :with-options))
+
+(defpackage :leveldb.database
+  (:nicknames :lvdb.db)
+  (:use :cl :cffi :lvdb.ffi)
+  (:shadow :open :close)
+  (:export :open :close))