Change tree keys logic

This commit is contained in:
mcrakhman 2022-10-15 17:09:25 +02:00 committed by Mikhail Iudin
parent ba7beb4d5a
commit 998e00d245
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
2 changed files with 28 additions and 21 deletions

View File

@ -24,15 +24,25 @@ func (a aclKeys) RawRecordKey(id string) []byte {
}
type treeKeys struct {
id string
id string
headsKey []byte
rootKey []byte
}
func newTreeKeys(id string) treeKeys {
return treeKeys{
id: id,
headsKey: joinStringsToBytes("t", id, "heads"),
rootKey: joinStringsToBytes("t", id, "rootId"),
}
}
func (t treeKeys) HeadsKey() []byte {
return joinStringsToBytes("t", t.id, "heads")
return t.headsKey
}
func (t treeKeys) RootIdKey() []byte {
return joinStringsToBytes("t", t.id, "rootId")
return t.rootKey
}
func (t treeKeys) RawChangeKey(id string) []byte {

View File

@ -10,15 +10,14 @@ import (
)
type treeStorage struct {
db *pogreb.DB
keys treeKeys
id string
headsKey []byte
root *treechangeproto.RawTreeChangeWithId
db *pogreb.DB
keys treeKeys
id string
root *treechangeproto.RawTreeChangeWithId
}
func newTreeStorage(db *pogreb.DB, treeId string) (ts storage.TreeStorage, err error) {
keys := treeKeys{treeId}
keys := newTreeKeys(treeId)
has, err := db.Has(keys.RootIdKey())
if err != nil {
return
@ -55,17 +54,16 @@ func newTreeStorage(db *pogreb.DB, treeId string) (ts storage.TreeStorage, err e
}
ts = &treeStorage{
db: db,
keys: keys,
headsKey: keys.HeadsKey(),
id: treeId,
root: rootWithId,
db: db,
keys: keys,
id: treeId,
root: rootWithId,
}
return
}
func createTreeStorage(db *pogreb.DB, payload storage.TreeStorageCreatePayload) (ts storage.TreeStorage, err error) {
keys := treeKeys{id: payload.TreeId}
keys := newTreeKeys(payload.TreeId)
has, err := db.Has(keys.RootIdKey())
if err != nil {
return
@ -100,11 +98,10 @@ func createTreeStorage(db *pogreb.DB, payload storage.TreeStorageCreatePayload)
}
ts = &treeStorage{
db: db,
keys: keys,
headsKey: keys.HeadsKey(),
id: payload.RootRawChange.Id,
root: payload.RootRawChange,
db: db,
keys: keys,
id: payload.RootRawChange.Id,
root: payload.RootRawChange,
}
return
}
@ -132,7 +129,7 @@ func (t *treeStorage) Heads() (heads []string, err error) {
func (t *treeStorage) SetHeads(heads []string) (err error) {
payload := createHeadsPayload(heads)
return t.db.Put(t.headsKey, payload)
return t.db.Put(t.keys.HeadsKey(), payload)
}
func (t *treeStorage) AddRawChange(change *treechangeproto.RawTreeChangeWithId) (err error) {