Refactor timestamp handling to use integers directly.

Replaced `FromInt64` with `FromInt` to simplify timestamp operations. Updated related methods, tests, and logic to consistently handle timestamps as integers, improving code clarity and reducing unnecessary conversions.
This commit is contained in:
2025-06-14 09:08:20 +01:00
parent 6a7ddb8aea
commit d5ae20ba94
8 changed files with 72 additions and 73 deletions

View File

@@ -70,70 +70,55 @@ func (d *D) Filter(f filter.F) (evSerials []*varint.V, err error) {
}
return
}
var since, until timestamp.Timestamp
if bf&hasSince != 0 {
since = *f.Since
}
if bf&hasUntil != 0 {
until = *f.Until
} else {
until = math.MaxInt64
}
// next, check for filters that only have since and/or until
if bf&(hasSince+hasUntil) != 0 {
var since, until timestamp.Timestamp
if bf&hasSince != 0 {
since = *f.Since
}
if bf&hasUntil != 0 {
until = *f.Until
} else {
until = math.MaxInt64
}
if bf&hasSince != 0 || bf&hasUntil != 0 {
if evSerials, err = d.GetEventSerialsByCreatedAtRange(since, until); chk.E(err) {
return
}
return
}
// next, kinds/since/until
if bf&(hasSince+hasUntil+hasKinds) == bf && bf&hasKinds != 0 {
var since, until timestamp.Timestamp
if bf&hasSince != 0 {
since = *f.Since
}
if bf&hasUntil != 0 {
until = *f.Until
} else {
until = math.MaxInt64
}
// next, kinds
if bf&hasKinds == hasKinds && ^hasKinds&bf == 0 {
if evSerials, err = d.GetEventSerialsByKindsCreatedAtRange(f.Kinds, since, until); chk.E(err) {
return
}
return
}
// next authors/since/until
if bf&(hasSince+hasUntil+hasAuthors) == bf && bf&hasAuthors != 0 {
var since, until timestamp.Timestamp
if bf&hasSince != 0 {
since = *f.Since
}
if bf&hasUntil != 0 {
until = *f.Until
} else {
until = math.MaxInt64
}
// next authors
if bf&hasAuthors == hasAuthors && ^hasAuthors&bf == 0 {
if evSerials, err = d.GetEventSerialsByAuthorsCreatedAtRange(f.Authors, since, until); chk.E(err) {
return
}
return
}
// next authors/kinds/since/until
if bf&(hasSince+hasUntil+hasKinds+hasAuthors) == bf && bf&(hasAuthors+hasKinds) != 0 {
var since, until timestamp.Timestamp
if bf&hasSince != 0 {
since = *f.Since
}
if bf&hasUntil != 0 {
until = *f.Until
} else {
until = math.MaxInt64
// next authors/kinds
ak := hasAuthors + hasKinds
if bf&(ak) == ak && ^ak&bf == 0 {
if evSerials, err = d.GetEventSerialsByKindsAuthorsCreatedAtRange(f.Kinds, f.Authors, since, until); chk.E(err) {
return
}
return
}
// next authors/tags
at := hasAuthors + hasTags
if bf&(at) == at && ^at&bf == 0 {
if evSerials, err = d.GetEventSerialsByKindsAuthorsCreatedAtRange(f.Kinds, f.Authors, since, until); chk.E(err) {
return
}
return
}
// next kinds/tags
// next
return
}

View File

@@ -118,7 +118,7 @@ func (d *D) GetEventById(evId []byte) (ev *event.E, err error) {
func (d *D) GetEventSerialsByCreatedAtRange(since, until timestamp.Timestamp) (sers []*varint.V, err error) {
// get the start (end) max possible index prefix
startCreatedAt, startSer := indexes.CreatedAtVars()
startCreatedAt.FromInt64(until.ToInt64())
startCreatedAt.FromInt(until.ToInt())
startSer.FromUint64(math.MaxUint64)
prf := new(bytes.Buffer)
if err = indexes.CreatedAtEnc(startCreatedAt, startSer).MarshalWrite(prf); chk.E(err) {
@@ -155,7 +155,7 @@ func (d *D) GetEventSerialsByKindsCreatedAtRange(kinds []int, since, until times
for _, k := range kinds {
kind, startCreatedAt, startSer := indexes.KindCreatedAtVars()
kind.Set(k)
startCreatedAt.FromInt64(until.ToInt64())
startCreatedAt.FromInt(until.ToInt())
startSer.FromUint64(math.MaxUint64)
prf := new(bytes.Buffer)
if err = indexes.KindCreatedAtEnc(kind, startCreatedAt, startSer).MarshalWrite(prf); chk.E(err) {
@@ -205,7 +205,7 @@ func (d *D) GetEventSerialsByAuthorsCreatedAtRange(pubkeys []string, since, unti
err = errorf.E("all pubkeys in authors field of filter failed to decode")
return
}
startCreatedAt.FromInt64(until.ToInt64())
startCreatedAt.FromInt(until.ToInt())
startSer.FromUint64(math.MaxUint64)
prf := new(bytes.Buffer)
if err = indexes.PubkeyCreatedAtEnc(pubkey, startCreatedAt, startSer).MarshalWrite(prf); chk.E(err) {
@@ -256,7 +256,7 @@ func (d *D) GetEventSerialsByKindsAuthorsCreatedAtRange(kinds []int, pubkeys []s
err = errorf.E("all pubkeys in authors field of filter failed to decode")
return
}
startCreatedAt.FromInt64(until.ToInt64())
startCreatedAt.FromInt(until.ToInt())
startSer.FromUint64(math.MaxUint64)
kind.Set(k)
prf := new(bytes.Buffer)

View File

@@ -59,7 +59,7 @@ func (d *D) GetEventIndexes(ev *event.E) (indices [][]byte, ser *varint.V, err e
}
ki := kindidx.FromKind(ev.Kind)
ca := &timestamp.T{}
ca.FromInt64(int64(ev.CreatedAt))
ca.FromInt(ev.CreatedAt.ToInt())
evIFiB := new(bytes.Buffer)
if err = indexes.FullIndexEnc(ser, fid, p, ki, ca).MarshalWrite(evIFiB); chk.E(err) {
return

View File

@@ -90,7 +90,7 @@ func TestFullIndex(t *testing.T) {
t.Fatal(err)
}
ki.Set(frand.Intn(math.MaxUint16))
ca.FromInt64(time.Now().Unix())
ca.FromInt(int(time.Now().Unix()))
ser.FromUint64(uint64(frand.Intn(math.MaxInt64)))
buf := new(bytes.Buffer)
fi := FullIndexEnc(ser, id, p, ki, ca)
@@ -156,7 +156,7 @@ func TestPubkeyCreatedAt(t *testing.T) {
if err = p.FromPubkey(frand.Bytes(schnorr.PubKeyBytesLen)); chk.E(err) {
t.Fatal(err)
}
ca.FromInt64(time.Now().Unix())
ca.FromInt(int(time.Now().Unix()))
ser.FromUint64(uint64(frand.Intn(math.MaxInt64)))
buf := new(bytes.Buffer)
fi := PubkeyCreatedAtEnc(p, ca, ser)
@@ -181,7 +181,7 @@ func TestCreatedAt(t *testing.T) {
var err error
for range 100 {
ca, ser := CreatedAtVars()
ca.FromInt64(time.Now().Unix())
ca.FromInt(int(time.Now().Unix()))
ser.FromUint64(uint64(frand.Intn(math.MaxInt64)))
buf := new(bytes.Buffer)
fi := CreatedAtEnc(ca, ser)
@@ -206,7 +206,7 @@ func TestFirstSeen(t *testing.T) {
var err error
for range 100 {
ser, ts := FirstSeenVars()
ts.FromInt64(time.Now().Unix())
ts.FromInt(int(time.Now().Unix()))
ser.FromUint64(uint64(frand.Intn(math.MaxInt64)))
buf := new(bytes.Buffer)
fs := FirstSeenEnc(ser, ts)

View File

@@ -1,48 +1,56 @@
package timestamp
import (
"encoding/binary"
"bytes"
"io"
"x.realy.lol/errorf"
"x.realy.lol/chk"
"x.realy.lol/database/indexes/types/varint"
timeStamp "x.realy.lol/timestamp"
)
const Len = 8
type T struct{ val []byte }
type T struct{ val int }
func (ts *T) FromInt64(timestamp int64) {
ts.val = make([]byte, Len)
binary.LittleEndian.PutUint64(ts.val, uint64(timestamp))
return
}
func (ts *T) FromInt(t int) { ts.val = t }
func (ts *T) FromInt64(t int64) { ts.val = int(t) }
func FromBytes(timestampBytes []byte) (ts *T, err error) {
if len(timestampBytes) != Len {
err = errorf.E("kind must be %d bytes long, got %d", Len, len(timestampBytes))
v := varint.New()
if err = v.UnmarshalRead(bytes.NewBuffer(timestampBytes)); chk.E(err) {
return
}
ts = &T{val: timestampBytes}
ts = &T{val: v.ToInt()}
return
}
func (ts *T) ToTimestamp() (timestamp timeStamp.Timestamp) {
return timeStamp.Timestamp(binary.LittleEndian.Uint64(ts.val))
return
}
func (ts *T) Bytes() (b []byte, err error) {
v := varint.New()
buf := new(bytes.Buffer)
if err = v.MarshalWrite(buf); chk.E(err) {
return
}
b = buf.Bytes()
return
}
func (ts *T) Bytes() (b []byte) { return ts.val }
func (ts *T) MarshalWrite(w io.Writer) (err error) {
_, err = w.Write(ts.val)
v := varint.New()
if err = v.MarshalWrite(w); chk.E(err) {
return
}
return
}
func (ts *T) UnmarshalRead(r io.Reader) (err error) {
if len(ts.val) < Len {
ts.val = make([]byte, Len)
} else {
ts.val = ts.val[:Len]
v := varint.New()
if err = v.UnmarshalRead(r); chk.E(err) {
return
}
_, err = r.Read(ts.val)
ts.val = v.ToInt()
return
}

View File

@@ -27,6 +27,8 @@ func FromBytes(ser []byte) (s *V, err error) {
func (vi *V) ToUint64() (ser uint64) { return vi.val }
func (vi *V) ToInt() (ser int) { return int(vi.val) }
func (vi *V) ToUint32() (v uint32) { return uint32(vi.val) }
func (vi *V) Bytes() (b []byte) {

View File

@@ -53,7 +53,11 @@ func (d *D) StoreEvent(ev *event.E) (err error) {
if err = indexes.LastAccessedEnc(ser).MarshalWrite(laI); chk.E(err) {
return
}
if err = d.Set(laI.Bytes(), ts.Bytes()); chk.E(err) {
var tsb []byte
if tsb, err = ts.Bytes(); chk.E(err) {
return
}
if err = d.Set(laI.Bytes(), tsb); chk.E(err) {
return
}
// AccessCounter

View File

@@ -15,5 +15,5 @@ func New[T constraints.Integer | constraints.Float](t T) Timestamp {
}
func (t Timestamp) Time() time.Time { return time.Unix(int64(t), 0) }
func (t Timestamp) ToInt64() int64 { return int64(t) }
func (t Timestamp) ToInt64() int64 { return int64(t) }
func (t Timestamp) ToInt() int { return int(t) }