MCPcopy Create free account
hub / github.com/GoMudEngine/GoMud / loadRecords

Method loadRecords

internal/users/index.go:95–136  ·  view source on GitHub ↗

loadRecords bulk-reads all records from disk into memory and builds lookup maps.

()

Source from the content-addressed store, hash-verified

93
94// loadRecords bulk-reads all records from disk into memory and builds lookup maps.
95func (idx *UserIndex) loadRecords() {
96 idx.byUsername = make(map[string]int64, idx.metaData.RecordCount)
97 idx.byUserId = make(map[int64]string, idx.metaData.RecordCount)
98 idx.highestUserId = 0
99
100 if idx.metaData.RecordCount == 0 {
101 idx.records = nil
102 return
103 }
104
105 f, err := os.Open(idx.Filename)
106 if err != nil {
107 return
108 }
109 defer f.Close()
110
111 dataSize := idx.metaData.RecordCount * idx.metaData.RecordSize
112 buf := make([]byte, dataSize)
113 if _, err := f.Seek(int64(idx.metaData.MetaDataSize), io.SeekStart); err != nil {
114 return
115 }
116 if _, err := io.ReadFull(f, buf); err != nil {
117 return
118 }
119
120 idx.records = make([]IndexUserRecord, idx.metaData.RecordCount)
121
122 for i := uint64(0); i < idx.metaData.RecordCount; i++ {
123 offset := i * idx.metaData.RecordSize
124 rec := &idx.records[i]
125 copy(rec.Username[:], buf[offset:offset+80])
126 rec.UserID = int64(binary.LittleEndian.Uint64(buf[offset+80 : offset+88]))
127
128 username := string(bytes.TrimRight(rec.Username[:], "\x00"))
129 idx.byUsername[username] = rec.UserID
130 idx.byUserId[rec.UserID] = username
131
132 if int(rec.UserID) > idx.highestUserId {
133 idx.highestUserId = int(rec.UserID)
134 }
135 }
136}
137
138// Create initializes a new empty index file with a header.
139func (idx *UserIndex) Create() error {

Callers 1

InitUserIndexFunction · 0.80

Calls 2

CloseMethod · 0.65
OpenMethod · 0.45

Tested by

no test coverage detected