logo

Read and Write in a File based Database: Creating Cool DB-I

March 8, 2025

When it comes to file-based storage, the method you choose can significantly impact your application's performance, scalability, and ease of use. From plain text to binary formats, each approach has its own set of strengths and weaknesses. In this blog, we’ll explore the different file storage methods, discuss their pros and cons, and dive into why binary storage often stands out as the preferred choice for performance and efficiency.

Plain Text Files: Simple and Human-Readable

Plain text files are one of the most straightforward ways to store data. They are human-readable, easy to edit, and require no additional dependencies to implement. This makes them ideal for use cases like configuration files, basic logging, or simple key-value storage. However, their simplicity comes at a cost. Plain text files are inefficient for large datasets, lack structured querying capabilities, and require parsing for any complex operations. While they work well for small-scale or temporary storage, they quickly become impractical as your data grows.

CSV: Structured but Limited

CSV (Comma-Separated Values) files are a step up from plain text, offering a simple row-column format for structured data. They are widely supported by tools like Excel and databases, making them a popular choice for tabular data storage and interchange. However, CSV files have their limitations. They lack data validation or constraints, making them prone to errors. They also struggle with large datasets and don’t support complex relationships like indexing or foreign keys. Despite these drawbacks, CSV files remain a lightweight and portable option for storing structured tabular data.

JSON, XML, and YAML: Flexible but Verbose

Structured markup formats like JSON, XML, and YAML are widely used for configuration files, APIs, and data exchange. These formats support hierarchical and nested data structures, making them more versatile than plain text or CSV. JSON and YAML, in particular, are human-readable and easy to understand, while XML offers strict validation through schemas.

However, these formats can be verbose and inefficient, especially for large datasets. XML, in particular, is known for its verbosity, while YAML’s indentation-sensitive syntax can lead to errors. Additionally, parsing these formats can introduce overhead, which may be a concern for performance-sensitive applications. Despite these challenges, JSON, XML, and YAML are excellent choices for configuration files, data exchange, and document storage in NoSQL-like applications.

Binary Files: Efficient and Powerful

Binary files are the go-to choice for performance-critical applications. They are extremely efficient in terms of space and speed, capable of storing any data format—from numbers to images—without the overhead of text-based formats. This makes them ideal for applications like game engines, media storage, and custom storage engines.

The downside of binary files is that they are not human-readable, requiring specific programs for reading and writing. This can make debugging more challenging, as you can’t simply open the file in a text editor to inspect its contents. However, for applications where speed and space efficiency are critical, binary storage is often the best choice.

Choosing the Right Storage Method

When deciding on a file storage method, it’s important to consider your specific needs. If human readability and simplicity are your top priorities, plain text or structured formats like JSON or YAML might be the way to go. For structured tabular data, CSV files offer a lightweight and portable solution. However, if performance and efficiency are critical, binary storage is often the best choice.

Storing Binary Data

Binary storage shines in scenarios where performance and efficiency are paramount. If you’re working with large volumes of structured or unstructured data, or if your application requires custom file formats, binary files offer unmatched advantages. They eliminate the overhead of text-based formats, allowing for faster read/write operations and more compact storage.

In Cool Db, I experimented using few ways to store binary in a file. At the end settled for this way:

   // Create a file for writing
	file, err := os.Create("test.bin")
    //handle error
	defer file.Close()

	message := "Hello CoolDb"

	// Write the length of the string first (as uint16)
    //LittleEndian is a way of storing binary in which the LSB is stored first.
	err = binary.Write(file, binary.LittleEndian, uint16(len(message)))
	//handle error

	// Write the string data as bytes
	_, err = file.Write([]byte(message))

Reading the file is quite similar too, create a new buffer and read the bytes from the file using binary.Read method.

Conclusion

Each file storage method has its place, but for high-performance and structured storage, binary formats often come out on top. They offer maximum efficiency for raw data storage, avoiding the overhead of text-based formats. Whether you’re building a game engine, a custom database, or a high-performance application, understanding the strengths and weaknesses of each storage method will help you make the right choice for your project.

References

encoding/binary in Go