erlang

How to read all the records of mnesia database in erlang?

耗尽温柔 提交于 2019-12-05 09:53:15
I ma new in erlang and I need to do some operations for all records I get from mnesia database. Result = mnesia:dirty_read(mydatabase, {key1, key2}), case Result of [] -> ?DEBUG("No such record found", []); [#mydatabase{key3 = Key3}] -> %% some operations end How can I add a loop to my code that execute some operations for all records? I am not even sure if the code above does it or not? You could use mnesia:foldl/3 for that. It iterates over all records in a table, passing along an "accumulator" value. It doesn't have an explicit "dirty" counterpart, so if you want to run it as a dirty

How to maintain state in Erlang?

六眼飞鱼酱① 提交于 2019-12-05 09:51:38
I have seen people use dict,ordict,record for maintaining state in many blogs that i have read.I find it as very vital concept. Generally I understand the meaning of mantaining state,recursions but when it comes to erlang..I am a little vague about how it is handled . Any help? State is the present arrangement of data. It is sometimes hard to remember this for two reasons: State means both the data in the program and the program's current point of execution and "mode". We build this up to be some magical thing unnecessarily. Consider this: "What is the process's state?" is asking about the

How to optimize the receive loop for thousands of messages in Erlang?

╄→гoц情女王★ 提交于 2019-12-05 09:48:55
In the chapter "Programming Multicore CPUs" of the Programming Erlang book, Joe Armstrong gives a nice example of parallelization of a map function: pmap(F, L) -> S = self(), %% make_ref() returns a unique reference %% we'll match on this later Ref = erlang:make_ref(), Pids = map(fun(I) -> spawn(fun() -> do_f(S, Ref, F, I) end) end, L), %% gather the results gather(Pids, Ref). do_f(Parent, Ref, F, I) -> Parent ! {self(), Ref, (catch F(I))}. gather([Pid|T], Ref) -> receive {Pid, Ref, Ret} -> [Ret|gather(T, Ref)] end; gather([], _) -> []. It works nicely, but I believe there is a bottleneck in

How to call stored procedure taking array using odbc:param_query in Erlang

故事扮演 提交于 2019-12-05 09:07:18
I have a stored procedure in db2 create type intArray as integer array[100]@ create or replace procedure sum(in numList intArray, out total integer) begin declare i, n integer; set n = CARDINALITY(numList); set i = 1; set total = 100; while (i <= n) do set total = total + numList[i]; set i = i + 1; end while; end@ I am trying to call through Erlang odbc:param_query. odbc:param_query(Ref, "CALL sum (?, ?)", [{sql_integer,[1]}, {sql_integer,out, [1]}]). The above is giving me proper return as {executed,1,[{101}]} But when I pass multiple values as odbc:param_query(Ref, "CALL sum (?, ?)", [{sql

In Erlang how do I convert a String to a binary value?

倖福魔咒の 提交于 2019-12-05 08:47:55
问题 In Erlang how do I convert a string to a binary value? String = "Hello" %% should be Binary = <<"Hello">> 回答1: In Erlang strings are represented as a list of integers. You can therefore use the list_to_binary (built-in-function, aka BIF). Here is an example I ran in the Erlang console (started with erl ): 1> list_to_binary("hello world"). <<"hello world">> 回答2: the unicode (utf-8/16/32) character set needs more number of bits to express characters that are greater than 1-byte in length: this

How to connect to PostgreSQL from Phoenix Web App via SSL?

若如初见. 提交于 2019-12-05 08:20:40
When trying to run Elixir (Phoenix) Web Application using PostgreSQL Database hosted 3 rd party " Database-as-a-Service " ( Azure Database for PostgreSQL ). We attempt to start the app with mix phoenix.server we see the following error: [info] Running Pxblog.Endpoint with Cowboy using http://localhost:4000 [error] GenServer #PID<0.247.0> terminating ** (FunctionClauseError) no function clause matching in Postgrex.Messages.decode_fields/1 (postgrex) lib/postgrex/messages.ex:339: Postgrex.Messages.decode_fields("") (postgrex) lib/postgrex/messages.ex:344: Postgrex.Messages.decode_fields/1

Must UTF-8 binaries include /utf8 in the binary literal in Erlang?

可紊 提交于 2019-12-05 08:20:11
In erlang, when defining a UTF-8 binary string, I need to specify the encoding in the binary literal, like this: Star = <<"★"/utf8>>. > <<226,152,133>> io:format("~ts~n", [Star]). > ★ > ok But, if the /utf8 encoding is omitted, the unicode characters are not handled correctly: Star1 = <<"★">>. > <<5>> io:format("~ts~n", [Star1]). > ^E > ok Is there a way that I can create literal binary strings like this without having to specify /utf8 in every binary I create? My code has quite a few binaries like this and things have become quite cluttered. Is there a way to set some sort of default encoding

Erlang: Is there a way to reload changed modules into an already running node with rebar?

╄→гoц情女王★ 提交于 2019-12-05 07:59:00
The rebar build tool seems pretty cool, but is there a way to recompile AND reload files into a currently running node with it? I don't see a need to bring rebar into it. I use reloader.erl from the Mochiweb project. Just build this module along with the rest of your program and call reloader:start() early in your app's initialization sequence. After that call, any time a module in your program changes on disk, the reloader will replace the running copy automatically. 来源: https://stackoverflow.com/questions/2955157/erlang-is-there-a-way-to-reload-changed-modules-into-an-already-running-node-wi

Node.Js in Erlang style?

我的梦境 提交于 2019-12-05 07:56:30
I am a complete noob when it comes to both Node.Js and Erlang. But wouldn't it be possible to build a Node.js app that emulates Erlang behavior? e.g. you pass json messages across an distributed node.js server park and even pass new code to those servers w/o going offline, just like erlang. If you have a message handler callback that is activated when a message is received, then this message handler could check if the message is a code update message and thus replace itself(the current handler) with the new code. So it should be possible to have Node.Js servers with no downtime for code

RabbitMQ安装及配置

泄露秘密 提交于 2019-12-05 07:01:09
RabbitMQ是一个在AMQP协议标准基础上完整的,可服用的企业消息系统。它遵循Mozilla Public License开源协议,采用Erlang语音实现的工业级的消息队列(MQ)服务器,Rabbi MQ是建立在Erlang OTP平台上,所以安装需要先安装Erlang语言包。 RabbitMQ及Erlang语言包地址: 链接:https://pan.baidu.com/s/1WD5ezbGrdpCruhcSgHWvtg 提取码:od1v 1 安装Erlang 所以在安装 RabbitMQ之前,需先安装Erlang(otp_win64_22.0.exe)。 安装过程中,全部点击 “下一步”就行。另外,需要配置一下系统环境变量: 2 安装RabbitMQ 运行 rabbitmq-server-3.7.15.exe执行文件,依旧可以不改变,默认进行安装。需要注意的是:默认安装的RabbitMQ监听端口是5672。 3 RabbitMQ插件安装 激活 RabbitMQ’s Management Plugin,使用Rabbit管理插件,可以更好的采用可视化方式查看RabbitMQ服务器实例的状态。 1、管理员运行cmd,打开命令窗口: 2、输入命令: "C:\ProgramFiles\RabbitMQServer\rabbitmq_server-3.7.15\sbin