Posted 05 June 2012 - 08:05 AM
Download
Overview:
So, I decided to try my hand at coding up a persistence API, with all the bells and whistles, to hopefully make it the API of choice for the very few people who don't decide to make one from scratch :)/>/> This API does NOT go in the apis folder, you just call shell.run("path/to/persistence") from wherever you have a dependency on it and it creates a table "persistence" that has all the functions in it, so you don't have to muck around with registering it or whatnot.
It uses metatables to be simple, minimalistic, all while preserving data types (number, boolean, string, table). It translates nested tables into nested folders. Essentially, you can create a "persistence table" by calling persistence.new("folder"), and any values you store to that table are stored to the folder, and any values you read from the table are read from the folder, automatically. You can easily read through folder and see all the data and data types, and even edit it manually (easy, but not recommended, no real point). Also, any tables you read out from a persistence table will also be persistence tables. There are also a number of utility functions included to make the persistence tables easier to work with, since they don't function QUITE like normal tables.
Example:
To store the initial position of a turtle, I can do the following:
Utility functions:
I plan to add a couple more features for custom data type storage (I have some of the hooks in already) but I will get to that as I need it. For now, if you have a table with a metatable (for those of you who know what that is) it will just be saved like any other table by iterating through the pairs(), stripping the metatable information.
Please post your comments, suggestions, and criticisms!
Overview:
So, I decided to try my hand at coding up a persistence API, with all the bells and whistles, to hopefully make it the API of choice for the very few people who don't decide to make one from scratch :)/>/> This API does NOT go in the apis folder, you just call shell.run("path/to/persistence") from wherever you have a dependency on it and it creates a table "persistence" that has all the functions in it, so you don't have to muck around with registering it or whatnot.
It uses metatables to be simple, minimalistic, all while preserving data types (number, boolean, string, table). It translates nested tables into nested folders. Essentially, you can create a "persistence table" by calling persistence.new("folder"), and any values you store to that table are stored to the folder, and any values you read from the table are read from the folder, automatically. You can easily read through folder and see all the data and data types, and even edit it manually (easy, but not recommended, no real point). Also, any tables you read out from a persistence table will also be persistence tables. There are also a number of utility functions included to make the persistence tables easier to work with, since they don't function QUITE like normal tables.
Example:
To store the initial position of a turtle, I can do the following:
data = persistence.new("datastore")
data.location = {x=10, y=-432,z=0,o=2}
Now I can restart the turtle and the data will be still be there. To access any of it, I can do anything like the following examples:
data = persistence.new("datastore")
print(data.location.x)
data = persistence.new("datastore")
data["location"]["y"] = 49
location = persistence.new("datastore/location")
location.z = location.z + 1
It should be fairly straightforward, but if you have any question, feel free to ask!Utility functions:
- persistence.path(ptable): returns the filesystem path of the given persistence table's folder
- persistence.list(ptable): returns a numbered list of keys in the table
- persistence.pairs(ptable): The pairs() function is broken for persistence tables so use this as a drop in replacement.
- persistence.table(ptable) returns a the table in the native, NON persistent format, for when you want the contained variables to NOT all be backed up to the filesystem.
I plan to add a couple more features for custom data type storage (I have some of the hooks in already) but I will get to that as I need it. For now, if you have a table with a metatable (for those of you who know what that is) it will just be saved like any other table by iterating through the pairs(), stripping the metatable information.
Please post your comments, suggestions, and criticisms!