Changed source to consistently use rid for rbyd ids

Originally it made sense to name the rbyd ids, well, ids, at least in
the internals of the rbyd functions. But this doesn't work well outside
of the rbyd code, where littlefs has to juggle several different id
types with different purposes:

- rid => rbyd-id, 31-bit index into an rbyd
- bid => btree-id, 31-bit index into a btree
- mid => mdir-id, 15-bit+15-bit index into the mtree
- did => directory-id, 31-bit unique identifier for directories

Even though context makes it clear which id the id refers to in the rbyd
internals, updating the name to rid makes it clearer that these are the
same type of id when looking at code both inside and outside the rbyd
functions.
This commit is contained in:
Christopher Haster
2023-08-07 09:50:25 -05:00
parent 64a1b46ea2
commit d77a173d5c
7 changed files with 2495 additions and 2492 deletions
+268 -265
View File
File diff suppressed because it is too large Load Diff
+14 -14
View File
@@ -310,7 +310,7 @@ class Rbyd:
return cls(block, data, rev, off, trunk_, weight) return cls(block, data, rev, off, trunk_, weight)
def lookup(self, id, tag): def lookup(self, rid, tag):
if not self: if not self:
return True, 0, -1, 0, 0, 0, b'', [] return True, 0, -1, 0, 0, 0, b'', []
@@ -326,9 +326,9 @@ class Rbyd:
# found an alt? # found an alt?
if alt & 0x4000: if alt & 0x4000:
# follow? # follow?
if ((id, tag & 0xfff) > (upper-weight_-1, alt & 0xfff) if ((rid, tag & 0xfff) > (upper-weight_-1, alt & 0xfff)
if alt & 0x2000 if alt & 0x2000
else ((id, tag & 0xfff) else ((rid, tag & 0xfff)
<= (lower+weight_, alt & 0xfff))): <= (lower+weight_, alt & 0xfff))):
lower += upper-lower-1-weight_ if alt & 0x2000 else 0 lower += upper-lower-1-weight_ if alt & 0x2000 else 0
upper -= upper-lower-1-weight_ if not alt & 0x2000 else 0 upper -= upper-lower-1-weight_ if not alt & 0x2000 else 0
@@ -362,13 +362,13 @@ class Rbyd:
# found tag # found tag
else: else:
id_ = upper-1 rid_ = upper-1
tag_ = alt tag_ = alt
w_ = id_-lower w_ = rid_-lower
done = not tag_ or (id_, tag_) < (id, tag) done = not tag_ or (rid_, tag_) < (rid, tag)
return done, id_, tag_, w_, j, d, self.data[j+d:j+d+jump], path return done, rid_, tag_, w_, j, d, self.data[j+d:j+d+jump], path
def __bool__(self): def __bool__(self):
return bool(self.trunk) return bool(self.trunk)
@@ -381,29 +381,29 @@ class Rbyd:
def __iter__(self): def __iter__(self):
tag = 0 tag = 0
id = -1 rid = -1
while True: while True:
done, id, tag, w, j, d, data, _ = self.lookup(id, tag+0x1) done, rid, tag, w, j, d, data, _ = self.lookup(rid, tag+0x1)
if done: if done:
break break
yield id, tag, w, j, d, data yield rid, tag, w, j, d, data
# create tree representation for debugging # create tree representation for debugging
def tree(self): def tree(self):
trunks = co.defaultdict(lambda: (-1, 0)) trunks = co.defaultdict(lambda: (-1, 0))
alts = co.defaultdict(lambda: {}) alts = co.defaultdict(lambda: {})
id, tag = -1, 0 rid, tag = -1, 0
while True: while True:
done, id, tag, w, j, d, data, path = self.lookup(id, tag+0x1) done, rid, tag, w, j, d, data, path = self.lookup(rid, tag+0x1)
# found end of tree? # found end of tree?
if done: if done:
break break
# keep track of trunks/alts # keep track of trunks/alts
trunks[j] = (id, tag) trunks[j] = (rid, tag)
for j_, j__, followed, c in path: for j_, j__, followed, c in path:
if followed: if followed:
@@ -510,7 +510,7 @@ def main(disk, roots=None, *,
print('btree %s, rev %d, weight %d' % ( print('btree %s, rev %d, weight %d' % (
btree.addr(), btree.rev, btree.weight)) btree.addr(), btree.rev, btree.weight))
# look up an id, while keeping track of the search path # look up a bid, while keeping track of the search path
def btree_lookup(bid, *, def btree_lookup(bid, *,
depth=None): depth=None):
rbyd = btree rbyd = btree
+10 -10
View File
@@ -316,7 +316,7 @@ class Rbyd:
return cls(block, data, rev, off, trunk_, weight) return cls(block, data, rev, off, trunk_, weight)
def lookup(self, id, tag): def lookup(self, rid, tag):
if not self: if not self:
return True, 0, -1, 0, 0, 0, b'', [] return True, 0, -1, 0, 0, 0, b'', []
@@ -332,9 +332,9 @@ class Rbyd:
# found an alt? # found an alt?
if alt & 0x4000: if alt & 0x4000:
# follow? # follow?
if ((id, tag & 0xfff) > (upper-weight_-1, alt & 0xfff) if ((rid, tag & 0xfff) > (upper-weight_-1, alt & 0xfff)
if alt & 0x2000 if alt & 0x2000
else ((id, tag & 0xfff) else ((rid, tag & 0xfff)
<= (lower+weight_, alt & 0xfff))): <= (lower+weight_, alt & 0xfff))):
lower += upper-lower-1-weight_ if alt & 0x2000 else 0 lower += upper-lower-1-weight_ if alt & 0x2000 else 0
upper -= upper-lower-1-weight_ if not alt & 0x2000 else 0 upper -= upper-lower-1-weight_ if not alt & 0x2000 else 0
@@ -368,13 +368,13 @@ class Rbyd:
# found tag # found tag
else: else:
id_ = upper-1 rid_ = upper-1
tag_ = alt tag_ = alt
w_ = id_-lower w_ = rid_-lower
done = not tag_ or (id_, tag_) < (id, tag) done = not tag_ or (rid_, tag_) < (rid, tag)
return done, id_, tag_, w_, j, d, self.data[j+d:j+d+jump], path return done, rid_, tag_, w_, j, d, self.data[j+d:j+d+jump], path
def __bool__(self): def __bool__(self):
return bool(self.trunk) return bool(self.trunk)
@@ -387,14 +387,14 @@ class Rbyd:
def __iter__(self): def __iter__(self):
tag = 0 tag = 0
id = -1 rid = -1
while True: while True:
done, id, tag, w, j, d, data, _ = self.lookup(id, tag+0x1) done, rid, tag, w, j, d, data, _ = self.lookup(rid, tag+0x1)
if done: if done:
break break
yield id, tag, w, j, d, data yield rid, tag, w, j, d, data
# btree lookup with this rbyd as the root # btree lookup with this rbyd as the root
def btree_lookup(self, f, block_size, bid, *, def btree_lookup(self, f, block_size, bid, *,
+13 -13
View File
@@ -318,7 +318,7 @@ class Rbyd:
return cls(block, data, rev, off, trunk_, weight) return cls(block, data, rev, off, trunk_, weight)
def lookup(self, id, tag): def lookup(self, rid, tag):
if not self: if not self:
return True, 0, -1, 0, 0, 0, b'', [] return True, 0, -1, 0, 0, 0, b'', []
@@ -334,9 +334,9 @@ class Rbyd:
# found an alt? # found an alt?
if alt & 0x4000: if alt & 0x4000:
# follow? # follow?
if ((id, tag & 0xfff) > (upper-weight_-1, alt & 0xfff) if ((rid, tag & 0xfff) > (upper-weight_-1, alt & 0xfff)
if alt & 0x2000 if alt & 0x2000
else ((id, tag & 0xfff) else ((rid, tag & 0xfff)
<= (lower+weight_, alt & 0xfff))): <= (lower+weight_, alt & 0xfff))):
lower += upper-lower-1-weight_ if alt & 0x2000 else 0 lower += upper-lower-1-weight_ if alt & 0x2000 else 0
upper -= upper-lower-1-weight_ if not alt & 0x2000 else 0 upper -= upper-lower-1-weight_ if not alt & 0x2000 else 0
@@ -370,13 +370,13 @@ class Rbyd:
# found tag # found tag
else: else:
id_ = upper-1 rid_ = upper-1
tag_ = alt tag_ = alt
w_ = id_-lower w_ = rid_-lower
done = not tag_ or (id_, tag_) < (id, tag) done = not tag_ or (rid_, tag_) < (rid, tag)
return done, id_, tag_, w_, j, d, self.data[j+d:j+d+jump], path return done, rid_, tag_, w_, j, d, self.data[j+d:j+d+jump], path
def __bool__(self): def __bool__(self):
return bool(self.trunk) return bool(self.trunk)
@@ -389,29 +389,29 @@ class Rbyd:
def __iter__(self): def __iter__(self):
tag = 0 tag = 0
id = -1 rid = -1
while True: while True:
done, id, tag, w, j, d, data, _ = self.lookup(id, tag+0x1) done, rid, tag, w, j, d, data, _ = self.lookup(rid, tag+0x1)
if done: if done:
break break
yield id, tag, w, j, d, data yield rid, tag, w, j, d, data
# create tree representation for debugging # create tree representation for debugging
def tree(self): def tree(self):
trunks = co.defaultdict(lambda: (-1, 0)) trunks = co.defaultdict(lambda: (-1, 0))
alts = co.defaultdict(lambda: {}) alts = co.defaultdict(lambda: {})
id, tag = -1, 0 rid, tag = -1, 0
while True: while True:
done, id, tag, w, j, d, data, path = self.lookup(id, tag+0x1) done, rid, tag, w, j, d, data, path = self.lookup(rid, tag+0x1)
# found end of tree? # found end of tree?
if done: if done:
break break
# keep track of trunks/alts # keep track of trunks/alts
trunks[j] = (id, tag) trunks[j] = (rid, tag)
for j_, j__, followed, c in path: for j_, j__, followed, c in path:
if followed: if followed:
+36 -36
View File
@@ -275,14 +275,14 @@ def dbg_log(data, block_size, rev, off, weight, *,
return bool(self.tags) return bool(self.tags)
# first figure out where each id comes from # first figure out where each rid comes from
weights = [] weights = []
lifetimes = [] lifetimes = []
def index(weights, id): def index(weights, rid):
for i, w in enumerate(weights): for i, w in enumerate(weights):
if id < w: if rid < w:
return i, id return i, rid
id -= w rid -= w
return len(weights), 0 return len(weights), 0
checkpoint_js = [0] checkpoint_js = [0]
@@ -317,18 +317,18 @@ def dbg_log(data, block_size, rev, off, weight, *,
if not tag & 0x4000: if not tag & 0x4000:
wastrunk = False wastrunk = False
# derive the current tag's id from alt weights # derive the current tag's rid from alt weights
delta = (lower_+upper_) - weight_ delta = (lower_+upper_) - weight_
weight_ = lower_+upper_ weight_ = lower_+upper_
id = lower_ + w-1 rid = lower_ + w-1
if (tag & 0xe000) != 0x2000 and not tag & 0x4000: if (tag & 0xe000) != 0x2000 and not tag & 0x4000:
# note we ignore out-of-bounds here for debugging # note we ignore out-of-bounds here for debugging
if delta > 0: if delta > 0:
# grow lifetimes # grow lifetimes
i, id_ = index(weights, lower_) i, rid_ = index(weights, lower_)
if id_ > 0: if rid_ > 0:
weights[i:i+1] = [id_, delta, weights[i]-id_] weights[i:i+1] = [rid_, delta, weights[i]-rid_]
lifetimes[i:i+1] = [ lifetimes[i:i+1] = [
lifetimes[i], Lifetime(j), lifetimes[i]] lifetimes[i], Lifetime(j), lifetimes[i]]
else: else:
@@ -339,18 +339,18 @@ def dbg_log(data, block_size, rev, off, weight, *,
elif delta < 0: elif delta < 0:
# shrink lifetimes # shrink lifetimes
i, id_ = index(weights, lower_) i, rid_ = index(weights, lower_)
delta_ = -delta delta_ = -delta
weights_ = weights.copy() weights_ = weights.copy()
lifetimes_ = lifetimes.copy() lifetimes_ = lifetimes.copy()
shrinks = set() shrinks = set()
while delta_ > 0 and i < len(weights_): while delta_ > 0 and i < len(weights_):
if weights_[i] > delta_: if weights_[i] > delta_:
delta__ = min(delta_, weights_[i]-id_) delta__ = min(delta_, weights_[i]-rid_)
delta_ -= delta__ delta_ -= delta__
weights_[i] -= delta__ weights_[i] -= delta__
i += 1 i += 1
id_ = 0 rid_ = 0
else: else:
delta_ -= weights_[i] delta_ -= weights_[i]
weights_[i:i+1] = [] weights_[i:i+1] = []
@@ -361,9 +361,9 @@ def dbg_log(data, block_size, rev, off, weight, *,
weights = weights_ weights = weights_
lifetimes = lifetimes_ lifetimes = lifetimes_
if id >= 0: if rid >= 0:
# attach tag to lifetime # attach tag to lifetime
i, id_ = index(weights, id) i, rid_ = index(weights, rid)
if i < len(weights): if i < len(weights):
lifetimes[i].add(j) lifetimes[i].add(j)
@@ -473,8 +473,8 @@ def dbg_log(data, block_size, rev, off, weight, *,
if not tag & 0x4000: if not tag & 0x4000:
wastrunk = False wastrunk = False
# derive the current tag's id from alt weights # derive the current tag's rid from alt weights
id = lower_ + w-1 rid = lower_ + w-1
# show human-readable tag representation # show human-readable tag representation
print('%s%08x:%s %*s%s%*s %-57s%s%s' % ( print('%s%08x:%s %*s%s%*s %-57s%s%s' % (
@@ -484,8 +484,8 @@ def dbg_log(data, block_size, rev, off, weight, *,
lifetime_width, lifetimerepr(j) if args.get('lifetimes') else '', lifetime_width, lifetimerepr(j) if args.get('lifetimes') else '',
'\x1b[90m' if color and j >= off else '', '\x1b[90m' if color and j >= off else '',
w_width, '' if (tag & 0xe000) != 0x0000 w_width, '' if (tag & 0xe000) != 0x0000
else '%d-%d' % (id-(w-1), id) if w > 1 else '%d-%d' % (rid-(w-1), rid) if w > 1
else id, else rid,
'%-22s%s' % ( '%-22s%s' % (
tagrepr(tag, w, size, j), tagrepr(tag, w, size, j),
' %s' % next(xxd( ' %s' % next(xxd(
@@ -548,7 +548,7 @@ def dbg_tree(data, block_size, rev, trunk, weight, *,
# lookup a tag, returning also the search path for decoration # lookup a tag, returning also the search path for decoration
# purposes # purposes
def lookup(id, tag): def lookup(rid, tag):
lower = -1 lower = -1
upper = weight upper = weight
path = [] path = []
@@ -561,9 +561,9 @@ def dbg_tree(data, block_size, rev, trunk, weight, *,
# found an alt? # found an alt?
if alt & 0x4000: if alt & 0x4000:
# follow? # follow?
if ((id, tag & 0xfff) > (upper-w-1, alt & 0xfff) if ((rid, tag & 0xfff) > (upper-w-1, alt & 0xfff)
if alt & 0x2000 if alt & 0x2000
else ((id, tag & 0xfff) <= (lower+w, alt & 0xfff))): else ((rid, tag & 0xfff) <= (lower+w, alt & 0xfff))):
lower += upper-lower-1-w if alt & 0x2000 else 0 lower += upper-lower-1-w if alt & 0x2000 else 0
upper -= upper-lower-1-w if not alt & 0x2000 else 0 upper -= upper-lower-1-w if not alt & 0x2000 else 0
j = j - jump j = j - jump
@@ -596,13 +596,13 @@ def dbg_tree(data, block_size, rev, trunk, weight, *,
# found tag # found tag
else: else:
id_ = upper-1 rid_ = upper-1
tag_ = alt tag_ = alt
w_ = id_-lower w_ = rid_-lower
done = not tag_ or (id_, tag_) < (id, tag) done = not tag_ or (rid_, tag_) < (rid, tag)
return done, id_, tag_, w_, j, d, jump, path return done, rid_, tag_, w_, j, d, jump, path
# precompute tree # precompute tree
t_width = 0 t_width = 0
@@ -610,15 +610,15 @@ def dbg_tree(data, block_size, rev, trunk, weight, *,
trunks = co.defaultdict(lambda: (-1, 0)) trunks = co.defaultdict(lambda: (-1, 0))
alts = co.defaultdict(lambda: {}) alts = co.defaultdict(lambda: {})
id, tag = -1, 0 rid, tag = -1, 0
while True: while True:
done, id, tag, w, j, d, size, path = lookup(id, tag+0x1) done, rid, tag, w, j, d, size, path = lookup(rid, tag+0x1)
# found end of tree? # found end of tree?
if done: if done:
break break
# keep track of trunks/alts # keep track of trunks/alts
trunks[j] = (id, tag) trunks[j] = (rid, tag)
for j_, j__, followed, c in path: for j_, j__, followed, c in path:
if followed: if followed:
@@ -698,7 +698,7 @@ def dbg_tree(data, block_size, rev, trunk, weight, *,
if t_depth > 0: if t_depth > 0:
t_width = 2*t_depth + 2 t_width = 2*t_depth + 2
def treerepr(id, tag): def treerepr(rid, tag):
if t_depth == 0: if t_depth == 0:
return '' return ''
@@ -732,7 +732,7 @@ def dbg_tree(data, block_size, rev, trunk, weight, *,
trunk = [] trunk = []
was = None was = None
for d in range(t_depth): for d in range(t_depth):
t, c, was = branchrepr((id, tag), d, was) t, c, was = branchrepr((rid, tag), d, was)
trunk.append('%s%s%s%s' % ( trunk.append('%s%s%s%s' % (
'\x1b[33m' if color and c == 'y' '\x1b[33m' if color and c == 'y'
@@ -756,9 +756,9 @@ def dbg_tree(data, block_size, rev, trunk, weight, *,
'data (truncated)' 'data (truncated)'
if not args.get('no_truncate') else '')) if not args.get('no_truncate') else ''))
id, tag = -1, 0 rid, tag = -1, 0
while True: while True:
done, id, tag, w, j, d, size, path = lookup(id, tag+0x1) done, rid, tag, w, j, d, size, path = lookup(rid, tag+0x1)
# found end of tree? # found end of tree?
if done: if done:
break break
@@ -766,10 +766,10 @@ def dbg_tree(data, block_size, rev, trunk, weight, *,
# show human-readable tag representation # show human-readable tag representation
print('%08x: %s%-57s' % ( print('%08x: %s%-57s' % (
j, j,
treerepr(id, tag) if args.get('tree') else '', treerepr(rid, tag) if args.get('tree') else '',
'%*s %-22s%s' % ( '%*s %-22s%s' % (
w_width, '%d-%d' % (id-(w-1), id) w_width, '%d-%d' % (rid-(w-1), rid)
if w > 1 else id if w > 1 else rid
if w > 0 else '', if w > 0 else '',
tagrepr(tag, w, size, j), tagrepr(tag, w, size, j),
' %s' % next(xxd( ' %s' % next(xxd(
+360 -360
View File
File diff suppressed because it is too large Load Diff
+1794 -1794
View File
File diff suppressed because it is too large Load Diff