fireproof-storage / fireproof-alpha

🔥 Cloudless realtime database
https://fireproof.storage
Other
81 stars 4 forks source link

normalize get/put/entries types #124

Open jchris opened 1 year ago

jchris commented 1 year ago

There are a handful of overlapping interfaces that are essentially an iterable blockstore with get/put. Here is advice formatted by ChatGPT (after I toured it through the code) on the interfaces and how to merge them.

Based on the code snippets you've provided and the notes, the get/put interfaces can be grouped into a few categories:

  1. Blockstore Interfaces: These interfaces are used for storing and retrieving blocks of data, typically identified by a CID (Content Identifier). The get method takes a CID as an argument and returns the associated block, while the put method takes a CID and a block of data and stores them. Examples of this interface include the TransactionBlockstore and InnerBlockstore classes in blockstore.js, and the getCarReader and getWriteableCarReader methods in the Base class.

  2. Event Interfaces: These interfaces are used for managing events, which are blocks of data associated with some action or change in the system. The get method takes an event identifier and returns the associated event, while the put method takes an event identifier and an event and stores them. Examples of this interface include the EventFetcher class in clock.js, and the createAndSaveNewEvent function in the core module.

  3. Bulk Operation Interfaces: These interfaces are used for performing bulk operations on a collection of data. The get method typically takes a collection identifier and returns the associated collection, while the put method takes a collection identifier and a collection of data and stores them. Examples of this interface include the doProllyBulk function in the core module.

  4. Key-Value Store Interfaces: These interfaces are used for storing and retrieving key-value pairs. The get method takes a key and returns the associated value, while the put method takes a key and a value and stores them. Examples of this interface include the put and get functions in the core module.

  5. Loader Interfaces: These interfaces are used for loading data into the system. The get method typically takes a data identifier and returns the loaded data, while the put method typically takes a data identifier and a block of data and loads it into the system. Examples of this interface include the getEmptyLoader function in blockstore.js, and the Loader.appropriate method in the Valet class.

Each of these interfaces has different use cases and is used in different parts of the system. The actual behavior of the get and put methods can vary depending on the specific implementation and the context in which they are used.

jchris commented 1 year ago

And a plan for cleanup (maybe a good plan?)

Minimizing the number of types in a codebase can help to reduce complexity and improve maintainability. Here's a plan to consolidate the get/put interfaces:

  1. Merge Blockstore and Event Interfaces: Both of these interfaces deal with storing and retrieving blocks of data, with the main difference being that the Event Interface is specifically for events. We could generalize the Blockstore Interface to handle any type of block, including events. This would involve modifying the get and put methods to handle event blocks, and updating all places in the code where the Event Interface is used to use the generalized Blockstore Interface instead.

  2. Merge Bulk Operation and Key-Value Store Interfaces: Both of these interfaces deal with storing and retrieving collections of data. The main difference is that the Bulk Operation Interface is designed for bulk operations, while the Key-Value Store Interface is designed for individual key-value pairs. We could generalize the Key-Value Store Interface to handle collections of key-value pairs, effectively turning it into a Bulk Operation Interface. This would involve modifying the get and put methods to handle collections of key-value pairs, and updating all places in the code where the Bulk Operation Interface is used to use the generalized Key-Value Store Interface instead.

  3. Merge Loader Interface with the Generalized Blockstore Interface: The Loader Interface is used for loading data into the system, which is similar to what the Blockstore Interface does. We could merge these two interfaces by expanding the Blockstore Interface to include loading functionality. This would involve adding a load method to the Blockstore Interface, and updating all places in the code where the Loader Interface is used to use the expanded Blockstore Interface instead.

After these steps, we would be left with two main interfaces: a generalized Blockstore Interface for handling blocks of data (including loading data), and a generalized Key-Value Store Interface for handling collections of key-value pairs. This would significantly reduce the number of types in the codebase, making it easier to understand and maintain.

jchris commented 1 year ago

I think 1 and 3 are obvious, we should probably wait to do 2 until https://github.com/fireproof-storage/fireproof/issues/17