1 /*===========================================================================
2  *
3  *                            PUBLIC DOMAIN NOTICE
4  *               National Center for Biotechnology Information
5  *
6  *  This software/database is a "United States Government Work" under the
7  *  terms of the United States Copyright Act.  It was written as part of
8  *  the author's official duties as a United States Government employee and
9  *  thus cannot be copyrighted.  This software/database is freely available
10  *  to the public for use. The National Library of Medicine and the U.S.
11  *  Government have not placed any restriction on its use or reproduction.
12  *
13  *  Although all reasonable efforts have been taken to ensure the accuracy
14  *  and reliability of the software and data, the NLM and the U.S.
15  *  Government do not and cannot warrant the performance or results that
16  *  may be obtained by using this software or data. The NLM and the U.S.
17  *  Government disclaim all warranties, express or implied, including
18  *  warranties of performance, merchantability or fitness for any particular
19  *  purpose.
20  *
21  *  Please cite the author in any work or product based on this material.
22  *
23  * ===========================================================================
24  *
25  */
26 #ifndef _h_align_writer_priv_
27 #define _h_align_writer_priv_
28 
29 #include <vdb/manager.h>
30 #include <vdb/database.h>
31 #include <vdb/table.h>
32 #include <kdb/meta.h>
33 
34 #include <align/writer-cmn.h>
35 
36 enum TableWriterColumn_Array {
37     ewcol_Ignore = 0x01,
38     ewcol_IsArray = 0x02,
39     ewcol_Temporary = 0x04
40 };
41 
42 /* use ewcol_Ignore to skip optional column when cursor is created */
43 typedef struct TableWriterColumn_struct {
44     uint32_t idx;
45     const char* name;
46     bitsz_t element_bits;
47     uint32_t flags;
48 } TableWriterColumn;
49 
50 typedef struct TableWriter TableWriter;
51 
52 rc_t CC TableWriter_Make(const TableWriter** cself, VDatabase *db, const char* table, const char* table_path /* NULL */);
53 
54 rc_t CC TableWriter_MakeMgr(const TableWriter** cself, VDBManager* mgr, const char* schema_path, const char* type, const char* table_path);
55 
56 rc_t CC TableWriter_MakeUpdate(const TableWriter** cself, VDatabase *db, const char* table);
57 
58 /* add scursor based on column list */
59 rc_t CC TableWriter_AddCursor(const TableWriter* cself, TableWriterColumn* cols, uint32_t col_qty, uint8_t* cursor_id);
60 
61 rc_t CC TableWriter_GetVTable(const TableWriter* cself, VTable** vtbl);
62 
63 rc_t CC TableWriter_Sign(const TableWriter *const cself,
64                          const char loader_name[],
65                          const ver_t loader_version,
66                          const char loader_date[],
67                          const char app_name[],
68                          const ver_t app_version);
69 
70 /* optionally return internal vtbl still open but committed */
71 rc_t CC TableWriter_Whack(const TableWriter* cself, bool commit, uint64_t* rows);
72 
73 /* commits and closes cursor_id, returns number of rows written in cursor, optionally */
74 rc_t CC TableWriter_CloseCursor(const TableWriter* cself, uint8_t cursor_id, uint64_t* rows);
75 
76 /* flushes any pending writes into the table */
77 rc_t CC TableWriter_Flush(const TableWriter *cself, const uint8_t cursor_id);
78 
79 /* CANNOT OPEN ROWS IN MULTIPLE CURSORS AT ONCE
80     Use sequence:
81     OpenRow
82     ColumnWrite - 0 (better set some defaults if not writing to a column!) or more times.
83     CloseRow
84  */
85 /* open a row for writing by cursor_id */
86 rc_t CC TableWriter_OpenRow(const TableWriter* cself, int64_t* rowid, const uint8_t cursor_id);
87 
88 /* open a specific row for writing by cursor_id */
89 rc_t CC TableWriter_OpenRowId(const TableWriter* cself, const int64_t rowid, const uint8_t cursor_id);
90 
91 /* return sequentially next rowid which will be created on OpenRow request */
92 rc_t CC TableWriter_GetNextRowId(const TableWriter* cself, int64_t* rowid, const uint8_t cursor_id);
93 
94 /* writes cursor_id cursor default value to column
95    column pointer must use same object as passed into AddCursor
96  */
97 rc_t CC TableWriter_ColumnDefault(const TableWriter* cself, const uint8_t cursor_id,
98                                   const TableWriterColumn* column, const TableWriterData *data);
99 
100 /* writes value to column into cursor last passed to OpenRow*
101    column pointer must use same object as passed into AddCursor
102  */
103 rc_t CC TableWriter_ColumnWrite(const TableWriter* cself,
104                                 const TableWriterColumn* column, const TableWriterData *data);
105 
106 /* closes row in cursor last used in OpenRow* */
107 rc_t CC TableWriter_CloseRow(const TableWriter* cself);
108 
109 #define TW_COL_WRITE_DEF(writer, curs, col, data) \
110     if( rc == 0 ) { \
111         rc = TableWriter_ColumnDefault(writer, curs, &(col), &(data)); \
112     }
113 
114 #define TW_COL_WRITE_DEF_VAR(writer, curs, col, var) \
115     if( rc == 0 ) { \
116         TableWriterData dz; \
117         dz.buffer = &(var); \
118         dz.elements = 1; \
119         TW_COL_WRITE_DEF(writer, curs, col, dz); \
120     }
121 
122 #define TW_COL_WRITE_DEF_BUF(writer, curs, col, buf, elems) \
123     if( rc == 0 ) { \
124         TableWriterData dz; \
125         dz.buffer = buf; \
126         dz.elements = elems; \
127         TW_COL_WRITE_DEF(writer, curs, col, dz); \
128     }
129 
130 #define TW_COL_WRITE(writer, col, data) \
131     if( rc == 0 && (col).idx != 0) { \
132         rc = TableWriter_ColumnWrite(writer, &(col), &(data)); \
133     }
134 
135 #define TW_COL_WRITE_VAR(writer, col, var) \
136     if( rc == 0 && (col).idx != 0) { \
137         TableWriterData dz; \
138         dz.buffer = &(var); \
139         dz.elements = 1; \
140         TW_COL_WRITE(writer, col, dz); \
141     }
142 
143 #define TW_COL_WRITE_BUF(writer, col, buf, elems) \
144     if( rc == 0 && (col).idx != 0) { \
145         TableWriterData dz; \
146         dz.buffer = buf; \
147         dz.elements = elems; \
148         TW_COL_WRITE(writer, col, dz); \
149     }
150 
151 #endif /* _h_align_writer_priv_ */
152