Loading...
Searching...
No Matches
StorageManagerExample.java
Go to the documentation of this file.
1import java.util.List;
2import java.util.ArrayList;
3import java.util.Map;
4import java.util.HashMap;
5
10public class StorageManagerExample {
11 private NavigineSdk sdk;
12 private StorageManager storageManager;
13 private KeyValueStorage userStorage;
14 private KeyValueStorage appStorage;
15 private KeyValueStorage cacheStorage;
16
18 initializeSdk();
19 setupStorages();
20 }
21
25 private void initializeSdk() {
26 try {
27 // [java_NavigineSdk_getInstance]
28 // Get SDK instance
29 sdk = NavigineSdk.getInstance();
30 // [java_NavigineSdk_getInstance]
31
32 // [java_NavigineSdk_setUserHash]
33 // Set user hash
34 sdk.setUserHash("USER-HASH-HERE");
35 // [java_NavigineSdk_setUserHash]
36
37 // [java_NavigineSdk_setServer]
38 // Set server URL (optional)
39 sdk.setServer("https://custom.navigine.com");
40 // [java_NavigineSdk_setServer]
41
42 // [java_NavigineSdk_getStorageManager]
43 // Get StorageManager for working with storages
44 storageManager = sdk.getStorageManager();
45 // [java_NavigineSdk_getStorageManager]
46
47 if (storageManager != null) {
48 System.out.println("StorageManager successfully initialized");
49 }
50 } catch (Exception e) {
51 System.out.println("Error initializing SDK: " + e.getMessage());
52 }
53 }
54
58 private void setupStorages() {
59 if (storageManager == null) {
60 System.out.println("StorageManager not initialized");
61 return;
62 }
63
64 // [java_StorageManager_getStorage]
65 // Get or create different storage instances
66 userStorage = storageManager.getStorage("user_preferences");
67 appStorage = storageManager.getStorage("app_settings");
68 cacheStorage = storageManager.getStorage("cache");
69 // [java_StorageManager_getStorage]
70
71 System.out.println("Storage instances created");
72 }
73
78 if (storageManager == null) {
79 System.out.println("StorageManager not initialized");
80 return;
81 }
82
83 // [java_StorageManager_getStorageList]
84 // Get list of all existing storages
85 List<String> storageList = storageManager.getStorageList();
86 System.out.println("Existing storages: " + storageList);
87 // [java_StorageManager_getStorageList]
88
89 // [java_StorageManager_getStorage1]
90 // Get or create storage by name
91 KeyValueStorage testStorage = storageManager.getStorage("test_storage");
92 System.out.println("Created test storage");
93 // [java_StorageManager_getStorage1]
94
95 // Demonstrate storage creation and listing
96 storageManager.getStorage("another_storage");
97 List<String> updatedList = storageManager.getStorageList();
98 System.out.println("Updated storage list: " + updatedList);
99
100 // [java_StorageManager_removeStorage]
101 // Remove storage and all its data
102 storageManager.removeStorage("test_storage");
103 System.out.println("Removed test storage");
104 // [java_StorageManager_removeStorage]
105
106 List<String> finalList = storageManager.getStorageList();
107 System.out.println("Final storage list: " + finalList);
108 }
109
114 if (userStorage == null) {
115 System.out.println("User storage not initialized");
116 return;
117 }
118
119 System.out.println("\n=== KeyValueStorage Methods Demonstration ===");
120
121 // [java_KeyValueStorage_putString]
122 // Store string values
123 userStorage.putString("user_name", "John Doe");
124 userStorage.putString("user_email", "john.doe@example.com");
125 System.out.println("Stored string values");
126 // [java_KeyValueStorage_putString]
127
128 // [java_KeyValueStorage_putInt]
129 // Store integer values
130 userStorage.putInt("user_age", 25);
131 userStorage.putInt("login_count", 42);
132 System.out.println("Stored integer values");
133 // [java_KeyValueStorage_putInt]
134
135 // [java_KeyValueStorage_putLong]
136 // Store long values
137 userStorage.putLong("registration_timestamp", 1640995200000L);
138 userStorage.putLong("last_login_timestamp", System.currentTimeMillis());
139 System.out.println("Stored long values");
140 // [java_KeyValueStorage_putLong]
141
142 // [java_KeyValueStorage_putBool]
143 // Store boolean values
144 userStorage.putBool("is_premium", true);
145 userStorage.putBool("notifications_enabled", false);
146 System.out.println("Stored boolean values");
147 // [java_KeyValueStorage_putBool]
148
149 // [java_KeyValueStorage_putFloat]
150 // Store float values
151 userStorage.putFloat("user_rating", 4.5f);
152 userStorage.putFloat("temperature", 23.5f);
153 System.out.println("Stored float values");
154 // [java_KeyValueStorage_putFloat]
155
156 // [java_KeyValueStorage_putDouble]
157 // Store double values
158 userStorage.putDouble("user_location_lat", 55.7558);
159 userStorage.putDouble("user_location_lng", 37.6176);
160 System.out.println("Stored double values");
161 // [java_KeyValueStorage_putDouble]
162
163 // [java_KeyValueStorage_contains]
164 // Check if keys exist
165 boolean hasUserName = userStorage.contains("user_name");
166 boolean hasNonExistent = userStorage.contains("non_existent_key");
167 System.out.println("Contains 'user_name': " + hasUserName);
168 System.out.println("Contains 'non_existent_key': " + hasNonExistent);
169 // [java_KeyValueStorage_contains]
170
171 // [java_KeyValueStorage_getKeys]
172 // Get all stored keys
173 List<String> allKeys = userStorage.getKeys();
174 System.out.println("All stored keys: " + allKeys);
175 // [java_KeyValueStorage_getKeys]
176
177 // [java_KeyValueStorage_getString]
178 // Retrieve string values with defaults
179 String userName = userStorage.getString("user_name", "Unknown");
180 String userEmail = userStorage.getString("user_email", "");
181 String nonExistent = userStorage.getString("non_existent_key", "default_value");
182 System.out.println("User name: " + userName);
183 System.out.println("User email: " + userEmail);
184 System.out.println("Non-existent key: " + nonExistent);
185 // [java_KeyValueStorage_getString]
186
187 // [java_KeyValueStorage_getInt]
188 // Retrieve integer values with defaults
189 int userAge = userStorage.getInt("user_age", 0);
190 int loginCount = userStorage.getInt("login_count", 0);
191 int nonExistentInt = userStorage.getInt("non_existent_int", -1);
192 System.out.println("User age: " + userAge);
193 System.out.println("Login count: " + loginCount);
194 System.out.println("Non-existent int: " + nonExistentInt);
195 // [java_KeyValueStorage_getInt]
196
197 // [java_KeyValueStorage_getLong]
198 // Retrieve long values with defaults
199 long regTimestamp = userStorage.getLong("registration_timestamp", 0);
200 long lastLogin = userStorage.getLong("last_login_timestamp", 0);
201 long nonExistentLong = userStorage.getLong("non_existent_long", -1);
202 System.out.println("Registration timestamp: " + regTimestamp);
203 System.out.println("Last login timestamp: " + lastLogin);
204 System.out.println("Non-existent long: " + nonExistentLong);
205 // [java_KeyValueStorage_getLong]
206
207 // [java_KeyValueStorage_getBool]
208 // Retrieve boolean values with defaults
209 boolean isPremium = userStorage.getBool("is_premium", false);
210 boolean notificationsEnabled = userStorage.getBool("notifications_enabled", true);
211 boolean nonExistentBool = userStorage.getBool("non_existent_bool", false);
212 System.out.println("Is premium: " + isPremium);
213 System.out.println("Notifications enabled: " + notificationsEnabled);
214 System.out.println("Non-existent bool: " + nonExistentBool);
215 // [java_KeyValueStorage_getBool]
216
217 // [java_KeyValueStorage_getFloat]
218 // Retrieve float values with defaults
219 float userRating = userStorage.getFloat("user_rating", 0.0f);
220 float temperature = userStorage.getFloat("temperature", 0.0f);
221 float nonExistentFloat = userStorage.getFloat("non_existent_float", -1.0f);
222 System.out.println("User rating: " + userRating);
223 System.out.println("Temperature: " + temperature);
224 System.out.println("Non-existent float: " + nonExistentFloat);
225 // [java_KeyValueStorage_getFloat]
226
227 // [java_KeyValueStorage_getDouble]
228 // Retrieve double values with defaults
229 double userLat = userStorage.getDouble("user_location_lat", 0.0);
230 double userLng = userStorage.getDouble("user_location_lng", 0.0);
231 double nonExistentDouble = userStorage.getDouble("non_existent_double", -1.0);
232 System.out.println("User location lat: " + userLat);
233 System.out.println("User location lng: " + userLng);
234 System.out.println("Non-existent double: " + nonExistentDouble);
235 // [java_KeyValueStorage_getDouble]
236
237 // [java_KeyValueStorage_remove]
238 // Remove specific keys
239 userStorage.remove("user_age");
240 userStorage.remove("non_existent_key"); // No-op
241 System.out.println("Removed 'user_age' key");
242 // [java_KeyValueStorage_remove]
243
244 // Verify removal
245 boolean stillHasAge = userStorage.contains("user_age");
246 System.out.println("Still contains 'user_age': " + stillHasAge);
247
248 // [java_KeyValueStorage_clear]
249 // Clear all data
250 userStorage.clear();
251 System.out.println("Cleared all data from user storage");
252 // [java_KeyValueStorage_clear]
253
254 // Verify clear
255 List<String> keysAfterClear = userStorage.getKeys();
256 System.out.println("Keys after clear: " + keysAfterClear);
257 }
258
263 if (userStorage == null || appStorage == null || cacheStorage == null) {
264 System.out.println("Storages not initialized");
265 return;
266 }
267
268 System.out.println("\n=== Multiple Storages Demonstration ===");
269
270 // Store different data in different storages
271 userStorage.putString("name", "John Doe");
272 userStorage.putInt("age", 25);
273 userStorage.putBool("is_premium", true);
274
275 appStorage.putString("theme", "dark");
276 appStorage.putInt("api_version", 2);
277 appStorage.putBool("debug_mode", false);
278
279 cacheStorage.putLong("last_update", System.currentTimeMillis());
280 cacheStorage.putString("api_token", "abc123");
281 cacheStorage.putDouble("cached_rating", 4.2);
282
283 // Retrieve data from different storages
284 System.out.println("User storage data:");
285 demonstrateStorageData(userStorage);
286
287 System.out.println("\nApp storage data:");
288 demonstrateStorageData(appStorage);
289
290 System.out.println("\nCache storage data:");
291 demonstrateStorageData(cacheStorage);
292 }
293
297 private void demonstrateStorageData(KeyValueStorage storage) {
298 List<String> keys = storage.getKeys();
299 for (String key : keys) {
300 if (storage.contains(key)) {
301 // Try to get value as string first
302 String stringValue = storage.getString(key, "");
303 if (!stringValue.isEmpty()) {
304 System.out.println(" " + key + ": " + stringValue + " (string)");
305 continue;
306 }
307
308 // Try integer
309 int intValue = storage.getInt(key, -999999);
310 if (intValue != -999999) {
311 System.out.println(" " + key + ": " + intValue + " (int)");
312 continue;
313 }
314
315 // Try long
316 long longValue = storage.getLong(key, -999999);
317 if (longValue != -999999) {
318 System.out.println(" " + key + ": " + longValue + " (long)");
319 continue;
320 }
321
322 // Try boolean
323 boolean boolValue = storage.getBool(key, false);
324 if (storage.contains(key)) {
325 System.out.println(" " + key + ": " + boolValue + " (bool)");
326 continue;
327 }
328
329 // Try float
330 float floatValue = storage.getFloat(key, -999999.0f);
331 if (floatValue != -999999.0f) {
332 System.out.println(" " + key + ": " + floatValue + " (float)");
333 continue;
334 }
335
336 // Try double
337 double doubleValue = storage.getDouble(key, -999999.0);
338 if (doubleValue != -999999.0) {
339 System.out.println(" " + key + ": " + doubleValue + " (double)");
340 }
341 }
342 }
343 }
344
349 if (storageManager == null) {
350 System.out.println("StorageManager not initialized");
351 return;
352 }
353
354 System.out.println("\n=== Storage Analytics ===");
355
356 // Analyze all storages
357 List<String> allStorages = storageManager.getStorageList();
358 System.out.println("Total storages: " + allStorages.size());
359
360 for (String storageName : allStorages) {
361 KeyValueStorage storage = storageManager.getStorage(storageName);
362 List<String> keys = storage.getKeys();
363 System.out.println("Storage '" + storageName + "' contains " + keys.size() + " items");
364
365 // Analyze data types
366 int stringCount = 0;
367 int intCount = 0;
368 int longCount = 0;
369 int boolCount = 0;
370 int floatCount = 0;
371 int doubleCount = 0;
372
373 for (String key : keys) {
374 // Count by trying to get each type
375 String stringValue = storage.getString(key, "");
376 if (!stringValue.isEmpty()) {
377 stringCount++;
378 continue;
379 }
380
381 int intValue = storage.getInt(key, -999999);
382 if (intValue != -999999) {
383 intCount++;
384 continue;
385 }
386
387 long longValue = storage.getLong(key, -999999);
388 if (longValue != -999999) {
389 longCount++;
390 continue;
391 }
392
393 boolean boolValue = storage.getBool(key, false);
394 if (storage.contains(key)) {
395 boolCount++;
396 continue;
397 }
398
399 float floatValue = storage.getFloat(key, -999999.0f);
400 if (floatValue != -999999.0f) {
401 floatCount++;
402 continue;
403 }
404
405 double doubleValue = storage.getDouble(key, -999999.0);
406 if (doubleValue != -999999.0) {
407 doubleCount++;
408 }
409 }
410
411 System.out.println(" - Strings: " + stringCount);
412 System.out.println(" - Integers: " + intCount);
413 System.out.println(" - Longs: " + longCount);
414 System.out.println(" - Booleans: " + boolCount);
415 System.out.println(" - Floats: " + floatCount);
416 System.out.println(" - Doubles: " + doubleCount);
417 }
418 }
419
424 if (storageManager == null) {
425 System.out.println("StorageManager not initialized");
426 return;
427 }
428
429 System.out.println("\n=== Data Migration Demonstration ===");
430
431 // Create old and new storages
432 KeyValueStorage oldStorage = storageManager.getStorage("old_storage");
433 KeyValueStorage newStorage = storageManager.getStorage("new_storage");
434
435 // Populate old storage with data
436 oldStorage.putString("name", "John Doe");
437 oldStorage.putInt("age", 25);
438 oldStorage.putBool("is_premium", true);
439 oldStorage.putDouble("rating", 4.5);
440
441 System.out.println("Old storage before migration:");
442 demonstrateStorageData(oldStorage);
443
444 // Migrate data from old to new storage
445 List<String> keys = oldStorage.getKeys();
446 for (String key : keys) {
447 // Migrate string data
448 String stringValue = oldStorage.getString(key, "");
449 if (!stringValue.isEmpty()) {
450 newStorage.putString(key, stringValue);
451 continue;
452 }
453
454 // Migrate integer data
455 int intValue = oldStorage.getInt(key, -999999);
456 if (intValue != -999999) {
457 newStorage.putInt(key, intValue);
458 continue;
459 }
460
461 // Migrate long data
462 long longValue = oldStorage.getLong(key, -999999);
463 if (longValue != -999999) {
464 newStorage.putLong(key, longValue);
465 continue;
466 }
467
468 // Migrate boolean data
469 boolean boolValue = oldStorage.getBool(key, false);
470 if (oldStorage.contains(key)) {
471 newStorage.putBool(key, boolValue);
472 continue;
473 }
474
475 // Migrate float data
476 float floatValue = oldStorage.getFloat(key, -999999.0f);
477 if (floatValue != -999999.0f) {
478 newStorage.putFloat(key, floatValue);
479 continue;
480 }
481
482 // Migrate double data
483 double doubleValue = oldStorage.getDouble(key, -999999.0);
484 if (doubleValue != -999999.0) {
485 newStorage.putDouble(key, doubleValue);
486 }
487 }
488
489 System.out.println("\nNew storage after migration:");
490 demonstrateStorageData(newStorage);
491
492 // Remove old storage
493 storageManager.removeStorage("old_storage");
494 System.out.println("\nRemoved old storage");
495
496 // Verify migration
497 List<String> finalStorages = storageManager.getStorageList();
498 System.out.println("Final storage list: " + finalStorages);
499 }
500
505 if (storageManager == null) {
506 System.out.println("StorageManager not initialized");
507 return;
508 }
509
510 System.out.println("\n=== Storage Cleanup Demonstration ===");
511
512 // Create some temporary storages
513 storageManager.getStorage("temp_cache_1");
514 storageManager.getStorage("temp_cache_2");
515 storageManager.getStorage("permanent_storage");
516
517 List<String> beforeCleanup = storageManager.getStorageList();
518 System.out.println("Storages before cleanup: " + beforeCleanup);
519
520 // Clean up temporary storages
521 List<String> storages = storageManager.getStorageList();
522 for (String storageName : storages) {
523 if (storageName.startsWith("temp_")) {
524 storageManager.removeStorage(storageName);
525 System.out.println("Removed temporary storage: " + storageName);
526 }
527 }
528
529 List<String> afterCleanup = storageManager.getStorageList();
530 System.out.println("Storages after cleanup: " + afterCleanup);
531 }
532
536 public void runAllDemonstrations() {
537 System.out.println("=== StorageManager and KeyValueStorage Examples ===\n");
538
539 demonstrateStorageManagerMethods();
540 demonstrateKeyValueStorageMethods();
541 demonstrateMultipleStorages();
542 demonstrateStorageAnalytics();
543 demonstrateDataMigration();
544 demonstrateStorageCleanup();
545
546 System.out.println("\n=== All demonstrations completed ===");
547 }
548
552 public void cleanup() {
553 if (storageManager != null) {
554 // Remove test storages
555 List<String> storages = storageManager.getStorageList();
556 for (String storageName : storages) {
557 if (storageName.startsWith("test_") ||
558 storageName.startsWith("temp_") ||
559 storageName.startsWith("old_") ||
560 storageName.startsWith("new_")) {
561 storageManager.removeStorage(storageName);
562 }
563 }
564 }
565 }
566
570 public static void main(String[] args) {
571 StorageManagerExample example = new StorageManagerExample();
572
573 // Run all demonstrations
574 example.runAllDemonstrations();
575
576 // Cleanup
577 example.cleanup();
578
579 System.out.println("Example completed successfully!");
580 }
581}