Skip to content

Commit 3a1ee5d

Browse files
authored
add some util api for sdk (#22627)
add some util api for sdk Approved by: @LeftHandCold
1 parent dfb6fd9 commit 3a1ee5d

19 files changed

+1218
-52
lines changed

clients/python/Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,20 @@ lint: ## Run code quality checks
286286
flake8 matrixone --max-line-length=125 --ignore=E203,W503
287287
@echo "$(GREEN)Code quality checks completed!$(NC)"
288288

289+
.PHONY: lint-fix
290+
lint-fix: ## Auto-fix linting issues (trailing whitespace, imports, formatting)
291+
@echo "$(BLUE)Auto-fixing linting issues...$(NC)"
292+
@echo "$(YELLOW)Removing trailing whitespace...$(NC)"
293+
find matrixone -name "*.py" -type f -exec sed -i '' 's/[[:space:]]*$$//' {} +
294+
@echo "$(YELLOW)Removing unused imports with autoflake...$(NC)"
295+
$(PYTHON) -m autoflake --remove-all-unused-imports --in-place --recursive matrixone || echo "$(YELLOW)⚠️ autoflake not available, skipping import cleanup$(NC)"
296+
@echo "$(YELLOW)Sorting imports with isort...$(NC)"
297+
$(PYTHON) -m isort matrixone || echo "$(YELLOW)⚠️ isort not available, skipping import sorting$(NC)"
298+
@echo "$(YELLOW)Formatting code with black...$(NC)"
299+
black matrixone --line-length=125
300+
@echo "$(GREEN)Linting issues auto-fixed!$(NC)"
301+
@echo "$(YELLOW)Run 'make lint' to verify fixes$(NC)"
302+
289303
.PHONY: format
290304
format: ## Format code with black
291305
@echo "$(BLUE)Formatting code...$(NC)"

clients/python/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ A comprehensive Python SDK for MatrixOne that provides SQLAlchemy-like interface
3131
- Table size and row count statistics
3232
- Column data distribution analysis
3333
- Index usage metrics
34+
- 🔍 **Secondary Index Verification**: Verify consistency of secondary indexes with main table
35+
- Get all secondary index table names
36+
- Get specific index table by index name
37+
- Verify row counts across main table and all indexes in single query
3438
- 📸 **Snapshot Management**: Create and manage database snapshots at multiple levels
3539
-**Point-in-Time Recovery**: PITR functionality for precise data recovery
3640
- 🔄 **Table Cloning**: Clone databases and tables efficiently with data replication
@@ -323,6 +327,27 @@ print(f"Original size: {table_stats['original_size']} bytes")
323327
print(f"Compressed size: {table_stats['compress_size']} bytes")
324328
```
325329

330+
### Secondary Index Verification
331+
332+
```python
333+
# Get all secondary index tables
334+
index_tables = client.get_secondary_index_tables('my_table')
335+
print(f"Found {len(index_tables)} secondary indexes")
336+
337+
# Get specific index by name
338+
physical_table = client.get_secondary_index_table_by_name('my_table', 'idx_name')
339+
print(f"Index 'idx_name' -> {physical_table}")
340+
341+
# Verify all indexes have same count as main table
342+
try:
343+
count = client.verify_table_index_counts('my_table')
344+
print(f"✓ Verification passed! Row count: {count}")
345+
except ValueError as e:
346+
print(f"✗ Index mismatch detected:")
347+
print(e)
348+
# Error includes all count details for debugging
349+
```
350+
326351
### Version Management
327352

328353
```python

clients/python/docs/examples.rst

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,90 @@ Modern ORM-style fulltext search with boolean_match and natural_match:
930930
931931
orm_fulltext_search_example()
932932
933+
Secondary Index Verification Examples
934+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
935+
936+
Verify consistency of secondary indexes with the main table:
937+
938+
.. code-block:: python
939+
940+
from matrixone import Client
941+
from matrixone.config import get_connection_params
942+
from matrixone.orm import declarative_base
943+
from sqlalchemy import Column, Integer, String, Index
944+
945+
def index_verification_example():
946+
host, port, user, password, database = get_connection_params()
947+
client = Client()
948+
client.connect(host=host, port=port, user=user, password=password, database=database)
949+
950+
# Define model with secondary indexes
951+
Base = declarative_base()
952+
953+
class Product(Base):
954+
__tablename__ = 'products'
955+
956+
id = Column(Integer, primary_key=True)
957+
name = Column(String(100))
958+
category = Column(String(50))
959+
price = Column(Integer)
960+
961+
# Define secondary indexes
962+
__table_args__ = (
963+
Index('idx_name', 'name'),
964+
Index('idx_category', 'category'),
965+
Index('idx_price', 'price'),
966+
)
967+
968+
# Create table with indexes
969+
client.create_table(Product)
970+
971+
# Insert data
972+
products = [
973+
{'id': i, 'name': f'Product {i}', 'category': f'Cat {i % 5}', 'price': i * 100}
974+
for i in range(1, 1001)
975+
]
976+
client.batch_insert(Product, products)
977+
978+
# 1. Get all secondary index tables
979+
index_tables = client.get_secondary_index_tables('products')
980+
print(f"Found {len(index_tables)} secondary indexes:")
981+
for idx_table in index_tables:
982+
print(f" {idx_table}")
983+
984+
# 2. Get specific index by name
985+
name_index = client.get_secondary_index_table_by_name('products', 'idx_name')
986+
category_index = client.get_secondary_index_table_by_name('products', 'idx_category')
987+
988+
print(f"\nIndex mappings:")
989+
print(f" idx_name -> {name_index}")
990+
print(f" idx_category -> {category_index}")
991+
992+
# 3. Verify all indexes have consistent row counts
993+
try:
994+
count = client.verify_table_index_counts('products')
995+
print(f"\n✓ All indexes verified! Row count: {count}")
996+
except ValueError as e:
997+
print(f"\n✗ Verification failed:")
998+
print(e)
999+
1000+
# 4. Use in production monitoring
1001+
import time
1002+
for i in range(3):
1003+
try:
1004+
count = client.verify_table_index_counts('products')
1005+
print(f"{time.ctime()}: ✓ Indexes OK ({count} rows)")
1006+
except ValueError as e:
1007+
print(f"{time.ctime()}: ✗ INDEX MISMATCH!")
1008+
print(e)
1009+
time.sleep(1)
1010+
1011+
# Clean up
1012+
client.drop_table(Product)
1013+
client.disconnect()
1014+
1015+
index_verification_example()
1016+
9331017
Error Handling Examples
9341018
~~~~~~~~~~~~~~~~~~~~~~~
9351019

clients/python/docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type safety and extensive documentation.
2525
fulltext_guide
2626
orm_guide
2727
metadata_guide
28+
index_verification_guide
2829

2930
.. toctree::
3031
:maxdepth: 2

0 commit comments

Comments
 (0)