Skip to content

Commit 4f74acc

Browse files
refactor the objs initial appprach
1 parent 8dc4f55 commit 4f74acc

File tree

2 files changed

+67
-17
lines changed

2 files changed

+67
-17
lines changed

entity/entry.go

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,9 @@ type Meta struct {
3232
}
3333

3434
func NewEntryWithData(key []byte, value []byte) *Entry {
35-
e := &Entry{}
36-
e.Key = key
37-
e.Value = value
38-
e.Meta = &Meta{
39-
TimeStamp: uint64(time.Now().Unix()),
40-
KeySize: uint32(len(key)),
41-
ValueSize: uint32(len(value)),
42-
}
43-
return e
44-
}
45-
46-
func NewEntry() *Entry {
47-
e := &Entry{
48-
Meta: &Meta{},
49-
}
35+
now := uint64(time.Now().Unix())
36+
meta := NewMeta().WithTimeStamp(now).WithKeySize(uint32(len(key))).WithValueSize(uint32(len(value)))
37+
e := NewEntry().WithKey(key).WithValue(value).WithMeta(meta)
5038
return e
5139
}
5240

@@ -92,3 +80,65 @@ func (e *Entry) GetCrc(buf []byte) uint32 {
9280
crc = crc32.Update(crc, crc32.IEEETable, e.Value)
9381
return crc
9482
}
83+
84+
func NewEntry() *Entry {
85+
return new(Entry)
86+
}
87+
88+
func (e *Entry) WithKey(key []byte) *Entry {
89+
e.Key = key
90+
return e
91+
}
92+
93+
func (e *Entry) WithValue(value []byte) *Entry {
94+
e.Value = value
95+
return e
96+
}
97+
98+
func (e *Entry) WithMeta(meta *Meta) *Entry {
99+
e.Meta = meta
100+
return e
101+
}
102+
103+
func NewMeta() *Meta {
104+
return new(Meta)
105+
}
106+
107+
func (m *Meta) WithPosition(pos uint64) *Meta {
108+
m.position = pos
109+
return m
110+
}
111+
112+
func (m *Meta) WithTimeStamp(timestamp uint64) *Meta {
113+
m.TimeStamp = timestamp
114+
return m
115+
}
116+
117+
func (m *Meta) WithKeySize(keySize uint32) *Meta {
118+
m.KeySize = keySize
119+
return m
120+
}
121+
122+
func (m *Meta) WithValueSize(valueSize uint32) *Meta {
123+
m.ValueSize = valueSize
124+
return m
125+
}
126+
127+
func (m *Meta) WithFlag(flag uint8) *Meta {
128+
m.Flag = flag
129+
return m
130+
}
131+
132+
func NewHint() *Hint {
133+
return new(Hint)
134+
}
135+
136+
func (h *Hint) WithFid(fid int) *Hint {
137+
h.Fid = fid
138+
return h
139+
}
140+
141+
func (h *Hint) WithOff(off int64) *Hint {
142+
h.Off = off
143+
return h
144+
}

storage/datafiles.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func (af *ActiveFile) WriterEntity(e entity.Entity) (h *entity.Hint, err error)
215215
if err != nil {
216216
return nil, err
217217
}
218-
h = &entity.Hint{Fid: af.fid, Off: af.off}
218+
h = entity.NewHint().WithFid(af.fid).WithOff(af.off)
219219
af.off += e.Size()
220220
return h, nil
221221
}
@@ -242,7 +242,7 @@ func (of *OldFile) ReadEntity(off int64, length int) (e *entity.Entry, err error
242242
}
243243

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

0 commit comments

Comments
 (0)