Permalink
Browse files

Basic scripting of couchstore.

Example script:

local function dump_docinfo(docinfo)
   print(string.format("  ID:     %s", docinfo:id()))
   print(string.format("  dbseq:  %d", docinfo:db_seq()))
   print(string.format("  rev:    %d", docinfo:rev()))
   print(string.format("  cas:    %d", docinfo:cas()))
   print(string.format("  exp:    %d", docinfo:exp()))
   print(string.format("  flags:  %d", docinfo:flags()))
   print(string.format("  del:    %d", docinfo:deleted()))
   print(string.format("  cmeta:  %d", docinfo:content_meta()))
   print(string.format("  len:    %d", #docinfo))
end

-- Open a DB (create if it doesn't exist)
local db = couch.open("/tmp/lua.couch", true)

-- Throw 100 keys in
for i = 0, 100, 1 do
   local k = "key" .. i
   print(string.format("Storing %s", k))
   db:save(k, "data for " .. i, 1)
end
db:commit()

-- Save a document with meta info
db:save("somekey", "someval", 1, 0, 8345752, 24, 10)
db:commit()

-- Grab it and show what we've got.
local data, docinfo = db:get("somekey")
dump_docinfo(docinfo)
print(string.format("Data: ``%s''", data))

print("---- iterating ----")

-- This does a bunch of junk after digging through the DB.
-- Considering only documents that haven't been deleted, grab values
-- from every even numbered doc by db_seq.  Print out stuff from the
-- rest.
db:changes(0, function(db, di)
                 -- Note that iteration returns deleted docs (obviously).
                 if di:deleted() == 0 then
                    if di:db_seq() % 2 == 0 then
                       local data = db:get_from_docinfo(di)
                       print(string.format("%s (%d, %d, %d, %d) -> %s",
                                           di:id(), di:db_seq(), di:cas(),
                                           di:exp(), di:flags(), data))
                    else
                       print(string.format("%s (%d, %d, %d, %d) [deleting]",
                                           di:id(), di:db_seq(), di:cas(),
                                           di:exp(), di:flags()))
                       db:delete(di:id())
                    end
                 end
              end)

db:commit()
db:close()
  • Loading branch information...
1 parent cdfb20f commit 018d840c5049292ed3dcfcb9da5619883f09eb37 @dustin dustin committed Mar 1, 2012
Showing with 2,320 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +8 −0 Makefile.am
  3. +666 −0 config/config.rpath
  4. +2 −0 configure.ac
  5. +110 −0 m4/lib-ld.m4
  6. +767 −0 m4/lib-link.m4
  7. +221 −0 m4/lib-prefix.m4
  8. +42 −0 m4/pandora_have_liblua.m4
  9. +503 −0 src/couchscript.cc
View
@@ -32,6 +32,7 @@
/configure
/couch_dbdump
/couch_dbinfo
+/couchscript
/docs/doxy
/libcouchstore*.changes
/libcouchstore*.deb
View
@@ -64,6 +64,14 @@ couch_dbinfo_LDADD = libcouchstore.la
#writetest_DEPENDENCIES = libcouchstore.la
#writetest_LDADD = libcouchstore.la
+if HAVE_LIBLUA
+bin_PROGRAMS += couchscript
+
+couchscript_SOURCES = src/couchscript.cc
+couchscript_LDADD = libcouchstore.la $(LTLIBLUA)
+couchscript_DEPENDENCIES = libcouchstore.la
+endif
+
#tests
check_PROGRAMS = testapp
TESTS = ${check_PROGRAMS}
Oops, something went wrong.

0 comments on commit 018d840

Please sign in to comment.