more of that.

This commit is contained in:
fiatjaf
2024-07-22 16:25:06 -03:00
parent 80db5b1c0a
commit 874ab3411d
3 changed files with 13 additions and 14 deletions

View File

@@ -79,8 +79,8 @@ func (n *Negentropy) Reconcile(query []byte) (output []byte, haveIds []string, n
func (n *Negentropy) reconcileAux(reader *bytes.Reader, haveIds, needIds *[]string) ([]byte, error) { func (n *Negentropy) reconcileAux(reader *bytes.Reader, haveIds, needIds *[]string) ([]byte, error) {
n.lastTimestampIn, n.lastTimestampOut = 0, 0 // Reset for each message n.lastTimestampIn, n.lastTimestampOut = 0, 0 // Reset for each message
var fullOutput []byte fullOutput := bytes.NewBuffer(make([]byte, 0, 5000))
fullOutput = append(fullOutput, protocolVersion) fullOutput.WriteByte(protocolVersion)
pv, err := reader.ReadByte() pv, err := reader.ReadByte()
if err != nil { if err != nil {
@@ -94,7 +94,7 @@ func (n *Negentropy) reconcileAux(reader *bytes.Reader, haveIds, needIds *[]stri
if n.IsInitiator { if n.IsInitiator {
return nil, fmt.Errorf("unsupported negentropy protocol version requested") return nil, fmt.Errorf("unsupported negentropy protocol version requested")
} }
return fullOutput, nil return fullOutput.Bytes(), nil
} }
var prevBound Bound var prevBound Bound
@@ -199,7 +199,7 @@ func (n *Negentropy) reconcileAux(reader *bytes.Reader, haveIds, needIds *[]stri
endBound := currBound endBound := currBound
n.storage.Iterate(lower, upper, func(item Item, index int) bool { n.storage.Iterate(lower, upper, func(item Item, index int) bool {
if n.ExceededFrameSizeLimit(len(fullOutput) + len(*responseIdsPtr)) { if n.ExceededFrameSizeLimit(fullOutput.Len() + len(*responseIdsPtr)) {
endBound = Bound{item} endBound = Bound{item}
upper = index upper = index
return false return false
@@ -222,7 +222,7 @@ func (n *Negentropy) reconcileAux(reader *bytes.Reader, haveIds, needIds *[]stri
partialOutput.Write(encodeVarInt(numResponseIds)) partialOutput.Write(encodeVarInt(numResponseIds))
partialOutput.Write(responseIds) partialOutput.Write(responseIds)
fullOutput = append(fullOutput, partialOutput.Bytes()...) partialOutput.WriteTo(fullOutput)
partialOutput.Reset() partialOutput.Reset()
} }
@@ -231,7 +231,7 @@ func (n *Negentropy) reconcileAux(reader *bytes.Reader, haveIds, needIds *[]stri
} }
// Check if the frame size limit is exceeded // Check if the frame size limit is exceeded
if n.ExceededFrameSizeLimit(len(fullOutput) + partialOutput.Len()) { if n.ExceededFrameSizeLimit(fullOutput.Len() + partialOutput.Len()) {
// Frame size limit exceeded, handle by encoding a boundary and fingerprint for the remaining range // Frame size limit exceeded, handle by encoding a boundary and fingerprint for the remaining range
remainingFingerprint, err := n.storage.Fingerprint(upper, n.storage.Size()) remainingFingerprint, err := n.storage.Fingerprint(upper, n.storage.Size())
if err != nil { if err != nil {
@@ -242,21 +242,22 @@ func (n *Negentropy) reconcileAux(reader *bytes.Reader, haveIds, needIds *[]stri
if err != nil { if err != nil {
panic(err) panic(err)
} }
fullOutput = append(fullOutput, encodedBound...)
fullOutput = append(fullOutput, encodeVarInt(FingerprintMode)...) fullOutput.Write(encodedBound)
fullOutput = append(fullOutput, remainingFingerprint.SV()...) fullOutput.WriteByte(FingerprintMode)
fullOutput.Write(remainingFingerprint.SV())
break // Stop processing further break // Stop processing further
} else { } else {
// Append the constructed output for this iteration // Append the constructed output for this iteration
fullOutput = append(fullOutput, partialOutput.Bytes()...) partialOutput.WriteTo(fullOutput)
} }
prevIndex = upper prevIndex = upper
prevBound = currBound prevBound = currBound
} }
return fullOutput, nil return fullOutput.Bytes(), nil
} }
func (n *Negentropy) SplitRange(lower, upper int, upperBound Bound, output *bytes.Buffer) { func (n *Negentropy) SplitRange(lower, upper int, upperBound Bound, output *bytes.Buffer) {

View File

@@ -22,7 +22,6 @@ type Storage interface {
Insert(nostr.Timestamp, string) error Insert(nostr.Timestamp, string) error
Seal() error Seal() error
IDSize() int
Size() int Size() int
Iterate(begin, end int, cb func(item Item, i int) bool) error Iterate(begin, end int, cb func(item Item, i int) bool) error
FindLowerBound(begin, end int, value Bound) (int, error) FindLowerBound(begin, end int, value Bound) (int, error)

View File

@@ -45,7 +45,6 @@ func (v *Vector) Seal() error {
} }
func (v *Vector) Size() int { return len(v.items) } func (v *Vector) Size() int { return len(v.items) }
func (v *Vector) IDSize() int { return v.idSize }
func (v *Vector) Iterate(begin, end int, cb func(Item, int) bool) error { func (v *Vector) Iterate(begin, end int, cb func(Item, int) bool) error {
for i := begin; i < end; i++ { for i := begin; i < end; i++ {