Lua Module Shelve

lua-users home
wiki

Difference (from prior major revision) (no other diffs)

Changed: 1c1
=== Shelve ===
shelve is a Lua module that transparently encodes Lua values (numbers, strings, booleans and tables) into files. Each stored element is accessible through a key, so you can treat a shelf file like you would treat a regular Lua table.

Changed: 3,7c3
== description ==

This module performs transparent encoding of Lua values (numbers, strings, booleans and tables) into files. Each stored element is accessible through a key, so you can treat a shelf file like you would treat a regular Lua table.

== quick tips ==
== Quick Tips ==

Changed: 11,12c7,8
{{{
data = shelve.open("my_data_file")
{{{!Lua
data = shelve.open("my_data_file")

Changed: 17,35c13,31
{{{
-- store some values
data.aNumber = 10
data.aString = "this is a string"
data.aTable =
{
"these", "are", "some", "strings", "contained",
"in", "a", "nested", "table",
nested = true,
items = 9
}

-- retrieve values, and print them
print(data.aString)
print("items: " .. data.aTable)

for k,v in data.aTable do
print(k,v)
end
{{{!Lua
-- store some values
data.aNumber = 10
data.aString = "this is a string"
data.aTable =
{
"these", "are", "some", "strings", "contained",
"in", "a", "nested", "table",
nested = true,
items = 9
}

-- retrieve values, and print them
print(data.aString)
print("items: " .. data.aTable)

for k,v in data.aTable do
print(k,v)
end

Changed: 40,45c36,41
{{{
-- retrieve keys and print them
keys = data()
for key in keys do
print(k)
end
{{{!Lua
-- retrieve keys and print them
keys = data()
for key in keys do
print(k)
end

Changed: 50,54c46,50
{{{
-- retrieve keys and print them
for key in data() do
print(k)
end
{{{!Lua
-- retrieve keys and print them
for key in data() do
print(k)
end

Changed: 59,60c55,56
{{{
data = nil
{{{!Lua
data = nil

Changed: 63c59
== author ==
== Author ==

Changed: 67,68c63

=== Downloads ===
== Download ==

Changed: 74c69
You will need Lua 5.0 beta or above, and either Gdbm or Ndbm. Gdbm is recommended.
== Dependencies ==

Added: 75a71
You will need Lua 5.0 beta or above, and either Gdbm or Ndbm. Gdbm is recommended.

Removed: 77d72


shelve is a Lua module that transparently encodes Lua values (numbers, strings, booleans and tables) into files. Each stored element is accessible through a key, so you can treat a shelf file like you would treat a regular Lua table.

Quick Tips

Use the shelve.open() function in order to create or open a new shelf file, like this:

data = shelve.open("my_data_file")

The data variable acts lika a Lua table, so you can store keyed values and read them using the known Lua syntax:

-- store some values
data.aNumber = 10
data.aString = "this is a string"
data.aTable  = 
  {
     "these", "are", "some", "strings", "contained",
     "in", "a", "nested", "table",
     nested = true,
     items  = 9
  }

-- retrieve values, and print them
print(data.aString)
print("items: " .. data.aTable)

for k,v in data.aTable do
  print(k,v)
end

If you want to know which keys are present in i shelf file, just do a call, as if the shelf was a function, and it will return a table with the same keys as the shelf file:

-- retrieve keys and print them
keys = data()
for key in keys do
  print(k)
end

or

-- retrieve keys and print them
for key in data() do
  print(k)
end

To manually close a shelf file, simply assign nil to it, and the Lua GarbageCollection will close it for you. Just stick this line to the example above to close the file:

data = nil

Author

AdrianPerez

Download

At this moment it's only available as source code. Latest version is 0.33. Version prior to 0.33 were buggy, so updating to version 0.33 or above is highly recommended.

Dependencies

You will need Lua 5.0 beta or above, and either Gdbm or Ndbm. Gdbm is recommended. Current version was tested with MacOS X 10.2 (Jaguar) and Linux.


RecentChanges · preferences
edit · history
Last edited October 31, 2009 10:12 pm GMT (diff)