#define FUSE_USE_VERSION 31 #include #include #include #include #include #include #include #include #include #include static sqlite3 *db = NULL; static const char *db_path = "/Users/accusys/markbase/data/users/warren.sqlite"; // Extreme optimization: very large buffer #define READ_CHUNK_SIZE 524288 // 512KB chunks for maximum throughput // Hash-based cache for faster lookup typedef struct { char node_id[64]; char file_path[512]; long file_size; unsigned int hash; } FileCacheEntry; #define CACHE_SIZE 1000 // Increased cache size static FileCacheEntry file_cache[CACHE_SIZE]; static int cache_count = 0; // Simple hash function static unsigned int simple_hash(const char *str) { unsigned int hash = 5381; int c; while ((c = *str++)) { hash = ((hash << 5) + hash) + c; } return hash; } // Fast hash-based lookup static FileCacheEntry* cache_lookup_fast(const char *node_id) { unsigned int hash = simple_hash(node_id); // Direct hash lookup (O(1) average) for (int i = 0; i < cache_count; i++) { if (file_cache[i].hash == hash && strcmp(file_cache[i].node_id, node_id) == 0) { return &file_cache[i]; } } return NULL; } static void cache_insert_fast(const char *node_id, const char *file_path, long file_size) { unsigned int hash = simple_hash(node_id); // Check if exists for (int i = 0; i < cache_count; i++) { if (file_cache[i].hash == hash && strcmp(file_cache[i].node_id, node_id) == 0) { return; } } // Insert if (cache_count < CACHE_SIZE) { strcpy(file_cache[cache_count].node_id, node_id); strcpy(file_cache[cache_count].file_path, file_path); file_cache[cache_count].file_size = file_size; file_cache[cache_count].hash = hash; cache_count++; } } // Inode cache for path → node_id mapping typedef struct { char path[256]; char node_id[64]; unsigned int path_hash; } PathCacheEntry; #define PATH_CACHE_SIZE 2000 static PathCacheEntry path_cache[PATH_CACHE_SIZE]; static int path_cache_count = 0; static char* path_cache_lookup(const char *path) { unsigned int hash = simple_hash(path); for (int i = 0; i < path_cache_count; i++) { if (path_cache[i].path_hash == hash && strcmp(path_cache[i].path, path) == 0) { return strdup(path_cache[i].node_id); } } return NULL; } static void path_cache_insert(const char *path, const char *node_id) { if (path_cache_count < PATH_CACHE_SIZE) { strcpy(path_cache[path_cache_count].path, path); strcpy(path_cache[path_cache_count].node_id, node_id); path_cache[path_cache_count].path_hash = simple_hash(path); path_cache_count++; } } static int init_db() { if (sqlite3_open_v2(db_path, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX, NULL) != SQLITE_OK) { fprintf(stderr, "Cannot open database\n"); return -1; } printf("Database opened\n"); return 0; } static void *mb_init(struct fuse_conn_info *conn, struct fuse_config *cfg) { (void) conn; cfg->kernel_cache = 1; init_db(); // Pre-cache ALL files (aggressive caching) const char *sql = "SELECT f.file_uuid, l.location, f.file_size " "FROM file_nodes f JOIN file_locations l ON f.file_uuid = l.file_uuid " "ORDER BY f.file_size DESC LIMIT 1000"; sqlite3_stmt *stmt; if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) { while (sqlite3_step(stmt) == SQLITE_ROW) { cache_insert_fast( (const char*)sqlite3_column_text(stmt, 0), (const char*)sqlite3_column_text(stmt, 1), sqlite3_column_int64(stmt, 2) ); } sqlite3_finalize(stmt); printf("Pre-cached %d files\n", cache_count); } return NULL; } static void mb_destroy(void *userdata) { (void) userdata; printf("Cache stats: %d files, %d paths\n", cache_count, path_cache_count); if (db) sqlite3_close(db); } // Optimized path lookup with caching static char* find_node_id(const char *path) { if (strcmp(path, "/") == 0) return NULL; // Check path cache first char *cached_node_id = path_cache_lookup(path); if (cached_node_id) return cached_node_id; char *path_copy = strdup(path); char *components[20]; int depth = 0; char *token = strtok(path_copy + 1, "/"); while (token && depth < 20) { components[depth++] = strdup(token); token = strtok(NULL, "/"); } free(path_copy); if (depth == 0) return NULL; char *current_parent_id = NULL; for (int level = 0; level < depth; level++) { const char *sql; sqlite3_stmt *stmt; if (level == 0) { sql = "SELECT node_id FROM file_nodes " "WHERE label = ? AND (parent_id IS NULL OR parent_id = '') LIMIT 1"; } else { sql = "SELECT node_id FROM file_nodes " "WHERE label = ? AND parent_id = ? LIMIT 1"; } if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK) { for (int i = 0; i < depth; i++) free(components[i]); if (current_parent_id) free(current_parent_id); return NULL; } sqlite3_bind_text(stmt, 1, components[level], -1, SQLITE_STATIC); if (level > 0 && current_parent_id) { sqlite3_bind_text(stmt, 2, current_parent_id, -1, SQLITE_STATIC); } if (sqlite3_step(stmt) != SQLITE_ROW) { sqlite3_finalize(stmt); for (int i = 0; i < depth; i++) free(components[i]); if (current_parent_id) free(current_parent_id); return NULL; } const char *found_node_id = (const char*)sqlite3_column_text(stmt, 0); if (current_parent_id) free(current_parent_id); current_parent_id = strdup(found_node_id); sqlite3_finalize(stmt); } for (int i = 0; i < depth; i++) free(components[i]); // Cache result path_cache_insert(path, current_parent_id); return current_parent_id; } static int mb_getattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi) { (void) fi; memset(stbuf, 0, sizeof(struct stat)); if (strcmp(path, "/") == 0) { stbuf->st_mode = S_IFDIR | 0755; stbuf->st_nlink = 2; return 0; } char *node_id = find_node_id(path); if (!node_id) return -ENOENT; // Check file cache for size info FileCacheEntry *cached = cache_lookup_fast(node_id); if (cached && cached->file_size > 0) { stbuf->st_mode = S_IFREG | 0644; stbuf->st_nlink = 1; stbuf->st_size = cached->file_size; free(node_id); return 0; } // Fallback: query database sqlite3_stmt *stmt; const char *sql = "SELECT node_type, file_size FROM file_nodes WHERE node_id = ?"; int result = -ENOENT; if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) { sqlite3_bind_text(stmt, 1, node_id, -1, SQLITE_STATIC); if (sqlite3_step(stmt) == SQLITE_ROW) { const char *node_type = (const char*)sqlite3_column_text(stmt, 0); long file_size = sqlite3_column_int64(stmt, 1); if (strcmp(node_type, "folder") == 0) { stbuf->st_mode = S_IFDIR | 0755; stbuf->st_nlink = 2; } else { stbuf->st_mode = S_IFREG | 0644; stbuf->st_nlink = 1; stbuf->st_size = file_size; } result = 0; } sqlite3_finalize(stmt); } free(node_id); return result; } static int mb_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi, enum fuse_readdir_flags flags) { (void) offset; (void) fi; (void) flags; filler(buf, ".", NULL, 0, FUSE_FILL_DIR_DEFAULTS); filler(buf, "..", NULL, 0, FUSE_FILL_DIR_DEFAULTS); if (strcmp(path, "/") == 0) { sqlite3_stmt *stmt; if (sqlite3_prepare_v2(db, "SELECT label FROM file_nodes WHERE parent_id IS NULL OR parent_id = ''", -1, &stmt, NULL) == SQLITE_OK) { while (sqlite3_step(stmt) == SQLITE_ROW) { filler(buf, (const char*)sqlite3_column_text(stmt, 0), NULL, 0, FUSE_FILL_DIR_DEFAULTS); } sqlite3_finalize(stmt); } return 0; } char *parent_node_id = find_node_id(path); if (!parent_node_id) return -ENOENT; sqlite3_stmt *stmt; if (sqlite3_prepare_v2(db, "SELECT label FROM file_nodes WHERE parent_id = ?", -1, &stmt, NULL) == SQLITE_OK) { sqlite3_bind_text(stmt, 1, parent_node_id, -1, SQLITE_STATIC); while (sqlite3_step(stmt) == SQLITE_ROW) { filler(buf, (const char*)sqlite3_column_text(stmt, 0), NULL, 0, FUSE_FILL_DIR_DEFAULTS); } sqlite3_finalize(stmt); } free(parent_node_id); return 0; } static int mb_open(const char *path, struct fuse_file_info *fi) { char *node_id = find_node_id(path); if (!node_id) return -ENOENT; free(node_id); return 0; } // Extreme optimized read static int mb_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { (void) fi; // Fast path: check caches first char *node_id = path_cache_lookup(path); if (!node_id) node_id = find_node_id(path); if (!node_id) return -ENOENT; FileCacheEntry *cached = cache_lookup_fast(node_id); char *file_path = NULL; if (cached) { file_path = strdup(cached->file_path); } else { // Single query sqlite3_stmt *stmt; if (sqlite3_prepare_v2(db, "SELECT location FROM file_locations WHERE file_uuid = ? LIMIT 1", -1, &stmt, NULL) != SQLITE_OK) { free(node_id); return -EIO; } sqlite3_bind_text(stmt, 1, node_id, -1, SQLITE_STATIC); if (sqlite3_step(stmt) != SQLITE_ROW) { sqlite3_finalize(stmt); free(node_id); return -ENOENT; } file_path = strdup((const char*)sqlite3_column_text(stmt, 0)); sqlite3_finalize(stmt); cache_insert_fast(node_id, file_path, 0); } free(node_id); // Direct file read with large chunks FILE *fp = fopen(file_path, "rb"); if (!fp) { free(file_path); return -ENOENT; } fseek(fp, offset, SEEK_SET); // Read in 512KB chunks for maximum throughput size_t total_read = 0; while (total_read < size) { size_t chunk = (size - total_read > READ_CHUNK_SIZE) ? READ_CHUNK_SIZE : size - total_read; size_t bytes = fread(buf + total_read, 1, chunk, fp); total_read += bytes; if (bytes < chunk) break; } fclose(fp); free(file_path); return total_read; } static int mb_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { (void) fi; char *node_id = find_node_id(path); if (!node_id) return -ENOENT; FileCacheEntry *cached = cache_lookup_fast(node_id); char *file_path = cached ? strdup(cached->file_path) : NULL; if (!file_path) { sqlite3_stmt *stmt; if (sqlite3_prepare_v2(db, "SELECT location FROM file_locations WHERE file_uuid = ? LIMIT 1", -1, &stmt, NULL) != SQLITE_OK) { free(node_id); return -EIO; } sqlite3_bind_text(stmt, 1, node_id, -1, SQLITE_STATIC); if (sqlite3_step(stmt) != SQLITE_ROW) { sqlite3_finalize(stmt); free(node_id); return -ENOENT; } file_path = strdup((const char*)sqlite3_column_text(stmt, 0)); sqlite3_finalize(stmt); } free(node_id); FILE *fp = fopen(file_path, "r+b"); if (!fp) { free(file_path); return -ENOENT; } fseek(fp, offset, SEEK_SET); size_t bytes_written = fwrite(buf, 1, size, fp); fclose(fp); free(file_path); return bytes_written; } static int mb_mkdir(const char *path, mode_t mode) { (void) mode; char *path_copy = strdup(path); char *last_slash = strrchr(path_copy, '/'); if (!last_slash || strlen(last_slash + 1) == 0) { free(path_copy); return -EINVAL; } char *folder_name = strdup(last_slash + 1); *last_slash = '\0'; char *parent_path = (strlen(path_copy) == 0) ? strdup("/") : strdup(path_copy); free(path_copy); char *parent_node_id = (strcmp(parent_path, "/") == 0) ? NULL : find_node_id(parent_path); char new_node_id[64]; snprintf(new_node_id, sizeof(new_node_id), "folder_%ld_%d", time(NULL), rand() % 10000); sqlite3_stmt *stmt; const char *sql = "INSERT INTO file_nodes (node_id, label, parent_id, node_type, file_size) " "VALUES (?, ?, ?, 'folder', 0)"; if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK) { free(folder_name); free(parent_path); if (parent_node_id) free(parent_node_id); return -EIO; } sqlite3_bind_text(stmt, 1, new_node_id, -1, SQLITE_STATIC); sqlite3_bind_text(stmt, 2, folder_name, -1, SQLITE_STATIC); if (parent_node_id) { sqlite3_bind_text(stmt, 3, parent_node_id, -1, SQLITE_STATIC); } else { sqlite3_bind_null(stmt, 3); } int result = sqlite3_step(stmt); sqlite3_finalize(stmt); free(folder_name); free(parent_path); if (parent_node_id) free(parent_node_id); return (result == SQLITE_DONE) ? 0 : -EIO; } static const struct fuse_operations mb_oper = { .init = mb_init, .destroy = mb_destroy, .getattr = mb_getattr, .readdir = mb_readdir, .open = mb_open, .read = mb_read, .write = mb_write, .mkdir = mb_mkdir, }; int main(int argc, char *argv[]) { printf("MarkBase FUSE v14.0 - Extreme Optimized\n"); printf("=======================================\n"); printf("Optimizations:\n"); printf(" - 512KB read chunks\n"); printf(" - Hash-based cache (O(1) lookup)\n"); printf(" - Path cache (2000 entries)\n"); printf(" - Pre-cache 1000 files\n"); printf(" - No mutex (single-threaded FUSE)\n"); printf("\n"); return fuse_main(argc, argv, &mb_oper, NULL); }