source: titan/titan/mediadbcache.h @ 39070

Last change on this file since 39070 was 16769, checked in by nit, 12 years ago

[titan] update mediadb and tmc

File size: 2.4 KB
Line 
1#ifndef MEDIADBCACHE_H
2#define MEDIADBCACHE_H
3
4void debugmediadbcache()
5{
6        int i, count = 0, maxcount = 0;;
7        struct mediadbcache* node = NULL;
8       
9        for(i = 0; i < MEDIADBCACHEMAX; i++)
10        {
11                node = mediadbcache[i];
12                count = 0;
13                while(node != NULL)
14                {
15                        maxcount++;
16                        count++;
17                        node = node->next;
18                }
19                printf("hash=%d, count=%d\n", i, count);
20        }
21        printf("maxcount=%d\n", maxcount);
22}
23
24// flag 0: path + file
25// flag 1: file
26struct mediadb* getmediadb(char* path, char* file, int flag)
27{
28        unsigned int hash;
29        struct mediadbcache* node = NULL;
30
31        hash = gethash(file) % MEDIADBCACHEMAX;
32        if(hash < 0 || hash >= MEDIADBCACHEMAX) hash = 0;
33
34        m_lock(&status.mediadbmutex, 17);
35        node = mediadbcache[hash];
36
37        while(node != NULL)
38        {
39                if(ostrcmp(file, node->file) == 0)
40                {
41                        if(flag == 0 && ostrrstr(path, node->path, -1, 1) == NULL)
42                        {
43                                node = node->next;
44                                continue;
45                        }
46                        m_unlock(&status.mediadbmutex, 17);
47                        return node->mediadbnode;
48                }
49
50                node = node->next;
51        }
52
53        m_unlock(&status.mediadbmutex, 17);
54        return NULL;
55}
56
57struct mediadbcache* modifymediadbcache(char* path, char* file, struct mediadb* mnode)
58{
59        unsigned int hash;
60        //struct mediadbcache* node = NULL, *prev = NULL, *newnode = NULL;
61        struct mediadbcache* node = NULL, *newnode = NULL;
62
63        hash = gethash(file) % MEDIADBCACHEMAX;
64        if(hash < 0 || hash >= MEDIADBCACHEMAX) hash = 0;
65
66        newnode = (struct mediadbcache*)calloc(1, sizeof(struct mediadbcache));
67        if(newnode == NULL)
68        {
69                err("no memory");
70                return NULL;
71        }
72
73  //TODO: copy file???
74        newnode->file = file;
75        newnode->path = path;
76        newnode->mediadbnode = mnode;
77
78        node = mediadbcache[hash];
79/*
80        prev = mediadbcache[hash];
81       
82        if(node != NULL)
83        {
84                while(node != NULL)
85                {
86                        prev = node;
87                        node = node->next;
88                }
89                prev->next = newnode;
90        }
91        else
92                mediadbcache[hash] = newnode;
93*/
94        mediadbcache[hash] = newnode;
95        newnode->next = node;
96
97        return newnode;
98}
99
100void delmediadbcache(char* file, struct mediadb* mnode)
101{
102        unsigned int hash;
103        struct mediadbcache *node = NULL, *prev = NULL;
104
105        hash = gethash(file) % MEDIADBCACHEMAX;
106        if(hash < 0 || hash >= MEDIADBCACHEMAX) hash = 0;
107        node = mediadbcache[hash];
108        prev = mediadbcache[hash];
109
110        while(node != NULL)
111        {
112                if(node->mediadbnode == mnode)
113                {
114                        if(node == mediadbcache[hash])
115                                mediadbcache[hash] = node->next;
116                        else
117                                prev->next = node->next;
118
119                        free(node);
120                        node = NULL;
121                        break;
122                }
123
124                prev = node;
125                node = node->next;
126        }
127}
128
129#endif
Note: See TracBrowser for help on using the repository browser.