summaryrefslogtreecommitdiff
path: root/src/mongo-sync-cursor.c
blob: b2492be6b88d8508227772fc467b9b6df2b29c2b (plain)
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
/* mongo-sync-cursor.c - libmongo-client cursor API on top of Sync
 * Copyright 2011, 2012 Gergely Nagy <algernon@balabit.hu>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/** @file src/mongo-sync-cursor.c
 * MongoDB Cursor API implementation.
 */

#include "config.h"
#include "mongo.h"
#include "libmongo-private.h"

#include <errno.h>

mongo_sync_cursor *
mongo_sync_cursor_new (mongo_sync_connection *conn, const gchar *ns,
                       mongo_packet *packet)
{
  mongo_sync_cursor *c;

  if (!conn)
    {
      errno = ENOTCONN;
      return NULL;
    }
  if (!ns || !packet)
    {
      errno = EINVAL;
      return NULL;
    }

  c = g_new0 (mongo_sync_cursor, 1);
  c->conn = conn;
  c->ns = g_strdup (ns);
  c->results = packet;
  c->offset = -1;

  mongo_wire_reply_packet_get_header (c->results, &c->ph);

  return c;
}

gboolean
mongo_sync_cursor_next (mongo_sync_cursor *cursor)
{
  if (!cursor)
    {
      errno = EINVAL;
      return FALSE;
    }
  errno = 0;

  if (cursor->offset >= cursor->ph.returned - 1)
    {
      gint32 ret = cursor->ph.returned;
      gint64 cid = cursor->ph.cursor_id;

      mongo_wire_packet_free (cursor->results);
      cursor->offset = -1;
      cursor->results = mongo_sync_cmd_get_more (cursor->conn, cursor->ns,
                                                 ret, cid);
      if (!cursor->results)
        return FALSE;
      mongo_wire_reply_packet_get_header (cursor->results, &cursor->ph);
    }
  cursor->offset++;
  return TRUE;
}

void
mongo_sync_cursor_free (mongo_sync_cursor *cursor)
{
  if (!cursor)
    {
      errno = ENOTCONN;
      return;
    }
  errno = 0;

  mongo_sync_cmd_kill_cursors (cursor->conn, 1, cursor->ph.cursor_id);
  g_free (cursor->ns);
  mongo_wire_packet_free (cursor->results);
  g_free (cursor);
}

bson *
mongo_sync_cursor_get_data (mongo_sync_cursor *cursor)
{
  bson *r;

  if (!cursor)
    {
      errno = EINVAL;
      return NULL;
    }

  if (!mongo_wire_reply_packet_get_nth_document (cursor->results,
                                                 cursor->offset + 1,
                                                 &r))
    {
      errno = ERANGE;
      return NULL;
    }
  bson_finish (r);
  return r;
}