libmongo-client 0.1.4
|
00001 /* mongo-sync-cursor.c - libmongo-client cursor API on top of Sync 00002 * Copyright 2011 Gergely Nagy <algernon@balabit.hu> 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00021 #include "config.h" 00022 #include "mongo.h" 00023 #include "libmongo-private.h" 00024 00025 #include <errno.h> 00026 00027 mongo_sync_cursor * 00028 mongo_sync_cursor_new (mongo_sync_connection *conn, const gchar *ns, 00029 mongo_packet *packet) 00030 { 00031 mongo_sync_cursor *c; 00032 00033 if (!conn) 00034 { 00035 errno = ENOTCONN; 00036 return NULL; 00037 } 00038 if (!ns || !packet) 00039 { 00040 errno = EINVAL; 00041 return NULL; 00042 } 00043 00044 c = g_new0 (mongo_sync_cursor, 1); 00045 c->conn = conn; 00046 c->ns = g_strdup (ns); 00047 c->results = packet; 00048 c->offset = -1; 00049 00050 mongo_wire_reply_packet_get_header (c->results, &c->ph); 00051 00052 return c; 00053 } 00054 00055 gboolean 00056 mongo_sync_cursor_next (mongo_sync_cursor *cursor) 00057 { 00058 if (!cursor) 00059 { 00060 errno = EINVAL; 00061 return FALSE; 00062 } 00063 errno = 0; 00064 00065 if (cursor->offset >= cursor->ph.returned - 1) 00066 { 00067 gint32 ret = cursor->ph.returned; 00068 gint64 cid = cursor->ph.cursor_id; 00069 00070 mongo_wire_packet_free (cursor->results); 00071 cursor->offset = -1; 00072 cursor->results = mongo_sync_cmd_get_more (cursor->conn, cursor->ns, 00073 ret, cid); 00074 if (!cursor->results) 00075 return FALSE; 00076 mongo_wire_reply_packet_get_header (cursor->results, &cursor->ph); 00077 } 00078 cursor->offset++; 00079 return TRUE; 00080 } 00081 00082 void 00083 mongo_sync_cursor_free (mongo_sync_cursor *cursor) 00084 { 00085 if (!cursor) 00086 { 00087 errno = ENOTCONN; 00088 return; 00089 } 00090 errno = 0; 00091 00092 mongo_sync_cmd_kill_cursors (cursor->conn, 1, cursor->ph.cursor_id); 00093 g_free (cursor->ns); 00094 mongo_wire_packet_free (cursor->results); 00095 g_free (cursor); 00096 } 00097 00098 bson * 00099 mongo_sync_cursor_get_data (mongo_sync_cursor *cursor) 00100 { 00101 bson *r; 00102 00103 if (!cursor) 00104 { 00105 errno = EINVAL; 00106 return NULL; 00107 } 00108 00109 if (!mongo_wire_reply_packet_get_nth_document (cursor->results, 00110 cursor->offset + 1, 00111 &r)) 00112 { 00113 errno = ERANGE; 00114 return NULL; 00115 } 00116 bson_finish (r); 00117 return r; 00118 }