rejetto forum

Software => HFS ~ HTTP File Server => Programmers corner => Topic started by: raybob on February 29, 2012, 04:02:31 AM

Title: Cool way to store data
Post by: raybob on February 29, 2012, 04:02:31 AM
This is used in FHFS... just thought I'd post it here since it can be extremely useful.

These functions just create simple files that store database-like values and can be retrieved at any time, on any page, with easy to use functions.  It functions somewhat like user-notes, except these values are global, not just for one user, and are stored in a separate file.

Put these under [request] in hfs.events:

Code: [Select]
{.set|loaddb|{:{.for each|value|{.replace|;|{.no pipe||.}|{.load|$1.db.}.}|{:{.if|{.^value.}|{:{.set table|$1|{.^value.}.}:}.}:}.}:}.}
{.set|writedb|{:{.^loaddb|$1.}{.save|$1.db|{.replace|$2={.from table|$1|$2.};||{.load|$1.db.}.}.}{.append|$1.db|$2=$3; .}{.^loaddb|$1.}:}.}
{.set|delkey|{:{.^loaddb|$1.}{.save|$1.db|{.replace|$2={.from table|$1|$2.};||{.load|$1.db.}.}.}{.^loaddb|$1.}:}.}

You use {.^loaddb|A.} to initialize a database for use, once per page, {.^writedb|A|B|C.} to write a new key or overwrite and old one, {.^delkey|A|B.} to delete a key and its value, and {.from table|A|B.} to expand to a value.

So if you want to write a value, you'd put in

Code: [Select]
{.^writedb|databasename|keyname|value.}

At the beginning of each page, before you can use {.from table.} to get a value, you must first load the database into memory, either using {.^loaddb|databasename.} or else {.^writedb.} which automatically loads the database, writes to it, and then reloads the new values.

So to call a value from a DB file, you might use something like:

Code: [Select]
{.^loaddb|databasename.}
{.from table|databasename|keyname|value.}

And then to delete a key, you would use

Code: [Select]
{.^delkey|databasename|keyname.}

For me, reading a db with 1000 keys took around 0.37 seconds consistently.  So it works pretty quickly.  

-- Raybob  :)
Title: Re: Cool way to store data
Post by: Dragon_Hunter on February 29, 2012, 09:52:23 PM
Hello! Where do you mean you can put this under the [request]

 Should it be under the [request] or at the end
Title: Re: Cool way to store data
Post by: raybob on February 29, 2012, 10:08:20 PM
Doesn't matter, as long as it's somewhere in the section.

For example, create a file called 'hfs.events' inside the same folder as hfs279.exe, and paste this into it.

Code: [Select]
{.set|loaddb|{:{.for each|value|{.replace|;|{.no pipe||.}|{.load|$1.db.}.}|{:{.if|{.^value.}|{:{.set table|$1|{.^value.}.}:}.}:}.}:}.}
{.set|writedb|{:{.^loaddb|$1.}{.save|$1.db|{.replace|$2={.from table|$1|$2.};||{.load|$1.db.}.}.}{.append|$1.db|$2=$3; .}{.^loaddb|$1.}:}.}
{.set|delkey|{:{.^loaddb|$1.}{.save|$1.db|{.replace|$2={.from table|$1|$2.};||{.load|$1.db.}.}.}{.^loaddb|$1.}:}.}

If you already have an events file with content under [request] then just add the 3 functions somewhere in your pre-existing script.