cyac icon indicating copy to clipboard operation
cyac copied to clipboard

Trie.get fails in case sensitive Trie after buffer load of saved data

Open Hockeychap opened this issue 5 years ago • 2 comments

Summary: Previously saved case sensitive data loads without error into trie. However trie.get(val) fails get item, even though iterating through trie.items() show correct item.

Env: Archlinux using python 3.9, Ubuntu 20.0.4 using python 3.8.5 Cyac Version: 1.2 Cython Version:

Steps to reproduce:

  1. Build a case insensitive trie
  2. Lookup data key (success)
  3. Save trie data out to file
  4. Load trie data from file
  5. Lookup data key (fails)

The code below will should be run twice:

  1. Run 1 - generates a two simple tries (one case sensitive, one insensitive) and saves them to two file
  2. Run 2- creates two tries and then executes the same lookups.

Script below:


from cyac import Trie
import os
import mmap

p=["M3 1AR", "M3 2AR", "M3 3AR"]

if os.path.isfile('./pinsens.bin'):
    # Load into new tries
    print("* * * Loaded from buffer * * * ")
    with open('pinsens.bin', 'r+b') as fins:
        bins = mmap.mmap(fins.fileno(), 0)
        cins = Trie.from_buff(bins)
        bins.flush()
    with open('psens.bin', 'r+b') as fsens:
        bsens = mmap.mmap(fsens.fileno(), 0)
        csen = Trie.from_buff(bsens)
        bsens.flush()
else:
    print("* * * Clean data")
    cins = Trie(ignore_case=True)
    csen = Trie()
    for x in p:
        cins.insert(x)
        csen.insert(x)
    cins.save('pinsens.bin')
    csen.save('psens.bin')

print("Case Insensitive")
print("{} is at {}".format(p[2], cins.get(p[2])))  # correctly returns 2
print("Case Sensitive")
print("{} is at {}".format(p[2], csen.get(p[2])))  # correctly returns 2 in first run, fails in second

for id in csen.items():
    print(id)


Hockeychap avatar Dec 10 '20 19:12 Hockeychap

@Hockeychap Hi, I have fixed it.

nppoly avatar Dec 11 '20 02:12 nppoly

Fast fix - thanks. Works and tested with 2 million row trie data set.

Hockeychap avatar Dec 11 '20 07:12 Hockeychap