Shop OBEX P1 Docs P2 Docs Learn Events
FSRW and indexing question — Parallax Forums

FSRW and indexing question

Don MDon M Posts: 1,652
edited 2014-03-06 10:23 in Propeller 1
I am writing fixed length (30 bytes) strings to a file on sd card using fsrw. Each new string is stored using the "append" mode. Works great.

Now my question- lets say I wanted to re-write the last string that was stored. Is there some sort of index pointer that is kept track of that I could "back up" and re-store a new string in the place of the last one?

And to take it a step further- would it be possible to re-write any one of those fixed length strings?

Thanks.
Don

Comments

  • Mike GMike G Posts: 2,702
    edited 2014-03-06 04:02
    Read the file into memory, find the row to update, update the found row, write all the data back to the SD card.

    What's the max number of 30 byte records? Can you post an example of a 30 byte record?
  • Heater.Heater. Posts: 21,230
    edited 2014-03-06 04:40
    This file system object has a fileSeek method so that you can easily access any part of the file you like:

    http://obex.parallax.com/object/16

  • Don MDon M Posts: 1,652
    edited 2014-03-06 08:43
    Mike G wrote: »
    Read the file into memory, find the row to update, update the found row, write all the data back to the SD card.

    This seems like brute force :-). Was thinking of trying to do something more on the fly.
    Mike G wrote: »
    What's the max number of 30 byte records?

    The file size as reported by FSRW is typically 1908 to 2524 bytes (?) The number of 30 byte strings is typically 16 to 30.
    Mike G wrote: »
    Can you post an example of a 30 byte record?

    "0824 This is one example ",13 It's a basic text string with a carriage return at the end.

    I was thinking it would be fairly easy to use the reported file size and subtract backwards to make my way through the strings but I don't know that I can tell FSRW to store the new 30 byte string at x location.
  • Don MDon M Posts: 1,652
    edited 2014-03-06 08:44
    Heater. wrote: »
    This file system object has a fileSeek method so that you can easily access any part of the file you like:

    http://obex.parallax.com/object/16

    Hmm... I see that but was hoping to use FSRW if possible.
  • Mike GMike G Posts: 2,702
    edited 2014-03-06 10:23
    Don M wrote: »
    This seems like brute force :-). Was thinking of trying to do something more on the fly.

    The file size as reported by FSRW is typically 1908 to 2524 bytes (?) The number of 30 byte strings is typically 16 to 30

    "0824 This is one example ",13 It's a basic text string with a carriage return at the end.

    I was thinking it would be fairly easy to use the reported file size and subtract backwards to make my way through the strings but I don't know that I can tell FSRW to store the new 30 byte string at x location.

    I would describe the method as simplistic. I guess brute force works too. The file is small so disk IO is not too bad; 30*30=900 bytes.

    If I understand, the idea is to...
    UPDATE file.txt WHERE [the data field] = <old 30 byte> string by SETing the [the data field] to the <new 30 byte> string

    To find the 30 byte string, assuming the index is unknown, copy the file contents from the SD card to a memory buffer in the prop.
    Loop through the buffer until the old string value is found. The loop count is the index if the old string value.
    BYTEMOVE the new value over the old value
    Write the buffer back to the the SD card.

    If the SD driver has seek capabilities, replace BYTEMOVE with SEEK/WRITE or do both a BYTEMOVE and SEEK/WRITE

    Now decide if keeping the buffered data makes sense. If there are a lot of updates, an in memory table might make sense. Maybe create a hash table to speed up the the search...

    If the data file has many records, consider ordering the data and use a binary search. However, ordered data makes inserts tricky.
Sign In or Register to comment.