Skip to content

Commit

Permalink
Merge pull request #13 from elliotchenzichang/professional
Browse files Browse the repository at this point in the history
refactor the objs initial appprach
  • Loading branch information
elliotchenzichang authored Jul 29, 2023
2 parents f0b5bc4 + 4f74acc commit f026e4a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 17 deletions.
80 changes: 65 additions & 15 deletions entity/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,9 @@ type Meta struct {
}

func NewEntryWithData(key []byte, value []byte) *Entry {
e := &Entry{}
e.Key = key
e.Value = value
e.Meta = &Meta{
TimeStamp: uint64(time.Now().Unix()),
KeySize: uint32(len(key)),
ValueSize: uint32(len(value)),
}
return e
}

func NewEntry() *Entry {
e := &Entry{
Meta: &Meta{},
}
now := uint64(time.Now().Unix())
meta := NewMeta().WithTimeStamp(now).WithKeySize(uint32(len(key))).WithValueSize(uint32(len(value)))
e := NewEntry().WithKey(key).WithValue(value).WithMeta(meta)
return e
}

Expand Down Expand Up @@ -92,3 +80,65 @@ func (e *Entry) GetCrc(buf []byte) uint32 {
crc = crc32.Update(crc, crc32.IEEETable, e.Value)
return crc
}

func NewEntry() *Entry {
return new(Entry)
}

func (e *Entry) WithKey(key []byte) *Entry {
e.Key = key
return e
}

func (e *Entry) WithValue(value []byte) *Entry {
e.Value = value
return e
}

func (e *Entry) WithMeta(meta *Meta) *Entry {
e.Meta = meta
return e
}

func NewMeta() *Meta {
return new(Meta)
}

func (m *Meta) WithPosition(pos uint64) *Meta {
m.position = pos
return m
}

func (m *Meta) WithTimeStamp(timestamp uint64) *Meta {
m.TimeStamp = timestamp
return m
}

func (m *Meta) WithKeySize(keySize uint32) *Meta {
m.KeySize = keySize
return m
}

func (m *Meta) WithValueSize(valueSize uint32) *Meta {
m.ValueSize = valueSize
return m
}

func (m *Meta) WithFlag(flag uint8) *Meta {
m.Flag = flag
return m
}

func NewHint() *Hint {
return new(Hint)
}

func (h *Hint) WithFid(fid int) *Hint {
h.Fid = fid
return h
}

func (h *Hint) WithOff(off int64) *Hint {
h.Off = off
return h
}
4 changes: 2 additions & 2 deletions storage/datafiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (af *ActiveFile) WriterEntity(e entity.Entity) (h *entity.Hint, err error)
if err != nil {
return nil, err
}
h = &entity.Hint{Fid: af.fid, Off: af.off}
h = entity.NewHint().WithFid(af.fid).WithOff(af.off)
af.off += e.Size()
return h, nil
}
Expand All @@ -242,7 +242,7 @@ func (of *OldFile) ReadEntity(off int64, length int) (e *entity.Entry, err error
}

func (of *OldFile) ReadEntityWithOutLength(off int64) (e *entity.Entry, err error) {
e = &entity.Entry{Meta: &entity.Meta{}}
e = entity.NewEntry().WithMeta(entity.NewMeta())
buf := make([]byte, entity.MetaSize)
n, err := of.fd.ReadAt(buf, off)
if err != nil {
Expand Down

0 comments on commit f026e4a

Please sign in to comment.