Skip to content

Commit

Permalink
TxOpID and TxOp tag cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Drew O'Meara committed Aug 26, 2024
1 parent d815f3d commit 1410a91
Show file tree
Hide file tree
Showing 11 changed files with 326 additions and 405 deletions.
17 changes: 6 additions & 11 deletions amp/amp.attrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,19 @@ func ErrorToValue(v error) tag.Value {
return artErr
}

func (v *TagID) MarshalToStore(in []byte) (out []byte, err error) {
func (v *TagUID) MarshalToStore(in []byte) (out []byte, err error) {
return MarshalPbToStore(v, in)
}

func (v *TagID) TagSpec() tag.Spec {
return AttrSpec.With("TagID")
func (v *TagUID) TagSpec() tag.Spec {
return AttrSpec.With("TagUID")
}

func (v *TagID) New() tag.Value {
return &TagID{}
func (v *TagUID) New() tag.Value {
return &TagUID{}
}

func (v *TagID) SetFromTime(t time.Time) {
func (v *TagUID) SetFromTime(t time.Time) {
tag := tag.FromTime(t, false)
v.ID_0 = int64(tag[0])
v.ID_1 = tag[1]
Expand Down Expand Up @@ -107,11 +107,6 @@ func (v *Tag) SetTagID(tagID tag.ID) {
v.TagID_2 = tagID[2]
}

// func (v *Tag) SetTimesatamp(t time.Time) {
// tag := tag.FromTime(t, false)
// v.Timestamp = int64(tag[0])
// }

func (v *Err) MarshalToStore(in []byte) (out []byte, err error) {
return MarshalPbToStore(v, in)
}
Expand Down
341 changes: 172 additions & 169 deletions amp/amp.pb.go

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions amp/amp.proto
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ enum TxField {
TxField_AttrID_1 = 5;
TxField_AttrID_2 = 6;

TxField_SI_0 = 7;
TxField_SI_1 = 8;
TxField_SI_2 = 9;
TxField_ItemID_0 = 7;
TxField_ItemID_1 = 8;
TxField_ItemID_2 = 9;

TxField_EditID_0 = 10;
TxField_EditID_1 = 11;
TxField_EditID_2 = 12;

TxField_NumFields = 13;
TxField_MaxFields = 24;
Expand Down Expand Up @@ -248,8 +249,8 @@ enum UrlScheme {



// TagID is a minimal wrapper for a tag.ID
message TagID {
// TagUID is a minimal wrapper for a tag.ID
message TagUID {
int64 ID_0 = 2; // UTC << 16 -- see tag.ID
fixed64 ID_1 = 3;
fixed64 ID_2 = 4;
Expand Down
30 changes: 17 additions & 13 deletions amp/api.app.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,25 +103,29 @@ type Pin interface {

// TxMsg is workhorse generic transport serialization sent between client and host.
type TxMsg struct {
TxEnvelope
Ops []TxOp // operations to perform on the target
OpsSorted bool // describes order of []Ops
DataStore []byte // marshalled data store for Ops serialized data
refCount int32 // see AddRef() / ReleaseRef()
TxEnvelope // public fields and routing tags
Ops []TxOp // operations to perform on the target
OpsSorted bool // describes order of []Ops
DataStore []byte // marshalled data store for Ops serialized data
refCount int32 // see AddRef() / ReleaseRef()
}

// ElementID is a multi-part LSM key: CellID / AttrID / SI
type ElementID [3]tag.ID

// TxOpID is TxOp atomic edit entry ID, functioning as a multi-part LSM key: CellID / AttrID / SI / EditID.
type TxOpID struct {
CellID tag.ID // target cell ID
AttrID tag.ID // attribute specification ID that expresses or implies operation
ItemID tag.ID // user-defined value / index / tag ID
EditID tag.ID // references the previous revision -- see tag.ForkEdit()
}

// TxOp is an atomic operation on a target cell and is a unit of change (or message) for any target.
// Values are typically LSM sorted, so use low order bytes before high order bytes.
// Note that x0 is the most significant and x2 is least significant bytes.
type TxOp struct {

// op key components in order of significance
CellID tag.ID // target cell
AttrID tag.ID // attribute specification to operate on
SI tag.ID // 24-byte client value of data being mutated
EditID EditID // references the previous revision -- see tag.ForkEdit()

// Op values -- not part of TxOp comparison
TxOpID // applicable cell, attribute, element, and edit IDs
OpCode TxOpCode // operation to perform
DataLen uint64 // length of data in TxMsg.DataStore
DataOfs uint64 // offset into TxMsg.DataStore
Expand Down
6 changes: 3 additions & 3 deletions amp/std/std.proto
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ message ChatEntry {
message Channel {
// Identifies a specific target tag ID this link points to.
int64 TagID_0 = 2;
fixed64 TagID_1 = 3;
fixed64 TagID_2 = 4;
int64 TagUID_0 = 2;
fixed64 TagUID_1 = 3;
fixed64 TagUID_2 = 4;
int64 SpecID_0 = 6;
fixed64 SpecID_1 = 7;
Expand Down
28 changes: 13 additions & 15 deletions amp/std/std.support.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,12 @@ func (w *cellWriter) PutText(propertyID tag.ID, val string) {
if w.err != nil {
return
}
txOp := amp.TxOp{
OpCode: amp.TxOpCode_UpsertElement,
CellID: w.cellID,
AttrID: CellProperties.ID,
SI: propertyID,
}
err := w.tx.MarshalOp(&txOp, &amp.Tag{
op := amp.TxOp{}
op.OpCode = amp.TxOpCode_UpsertElement
op.CellID = w.cellID
op.AttrID = CellProperties.ID
op.ItemID = propertyID
err := w.tx.MarshalOp(&op, &amp.Tag{
Text: val,
})
if err != nil {
Expand All @@ -175,13 +174,12 @@ func (w *cellWriter) PutItem(propertyID tag.ID, val tag.Value) {
if w.err != nil {
return
}
txOp := amp.TxOp{
OpCode: amp.TxOpCode_UpsertElement,
CellID: w.cellID,
AttrID: CellProperties.ID,
SI: propertyID,
}
if err := w.tx.MarshalOp(&txOp, val); err != nil {
op := amp.TxOp{}
op.OpCode = amp.TxOpCode_UpsertElement
op.CellID = w.cellID
op.AttrID = CellProperties.ID
op.ItemID = propertyID
if err := w.tx.MarshalOp(&op, val); err != nil {
w.err = err
}
}
Expand Down Expand Up @@ -242,7 +240,7 @@ func (tx *TxMsg) Upsert(literal any, keys ...tag.ID) {
break
// case *tag.ID:
// tag = &amp.Tag{}
// tag.SetTagID(v)
// tag.SetTagUID(v)
default:
panic("unsupported type")
return
Expand Down
81 changes: 30 additions & 51 deletions amp/support.tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,6 @@ var gTxMsgPool = sync.Pool{
},
}

/*
func CopyMsg(src *TxMsg) *TxMsg {
msg := NewMsg()
if src != nil {
valBuf := append(msg.ValBuf[:0], src.ValBuf...)
*msg = *src
msg.ValBuf = valBuf
}
return msg
}
*/

func (tx *TxEnvelope) SetContextID(ID tag.ID) {
tx.ContextID_0 = int64(ID[0])
tx.ContextID_1 = ID[1]
Expand Down Expand Up @@ -109,12 +95,12 @@ func MarshalAttr(cellID, attrID tag.ID, attrVal tag.Value) (*TxMsg, error) {
return nil, ErrCode_InternalErr.Error("MarshalAttr: missing builtin tag.Spec")
}
}
op := TxOp{
CellID: cellID,
AttrID: attrID,
EditID: tx.GenesisID(),
OpCode: TxOpCode_UpsertElement,
}
op := TxOp{}
op.CellID = cellID
op.AttrID = attrID
op.EditID = tag.Genesis(tx.GenesisID())
op.OpCode = TxOpCode_UpsertElement

if err := tx.MarshalOp(&op, attrVal); err != nil {
return nil, err
}
Expand All @@ -133,23 +119,23 @@ func (tx *TxMsg) UnmarshalOpValue(idx int, out tag.Value) error {

func (tx *TxMsg) LoadFirst(attrID tag.ID, dst tag.Value) error {
for i, op := range tx.Ops {
if op.AttrID == attrID {
if op.CellID == attrID {
return tx.UnmarshalOpValue(i, dst)
}
}
return ErrAttrNotFound
}

func (tx *TxMsg) Load(cellID, attrID, SI tag.ID, dst tag.Value) error {
func (tx *TxMsg) Load(cellID, attrID, itemID tag.ID, dst tag.Value) error {
tx.sortOps()

target := &TxOp{
find := &TxOpID{
CellID: cellID,
AttrID: attrID,
SI: SI,
ItemID: itemID,
}
idx, found := sort.Find(len(tx.Ops), func(i int) int {
return tx.Ops[i].CompareTo(target)
return tx.Ops[i].CompareTo(find)
})
if !found {
return ErrPropertyNotFound
Expand All @@ -167,7 +153,7 @@ func (tx *TxMsg) sortOps() {
if !tx.OpsSorted {
tx.OpsSorted = true
sort.Slice(tx.Ops, func(i, j int) bool {
return tx.Ops[i].CompareTo(&tx.Ops[j]) < 0
return tx.Ops[i].TxOpID.CompareTo(&tx.Ops[j].TxOpID) < 0
})
}
}
Expand Down Expand Up @@ -202,15 +188,15 @@ func (tx *TxMsg) CheckMetaAttr(reg Registry) (tag.Value, error) {
return val, nil
}

func (tx *TxMsg) Upsert(cellID, attrID, SI tag.ID, val tag.Value) error {
txOp := TxOp{
OpCode: TxOpCode_UpsertElement,
CellID: cellID,
AttrID: attrID,
EditID: tx.GenesisID(),
SI: SI,
}
return tx.MarshalOp(&txOp, val)
func (tx *TxMsg) Upsert(cellID, attrID, itemID tag.ID, val tag.Value) error {
op := TxOp{}
op.OpCode = TxOpCode_UpsertElement
op.CellID = cellID
op.AttrID = attrID
op.ItemID = itemID
op.EditID = tag.Genesis(tx.GenesisID())

return tx.MarshalOp(&op, val)
}

// Marshals a TxOp and optional value to the given Tx's to and data store.
Expand Down Expand Up @@ -382,9 +368,9 @@ func (tx *TxMsg) MarshalOps(dst []byte) []byte {
op_cur[TxField_AttrID_1] = op.AttrID[1]
op_cur[TxField_AttrID_2] = op.AttrID[2]

op_cur[TxField_SI_0] = op.SI[0]
op_cur[TxField_SI_1] = op.SI[1]
op_cur[TxField_SI_2] = op.SI[2]
op_cur[TxField_ItemID_0] = op.ItemID[0]
op_cur[TxField_ItemID_1] = op.ItemID[1]
op_cur[TxField_ItemID_2] = op.ItemID[2]

op_cur[TxField_EditID_0] = op.EditID[0]
op_cur[TxField_EditID_1] = op.EditID[1]
Expand Down Expand Up @@ -490,39 +476,32 @@ func (tx *TxMsg) UnmarshalBody(src []byte) error {
op.AttrID[1] = op_cur[TxField_AttrID_1]
op.AttrID[2] = op_cur[TxField_AttrID_2]

op.SI[0] = op_cur[TxField_SI_0]
op.SI[1] = op_cur[TxField_SI_1]
op.SI[2] = op_cur[TxField_SI_2]
op.ItemID[0] = op_cur[TxField_ItemID_0]
op.ItemID[1] = op_cur[TxField_ItemID_1]
op.ItemID[2] = op_cur[TxField_ItemID_2]

op.EditID[0] = op_cur[TxField_EditID_0]
op.EditID[1] = op_cur[TxField_EditID_1]
op.EditID[2] = 0
op.EditID[2] = op_cur[TxField_EditID_2]

tx.Ops = append(tx.Ops, op)
}

return nil
}

func (op *TxOp) CompareTo(oth *TxOp) int {
func (op *TxOpID) CompareTo(oth *TxOpID) int {
if diff := op.CellID.CompareTo(oth.CellID); diff != 0 {
return int(diff)
}
if diff := op.AttrID.CompareTo(oth.AttrID); diff != 0 {
return int(diff)
}
if diff := op.SI.CompareTo(oth.SI); diff != 0 {
if diff := op.ItemID.CompareTo(oth.ItemID); diff != 0 {
return int(diff)
}
if diff := op.EditID.CompareTo(oth.EditID); diff != 0 {
return int(diff)
}
return 0
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
Loading

0 comments on commit 1410a91

Please sign in to comment.