forked from haiwen/seafile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunkserv-mgr.c
127 lines (96 loc) · 3.08 KB
/
chunkserv-mgr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#include <stdio.h>
#include <string.h>
#include <ccnet.h>
#include "seafile-session.h"
#include "chunkserv-mgr.h"
#include "processors/putcs-proc.h"
#define CHUNKSERVER_DB "chunkserver.db"
SeafCSManager *
seaf_cs_manager_new (SeafileSession *seaf)
{
SeafCSManager *mgr = g_new0 (SeafCSManager, 1);
char *db_path = g_build_filename (seaf->seaf_dir, CHUNKSERVER_DB, NULL);
if (sqlite_open_db (db_path, &mgr->db) < 0) {
g_critical ("Failed to open chunk server db\n");
g_free (db_path);
g_free (mgr);
return NULL;
}
mgr->seaf = seaf;
mgr->chunk_servers = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free, NULL);
return mgr;
}
static int
load_chunk_servers (SeafCSManager *mgr)
{
char sql[256];
sqlite3_stmt *stmt;
int result;
char *cs_id;
snprintf (sql, 256, "SELECT cs_id FROM chunkservers;");
stmt = sqlite_query_prepare (mgr->db, sql);
if (!stmt)
return -1;
while ((result = sqlite3_step (stmt)) == SQLITE_ROW) {
cs_id = (char *) sqlite3_column_text (stmt, 0);
g_hash_table_insert (mgr->chunk_servers, g_strdup(cs_id), NULL);
}
if (result == SQLITE_ERROR) {
const gchar *str = sqlite3_errmsg (mgr->db);
g_warning ("Couldn't execute query, error: %d->'%s'\n",
result, str ? str : "no error given");
sqlite3_finalize (stmt);
return -1;
}
sqlite3_finalize (stmt);
/* Add myself as chunk server by default. */
g_hash_table_insert (mgr->chunk_servers,
g_strdup(mgr->seaf->session->base.id),
NULL);
return 0;
}
static void
register_processors (SeafCSManager *mgr)
{
CcnetClient *client = mgr->seaf->session;
ccnet_register_service (client, "seafile-putcs", "basic",
SEAFILE_TYPE_PUTCS_PROC, NULL);
}
int
seaf_cs_manager_start (SeafCSManager *mgr)
{
const char *sql;
register_processors (mgr);
sql = "CREATE TABLE IF NOT EXISTS chunkservers "
"(id INTEGER PRIMARY KEY, cs_id TEXT);";
if (sqlite_query_exec (mgr->db, sql) < 0)
return -1;
return (load_chunk_servers (mgr));
}
int
seaf_cs_manager_add_chunk_server (SeafCSManager *mgr, const char *cs_id)
{
char sql[256];
snprintf (sql, 256, "INSERT INTO chunkservers VALUES (NULL, '%s');", cs_id);
if (sqlite_query_exec (mgr->db, sql) < 0)
return -1;
g_hash_table_insert (mgr->chunk_servers, g_strdup(cs_id), NULL);
return 0;
}
int
seaf_cs_manager_del_chunk_server (SeafCSManager *mgr, const char *cs_id)
{
char sql[256];
snprintf (sql, 256, "DELETE FROM chunkservers WHERE cs_id = '%s';", cs_id);
if (sqlite_query_exec (mgr->db, sql) < 0)
return -1;
g_hash_table_remove (mgr->chunk_servers, cs_id);
return 0;
}
GList*
seaf_cs_manager_get_chunk_servers (SeafCSManager *mgr)
{
return (g_hash_table_get_keys(mgr->chunk_servers));
}