msiemens / tinydb

TinyDB is a lightweight document oriented database optimized for your happiness :)
https://tinydb.readthedocs.org
MIT License
6.84k stars 549 forks source link

3 Insert calls to table, only 2 go through #566

Closed MartinKurtz closed 3 months ago

MartinKurtz commented 3 months ago

This is from a piece of software for a students project for a computermuseum, dealing with all kinds of weird data formats, and it has a split operation that splits one file into 2 subfiles by inserting a second subfile into the DB and modifying the first to be the first subfile after the split.

Excerpts of my Code:

The original Insertion of the file into the DB

        with open(file_path, 'w') as procfile:
            procfile.write(sha + '\n')

        print("CREATING FILE WITH SHA")
        self.files.insert({"sha256": sha,
                           "path": file.file_path,
                           "begin": file.startInOriginalFile,
                           "end": file.endInOriginalFile,
                           "length": file.fileLength,
                           "binAddrLength": file.binAddrLength,
                           "subfiles":
                               [
                                   sha + "." + file.startInOriginalFile + "." + file.endInOriginalFile,

                               ]
                           })
        self.subfiles.insert({"sha256": sha,
                              "firstByte": file.startInOriginalFile,
                              "lastByte": file.endInOriginalFile,
                              "type": file.getType(),
                              "subtype": file.getSubType(),
                              "notes": file.getNotes(),
                              "bytemask": file.bytemask,
                              "wordsize": file.wordSize,
                              "wordmask": file.wordMask})

The Insertion of a subfile:

    def addSubfile(self,sha256,startAddr,subfileData):
        print("adding subfile",subfileData["firstByte"],subfileData["lastByte"])
        print(subfileData)
        self.subfiles.insert(subfileData)

The first split call that should insert

split into 0b000000 0b001000 0b001001 0b011010
adding subfile 0b001001 0b011010
{'bytemask': '--------', 'firstByte': '0b001001', 'lastByte': '0b011010', 'notes': 'notes', 'sha256': '71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73', 'subtype': 'unknown', 'type': 'unknown', 'wordmask': '--------', 'wordsize': 1}

the second one

split into 0b001001 0b010000 0b010001 0b011010
adding subfile 0b010001 0b011010
{'bytemask': '--------', 'firstByte': '0b010001', 'lastByte': '0b011010', 'notes': 'notes', 'sha256': '71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73', 'subtype': 'unknown', 'type': 'unknown', 'wordmask': '--------', 'wordsize': 1}

The two relevant tables of the DB look like this after the call:

    "files": {
        "1": {
            "begin": "0b000000",
            "binAddrLength": 6,
            "end": "0b011010",
            "length": 26,
            "path": "../test/testdata/abc.txt",
            "sha256": "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73",
            "subfiles": [
                "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73.0b000000.0b011010"
            ]
        }
    },
    "subfiles": {
        "1": {
            "bytemask": "--------",
            "firstByte": "0b000000",
            "lastByte": "0b001000",
            "notes": "notes",
            "sha256": "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73",
            "subtype": "unknown",
            "type": "unknown",
            "wordmask": "--------",
            "wordsize": 1
        },
        "2": {
            "bytemask": "--------",
            "firstByte": "0b010001",
            "lastByte": "0b011010",
            "notes": "notes",
            "sha256": "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73",
            "subtype": "unknown",
            "type": "unknown",
            "wordmask": "--------",
            "wordsize": 1
        }
    }
}

What do i expect to happen? after 2 splits that are confirmed to be happening, i expect there to be 3 subfiles in that table, and after all the insert is getting called twice on split and once on creation.

What happens? The DB does not seem to take the second insert call(first one after file creation) causing my program to crash when running further. You can see from the excerpt of the console output that the insert method is definitely getting called, however only one of those 2 calls actually results in an actual insertion happening.

MartinKurtz commented 3 months ago

After a suggestion from a friend, it turns out PyCharm was misreporting the version of tinydb installed was 4.8.0 when actually it was 3.15.2, updating to 4.8.0 from outside of PyCharm resolved the issue

msiemens commented 1 month ago

@MartinKurtz Great to hear that you found the issue 🙂