Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added Baser.gids as oset of Group Hab pres (hids) #648

Merged
merged 1 commit into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/keri/app/habbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,8 @@ def deleteHab(self, name):

del self.habs[hab.pre]
self.db.prefixes.remove(hab.pre)
if hab.pre in self.db.gids:
self.db.gids.remove(hab.pre)

return True

Expand Down
62 changes: 15 additions & 47 deletions src/keri/db/basing.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,52 +68,6 @@ class komerdict(dict):
record from dataabase to class instance. if no mapping function then just
return the dataclass record as value.
"""
__slots__ = ('db') # no .__dict__ just for db reference

def __init__(self, *pa, **kwa):
super(dbdict, self).__init__(*pa, **kwa)
self.db = None

def __getitem__(self, k):
try:
return super(dbdict, self).__getitem__(k)
except KeyError as ex:
if not self.db:
raise ex # reraise KeyError
if (ksr := self.db.states.get(keys=k)) is None:
raise ex # reraise KeyError
try:
kever = eventing.Kever(state=ksr, db=self.db)
except kering.MissingEntryError: # no kel event for keystate
raise ex # reraise KeyError
self.__setitem__(k, kever)
return kever

def __contains__(self, k):
if not super(dbdict, self).__contains__(k):
try:
self.__getitem__(k)
return True
except KeyError:
return False
else:
return True

def get(self, k, default=None):
"""Override of dict get method

Parameters:
k (str): key for dict
default: default value to return if not found

Returns:
kever: converted from underlying dict or database

"""
if not super(dbdict, self).__contains__(k):
return default
else:
return self.__getitem__(k)

'''

Expand Down Expand Up @@ -1198,6 +1152,9 @@ def reload(self):
continue
self.kevers[kever.prefixer.qb64] = kever
self.prefixes.add(kever.prefixer.qb64)
if data.mid: # group hab
self.gids.add(data.hid)

elif data.mid is None: # in .habs but no corresponding key state and not a group so remove
removes.append(keys) # no key state or KEL event for .hab record

Expand All @@ -1217,6 +1174,8 @@ def reload(self):
continue
self.kevers[kever.prefixer.qb64] = kever
self.prefixes.add(kever.prefixer.qb64)
if data.mid: # group hab
self.gids.add(data.hid)
elif data.mid is None: # in .habs but no corresponding key state and not a group so remove
removes.append(keys) # no key state or KEL event for .hab record

Expand All @@ -1238,7 +1197,7 @@ def clean(self):
temp=self.temp,
headDirPath=self.headDirPath,
perm=self.perm,
clean=True) as copy:
clean=True) as copy: # copy is Baser instance

with reopenDB(db=self, reuse=True, readonly=True): # reopen as readonly
if not os.path.exists(self.path):
Expand All @@ -1262,7 +1221,12 @@ def clean(self):
if val.hid in copy.kevers: # only copy habs that verified
copy.habs.put(keys=keys, val=val)
copy.prefixes.add(val.hid)
if val.mid: # a group hab
copy.gids.add(val.hid)

# ToDo XXXX
# is this obsolete? Should this be removed or should this be
# be the Signator name not the default name of the Habery?
if not copy.habs.get(keys=(self.name,)):
raise ValueError("Error cloning habs, missing orig name={}."
"".format(self.name))
Expand Down Expand Up @@ -1299,6 +1263,10 @@ def clean(self):
self.prefixes.clear()
self.prefixes.update(copy.prefixes)

# clear and clone .gids
self.gids.clear()
self.gids.update(copy.gids)

with reopenDB(db=self, reuse=True): # make sure can reopen
if not isinstance(self.env, lmdb.Environment):
raise ValueError("Error cloning, unable to reopen."
Expand Down
2 changes: 1 addition & 1 deletion tests/db/test_basing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1708,7 +1708,7 @@ def test_clean_baser():
name = "nat"
# with basing.openDB(name="nat") as natDB, keeping.openKS(name="nat") as natKS:
with habbing.openHby(name=name) as hby: # default is temp=True
natHab = hby.makeHab(name=name, isith='2', icount=3)
natHab = hby.makeHab(name=name, isith='2', icount=3) # default Hab
# setup Nat's habitat using default salt multisig already incepts
#natHab = habbing.Habitat(name='nat', ks=natKS, db=natDB,
#isith='2', icount=3, temp=True)
Expand Down
Loading