table: fixed bug when field length > 128.
[libastrodb.git] / examples / custom.c
blob087de18104eb65cce214fb9aaf699e41f26c90ea
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 * Copyright (C) 2005 Liam Girdwood
19 #include <stdlib.h>
20 #include <errno.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <sys/time.h>
24 #include <libastrodb/astrodb.h>
26 static struct timeval start, end;
28 inline static void start_timer(void)
30 gettimeofday(&start, NULL);
33 static void end_timer(int objects, int bytes)
35 double usecs;
36 gettimeofday(&end, NULL);
37 usecs = (end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec);
39 if (bytes)
40 printf ("Time(secs) %f : %9.0f objects per sec %9.0f bytes per secs\n",
41 usecs / 1000000.0, (1000000.0 / usecs) * objects,
42 (1000000.0 / usecs) * bytes);
43 else
44 printf("Time(secs) %f : %9.0f objects per sec\n",
45 usecs / 1000000.0, (1000000.0 / usecs) * objects);
48 static void print_progress(float percent, char* msg, void* data)
50 printf("Progress %2.2f %s\n", percent, msg);
53 struct sky2k {
54 struct astrodb_object_deep d;
55 float RV;
56 char HD[6];
57 char sp[3];
60 static struct astrodb_schema_object sky2k_fields[] = {
61 astrodb_member("RA", "RA", struct sky2k, d.posn.ra, CT_DOUBLE, "degrees", 0, NULL),
62 astrodb_member("DEC", "DEC", struct sky2k, d.posn.dec, CT_DOUBLE, "degrees", 0, NULL),
63 astrodb_gmember("RA Hours", "RAh", struct sky2k, d.posn.ra, CT_DOUBLE_HMS_HRS, "hours", 2, NULL),
64 astrodb_gmember("RA Minutes", "RAm", struct sky2k, d.posn.ra, CT_DOUBLE_HMS_MINS, "minutes", 1, NULL),
65 astrodb_gmember("RA Seconds", "RAs", struct sky2k, d.posn.ra, CT_DOUBLE_HMS_SECS, "seconds", 0, NULL),
66 astrodb_gmember("DEC Degrees", "DEd", struct sky2k, d.posn.dec, CT_DOUBLE_DMS_DEGS, "degrees", 3, NULL),
67 astrodb_gmember("DEC Minutes", "DEm", struct sky2k, d.posn.dec, CT_DOUBLE_DMS_MINS, "minutes", 2, NULL),
68 astrodb_gmember("DEC Seconds", "DEs", struct sky2k, d.posn.dec, CT_DOUBLE_DMS_SECS, "seconds", 1, NULL),
69 astrodb_gmember("DEC sign", "DE-", struct sky2k, d.posn.dec, CT_SIGN, "", 0, NULL),
70 astrodb_member("Visual Mag", "Vmag", struct sky2k, d.Vmag, CT_FLOAT, "", 0, NULL),
71 astrodb_member("RV", "RV", struct sky2k, RV, CT_FLOAT, "", 0, NULL),
72 astrodb_member("Sp", "Sp", struct sky2k, sp, CT_STRING, "", 0, NULL),
73 astrodb_member("HD", "HD", struct sky2k, HD, CT_STRING, "", 0, NULL),
77 /*
78 * Get all the objects in the table.
80 static int get1(struct astrodb_table * table)
82 int count = 0;
83 struct astrodb_slist *res = NULL;
85 printf("Get all table objects\n");
86 astrodb_table_unclip(table);
87 count = astrodb_table_get_objects(table, &res, ADB_SMEM);
88 printf(" Got %d objects\n", count);
89 astrodb_table_put_objects(res);
90 return 0;
93 int main (int argc, char *argv[])
95 struct astrodb_db *db = NULL;
96 struct astrodb_library *lib = NULL;
97 struct astrodb_table *table = NULL;
98 int table_size, object_size, err;
100 printf("%s using libastro %s\n", argv[0], astrodb_get_version());
102 /* set the remote db and initialise local repository/cache */
103 lib = astrodb_open_library("ftp://cdsarc.u-strasbg.fr/pub/cats",
104 "lnc-test");
105 if (lib == NULL) {
106 printf("failed to open library\n");
107 return -1;
110 /* create a dbalog, using class V and dbalog 109 (Skymap2000) */
111 /* ra,dec,mag bounds are set here along with the 3d tile array size */
112 db = astrodb_create_db(lib, "V", "109",
113 0.0, 360.0, -90.0, 90.0, 15.0, -2.0, 0);
114 if (db == NULL) {
115 printf("failed to open db\n");
116 return -1;
119 /* use the first table in this example */
120 table = astrodb_table_create(db, "sky2kv4", ADB_MEM | ADB_FILE);
121 if (table == NULL) {
122 printf("failed to create table\n");
123 return -1;
126 err = astrodb_table_register_schema(table, sky2k_fields,
127 astrodb_size(sky2k_fields));
128 if (err < 0) {
129 printf("failed to register schema\n");
130 return -1;
133 /* Vmag is blank in some records in the table, so we use can Vder
134 * as an alternate field.
136 if (astrodb_table_alt_column(table, "Vmag", "Vder", 0))
137 printf("failed to add alt index\n");
139 /* We want to quickly search the table based on object ID and HD number */
140 if (astrodb_table_hash_key(table, "ID"))
141 printf("failed to hash on ID\n");
142 if (astrodb_table_hash_key(table, "HD"))
143 printf("failed to hash on HD\n");
145 /* Import the table from remote/local repo into memory/disk cache */
146 start_timer();
147 err = astrodb_table_open(table, 0, 0, 0);
148 if (err < 0) {
149 printf("failed to open table\n");
150 return -1;
153 table_size = astrodb_table_get_size(table);
154 object_size = astrodb_table_get_row_size(table);
155 end_timer(table_size, table_size * object_size);
157 /* we can now perform operations on the dbalog data !!! */
158 get1(table);
160 /* get some hashed id's */
161 printf("object %p\n", astrodb_table_get_object(table, " 7000303", "ID"));
162 printf("object %p\n", astrodb_table_get_object(table, "7000303", "ID"));
163 printf("object %p\n", astrodb_table_get_object(table, "73256", "HD"));
165 /* were done with the table */
166 astrodb_table_close(table);
168 /* were now done with dbalog */
169 astrodb_db_free(db);
170 astrodb_close_library(lib);
172 return 0;