familia 2.0.0.pre26 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,222 +0,0 @@
1
- # Migrating Guide: Relationships System (v2.0.0-pre7)
2
-
3
- This guide covers adopting the comprehensive relationships system introduced in v2.0.0-pre7.
4
-
5
- ## Relationships System Overview
6
-
7
- The v2.0.0-pre7 release introduces three powerful relationship types:
8
-
9
- - **`tracked_in`** - Multi-presence tracking with score encoding
10
- - **`indexed_by`** - O(1) hash-based lookups
11
- - **`member_of`** - Bidirectional membership with collision-free naming
12
-
13
- ## Migration Steps
14
-
15
- ### 1. Enable Relationships Feature
16
-
17
- Add the relationships feature to your models:
18
-
19
- ```ruby
20
- class Customer < Familia::Horreum
21
- feature :relationships # Required for all relationship types
22
-
23
- identifier_field :custid
24
- field :custid, :name, :email
25
- end
26
- ```
27
-
28
- ### 2. Choose Appropriate Relationship Types
29
-
30
- **For Tracking Collections (sorted sets):**
31
- ```ruby
32
- class Customer < Familia::Horreum
33
- feature :relationships
34
-
35
- # Global tracking with score-based sorting
36
- tracked_in :all_customers, type: :sorted_set, score: :created_at
37
-
38
- # Scoped tracking
39
- tracked_in :active_users, type: :sorted_set, score: :last_seen
40
- end
41
- ```
42
-
43
- **For Fast Lookups (hash indexes):**
44
- ```ruby
45
- class Customer < Familia::Horreum
46
- feature :relationships
47
-
48
- # Global email index for O(1) lookups
49
- indexed_by :email, :email_lookup, target: :global
50
-
51
- # Domain-specific indexes
52
- indexed_by :account_id, :account_lookup, target: Domain
53
- end
54
- ```
55
-
56
- **For Bidirectional Membership:**
57
- ```ruby
58
- class Domain < Familia::Horreum
59
- feature :relationships
60
-
61
- # Belongs to customer's domains collection
62
- participates_in Customer, :domains, type: :set
63
- end
64
- ```
65
-
66
- ### 3. Implement Permission Systems
67
-
68
- For access-controlled relationships:
69
-
70
- ```ruby
71
- class Document < Familia::Horreum
72
- feature :relationships
73
- include Familia::Features::Relationships::PermissionManagement
74
-
75
- # Permission-aware tracking
76
- tracked_in Customer, :documents, score: :created_at
77
-
78
- permission_tracking :user_permissions
79
-
80
- def permission_bits
81
- # Define permission levels (bit-encoded)
82
- @permission_bits || 1 # Default: read-only
83
- end
84
- end
85
- ```
86
-
87
- ### 4. Update Queries
88
-
89
- **Before (manual db operations):**
90
- ```ruby
91
- # Manual sorted set operations
92
- redis.zadd("all_customers", customer.created_at, customer.custid)
93
- customers = redis.zrange("all_customers", 0, -1)
94
- ```
95
-
96
- **After (relationship methods):**
97
- ```ruby
98
- # Automatic method generation
99
- Customer.add_to_all_customers(customer)
100
- customers = Customer.all_customers.to_a
101
- ```
102
-
103
- ## Relationship Method Patterns
104
-
105
- ### `tracked_in` Methods
106
- ```ruby
107
- # Class methods generated:
108
- Customer.add_to_all_customers(customer)
109
- Customer.remove_from_all_customers(customer)
110
- Customer.all_customers # Access sorted set directly
111
- ```
112
-
113
- ### `indexed_by` Methods
114
- ```ruby
115
- # Class methods generated:
116
- Customer.add_to_email_lookup(customer)
117
- Customer.remove_from_email_lookup(customer)
118
- Customer.email_lookup.get(email) # O(1) lookup
119
- ```
120
-
121
- ### `member_of` Methods
122
- ```ruby
123
- # Instance methods generated:
124
- domain.add_to_customer_domains(customer)
125
- domain.remove_from_customer_domains(customer)
126
- domain.in_customer_domains?(customer)
127
- ```
128
-
129
- ## Permission System Migration
130
-
131
- ### Basic Permission Encoding
132
- ```ruby
133
- # Permission levels (bit-encoded)
134
- READ = 1 # 001
135
- WRITE = 4 # 100
136
- DELETE = 32 # 100000
137
- ADMIN = 128 # 10000000
138
-
139
- # Combine permissions
140
- user_permissions = READ | WRITE # Can read and write
141
- admin_permissions = ADMIN # All permissions via hierarchy
142
- ```
143
-
144
- ### Time-Based Permission Scores
145
- ```ruby
146
- # Encode timestamp with permission bits
147
- timestamp = Familia.now.to_i
148
- permissions = READ | WRITE
149
- score = Familia::Features::Relationships::ScoreEncoding.encode_score(timestamp, permissions)
150
-
151
- # Query by permission level
152
- documents_with_write_access = customer.documents.accessible_items(
153
- permissions: WRITE,
154
- collection_key: customer.documents.key
155
- )
156
- ```
157
-
158
- ## Performance Considerations
159
-
160
- ### Relationship Type Selection
161
-
162
- **Use `indexed_by` for:**
163
- - Unique field lookups (email, username, ID)
164
- - Fields that need O(1) access
165
- - Reference relationships
166
-
167
- **Use `tracked_in` for:**
168
- - Time-ordered collections
169
- - Scored/ranked relationships
170
- - Permission-based access control
171
-
172
- **Use `member_of` for:**
173
- - Simple membership tracking
174
- - Bidirectional relationships
175
- - UnsortedSet-based collections
176
-
177
- ### Optimization Tips
178
-
179
- **Batch Operations:**
180
- ```ruby
181
- # Instead of multiple individual operations
182
- customers.each { |c| Customer.add_to_all_customers(c) }
183
-
184
- # Use batch operations where available
185
- Customer.all_customers.add_batch(customers.map(&:identifier),
186
- customers.map(&:created_at))
187
- ```
188
-
189
- **Permission Queries:**
190
- ```ruby
191
- # Efficient permission filtering
192
- accessible_docs = document.accessible_items(
193
- collection_key: customer.documents.key,
194
- minimum_permissions: READ
195
- )
196
- ```
197
-
198
- ## Breaking Changes
199
-
200
- ### Method Name Changes
201
- - Relationship methods follow new naming patterns
202
- - Previous manual db operations need updating
203
- - Collection access methods standardized
204
-
205
- ### Permission System
206
- - New bit-encoded permission system
207
- - Time-based scoring for temporal access
208
- - Permission hierarchy implementation required
209
-
210
- ## Next Steps
211
-
212
- 1. **Analyze Existing Relationships:** Review current db operations for optimization opportunities
213
- 2. **Choose Relationship Types:** Select appropriate types based on usage patterns
214
- 3. **Implement Permissions:** Add access control where needed
215
- 4. **Update Queries:** Replace manual operations with generated relationship methods
216
- 5. **Test Performance:** Benchmark relationship operations under load
217
-
218
- ## Resources
219
-
220
- - [Relationships Methods Guide](../guides/relationships-methods.md) - Complete method reference
221
- - [Relationships Guide](../guides/Relationships-Guide.md) - Implementation examples
222
- - [Performance Guide](../guides/Implementation-Guide.md) - Optimization strategies