|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { dbRunner } from './lib/util.js'; |
| 3 | +describe('Database Properties', () => { |
| 4 | + it('should get string property from database', () => dbRunner(async ({ db }) => { |
| 5 | + // Put some data to ensure the database has stats |
| 6 | + await db.put('key1', 'value1'); |
| 7 | + await db.put('key2', 'value2'); |
| 8 | + await db.flush(); |
| 9 | + |
| 10 | + // Get level stats property |
| 11 | + const levelStats = db.getDBProperty('rocksdb.levelstats'); |
| 12 | + expect(levelStats).toBeDefined(); |
| 13 | + expect(typeof levelStats).toBe('string'); |
| 14 | + expect(levelStats.length).toBeGreaterThan(0); |
| 15 | + })); |
| 16 | + |
| 17 | + it('should get stats property from database', () => dbRunner(async ({ db }) => { |
| 18 | + // Put some data |
| 19 | + await db.put('foo', 'bar'); |
| 20 | + await db.flush(); |
| 21 | + |
| 22 | + // Get stats property |
| 23 | + const stats = db.getDBProperty('rocksdb.stats'); |
| 24 | + expect(stats).toBeDefined(); |
| 25 | + expect(typeof stats).toBe('string'); |
| 26 | + expect(stats.length).toBeGreaterThan(0); |
| 27 | + })); |
| 28 | + |
| 29 | + it('should get integer property from database', () => dbRunner(async ({ db }) => { |
| 30 | + // Put some data |
| 31 | + await db.put('key1', 'value1'); |
| 32 | + await db.put('key2', 'value2'); |
| 33 | + |
| 34 | + // Get estimated number of keys |
| 35 | + const numKeys = db.getDBIntProperty('rocksdb.estimate-num-keys'); |
| 36 | + expect(numKeys).toBeDefined(); |
| 37 | + expect(typeof numKeys).toBe('number'); |
| 38 | + expect(numKeys).toBeGreaterThan(0); |
| 39 | + })); |
| 40 | + |
| 41 | + it('should get num-files-at-level property', () => dbRunner(async ({ db }) => { |
| 42 | + // Put some data and flush to create files |
| 43 | + await db.put('key1', 'value1'); |
| 44 | + await db.put('key2', 'value2'); |
| 45 | + await db.flush(); |
| 46 | + |
| 47 | + // Get number of files at level 0, for some reason this is a string property |
| 48 | + const numFiles = +db.getDBProperty('rocksdb.num-files-at-level0'); |
| 49 | + expect(numFiles).toBeDefined(); |
| 50 | + expect(typeof numFiles).toBe('number'); |
| 51 | + expect(numFiles).toBeGreaterThan(0); |
| 52 | + })); |
| 53 | + |
| 54 | + it('should test blob files with num-blob-files property', () => dbRunner(async ({ db }) => { |
| 55 | + // Create large values that should trigger blob storage |
| 56 | + // RocksDB typically uses blobs for values larger than a threshold (often 1KB or configurable) |
| 57 | + const largeValue = 'x'.repeat(10000); // 10KB value to ensure blob creation |
| 58 | + |
| 59 | + // Write multiple large values |
| 60 | + await db.put('blob1', largeValue); |
| 61 | + await db.put('blob2', largeValue); |
| 62 | + await db.put('blob3', largeValue); |
| 63 | + await db.flush(); |
| 64 | + |
| 65 | + // Get number of blob files |
| 66 | + const numBlobFiles = db.getDBIntProperty('rocksdb.num-blob-files'); |
| 67 | + expect(numBlobFiles).toBeDefined(); |
| 68 | + expect(typeof numBlobFiles).toBe('number'); |
| 69 | + expect(numBlobFiles).toBeGreaterThan(0); |
| 70 | + |
| 71 | + // Note: Whether blobs are actually created depends on RocksDB configuration |
| 72 | + // The test verifies the method works, not necessarily that blobs are created |
| 73 | + })); |
| 74 | + |
| 75 | + it('should get live-blob-file-size property', () => dbRunner(async ({ db }) => { |
| 76 | + // Create large values |
| 77 | + const largeValue = 'x'.repeat(10000); |
| 78 | + await db.put('blob1', largeValue); |
| 79 | + await db.put('blob2', largeValue); |
| 80 | + await db.flush(); |
| 81 | + |
| 82 | + // Get live blob file size |
| 83 | + const blobSize = db.getDBIntProperty('rocksdb.live-blob-file-size'); |
| 84 | + expect(blobSize).toBeDefined(); |
| 85 | + expect(typeof blobSize).toBe('number'); |
| 86 | + expect(blobSize).toBeGreaterThan(0); |
| 87 | + })); |
| 88 | + |
| 89 | + it('should get total-blob-file-size property', () => dbRunner(async ({ db }) => { |
| 90 | + // Create large values |
| 91 | + const largeValue = 'x'.repeat(10000); |
| 92 | + await db.put('blob1', largeValue); |
| 93 | + await db.flush(); |
| 94 | + |
| 95 | + // Get total blob file size |
| 96 | + const totalBlobSize = db.getDBIntProperty('rocksdb.total-blob-file-size'); |
| 97 | + expect(totalBlobSize).toBeDefined(); |
| 98 | + expect(typeof totalBlobSize).toBe('number'); |
| 99 | + expect(totalBlobSize).toBeGreaterThan(0); |
| 100 | + })); |
| 101 | + |
| 102 | + it('should get background-errors property', () => dbRunner(async ({ db }) => { |
| 103 | + const bgErrors = db.getDBIntProperty('rocksdb.background-errors'); |
| 104 | + expect(bgErrors).toBeDefined(); |
| 105 | + expect(typeof bgErrors).toBe('number'); |
| 106 | + expect(bgErrors).toBe(0); // Should be 0 for a healthy database |
| 107 | + })); |
| 108 | + |
| 109 | + it('should get cur-size-all-mem-tables property', () => dbRunner(async ({ db }) => { |
| 110 | + await db.put('key1', 'value1'); |
| 111 | + await db.put('key2', 'value2'); |
| 112 | + |
| 113 | + const memTableSize = db.getDBIntProperty('rocksdb.cur-size-all-mem-tables'); |
| 114 | + expect(memTableSize).toBeDefined(); |
| 115 | + expect(typeof memTableSize).toBe('number'); |
| 116 | + expect(memTableSize).toBeGreaterThan(0); |
| 117 | + })); |
| 118 | + |
| 119 | + it('should throw error for invalid string property', () => dbRunner(async ({ db }) => { |
| 120 | + expect(() => { |
| 121 | + db.getDBProperty(undefined as any); |
| 122 | + }).toThrow('Property name is required'); |
| 123 | + |
| 124 | + expect(() => { |
| 125 | + db.getDBProperty('invalid.property.name.that.does.not.exist'); |
| 126 | + }).toThrow('Failed to get database property'); |
| 127 | + })); |
| 128 | + |
| 129 | + it('should throw error for invalid integer property', () => dbRunner(async ({ db }) => { |
| 130 | + expect(() => { |
| 131 | + db.getDBIntProperty(undefined as any); |
| 132 | + }).toThrow('Property name is required'); |
| 133 | + |
| 134 | + expect(() => { |
| 135 | + db.getDBIntProperty('invalid.property.name.that.does.not.exist'); |
| 136 | + }).toThrow('Failed to get database integer property'); |
| 137 | + })); |
| 138 | + |
| 139 | + it('should throw error when database is not open', () => dbRunner({ skipOpen: true }, async ({ db }) => { |
| 140 | + expect(() => { |
| 141 | + db.getDBProperty('rocksdb.stats'); |
| 142 | + }).toThrow('Database not open'); |
| 143 | + |
| 144 | + expect(() => { |
| 145 | + db.getDBIntProperty('rocksdb.estimate-num-keys'); |
| 146 | + }).toThrow('Database not open'); |
| 147 | + })); |
| 148 | +}); |
0 commit comments