Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

#!/usr/bin/env python 

 

 

""" 

@file ion/core/data/index_store_service.py 

@author Matt Rodriguez 

@author David Stuebe 

@brief Service which fronts the index store capability through the messaging to a single back end. 

""" 

 

import ion.util.ionlog 

log = ion.util.ionlog.getLogger(__name__) 

from twisted.internet import defer 

 

from ion.core.process.process import ProcessFactory 

from ion.core.process.service_process import ServiceProcess, ServiceClient 

from ion.core.object import object_utils 

 

from ion.core.data.store import IStore, Store 

 

 

from zope.interface import implements 

 

from ion.core import ioninit 

CONF = ioninit.config(__name__) 

 

 

 

QUERY_ATTRIBUTES_TYPE = object_utils.create_type_identifier(object_id=17, version=1) 

ROW_TYPE = object_utils.create_type_identifier(object_id=18, version=1) 

ROWS_TYPE = object_utils.create_type_identifier(object_id=19, version=1) 

INDEXED_ATTRIBUTES_TYPE = object_utils.create_type_identifier(object_id=20, version=1) 

ROW_INDEX_UPDATE_TYPE = object_utils.create_type_identifier(object_id=21, version=1) 

 

 

class StoreServiceException(Exception): 

    """ 

    Exceptions that originate in the IndexStoreService class 

    """ 

 

class StoreService(ServiceProcess): 

    """ 

    @brief IndexStoreService 

 

    This is not a ION service. It is part of a test harness to provide a pure, in memory backend for the data store 

    and the association service 

 

    TODO, this class does not catch any exceptions from the business logic class.  

    """ 

 

    # Declaration of service 

    declare = ServiceProcess.service_declare(name='store_service', version='0.1.0', dependencies=[]) 

 

    def __init__(self, *args, **kwargs): 

        # Service class initializer. Basic config, but no yields allowed. 

        ServiceProcess.__init__(self, *args, **kwargs) 

 

        log.info(self.spawn_args) 

        if self.spawn_args.get('indices'): 

            # Check to make sure store and index store were not mixed up... 

            raise StoreServiceException('Invalid Spawn Arg indicies passed to store service!') 

 

 

 

    #@defer.inlineCallbacks 

    def slc_activate(self, *args): 

        """ 

        Activation can be automatic when the process is spawned or triggered by 

        a message from the client. 

         

        First, default to bootstrapping from the spawn args. Create all resources 

        by describing the cluster! 

         

        Second, add hooks to override the spawn args and take the storage resource 

        reference from a message in op_activate - connecting to an already active 

        system! 

         

        """ 

 

        self._store = Store() 

 

        log.info("Created Index Store Service") 

 

 

 

    @defer.inlineCallbacks 

    def op_put(self, request, headers, msg): 

        """ 

        @note, puts a row into the Cassandra cluster.  

        @retval does not return anything 

        """ 

        key = request.key 

        value = request.value 

 

        yield self._store.put(key,value) 

 

        yield self.reply_ok(msg) 

 

 

    @defer.inlineCallbacks 

    def op_get(self, request, headers, msg): 

        """ 

        @note Gets a row from the Cassandra cluster 

        If the row does not exist then leave the value field in the CassandraIndexedRow empty. 

        @param request is a CassandraRow message object 

        @retval Returns a CassandraRow message in the response    

        """ 

 

        value = yield self._store.get(request.key) 

        response = yield self.message_client.create_instance(ROW_TYPE) 

        response.key = request.key 

 

        if value is not None: 

            response.value = value 

 

        # Consider using raise with a not found response? 

        yield self.reply_ok(msg, response) 

 

    @defer.inlineCallbacks 

    def op_remove(self, request, headers, msg): 

        """ 

        @note removes a row 

        @param request is a CassandraRow message object 

        @retval does not return anything 

        """ 

 

        yield self._store.remove(request.key) 

        yield self.reply_ok(msg) 

 

    @defer.inlineCallbacks 

    def op_has_key(self, request, headers, msg): 

        """ 

        @note sees if key exists in the cluster 

        @request is a CassandraRow message object 

        @retval return a string that is "True" or "False" in a CassandraRow message 

        """ 

        key_exists = yield self._store.has_key(request.key) 

        log.info("key_exists: " + str(key_exists)) 

        response = yield self.message_client.create_instance(ROW_TYPE) 

        response.value = str(int(key_exists)) 

        yield self.reply_ok(msg, response) 

 

 

# Spawn of the process using the module name 

factory = ProcessFactory(StoreService) 

 

 

class StoreServiceClient(ServiceClient): 

    """ 

    This interface will change, because we have to define the ION resources. We probably want 

    convenience methods to query by name, type, etc... 

     

    TODO have this implement the Indexstore interface 

    """ 

    implements(IStore) 

 

    def __init__(self, proc=None, **kwargs): 

        if not 'targetname' in kwargs: 

            kwargs['targetname'] = 'store_service' 

        ServiceClient.__init__(self, proc, **kwargs) 

 

        self.mc = proc.message_client 

 

 

    @defer.inlineCallbacks 

    def put(self, key, value): 

        log.info("Called Store Service client: put") 

 

        row = yield self.mc.create_instance(ROW_TYPE) 

        row.key = key 

        row.value = value 

 

        (content, headers, msg) = yield self.rpc_send('put', row) 

 

 

        defer.returnValue(content) 

 

 

    @defer.inlineCallbacks 

    def get(self, key): 

        log.info("Called Store Service client: get") 

        row = yield self.mc.create_instance(ROW_TYPE) 

        row.key = key 

 

        (result, headers, msg) = yield self.rpc_send('get',row) 

 

        if not result.value is '': 

            defer.returnValue(result.value) 

        else: 

            defer.returnValue(None) 

 

    @defer.inlineCallbacks 

    def remove(self, key): 

        log.info("Called Store Service client: remove") 

        row = yield self.mc.create_instance(ROW_TYPE) 

        row.key = key 

 

        (content, headers, msg) = yield self.rpc_send('remove', row) 

        defer.returnValue(content) 

 

    @defer.inlineCallbacks 

    def has_key(self, key): 

        log.info("Called Store Service client: has_key") 

        row = yield self.mc.create_instance(ROW_TYPE) 

        row.key = key 

        (result, headers, msg) = yield self.rpc_send('has_key', row) 

        ret = bool(int(result.value)) 

        log.info("%s" % (ret,)) 

        defer.returnValue(ret)