package com.cube.api;
import com.cube.storage.LSMStorageEngine;
import com.cube.storage.StorageEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.*;
/**
* REST API Controller for Cube database
*/
@RestController
@RequestMapping("/api/v1")
public class CubeController {
private static final Logger logger = LoggerFactory.getLogger(CubeController.class);
@Autowired
private LSMStorageEngine storageEngine;
@PostMapping("/put")
public ResponseEntity<Map<String, Object>> put(@RequestBody Map<String, String> request) {
try {
String key = request.get("key");
String value = request.get("value");
if (key == null || value == null) {
return ResponseEntity.badRequest().body(Map.of(
"success", false,
"message", "Key and value are required"
));
}
storageEngine.put(key, value.getBytes());
return ResponseEntity.ok(Map.of(
"success", true,
"message", "Value stored successfully",
"key", key
));
} catch (Exception e) {
logger.error("Put operation failed", e);
return ResponseEntity.internalServerError().body(Map.of(
"success", false,
"message", "Error: " + e.getMessage()
));
}
}
@GetMapping("/get/{key}")
public ResponseEntity<Map<String, Object>> get(@PathVariable String key) {
try {
byte[] value = storageEngine.get(key);
if (value == null) {
return ResponseEntity.ok(Map.of(
"success", true,
"found", false,
"key", key
));
}
return ResponseEntity.ok(Map.of(
"success", true,
"found", true,
"key", key,
"value", new String(value)
));
} catch (Exception e) {
logger.error("Get operation failed", e);
return ResponseEntity.internalServerError().body(Map.of(
"success", false,
"message", "Error: " + e.getMessage()
));
}
}
@DeleteMapping("/delete/{key}")
public ResponseEntity<Map<String, Object>> delete(@PathVariable String key) {
try {
storageEngine.delete(key);
return ResponseEntity.ok(Map.of(
"success", true,
"message", "Key deleted",
"key", key
));
} catch (Exception e) {
logger.error("Delete operation failed", e);
return ResponseEntity.internalServerError().body(Map.of(
"success", false,
"message", "Error: " + e.getMessage()
));
}
}
@GetMapping("/scan")
public ResponseEntity<Map<String, Object>> scan(@RequestParam String prefix) {
try {
Map<String, String> results = new LinkedHashMap<>();
Iterator<Map.Entry<String, byte[]>> entries = storageEngine.scanEntries(prefix);
while (entries.hasNext()) {
Map.Entry<String, byte[]> entry = entries.next();
results.put(entry.getKey(), new String(entry.getValue()));
}
return ResponseEntity.ok(Map.of(
"success", true,
"prefix", prefix,
"count", results.size(),
"results", results
));
} catch (Exception e) {
logger.error("Scan operation failed", e);
return ResponseEntity.internalServerError().body(Map.of(
"success", false,
"message", "Error: " + e.getMessage()
));
}
}
@GetMapping("/stats")
public ResponseEntity<Map<String, Object>> stats() {
try {
StorageEngine.StorageStats stats = storageEngine.getStats();
return ResponseEntity.ok(Map.of(
"success", true,
"stats", Map.of(
"totalKeys", stats.getTotalKeys(),
"totalSize", stats.getTotalSize(),
"memtableSize", stats.getMemtableSize(),
"sstableCount", stats.getSstableCount()
)
));
} catch (Exception e) {
logger.error("Stats operation failed", e);
return ResponseEntity.internalServerError().body(Map.of(
"success", false,
"message", "Error: " + e.getMessage()
));
}
}
@PostMapping("/flush")
public ResponseEntity<Map<String, Object>> flush() {
try {
storageEngine.flush();
return ResponseEntity.ok(Map.of(
"success", true,
"message", "Flush completed"
));
} catch (Exception e) {
logger.error("Flush operation failed", e);
return ResponseEntity.internalServerError().body(Map.of(
"success", false,
"message", "Error: " + e.getMessage()
));
}
}
@PostMapping("/compact")
public ResponseEntity<Map<String, Object>> compact() {
try {
storageEngine.compact();
return ResponseEntity.ok(Map.of(
"success", true,
"message", "Compaction completed"
));
} catch (Exception e) {
logger.error("Compaction operation failed", e);
return ResponseEntity.internalServerError().body(Map.of(
"success", false,
"message", "Error: " + e.getMessage()
));
}
}
@GetMapping("/health")
public ResponseEntity<Map<String, Object>> health() {
return ResponseEntity.ok(Map.of(
"status", "UP",
"database", "Cube DB",
"version", "1.0.0"
));
}
}