|
| 1 | +# Content Addressable Storage |
| 2 | + |
| 3 | +## Introduction to CAS |
| 4 | + |
| 5 | +Content Addressable Storage, or `CAS`, is a storage system that assigns |
| 6 | +unique addresses to the data stored. It is very useful for data deduplicaton |
| 7 | +and creating unique identifiers. |
| 8 | + |
| 9 | +Unlike other kinds of storage systems, like file systems, CAS is immutable. It |
| 10 | +is more reliable to model a computation by representing the inputs and outputs |
| 11 | +of the computation using objects stored in CAS. |
| 12 | + |
| 13 | +The basic unit of the CAS library is a CASObject, where it contains: |
| 14 | + |
| 15 | +* Data: arbitrary data |
| 16 | +* References: references to other CASObject |
| 17 | + |
| 18 | +It can be conceptually modeled as something like: |
| 19 | + |
| 20 | +``` |
| 21 | +struct CASObject { |
| 22 | + ArrayRef<char> Data; |
| 23 | + ArrayRef<CASObject*> Refs; |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +With this abstraction, it is possible to compose `CASObject`s into a DAG that is |
| 28 | +capable of representing complicated data structures, while still allowing data |
| 29 | +deduplication. Note you can compare two DAGs by just comparing the CASObject |
| 30 | +hash of two root nodes. |
| 31 | + |
| 32 | + |
| 33 | +## LLVM CAS Library User Guide |
| 34 | + |
| 35 | +The CAS-like storage provided in LLVM is `llvm::cas::ObjectStore`. |
| 36 | +To reference a CASObject, there are few different abstractions provided |
| 37 | +with different trade-offs: |
| 38 | + |
| 39 | +### ObjectRef |
| 40 | + |
| 41 | +`ObjectRef` is a lightweight reference to a CASObject stored in the CAS. |
| 42 | +This is the most commonly used abstraction and it is cheap to copy/pass |
| 43 | +along. It has following properties: |
| 44 | + |
| 45 | +* `ObjectRef` is only meaningful within the `ObjectStore` that created the ref. |
| 46 | +`ObjectRef` created by different `ObjectStore` cannot be cross-referenced or |
| 47 | +compared. |
| 48 | +* `ObjectRef` doesn't guarantee the existence of the CASObject it points to. An |
| 49 | +explicit load is required before accessing the data stored in CASObject. |
| 50 | +This load can also fail, for reasons like (but not limited to): object does |
| 51 | +not exist, corrupted CAS storage, operation timeout, etc. |
| 52 | +* If two `ObjectRef` are equal, it is guaranteed that the object they point to |
| 53 | +are identical (if they exist). If they are not equal, the underlying objects are |
| 54 | +guaranteed to be not the same. |
| 55 | + |
| 56 | +### ObjectProxy |
| 57 | + |
| 58 | +`ObjectProxy` represents a loaded CASObject. With an `ObjectProxy`, the |
| 59 | +underlying stored data and references can be accessed without the need |
| 60 | +of error handling. The class APIs also provide convenient methods to |
| 61 | +access underlying data. The lifetime of the underlying data is equal to |
| 62 | +the lifetime of the instance of `ObjectStore` unless explicitly copied. |
| 63 | + |
| 64 | +### CASID |
| 65 | + |
| 66 | +`CASID` is the hash identifier for CASObjects. It owns the underlying |
| 67 | +storage for hash value so it can be expensive to copy and compare depending |
| 68 | +on the hash algorithm. `CASID` is generally only useful in rare situations |
| 69 | +like printing raw hash value or exchanging hash values between different |
| 70 | +CAS instances with the same hashing schema. |
| 71 | + |
| 72 | +### ObjectStore |
| 73 | + |
| 74 | +`ObjectStore` is the CAS-like object storage. It provides API to save |
| 75 | +and load CASObjects, for example: |
| 76 | + |
| 77 | +``` |
| 78 | +ObjectRef A, B, C; |
| 79 | +Expected<ObjectRef> Stored = ObjectStore.store("data", {A, B}); |
| 80 | +Expected<ObjectProxy> Loaded = ObjectStore.getProxy(C); |
| 81 | +``` |
| 82 | + |
| 83 | +It also provides APIs to convert between `ObjectRef`, `ObjectProxy` and |
| 84 | +`CASID`. |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +## CAS Library Implementation Guide |
| 89 | + |
| 90 | +The LLVM ObjectStore API was designed so that it is easy to add |
| 91 | +customized CAS implementations that are interchangeable with the builtin |
| 92 | +ones. |
| 93 | + |
| 94 | +To add your own implementation, you just need to add a subclass to |
| 95 | +`llvm::cas::ObjectStore` and implement all its pure virtual methods. |
| 96 | +To be interchangeable with LLVM ObjectStore, the new CAS implementation |
| 97 | +needs to conform to following contracts: |
| 98 | + |
| 99 | +* Different CASObjects stored in the ObjectStore need to have a different hash |
| 100 | +and result in a different `ObjectRef`. Similarly, the same CASObject should have |
| 101 | +the same hash and the same `ObjectRef`. Note: two different CASObjects with |
| 102 | +identical data but different references are considered different objects. |
| 103 | +* `ObjectRef`s are only comparable within the same `ObjectStore` instance, and |
| 104 | +can be used to determine the equality of the underlying CASObjects. |
| 105 | +* The loaded objects from the ObjectStore need to have a lifetime at least as |
| 106 | +long as the ObjectStore itself so it is always legal to access the loaded data |
| 107 | +without holding on the `ObjectProxy` until the `ObjectStore` is destroyed. |
| 108 | + |
| 109 | + |
| 110 | +If not specified, the behavior can be implementation defined. For example, |
| 111 | +`ObjectRef` can be used to point to a loaded CASObject so |
| 112 | +`ObjectStore` never fails to load. It is also legal to use a stricter model |
| 113 | +than required. For example, the underlying value inside `ObjectRef` can be |
| 114 | +the unique indentities of the objects across multiple `ObjectStore` instances, |
| 115 | +but comparing such `ObjectRef` from different `ObjectStore` is still illegal. |
| 116 | + |
| 117 | +For CAS library implementers, there is also an `ObjectHandle` class that |
| 118 | +is an internal representation of a loaded CASObject reference. |
| 119 | +`ObjectProxy` is just a pair of `ObjectHandle` and `ObjectStore`, and |
| 120 | +just like `ObjectRef`, `ObjectHandle` is only useful when paired with |
| 121 | +the `ObjectStore` that knows about the loaded CASObject. |
0 commit comments