Documentation ¶
Index ¶
- Constants
- type Error
- type KeyValueStore
- func (kvs *KeyValueStore) Compact()
- func (kvs *KeyValueStore) Delete(key []byte)
- func (kvs *KeyValueStore) LoadIndex(data []byte) error
- func (kvs *KeyValueStore) PrintAllKeyValuePairs()
- func (kvs *KeyValueStore) Read(key []byte) ([]byte, error)
- func (kvs *KeyValueStore) RebuildIndex()
- func (kvs *KeyValueStore) SaveIndex() ([]byte, error)
- func (kvs *KeyValueStore) Write(key, value []byte)
- type Record
- type RecordType
Constants ¶
const ( // ErrorKeyDeleted is returned when trying to read a key that has been deleted. ErrorKeyDeleted = Error("key is deleted") // ErrorKeyNotFound is returned when trying to read a key that does not exist. ErrorKeyNotFound = Error("key not found") // ErrorChecksumMismatch is returned when a record's calculated checksum // does not match its stored checksum, indicating Data corruption. ErrorChecksumMismatch = Error("checksum mismatch") )
Define constants for common error scenarios in KeyValueStore operations.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Error ¶
type Error string
Error is a custom error type that wraps a string. It is used for providing specific error messages related to the KeyValueStore operations.
type KeyValueStore ¶
type KeyValueStore struct { sync.RWMutex // Embed the RWMutex to ensure thread safety during concurrent read and write operations. Data []byte // A byte slice that holds the serialized records. Index map[string]int64 // A map that maps keys (as strings) to their position in the Data byte slice. }
KeyValueStore is a simple key-value store implementation. It utilizes a byte slice (Data) to store serialized records and a map (Index) to map keys to their position in the Data byte slice. The KeyValueStore struct also embeds the sync.RWMutex to ensure thread safety during concurrent read and write operations.
func (*KeyValueStore) Compact ¶
func (kvs *KeyValueStore) Compact()
Compact iterates through the KeyValueStore's Data byte slice, identifies the latest records for each key, and rebuilds the Data slice and Index, effectively removing any deleted or outdated records. This method is useful for reducing the storage size and improving the performance of the KeyValueStore.
func (*KeyValueStore) Delete ¶
func (kvs *KeyValueStore) Delete(key []byte)
Delete takes a key in the form of a byte slice and marks the associated record as deleted in the KeyValueStore instance. It achieves this by creating a new Record with the RecordType set to RecordTypeDeleted and appending it to the Data byte slice. It also updates the Index map to map the key to the position of the new deleted record.
func (*KeyValueStore) LoadIndex ¶
func (kvs *KeyValueStore) LoadIndex(data []byte) error
LoadIndex deserializes a byte slice containing a serialized Index (a map of keys to their position in the Data byte slice) using the gob package, and restores the deserialized Index to the KeyValueStore. This method can be used to load a previously saved Index from disk or another storage medium.
func (*KeyValueStore) PrintAllKeyValuePairs ¶
func (kvs *KeyValueStore) PrintAllKeyValuePairs()
PrintAllKeyValuePairs iterates through the KeyValueStore's Data byte slice, deserializes each record, and prints the key, value, and record type for each record. This method is useful for debugging and getting an overview of the KeyValueStore's contents.
func (*KeyValueStore) Read ¶
func (kvs *KeyValueStore) Read(key []byte) ([]byte, error)
Read takes a key in the form of a byte slice and retrieves the associated value from the KeyValueStore instance. It returns the value as a byte slice, or an error if the key is not found, deleted, or there's a checksum mismatch.
func (*KeyValueStore) RebuildIndex ¶
func (kvs *KeyValueStore) RebuildIndex()
RebuildIndex iterates through the KeyValueStore's Data byte slice, deserializes each record, and rebuilds the Index by calculating the position of each key in the Data slice. This method is useful when the Index has been lost or corrupted and needs to be reconstructed.
func (*KeyValueStore) SaveIndex ¶
func (kvs *KeyValueStore) SaveIndex() ([]byte, error)
SaveIndex serializes the KeyValueStore's Index (a map of keys to their position in the Data byte slice) using the gob package, and returns the serialized Index as a byte slice. This method can be used to persist the Index to disk or another storage medium, for later restoration.
func (*KeyValueStore) Write ¶
func (kvs *KeyValueStore) Write(key, value []byte)
Write takes a key and a value, both in byte slices, and stores them in the KeyValueStore instance. This method is responsible for creating a new Record with the given key-value pair and appending it to the Data byte slice. It also updates the Index map to map the key to the position of the new record.
type Record ¶
type Record struct { Crc uint32 // 4 bytes: The CRC-32 checksum used to ensure the integrity of the stored record. Type RecordType // 1 byte: The record type, which can be either RecordTypeNormal or RecordTypeDeleted. Key []byte // Variable length: The key of the key-value pair. Value []byte // Variable length: The value of the key-value pair. KeyLength uint32 // 4 bytes: The length of the key. ValueLength uint32 // 4 bytes: The length of the value. }
Record represents a single key-value pair along with its metadata in the KeyValueStore. It contains fields for the CRC checksum, record type (normal or deleted), key, value, key length, and value length.
type RecordType ¶
type RecordType uint8
RecordType is a custom uint8 type that represents the type of a record. It is used to differentiate between normal and deleted records in the KeyValueStore.
const ( // RecordTypeNormal represents a normal record, which contains a key-value pair. RecordTypeNormal RecordType = iota // RecordTypeDeleted represents a deleted record, which is marked as deleted but not removed from the Data slice. RecordTypeDeleted )
Define constants for the different record types.