Author Topic: Cool way to store data  (Read 1186 times)

0 Members and 1 Guest are viewing this topic.

Offline raybob

  • Tireless poster
  • ****
  • Posts: 453
    • View Profile
    • FileSplat.com
Cool way to store data
« 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  :)
« Last Edit: March 01, 2012, 12:05:12 AM by raybob »

Offline Dragon_Hunter

  • Occasional poster
  • *
  • Posts: 52
    • View Profile
Re: Cool way to store data
« Reply #1 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

Offline raybob

  • Tireless poster
  • ****
  • Posts: 453
    • View Profile
    • FileSplat.com
Re: Cool way to store data
« Reply #2 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.
« Last Edit: March 01, 2012, 05:11:40 AM by raybob »