1/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
3/**
4 * @copyright 2013 Couchbase, Inc.
5 *
6 * @author Sarath Lakshman  <sarath@couchbase.com>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9 * use this file except in compliance with the License. You may obtain a copy of
10 * the License at
11 *
12 *  http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 * License for the specific language governing permissions and limitations under
18 * the License.
19 **/
20
21#include "config.h"
22#include "../view_group.h"
23#include "../util.h"
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <inttypes.h>
28#include "util.h"
29
30#define BUF_SIZE 8192
31
32int main(int argc, char *argv[])
33{
34    view_group_info_t *group_info = NULL;
35    uint64_t purge_count;
36    int ret = 2;
37    uint64_t header_pos;
38    view_error_t error_info = {NULL, NULL};
39    cb_thread_t exit_thread;
40
41    (void) argc;
42    (void) argv;
43
44    /*
45     * Disable buffering for stdout
46     */
47    setvbuf(stdout, (char *) NULL, _IONBF, 0);
48
49    if (set_binary_mode() < 0) {
50        fprintf(stderr, "Error setting binary mode\n");
51        goto out;
52    }
53
54    group_info = couchstore_read_view_group_info(stdin, stderr);
55    if (group_info == NULL) {
56        ret = COUCHSTORE_ERROR_ALLOC_FAIL;
57        goto out;
58    }
59
60    ret = start_exit_listener(&exit_thread);
61    if (ret) {
62        fprintf(stderr, "Error starting stdin exit listener thread\n");
63        goto out;
64    }
65
66    ret = couchstore_cleanup_view_group(group_info,
67                                        &header_pos,
68                                        &purge_count,
69                                        &error_info);
70
71    if (ret != COUCHSTORE_SUCCESS) {
72        if (error_info.error_msg != NULL && error_info.view_name != NULL) {
73            fprintf(stderr,
74                    "Error cleaning up index for view `%s`, reason: %s\n",
75                    error_info.view_name,
76                    error_info.error_msg);
77        }
78        goto out;
79    }
80
81    fprintf(stdout, "PurgedCount %"PRIu64"\n", purge_count);
82
83out:
84    couchstore_free_view_group_info(group_info);
85    free((void *) error_info.error_msg);
86    free((void *) error_info.view_name);
87
88    ret = (ret < 0) ? (100 + ret) : ret;
89    _exit(ret);
90}
91